Support object syntax for validateArguments as well
This commit is contained in:
parent
88630602c3
commit
b82a7d359d
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
const isArguments = require("is-arguments");
|
const isArguments = require("is-arguments");
|
||||||
const flatten = require("flatten");
|
const flatten = require("flatten");
|
||||||
|
const asExpression = require("as-expression");
|
||||||
|
|
||||||
const ValidationError = require("@validatem/error");
|
const ValidationError = require("@validatem/error");
|
||||||
const annotateErrors = require("@validatem/annotate-errors");
|
const annotateErrors = require("@validatem/annotate-errors");
|
||||||
|
@ -17,32 +18,57 @@ module.exports = createValidationMethod((args, argumentDefinitions) => {
|
||||||
throw new Error(`First argument is not an 'arguments' object; maybe you forgot to put it before the validation rules?`);
|
throw new Error(`First argument is not an 'arguments' object; maybe you forgot to put it before the validation rules?`);
|
||||||
} else if (argumentDefinitions == null) {
|
} else if (argumentDefinitions == null) {
|
||||||
throw new Error(`Validation rules (second argument) are missing; maybe you forgot to specify them?`);
|
throw new Error(`Validation rules (second argument) are missing; maybe you forgot to specify them?`);
|
||||||
} else if (args.length > argumentDefinitions.length) {
|
} else {
|
||||||
|
let usesArrayAPI = (Array.isArray(argumentDefinitions));
|
||||||
|
|
||||||
|
let definitionCount = (usesArrayAPI)
|
||||||
|
? argumentDefinitions.length
|
||||||
|
: Object.keys(argumentDefinitions).length;
|
||||||
|
|
||||||
|
if (args.length > definitionCount) {
|
||||||
return {
|
return {
|
||||||
errors: [ new ValidationError(`Got ${args.length} arguments, but only expected ${argumentDefinitions.length}`) ],
|
errors: [ new ValidationError(`Got ${args.length} arguments, but only expected ${definitionCount}`) ],
|
||||||
// Cast the below to an array, for consistency with *success* output, in case we ever want to expose the new values to the user in an error case too.
|
// Cast the below to an array, for consistency with *success* output, in case we ever want to expose the new values to the user in an error case too.
|
||||||
newValue: Array.from(args)
|
newValue: Array.from(args)
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
let results = argumentDefinitions.map((definition, i) => {
|
let normalizedDefinitions = asExpression(() => {
|
||||||
let argument = args[i];
|
if (usesArrayAPI) {
|
||||||
let [ argumentName, ...argumentRules ] = definition;
|
return argumentDefinitions.map((definition) => {
|
||||||
|
let [ name, ...rules ] = definition;
|
||||||
|
|
||||||
if (typeof argumentName !== "string") {
|
if (typeof name !== "string") {
|
||||||
throw new Error("First item in the argument rules list must be the argument name");
|
throw new Error("First item in the argument rules list must be the argument name");
|
||||||
} else {
|
} else {
|
||||||
let validatorResult = applyValidators(argument, argumentRules);
|
return {
|
||||||
|
name: name,
|
||||||
|
rules: rules
|
||||||
|
};
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
return Object.entries(argumentDefinitions).map(([ key, value ]) => {
|
||||||
|
return {
|
||||||
|
name: key,
|
||||||
|
rules: value
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let results = normalizedDefinitions.map(({ name, rules }, i) => {
|
||||||
|
let argument = args[i];
|
||||||
|
let validatorResult = applyValidators(argument, rules);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
errors: annotateErrors({
|
errors: annotateErrors({
|
||||||
pathSegments: [ argumentName ],
|
pathSegments: [ name ],
|
||||||
errors: validatorResult.errors
|
errors: validatorResult.errors
|
||||||
}),
|
}),
|
||||||
newValue: (validatorResult.newValue !== undefined)
|
newValue: (validatorResult.newValue !== undefined)
|
||||||
? validatorResult.newValue
|
? validatorResult.newValue
|
||||||
: argument
|
: argument
|
||||||
};
|
};
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let combinedErrors = results.map((result) => result.errors);
|
let combinedErrors = results.map((result) => result.errors);
|
||||||
|
@ -55,4 +81,5 @@ module.exports = createValidationMethod((args, argumentDefinitions) => {
|
||||||
newValue: newValues
|
newValue: newValues
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue