Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | 1x 1x 1x 1x 108x 5x 103x 91x 12x 12x 1x 1x 11x 2x 1x 2x 3x 1x 1x 2x 2x 1x 2x 6x 1x 5x 6x 10x 1x | const isObject = require('lodash.isobject');
const isNumber = require('lodash.isnumber');
const isBoolean = require('lodash.isboolean');
const isString = require('lodash.isstring');
// This is used in a reduce handler. Accumulator should have two properties,
// errors (array) and percentTotal (number)
function validateValueByRule(accumulator, value, key, property, rule) {
// If value is not an object, we can't validate it by the rule.
// A previous validator will have reported this error.
if (!isObject(value)) {
return accumulator;
}
// property does not exist on value, this is valid
if (!Object.prototype.hasOwnProperty.call(value, property)) {
return accumulator;
}
const propertyValue = value[property];
if (propertyValue === undefined) {
accumulator.errors.push(new Error(`property "${property}" is undefined for key "${key}"`));
return accumulator;
}
switch (rule.type) {
case 'equal':
if (
!isString(propertyValue)
&& !isNumber(propertyValue)
&& !isBoolean(propertyValue)
) {
accumulator.errors.push(new Error(`equal property "${property}" must be a string, number, or boolean for key "${key}"`));
}
break;
case 'list':
if (!Array.isArray(propertyValue)) {
accumulator.errors.push(new Error(`list property "${property}" must be an array for key "${key}"`));
return accumulator;
}
propertyValue.forEach((listValue) => {
if (
!isString(listValue)
&& !isNumber(listValue)
&& !isBoolean(listValue)
) {
accumulator.errors.push(new Error(`values in list property "${property}" must be a string, number, or boolean for key "${key}"`));
}
});
break;
case 'percent':
if (
!isNumber(propertyValue)
|| propertyValue > 100
|| propertyValue < 0
) {
accumulator.errors.push(new Error(`percent property "${property}" must be a number between 0 and 100 inclusive for key "${key}"`));
} else {
accumulator.percentTotal += propertyValue;
}
break;
/* eslint default-case: 0 */
}
return accumulator;
}
module.exports = validateValueByRule;
|