Skip to content

Array doesn't validate correctly if includes() uses valid() #124

@papajuans

Description

@papajuans
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: ['a@b.com'] }, schema);
var err2 = Joi.validate({ emails: ['a@b.com','foobar'] }, schema);

console.log(err1);
console.log(err2); // contains error since `foobar` isn't a valid email.

Metadata

Metadata

Assignees

Labels

bugBug or defect

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions