Skip to content

Commit

Permalink
[fixed] reach with lazy()
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense committed Apr 14, 2016
1 parent 5252948 commit ab78f54
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
3 changes: 1 addition & 2 deletions src/object.js
Expand Up @@ -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)

Expand Down
9 changes: 6 additions & 3 deletions src/util/lazy.js
Expand Up @@ -2,22 +2,25 @@ 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')

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)
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/util/reach.js
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)._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);
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)
return obj && obj.resolve(value, parent, value)
}
25 changes: 22 additions & 3 deletions test/yup.js
Expand Up @@ -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);
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit ab78f54

Please sign in to comment.