Skip to content

Commit

Permalink
Run empty prior to other checks
Browse files Browse the repository at this point in the history
Fixes #945.
  • Loading branch information
Marsup committed Jul 24, 2016
1 parent 079c4d6 commit b252ea9
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
9 changes: 4 additions & 5 deletions lib/any.js
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,10 @@ module.exports = internals.Any = class {
value = coerced.value;
}

if (this._flags.empty && !this._flags.empty._validate(value, null, internals.defaults).errors) {
value = undefined;
}

// Check presence requirements

const presence = this._flags.presence || options.presence;
Expand Down Expand Up @@ -515,11 +519,6 @@ module.exports = internals.Any = class {
return finish();
}

if (this._flags.empty && !this._flags.empty._validate(value, null, internals.defaults).errors) {
value = undefined;
return finish();
}

// Check allowed and denied values using the original value

if (this._valids.has(value, state, options, this._flags.insensitive)) {
Expand Down
50 changes: 47 additions & 3 deletions test/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -788,17 +788,61 @@ describe('array', () => {

const schema = Joi.array().items(Joi.object().empty({}).required());

Helper.validate(schema, [
[[{}, { c: 3 }], false, null, '"value" at position 0 fails because ["0" is required]']
], done);
});

it('errors on undefined value after custom validation with required', (done) => {

const customJoi = Joi.extend({
name: 'myType',
rules: [
{
name: 'foo',
validate(params, value, state, options) {

return undefined;
}
}
]
});

const schema = Joi.array().items(customJoi.myType().foo().required());

Helper.validate(schema, [
[[{}, { c: 3 }], false, null, '"value" must not be a sparse array']
], done);
});

it('errors on undefined value after custom validation with required and abortEarly false', (done) => {

const customJoi = Joi.extend({
name: 'myType',
rules: [
{
name: 'foo',
validate(params, value, state, options) {

return undefined;
}
}
]
});

const schema = Joi.array().items(customJoi.myType().foo().required()).options({ abortEarly: false });

Helper.validate(schema, [
[[{}, { c: 3 }], false, null, '"value" must not be a sparse array. "value" must not be a sparse array']
], done);
});

it('errors on undefined value after validation with required and abortEarly false', (done) => {

const schema = Joi.array().items(Joi.object().empty({}).required()).options({ abortEarly: false });

Helper.validate(schema, [
[[{}, 3], false, null, '"value" must not be a sparse array. "value" at position 1 fails because ["1" must be an object]']
[[{}, 3], false, null, '"value" at position 0 fails because ["0" is required]. "value" at position 1 fails because ["1" must be an object]. "value" does not contain 1 required value(s)']
], done);
});

Expand All @@ -813,7 +857,7 @@ describe('array', () => {

it('errors on undefined value after validation with ordered and abortEarly false', (done) => {

const schema = Joi.array().ordered(Joi.object().empty({}).required()).options({ abortEarly: false });
const schema = Joi.array().ordered(Joi.object().empty({})).options({ abortEarly: false });

Helper.validate(schema, [
[[{}, 3], false, null, '"value" must not be a sparse array. "value" at position 1 fails because array must contain at most 1 items']
Expand Down Expand Up @@ -844,7 +888,7 @@ describe('array', () => {
const schema = Joi.array().items(Joi.object().empty({}).required()).sparse();

Helper.validate(schema, [
[[{ a: 1 }, {}, { c: 3 }], true, null, [{ a: 1 }, undefined, { c: 3 }]]
[[{ a: 1 }, {}, { c: 3 }], false, null, '"value" at position 1 fails because ["1" is required]']
], done);
});

Expand Down

0 comments on commit b252ea9

Please sign in to comment.