From f8278223835b0a0d0cc8651619410e16a9b71c08 Mon Sep 17 00:00:00 2001 From: Jason Quense Date: Fri, 22 Apr 2016 21:19:12 -0400 Subject: [PATCH] [changed] validate() on objects won't cast nested schema with strict() --- lib/object.js | 2 +- lib/util/lazy.js | 2 ++ src/object.js | 10 ++++++++-- test/object.js | 12 ++++++++++++ 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/object.js b/lib/object.js index cd36d0c58..d42b6e074 100644 --- a/lib/object.js +++ b/lib/object.js @@ -279,7 +279,7 @@ function sortFields(fields) { if (! ~excludes.indexOf(key + '-' + node)) edges.push([key, node]); }; - if (Ref.isRef(value) && !value.isContext) addNode(value.path);else if (isSchema(value)) value._deps.forEach(addNode); + if (Ref.isRef(value) && !value.isContext) addNode(value.path);else if (isSchema(value) && value._deps) value._deps.forEach(addNode); } }return toposort.array(nodes, edges).reverse(); } \ No newline at end of file diff --git a/lib/util/lazy.js b/lib/util/lazy.js index 0025480f8..bd7515d89 100644 --- a/lib/util/lazy.js +++ b/lib/util/lazy.js @@ -41,5 +41,7 @@ var Lazy = function () { return Lazy; }(); +Lazy.prototype.__isYupSchema__ = true; + exports.default = Lazy; module.exports = exports['default']; \ No newline at end of file diff --git a/src/object.js b/src/object.js index 85af273d7..a1ebfbf1b 100644 --- a/src/object.js +++ b/src/object.js @@ -85,7 +85,7 @@ inherits(ObjectSchema, MixedSchema, { , extra = Object.keys(value).filter(v => this._nodes.indexOf(v) === -1) , props = this._nodes.concat(extra); - let innerOptions = { ...opts, parent: {} }; + let innerOptions = { ...opts, parent: {}, __validating: false }; value = transform(props, function(obj, prop) { let field = fields[prop] @@ -94,10 +94,14 @@ inherits(ObjectSchema, MixedSchema, { if (field) { let fieldValue; + let 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 @@ -117,6 +121,8 @@ inherits(ObjectSchema, MixedSchema, { endEarly = this._option('abortEarly', opts) recursive = this._option('recursive', opts) + opts = {...opts, __validating: true }; + return MixedSchema.prototype._validate .call(this, _value, opts) .catch(endEarly ? null : err => { diff --git a/test/object.js b/test/object.js index 97966be60..c72686f16 100644 --- a/test/object.js +++ b/test/object.js @@ -122,6 +122,18 @@ describe('Object types', function(){ err.message.should.match(/must be a `string` type/) }) + it ('should respect child schema with strict()', async () => { + inst = object({ + field: number().strict() + }) + + let err = await inst.validate({ field: '5' }).should.be.rejected + + err.message.should.match(/must be a `number` type/) + + inst.cast({ field: '5' }).should.eql({ field: 5 }) + }) + it('should handle custom validation', async function(){ var inst = object().shape({ prop: mixed(),