Skip to content

Commit

Permalink
[added] options to lazy resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense committed Apr 21, 2016
1 parent b38ca93 commit c553cc0
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/mixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ SchemaType.prototype = {
return !this._typeCheck || this._typeCheck(v)
},

resolve(context, parent) {
resolve({ context, parent }) {
if (this._conditions.length) {
return this._conditions.reduce((schema, match) =>
match.resolve(schema, match.getValue(parent, context)), this)
Expand All @@ -121,7 +121,7 @@ SchemaType.prototype = {
},

cast(value, opts = {}) {
let schema = this.resolve(opts.context, opts.parent)
let schema = this.resolve(opts)

return schema._cast(value, opts)
},
Expand All @@ -142,7 +142,7 @@ SchemaType.prototype = {
if (typeof options === 'function')
cb = options, options = {}

let schema = this.resolve(options.context, options.parent)
let schema = this.resolve(options)

return nodeify(schema._validate(value, options), cb)
},
Expand Down
13 changes: 7 additions & 6 deletions src/util/lazy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,26 @@ var { isSchema } = require('./_')

class Lazy {
constructor(mapFn) {
this._resolve = (value) => {
let schema = mapFn(value)
this._resolve = (...args) => {
let schema = mapFn(...args)
if (!isSchema(schema))
throw new TypeError('lazy() functions must return a valid schema')

return schema
}
}
resolve(context, parent, value) {
return this._resolve(value)

resolve({ value, ...rest }) {
return this._resolve(value, rest)
}

cast(value, options) {
return this._resolve(value)
return this._resolve(value, options)
.cast(value, options)
}

validate(value, options) {
return this._resolve(value)
return this._resolve(value, options)
.validate(value, options)
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/util/reach.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = function (obj, path, value, context) {

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

if (value) {

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

if (!isArray) {
obj = obj.resolve(context, parent, value);
obj = obj.resolve({ context, parent, value });

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

return obj && obj.resolve(value, parent, value)
return obj && obj.resolve({ context, parent, value })
}
12 changes: 11 additions & 1 deletion test/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ describe('Object types', function(){
let value = await inst.validate({ field: 5 })

value.field.should.equal('5')

castSpy.should.have.been.calledOnce

string.prototype._cast.restore()
Expand Down Expand Up @@ -370,6 +370,16 @@ describe('Object types', function(){
inst.cast({ nested: 'foo' })
})

it('should be passed the options', (done) => {
let opts = {}
let inst = lazy((_, options) => {
options.should.equal(opts)
done()
})

inst.cast({ nested: 'foo' }, opts)
})

it('should always return a schema', () => {
(() => lazy(() => {}).cast())
.should.throw(/must return a valid schema/)
Expand Down

0 comments on commit c553cc0

Please sign in to comment.