Skip to content
This repository was archived by the owner on Feb 19, 2024. It is now read-only.

Commit acd86d6

Browse files
committed
Add Hapi/joi validator to commaFormating
1 parent a7c170d commit acd86d6

2 files changed

Lines changed: 31 additions & 4 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
},
6868
"dependencies": {
6969
"@babel/runtime": "^7.4.3",
70+
"@hapi/joi": "^17.1.1",
7071
"@mapbox/geojsonhint": "^3.0.0",
7172
"@onaio/superset-connector": "^0.0.13",
7273
"babel-core": "v6.25.0",

src/utils/commaFormatting.js

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,35 @@
1+
const Joi = require('@hapi/joi');
2+
13
/**
2-
*
3-
* @param {Object} layerObj layer specification/config object
4-
* @param {Object} data data to be formatted a label/popup
4+
*
5+
* @param {Object} layerObj layer specification/config object
6+
* @param {Object} data data to be formatted a label/popup
57
* @param {boolean} popup flag to indicate if call is coming from popup or not
68
*/
79
export default function commaFormatting(layerObj, data, popup) {
10+
const joiSchema = Joi.object({
11+
dataMap: Joi.object()
12+
.required()
13+
.messages({
14+
'object.base': `"a" should be a type of 'object'`,
15+
'any.required': `"a" is a required field`,
16+
}),
17+
popupFlag: Joi.boolean()
18+
.required()
19+
.messages({
20+
'boolean.base': `"a" should be a type of 'boolean'`,
21+
'any.required': `"a" is a required field`,
22+
}),
23+
});
24+
25+
const validationResult = joiSchema.validate(
26+
{ dataMap: {}, popupFlag: false },
27+
{ abortEarly: false }
28+
);
29+
if (validationResult.error) {
30+
alert(validationResult.error);
31+
}
32+
833
const str = popup
934
? layerObj.popup && layerObj.popup.body
1035
: layerObj.labels && layerObj.labels.label;
@@ -13,7 +38,8 @@ export default function commaFormatting(layerObj, data, popup) {
1338
const rxp = /{{([^}]+)}/g;
1439
let foundZero = false;
1540
// Get flags to show/hide zero values on labels and popup
16-
const showZerosOnLabels = layerObj.labels && layerObj.labels.showZeros !== undefined ? layerObj.labels.showZeros : true;
41+
const showZerosOnLabels =
42+
layerObj.labels && layerObj.labels.showZeros !== undefined ? layerObj.labels.showZeros : true;
1743

1844
for (let c = rxp.exec(str); c !== null; c = rxp.exec(str)) {
1945
found.push(c[1]);

0 commit comments

Comments
 (0)