Skip to content

Commit ab78f54

Browse files
committed
[fixed] reach with lazy()
1 parent 5252948 commit ab78f54

File tree

4 files changed

+32
-11
lines changed

4 files changed

+32
-11
lines changed

src/object.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,8 @@ inherits(ObjectSchema, MixedSchema, {
112112

113113
_validate(_value, opts = {}) {
114114
var errors = []
115-
, endEarly, isStrict, recursive;
115+
, endEarly, recursive;
116116

117-
isStrict = this._option('strict', opts)
118117
endEarly = this._option('abortEarly', opts)
119118
recursive = this._option('recursive', opts)
120119

src/util/lazy.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,25 @@ var { isSchema } = require('./_')
22

33
class Lazy {
44
constructor(mapFn) {
5-
this.resolve = (value) => {
5+
this._resolve = (value) => {
66
let schema = mapFn(value)
77
if (!isSchema(schema))
88
throw new TypeError('lazy() functions must return a valid schema')
99

1010
return schema
1111
}
1212
}
13+
resolve(context, parent, value) {
14+
return this._resolve(value)
15+
}
1316

1417
cast(value, options) {
15-
return this.resolve(value)
18+
return this._resolve(value)
1619
.cast(value, options)
1720
}
1821

1922
validate(value, options) {
20-
return this.resolve(value)
23+
return this._resolve(value)
2124
.validate(value, options)
2225
}
2326
}

src/util/reach.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = function (obj, path, value, context) {
1414

1515
if (isArray || has(obj, '_subType')) { // we skipped an array
1616
let idx = isArray ? parseInt(part, 10) : 0
17-
obj = obj.resolve(context, parent)._subType;
17+
obj = obj.resolve(context, parent, value)._subType;
1818

1919
if (value) {
2020

@@ -29,7 +29,7 @@ module.exports = function (obj, path, value, context) {
2929
}
3030

3131
if (!isArray) {
32-
obj = obj.resolve(context, parent);
32+
obj = obj.resolve(context, parent, value);
3333

3434
if (!has(obj, 'fields') || !has(obj.fields, part))
3535
throw new Error(
@@ -45,5 +45,5 @@ module.exports = function (obj, path, value, context) {
4545
}
4646
})
4747

48-
return obj && obj.resolve(value, parent)
48+
return obj && obj.resolve(value, parent, value)
4949
}

test/yup.js

+22-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ var Promise = require('promise/src/es6-extensions')
55
, chaiAsPromised = require('chai-as-promised')
66
, reach = require('../src/util/reach')
77
, BadSet = require('../src/util/set')
8-
, number = require('../src/number')
9-
, array = require('../src/array')
10-
, object = require('../src/object')
8+
, { object, array, string, lazy, number } = require('../src')
119
, _ = require('../src/util/_');
1210

1311
chai.use(chaiAsPromised);
@@ -131,6 +129,27 @@ describe('Yup', function(){
131129
})
132130
})
133131

132+
it('should reach through lazy', async () => {
133+
let types = {
134+
'1': object({ foo: string() }),
135+
'2': object({ foo: number() })
136+
}
137+
138+
let err = await object({
139+
x: array(
140+
lazy(val => types[val.type])
141+
)
142+
})
143+
.strict()
144+
.validate({ x: [
145+
{ type: 1, foo: '4' },
146+
{ type: 2, foo: '5' }
147+
]})
148+
.should.be.rejected
149+
150+
err.message.should.match(/must be a `number` type/)
151+
})
152+
134153
describe('BadSet', function(){
135154
it('should preserve primitive types', function(){
136155
var set = new BadSet()

0 commit comments

Comments
 (0)