From c553cc0ebae56cb59ec8b59dc208674b0138e5b4 Mon Sep 17 00:00:00 2001 From: Jason Quense Date: Thu, 21 Apr 2016 07:19:52 -0400 Subject: [PATCH] [added] options to lazy resolve --- src/mixed.js | 6 +++--- src/util/lazy.js | 13 +++++++------ src/util/reach.js | 6 +++--- test/object.js | 12 +++++++++++- 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/mixed.js b/src/mixed.js index 96f79a5..2a1e623 100644 --- a/src/mixed.js +++ b/src/mixed.js @@ -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) @@ -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) }, @@ -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) }, diff --git a/src/util/lazy.js b/src/util/lazy.js index a1c4195..15d26a3 100644 --- a/src/util/lazy.js +++ b/src/util/lazy.js @@ -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) } } diff --git a/src/util/reach.js b/src/util/reach.js index a554a96..f20215b 100644 --- a/src/util/reach.js +++ b/src/util/reach.js @@ -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) { @@ -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( @@ -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 }) } diff --git a/test/object.js b/test/object.js index 3c39c6d..97966be 100644 --- a/test/object.js +++ b/test/object.js @@ -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() @@ -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/)