Skip to content

Commit

Permalink
Merge pull request #2642 from sideway/fix/array-items-bug
Browse files Browse the repository at this point in the history
Fix issue with only required items. Fixes #2620.
  • Loading branch information
hueniverse authored Aug 1, 2021
2 parents 371cc32 + 4f64d65 commit ee490ad
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/types/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ module.exports = Any.extend({
continue;
}

if (schema.$_terms._inclusions.length &&
if ((schema.$_terms._inclusions.length || schema.$_terms._requireds.length) &&
!isValid) {

if (stripUnknown) {
Expand Down
13 changes: 13 additions & 0 deletions test/types/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,19 @@ describe('array', () => {
]);
});

it('validates that values are validated when all items are required', () => {

const schema = Joi.array().items(Joi.string().valid('one').required(), Joi.string().valid('two').required());
Helper.validate(schema, [
[['one', 'one', 'two', 'two', 'three'], false, {
message: '"[4]" does not match any of the allowed types',
path: [4],
type: 'array.includes',
context: { pos: 4, value: 'three', label: '[4]', key: 4 }
}]
]);
});

it('validates that a required value exists with abortEarly = false', () => {

const schema = Joi.array()
Expand Down

0 comments on commit ee490ad

Please sign in to comment.