Skip to content

Commit b70c240

Browse files
committed
Add options support on any().validate()
Fixes #884.
1 parent 0ada70e commit b70c240

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

lib/any.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -656,16 +656,13 @@ internals.Any.prototype._validateWithOptions = function (value, options, callbac
656656
};
657657

658658

659-
internals.Any.prototype.validate = function (value, callback) {
659+
internals.Any.prototype.validate = function (value, options, callback) {
660660

661-
const result = this._validate(value, null, internals.defaults);
662-
const errors = Errors.process(result.errors, value);
663-
664-
if (callback) {
665-
return callback(errors, result.value);
661+
if (typeof options === 'function') {
662+
return this._validateWithOptions(value, null, options);
666663
}
667664

668-
return { error: errors, value: result.value };
665+
return this._validateWithOptions(value, options, callback);
669666
};
670667

671668

test/any.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,47 @@ describe('any', () => {
989989
});
990990
});
991991

992+
describe('validate()', () => {
993+
994+
it('accepts only value (sync way)', (done) => {
995+
996+
const schema = Joi.number();
997+
const result = schema.validate('2');
998+
expect(result).to.deep.equal({ value: 2, error: null });
999+
done();
1000+
});
1001+
1002+
it('accepts value and callback', (done) => {
1003+
1004+
const schema = Joi.number();
1005+
schema.validate('2', (err, value) => {
1006+
1007+
expect(err).to.not.exist();
1008+
expect(value).to.equal(2);
1009+
done();
1010+
});
1011+
});
1012+
1013+
it('accepts value and options', (done) => {
1014+
1015+
const schema = Joi.number();
1016+
const result = schema.validate('2', { convert: false });
1017+
expect(result.error).to.be.an.error('"value" must be a number');
1018+
done();
1019+
});
1020+
1021+
it('accepts value, options and callback', (done) => {
1022+
1023+
const schema = Joi.number();
1024+
schema.validate('2', { convert: false }, (err, value) => {
1025+
1026+
expect(err).to.be.an.error('"value" must be a number');
1027+
done();
1028+
});
1029+
});
1030+
1031+
});
1032+
9921033
describe('concat()', () => {
9931034

9941035
it('throws when schema is not any', (done) => {

0 commit comments

Comments
 (0)