joi
joi copied to clipboard
Array doesn't validate correctly if includes() uses valid()
var Joi = require('joi');
var T = Joi.types;
var schema = {
accepted_payments: T.Array().includes(T.String().valid('amex','visa'))
};
var err1 = Joi.validate({ accepted_payments: ['amex'] }, schema);
var err2 = Joi.validate({ accepted_payments: ['visa','mc'] }, schema);
console.log(err1);
console.log(err2);
err2 should have an error since mc is not a valid value for the strings defined inside includes().
Looking through array.js, it looks like there are explicit tests for include() when validation modifiers are used that ultimately call BaseType.prototype.add (like String().regexp() or Number().integer()), but none when using valid() or allow().
In other words, the following behaves as expected:
var Joi = require('joi');
var T = Joi.types;
var schema = {
emails: T.Array().includes(T.String().email())
};
var err1 = Joi.validate({ emails: ['[email protected]'] }, schema);
var err2 = Joi.validate({ emails: ['[email protected]','foobar'] }, schema);
console.log(err1);
console.log(err2); // contains error since `foobar` isn't a valid email.
I was trying to work on this yesterday but realized that the "right" way would probably involve some fairly involved re-jiggering. Do you guys have a position on whether or not the above behavior is desired?