Comparing version 0.15.0 to 0.16.0
@@ -0,1 +1,8 @@ | ||
v0.16.0 - Tue, 05 Apr 2016 20:17:40 GMT | ||
--------------------------------------- | ||
- [75739b8](../../commit/75739b8) [added] context sensitive reach() | ||
v0.15.0 - Tue, 29 Mar 2016 14:56:15 GMT | ||
@@ -2,0 +9,0 @@ --------------------------------------- |
@@ -39,2 +39,4 @@ 'use strict'; | ||
this._subType = null; | ||
this.withMutation(function () { | ||
@@ -41,0 +43,0 @@ _this.transform(function (values) { |
@@ -7,2 +7,6 @@ 'use strict'; | ||
var _require2 = require('./_'); | ||
var has = _require2.has; | ||
var trim = function trim(part) { | ||
@@ -12,13 +16,31 @@ return part.substr(0, part.length - 1).substr(1); | ||
module.exports = function (obj, path) { | ||
forEach(path, function (part, isBracket, isArray) { | ||
if (isArray) obj = obj._subType;else { | ||
if (obj._subType) // we skipped an array | ||
obj = obj._subType; | ||
module.exports = function (obj, path, value, context) { | ||
// if only one "value" arg then use it for both | ||
context = context || value; | ||
obj = obj.fields[isBracket ? trim(part) : part]; | ||
var parent = undefined, | ||
lastPart = undefined; | ||
forEach(path, function (_part, isBracket, isArray) { | ||
var part = isBracket ? trim(_part) : _part; | ||
if (isArray || has(obj, '_subType')) { | ||
// we skipped an array | ||
obj = obj._resolve(context, parent)._subType; | ||
value = value && value[0]; | ||
} | ||
if (!isArray) { | ||
obj = obj._resolve(context, parent); | ||
if (!has(obj, 'fields')) throw new Error('The schema does not contain the path: ' + path + '. ' + ('(failed at: ' + lastPart + ' which is a type: "' + obj._type + '") ')); | ||
obj = obj.fields[part]; | ||
parent = value; | ||
value = value && value[part]; | ||
lastPart = isBracket ? '[' + _part + ']' : '.' + _part; | ||
} | ||
}); | ||
return obj; | ||
return obj._resolve(parent); | ||
}; |
@@ -30,2 +30,3 @@ 'use strict'; | ||
this.path = this.isContext ? this.key.slice(this.prefix.length) : this.key; | ||
this._get = getter(this.path); | ||
this.map = mapFn || function (value) { | ||
@@ -41,3 +42,3 @@ return value; | ||
var value = getter(this.path)(isContext ? context : parent || context); | ||
var value = this._get(isContext ? context : parent || context); | ||
@@ -44,0 +45,0 @@ return this.map(value); |
{ | ||
"name": "yup", | ||
"version": "0.15.0", | ||
"version": "0.16.0", | ||
"description": "Dead simple Object schema validation", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -22,3 +22,3 @@ | ||
- [`yup`](#yup) | ||
- [`.reach(Schema schema, String path, Object options)`](#reachschema-schema-string-path-object-options) | ||
- [`.reach(Schema schema, String path, [Object value, Object context])`](#reachschema-schema-string-path-object-value-object-context) | ||
- [`.addMethod(schemaType, name, method)`](#addmethodschematype-name-method) | ||
@@ -143,6 +143,9 @@ - [`ValidationError(String|Array<String> errors, Any value, String path)`](#validationerrorstringarraystring-errors-any-value-string-path) | ||
#### `.reach(Schema schema, String path, Object options)` | ||
#### `.reach(Schema schema, String path, [Object value, Object context])` | ||
For nested schema's `yup.reach` will retrieve a nested schema based on the provided path. | ||
For nested schema that need to resolve dynamically, you can provide a `value` and optionally | ||
a `context` object. | ||
```js | ||
@@ -149,0 +152,0 @@ var schema = object().shape({ |
@@ -23,2 +23,4 @@ 'use strict'; | ||
this._subType = null; | ||
this.withMutation(() => { | ||
@@ -25,0 +27,0 @@ this.transform(function(values) { |
'use strict'; | ||
let { forEach } = require('property-expr'); | ||
let { forEach } = require('property-expr') | ||
, { has } = require('./_'); | ||
let trim = part => part.substr(0, part.length - 1).substr(1) | ||
module.exports = function (obj, path) { | ||
forEach(path, (part, isBracket, isArray) => { | ||
if( isArray) | ||
obj = obj._subType | ||
else { | ||
if (obj._subType) // we skipped an array | ||
obj = obj._subType | ||
obj = obj.fields[isBracket ? trim(part) : part] | ||
} | ||
module.exports = function (obj, path, value, context) { | ||
// if only one "value" arg then use it for both | ||
context = context || value; | ||
let parent, lastPart; | ||
forEach(path, (_part, isBracket, isArray) => { | ||
let part = isBracket ? trim(_part) : _part; | ||
if (isArray || has(obj, '_subType')) { // we skipped an array | ||
obj = obj._resolve(context, parent)._subType; | ||
value = value && value[0] | ||
} | ||
if (!isArray) { | ||
obj = obj._resolve(context, parent); | ||
if (!has(obj, 'fields')) | ||
throw new Error( | ||
`The schema does not contain the path: ${path}. ` + | ||
`(failed at: ${lastPart} which is a type: "${obj._type}") ` | ||
) | ||
obj = obj.fields[part] | ||
parent = value; | ||
value = value && value[part] | ||
lastPart = isBracket ? '[' + _part + ']' : '.' + _part | ||
} | ||
}) | ||
return obj | ||
return obj._resolve(parent) | ||
} |
@@ -21,2 +21,3 @@ var getter = require('property-expr').getter | ||
this.path = this.isContext ? this.key.slice(this.prefix.length) : this.key | ||
this._get = getter(this.path) | ||
this.map = mapFn || (value => value); | ||
@@ -31,3 +32,3 @@ } | ||
let value = getter(this.path)(isContext ? context : (parent || context)) | ||
let value = this._get(isContext ? context : (parent || context)) | ||
@@ -34,0 +35,0 @@ return this.map(value) |
@@ -88,2 +88,46 @@ 'use strict'; | ||
it('should REACH conditionally correctly', function(){ | ||
var num = number() | ||
, inst = object().shape({ | ||
num: number().max(4), | ||
nested: object() | ||
.shape({ | ||
arr: array().when('$bar', function(bar) { | ||
return bar !== 3 | ||
? array().of(number()) | ||
: array().of( | ||
object().shape({ | ||
foo: number(), | ||
num: number().when('foo', (foo) => { | ||
if (foo === 5) | ||
return num | ||
}) | ||
}) | ||
) | ||
}) | ||
}) | ||
}) | ||
let context = { bar: 3 } | ||
let value = { | ||
bar: 3, | ||
nested: { | ||
arr: [{ foo: 5 }] | ||
} | ||
} | ||
reach(inst, 'nested.arr.num', value).should.equal(num) | ||
reach(inst, 'nested.arr[].num', value).should.equal(num) | ||
reach(inst, 'nested.arr.num', value, context).should.equal(num) | ||
reach(inst, 'nested.arr[].num', value, context).should.equal(num) | ||
reach(inst, 'nested.arr[1].num', value, context).should.equal(num) | ||
reach(inst, 'nested["arr"][1].num', value, context).should.not.equal(number()) | ||
return reach(inst, 'nested.arr[].num', value, context).isValid(5) | ||
.then((valid) => { | ||
valid.should.equal(true) | ||
}) | ||
}) | ||
describe('BadSet', function(){ | ||
@@ -90,0 +134,0 @@ it('should preserve primitive types', function(){ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
366497
9591
894