From ab78f545fef634509dc5827c59ef466395cce006 Mon Sep 17 00:00:00 2001 From: Jason Quense Date: Thu, 14 Apr 2016 15:11:24 -0400 Subject: [PATCH] [fixed] reach with lazy() --- src/object.js | 3 +-- src/util/lazy.js | 9 ++++++--- src/util/reach.js | 6 +++--- test/yup.js | 25 ++++++++++++++++++++++--- 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/object.js b/src/object.js index 3c720b1a3..e2e110748 100644 --- a/src/object.js +++ b/src/object.js @@ -112,9 +112,8 @@ inherits(ObjectSchema, MixedSchema, { _validate(_value, opts = {}) { var errors = [] - , endEarly, isStrict, recursive; + , endEarly, recursive; - isStrict = this._option('strict', opts) endEarly = this._option('abortEarly', opts) recursive = this._option('recursive', opts) diff --git a/src/util/lazy.js b/src/util/lazy.js index 30526fec9..a1c4195bb 100644 --- a/src/util/lazy.js +++ b/src/util/lazy.js @@ -2,7 +2,7 @@ var { isSchema } = require('./_') class Lazy { constructor(mapFn) { - this.resolve = (value) => { + this._resolve = (value) => { let schema = mapFn(value) if (!isSchema(schema)) throw new TypeError('lazy() functions must return a valid schema') @@ -10,14 +10,17 @@ class Lazy { return schema } } + resolve(context, parent, value) { + return this._resolve(value) + } cast(value, options) { - return this.resolve(value) + return this._resolve(value) .cast(value, options) } validate(value, options) { - return this.resolve(value) + return this._resolve(value) .validate(value, options) } } diff --git a/src/util/reach.js b/src/util/reach.js index ea8a712f8..a554a9685 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)._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); + 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) + return obj && obj.resolve(value, parent, value) } diff --git a/test/yup.js b/test/yup.js index 6c440a3d3..a6e9b14ff 100644 --- a/test/yup.js +++ b/test/yup.js @@ -5,9 +5,7 @@ var Promise = require('promise/src/es6-extensions') , chaiAsPromised = require('chai-as-promised') , reach = require('../src/util/reach') , BadSet = require('../src/util/set') - , number = require('../src/number') - , array = require('../src/array') - , object = require('../src/object') + , { object, array, string, lazy, number } = require('../src') , _ = require('../src/util/_'); chai.use(chaiAsPromised); @@ -131,6 +129,27 @@ describe('Yup', function(){ }) }) + it('should reach through lazy', async () => { + let types = { + '1': object({ foo: string() }), + '2': object({ foo: number() }) + } + + let err = await object({ + x: array( + lazy(val => types[val.type]) + ) + }) + .strict() + .validate({ x: [ + { type: 1, foo: '4' }, + { type: 2, foo: '5' } + ]}) + .should.be.rejected + + err.message.should.match(/must be a `number` type/) + }) + describe('BadSet', function(){ it('should preserve primitive types', function(){ var set = new BadSet()