Add validateOptions, oneOf, forbidden, isBuffer

parse-stacktrace-fix
Sven Slootweg 5 years ago
parent 580b25c673
commit 06e594211d

@ -3,9 +3,16 @@
const createError = require("create-error"); const createError = require("create-error");
const assureArray = require("assure-array"); const assureArray = require("assure-array");
const defaultValue = require("default-value"); const defaultValue = require("default-value");
const util = require("util");
let ValidationError = createError("ValidationError", { path: [] }); let ValidationError = createError("ValidationError", { path: [] });
function allowExtraProperties (rules) {
return function (value) {
return validateObject(value, rules, true);
};
}
function validateValue(value, argRules) { function validateValue(value, argRules) {
let argRules_ = assureArray(argRules); let argRules_ = assureArray(argRules);
let isRequired = argRules_.some((rule) => rule.___validationMarker === "required"); let isRequired = argRules_.some((rule) => rule.___validationMarker === "required");
@ -21,6 +28,7 @@ function validateValue(value, argRules) {
for (let rule of actualRules) { for (let rule of actualRules) {
if (typeof rule === "object") { if (typeof rule === "object") {
/* FIXME: Check that this isn't an array, Date, Buffer, ... */
errors = validateObject(value, rule); errors = validateObject(value, rule);
} else { } else {
try { try {
@ -125,17 +133,24 @@ function aggregrateErrors(errors) {
} }
} }
module.exports = { function validateArguments(args, rules) {
validateArguments: function (args, rules) { let errors = validateArgumentList(args, rules);
let errors = validateArgumentList(args, rules);
aggregrateErrors(errors); aggregrateErrors(errors);
}, }
module.exports = {
validateArguments: validateArguments,
validateValue: function (value, rules) { validateValue: function (value, rules) {
let errors = validateValue(value, assureArray(rules)); let errors = validateValue(value, assureArray(rules));
aggregrateErrors(errors); aggregrateErrors(errors);
}, },
validateOptions: function (args, optionsRules) {
return validateArguments(args, [
["options", optionsRules]
]);
},
ValidationError: ValidationError, ValidationError: ValidationError,
required: {___validationMarker: "required"}, required: {___validationMarker: "required"},
isFunction: function (value) { isFunction: function (value) {
@ -163,6 +178,11 @@ module.exports = {
throw new ValidationError("Must be a Date object"); throw new ValidationError("Must be a Date object");
} }
}, },
isBuffer: function (value) {
if (!(value instanceof Buffer)) {
throw new ValidationError("Must be a Buffer object");
}
},
either: function (...alternatives) { either: function (...alternatives) {
if (alternatives.length === 0) { if (alternatives.length === 0) {
throw new Error("Must specify at least one alternative"); throw new Error("Must specify at least one alternative");
@ -189,24 +209,28 @@ module.exports = {
} }
}, },
when: function (predicate, rules) { when: function (predicate, rules) {
let rules_ = assureArray(rules).map((rule) => { if (rules == null) {
if (typeof rule === "object") { throw new Error("No rules specified for a `when` validation clause; did you misplace a parenthese?");
/* We automatically allow extraneous properties in a `when` clause, because it'll generally be used as a partial addition to an otherwise-fully-specified object structure. */ } else {
return allowExtraProperties(rule); let rules_ = assureArray(rules).map((rule) => {
} else { if (typeof rule === "object") {
return rule; /* We automatically allow extraneous properties in a `when` clause, because it'll generally be used as a partial addition to an otherwise-fully-specified object structure. */
} return allowExtraProperties(rule);
}); } else {
return rule;
return function (value) { }
let matches = predicate(value); });
if (matches) { return function (value) {
return validateValue(value, rules_); let matches = predicate(value);
} else {
return []; if (matches) {
} return validateValue(value, rules_);
}; } else {
return [];
}
};
}
}, },
matchesFormat: function (regex) { matchesFormat: function (regex) {
return function (value) { return function (value) {
@ -215,6 +239,19 @@ module.exports = {
} }
}; };
}, },
oneOf: function (validValues) {
if (Array.isArray(validValues)) {
let validValueSet = new Set(validValues);
return function (value) {
if (!validValueSet.has(value)) {
throw new ValidationError(`Must be one of: ${validValues.map((item) => util.inspect(item)).join(", ")}`);
}
}
} else {
throw new Error("Argument to `oneOf` must be an array of values");
}
},
arrayOf: function (rules) { arrayOf: function (rules) {
let rules_ = assureArray(rules); let rules_ = assureArray(rules);
@ -256,9 +293,10 @@ module.exports = {
} }
}; };
}, },
allowExtraProperties: function (rules) { allowExtraProperties: allowExtraProperties,
return function (value) { forbidden: function (value) {
return validateObject(value, rules, true); if (value != null) {
}; throw new ValidationError("Value exists in a place that should be empty");
}
} }
}; };

Loading…
Cancel
Save