Skip to content

Commit

Permalink
[changed] validate() on objects won't cast nested schema with strict()
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense committed Apr 23, 2016
1 parent 74b21c3 commit f827822
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
2 changes: 2 additions & 0 deletions lib/util/lazy.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,7 @@ var Lazy = function () {
return Lazy;
}();

Lazy.prototype.__isYupSchema__ = true;

exports.default = Lazy;
module.exports = exports['default'];
10 changes: 8 additions & 2 deletions src/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Expand All @@ -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 => {
Expand Down
12 changes: 12 additions & 0 deletions test/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit f827822

Please sign in to comment.