You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.0 KiB
JavaScript
49 lines
1.0 KiB
JavaScript
"use strict";
|
|
|
|
const combinator = require("@validatem/combinator");
|
|
const validationResult = require("@validatem/validation-result");
|
|
const annotateErrors = require("@validatem/annotate-errors");
|
|
const isArray = require("@validatem/is-array");
|
|
|
|
module.exports = function (rules) {
|
|
let validator = combinator((value, applyValidators) => {
|
|
let allErrors = [];
|
|
|
|
function validateArray(array, path = []) {
|
|
let newArray = [];
|
|
|
|
array.forEach((item, i) => {
|
|
let itemPath = path.concat([ i ]);
|
|
|
|
if (Array.isArray(item)) {
|
|
newArray.push(validateArray(item, itemPath));
|
|
} else {
|
|
let { errors, newValue } = applyValidators(item, rules);
|
|
|
|
let annotatedErrors = annotateErrors({
|
|
pathSegments: itemPath,
|
|
errors: errors
|
|
});
|
|
|
|
newArray.push(newValue);
|
|
allErrors.push(...annotatedErrors);
|
|
}
|
|
});
|
|
|
|
return newArray;
|
|
}
|
|
|
|
return validationResult({
|
|
errors: allErrors,
|
|
newValue: validateArray(value)
|
|
});
|
|
});
|
|
|
|
validator.callIfNull = false;
|
|
|
|
return [
|
|
isArray,
|
|
validator
|
|
];
|
|
};
|