thrownewValidationError("Must be a Buffer object");
}
},
either:function(...alternatives){
if(alternatives.length===0){
thrownewError("Must specify at least one alternative");
@ -189,24 +209,28 @@ module.exports = {
}
},
when:function(predicate,rules){
letrules_=assureArray(rules).map((rule)=>{
if(typeofrule==="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. */
returnallowExtraProperties(rule);
}else{
returnrule;
}
});
if(rules==null){
thrownewError("No rules specified for a `when` validation clause; did you misplace a parenthese?");
}else{
letrules_=assureArray(rules).map((rule)=>{
if(typeofrule==="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. */
returnallowExtraProperties(rule);
}else{
returnrule;
}
});
returnfunction(value){
letmatches=predicate(value);
returnfunction(value){
letmatches=predicate(value);
if(matches){
returnvalidateValue(value,rules_);
}else{
return[];
}
};
if(matches){
returnvalidateValue(value,rules_);
}else{
return[];
}
};
}
},
matchesFormat:function(regex){
returnfunction(value){
@ -215,6 +239,19 @@ module.exports = {
}
};
},
oneOf:function(validValues){
if(Array.isArray(validValues)){
letvalidValueSet=newSet(validValues);
returnfunction(value){
if(!validValueSet.has(value)){
thrownewValidationError(`Must be one of: ${validValues.map((item)=>util.inspect(item)).join(", ")}`);
}
}
}else{
thrownewError("Argument to `oneOf` must be an array of values");
}
},
arrayOf:function(rules){
letrules_=assureArray(rules);
@ -256,9 +293,10 @@ module.exports = {
}
};
},
allowExtraProperties:function(rules){
returnfunction(value){
returnvalidateObject(value,rules,true);
};
allowExtraProperties:allowExtraProperties,
forbidden:function(value){
if(value!=null){
thrownewValidationError("Value exists in a place that should be empty");