Skip to content

Commit f827822

Browse files
committed
[changed] validate() on objects won't cast nested schema with strict()
1 parent 74b21c3 commit f827822

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

lib/object.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ function sortFields(fields) {
279279
if (! ~excludes.indexOf(key + '-' + node)) edges.push([key, node]);
280280
};
281281

282-
if (Ref.isRef(value) && !value.isContext) addNode(value.path);else if (isSchema(value)) value._deps.forEach(addNode);
282+
if (Ref.isRef(value) && !value.isContext) addNode(value.path);else if (isSchema(value) && value._deps) value._deps.forEach(addNode);
283283
}
284284
}return toposort.array(nodes, edges).reverse();
285285
}

lib/util/lazy.js

+2
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,7 @@ var Lazy = function () {
4141
return Lazy;
4242
}();
4343

44+
Lazy.prototype.__isYupSchema__ = true;
45+
4446
exports.default = Lazy;
4547
module.exports = exports['default'];

src/object.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ inherits(ObjectSchema, MixedSchema, {
8585
, extra = Object.keys(value).filter(v => this._nodes.indexOf(v) === -1)
8686
, props = this._nodes.concat(extra);
8787

88-
let innerOptions = { ...opts, parent: {} };
88+
let innerOptions = { ...opts, parent: {}, __validating: false };
8989

9090
value = transform(props, function(obj, prop) {
9191
let field = fields[prop]
@@ -94,10 +94,14 @@ inherits(ObjectSchema, MixedSchema, {
9494
if (field) {
9595
let fieldValue;
9696

97+
let strict = field._options && field._options.strict;
98+
9799
if (field._strip === true)
98100
return
99101

100-
fieldValue = field.cast(value[prop], innerOptions)
102+
fieldValue = !opts.__validating || !strict
103+
? field.cast(value[prop], innerOptions)
104+
: value[prop]
101105

102106
if (fieldValue !== undefined)
103107
obj[prop] = fieldValue
@@ -117,6 +121,8 @@ inherits(ObjectSchema, MixedSchema, {
117121
endEarly = this._option('abortEarly', opts)
118122
recursive = this._option('recursive', opts)
119123

124+
opts = {...opts, __validating: true };
125+
120126
return MixedSchema.prototype._validate
121127
.call(this, _value, opts)
122128
.catch(endEarly ? null : err => {

test/object.js

+12
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,18 @@ describe('Object types', function(){
122122
err.message.should.match(/must be a `string` type/)
123123
})
124124

125+
it ('should respect child schema with strict()', async () => {
126+
inst = object({
127+
field: number().strict()
128+
})
129+
130+
let err = await inst.validate({ field: '5' }).should.be.rejected
131+
132+
err.message.should.match(/must be a `number` type/)
133+
134+
inst.cast({ field: '5' }).should.eql({ field: 5 })
135+
})
136+
125137
it('should handle custom validation', async function(){
126138
var inst = object().shape({
127139
prop: mixed(),

0 commit comments

Comments
 (0)