Add validateOptions, oneOf, forbidden, isBuffer

pull/4/head
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,6 +209,9 @@ module.exports = {
} }
}, },
when: function (predicate, rules) { when: function (predicate, rules) {
if (rules == null) {
throw new Error("No rules specified for a `when` validation clause; did you misplace a parenthese?");
} else {
let rules_ = assureArray(rules).map((rule) => { let rules_ = assureArray(rules).map((rule) => {
if (typeof rule === "object") { if (typeof rule === "object") {
/* 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. */ /* 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. */
@ -207,6 +230,7 @@ module.exports = {
return []; 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