Skip to content

Commit c553cc0

Browse files
committed
[added] options to lazy resolve
1 parent b38ca93 commit c553cc0

File tree

4 files changed

+24
-13
lines changed

4 files changed

+24
-13
lines changed

src/mixed.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ SchemaType.prototype = {
111111
return !this._typeCheck || this._typeCheck(v)
112112
},
113113

114-
resolve(context, parent) {
114+
resolve({ context, parent }) {
115115
if (this._conditions.length) {
116116
return this._conditions.reduce((schema, match) =>
117117
match.resolve(schema, match.getValue(parent, context)), this)
@@ -121,7 +121,7 @@ SchemaType.prototype = {
121121
},
122122

123123
cast(value, opts = {}) {
124-
let schema = this.resolve(opts.context, opts.parent)
124+
let schema = this.resolve(opts)
125125

126126
return schema._cast(value, opts)
127127
},
@@ -142,7 +142,7 @@ SchemaType.prototype = {
142142
if (typeof options === 'function')
143143
cb = options, options = {}
144144

145-
let schema = this.resolve(options.context, options.parent)
145+
let schema = this.resolve(options)
146146

147147
return nodeify(schema._validate(value, options), cb)
148148
},

src/util/lazy.js

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

33
class Lazy {
44
constructor(mapFn) {
5-
this._resolve = (value) => {
6-
let schema = mapFn(value)
5+
this._resolve = (...args) => {
6+
let schema = mapFn(...args)
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)
13+
14+
resolve({ value, ...rest }) {
15+
return this._resolve(value, rest)
1516
}
1617

1718
cast(value, options) {
18-
return this._resolve(value)
19+
return this._resolve(value, options)
1920
.cast(value, options)
2021
}
2122

2223
validate(value, options) {
23-
return this._resolve(value)
24+
return this._resolve(value, options)
2425
.validate(value, options)
2526
}
2627
}

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, value)._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, value);
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, value)
48+
return obj && obj.resolve({ context, parent, value })
4949
}

test/object.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ describe('Object types', function(){
105105
let value = await inst.validate({ field: 5 })
106106

107107
value.field.should.equal('5')
108-
108+
109109
castSpy.should.have.been.calledOnce
110110

111111
string.prototype._cast.restore()
@@ -370,6 +370,16 @@ describe('Object types', function(){
370370
inst.cast({ nested: 'foo' })
371371
})
372372

373+
it('should be passed the options', (done) => {
374+
let opts = {}
375+
let inst = lazy((_, options) => {
376+
options.should.equal(opts)
377+
done()
378+
})
379+
380+
inst.cast({ nested: 'foo' }, opts)
381+
})
382+
373383
it('should always return a schema', () => {
374384
(() => lazy(() => {}).cast())
375385
.should.throw(/must return a valid schema/)

0 commit comments

Comments
 (0)