From 816e60757a78cbf28edbe9061c9d4ddc474d3493 Mon Sep 17 00:00:00 2001 From: Jason Quense Date: Mon, 25 Apr 2016 11:01:00 -0400 Subject: [PATCH] [added] validation params to ValidationError --- lib/object.js | 8 ++++++-- src/util/createValidation.js | 3 ++- test/object.js | 10 +++++++++- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/object.js b/lib/object.js index d42b6e0..a0b4fc4 100644 --- a/lib/object.js +++ b/lib/object.js @@ -96,7 +96,7 @@ inherits(ObjectSchema, MixedSchema, { }), props = this._nodes.concat(extra); - var innerOptions = _extends({}, opts, { parent: {} }); + var innerOptions = _extends({}, opts, { parent: {}, __validating: false }); value = transform(props, function (obj, prop) { var field = fields[prop]; @@ -105,9 +105,11 @@ inherits(ObjectSchema, MixedSchema, { if (field) { var fieldValue = void 0; + var strict = field._options && field._options.strict; + if (field._strip === true) return; - fieldValue = field.cast(value[prop], innerOptions); + fieldValue = !opts.__validating || !strict ? field.cast(value[prop], innerOptions) : value[prop]; if (fieldValue !== undefined) obj[prop] = fieldValue; } else if (exists && !strip) obj[prop] = value[prop]; @@ -127,6 +129,8 @@ inherits(ObjectSchema, MixedSchema, { endEarly = this._option('abortEarly', opts); recursive = this._option('recursive', opts); + opts = _extends({}, opts, { __validating: true }); + return MixedSchema.prototype._validate.call(this, _value, opts).catch(endEarly ? null : function (err) { errors.push(err); return err.value; diff --git a/src/util/createValidation.js b/src/util/createValidation.js index 3440c59..6741dba 100644 --- a/src/util/createValidation.js +++ b/src/util/createValidation.js @@ -17,11 +17,12 @@ function createErrorFactory({ value, label, resolve, ...opts}) { return function createError({ path = opts.path, message = opts.message, type = opts.name, params } = {}) { params = resolveParams(opts.params, params, resolve) - return new ValidationError( + return Object.assign(new ValidationError( formatError(message, { path, value, label, ...params }) , value , path , type) + , { params }) } } diff --git a/test/object.js b/test/object.js index c72686f..89debae 100644 --- a/test/object.js +++ b/test/object.js @@ -122,7 +122,7 @@ describe('Object types', function(){ err.message.should.match(/must be a `string` type/) }) - it ('should respect child schema with strict()', async () => { + it('should respect child schema with strict()', async () => { inst = object({ field: number().strict() }) @@ -132,6 +132,14 @@ describe('Object types', function(){ err.message.should.match(/must be a `number` type/) inst.cast({ field: '5' }).should.eql({ field: 5 }) + + err = await object({ + port: number() + .strict() + .integer() + }) + .validate({ port: 'asdad' }) + .should.be.rejected }) it('should handle custom validation', async function(){