Skip to content

Commit 9095d4c

Browse files
committed
rebuild
1 parent 0448201 commit 9095d4c

File tree

4 files changed

+35
-13
lines changed

4 files changed

+35
-13
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ json separate from validating it, via the `cast` method.
1919
- [Usage](#usage)
2020
- [API](#api)
2121
- [`yup`](#yup)
22-
- [`.reach(Schema schema, String path, Object options)`](#reachschema-schema-string-path-object-options)
22+
- [`.reach(Schema schema, String path, [Object value, Object context])`](#reachschema-schema-string-path-object-value-object-context)
2323
- [`.addMethod(schemaType, name, method)`](#addmethodschematype-name-method)
2424
- [`ValidationError(String|Array<String> errors, Any value, String path)`](#validationerrorstringarraystring-errors-any-value-string-path)
2525
- [`ref(String path, Object options)`](#refstring-path-object-options)

lib/array.js

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ function ArraySchema() {
3636

3737
MixedSchema.call(this, { type: 'array' });
3838

39+
this._subType = null;
40+
3941
this.withMutation(function () {
4042
_this.transform(function (values) {
4143
if (typeof values === 'string') try {

lib/util/reach.js

+30-7
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,42 @@ var _require = require('property-expr');
44

55
var forEach = _require.forEach;
66

7+
var _require2 = require('./_');
8+
9+
var has = _require2.has;
10+
711
var trim = function trim(part) {
812
return part.substr(0, part.length - 1).substr(1);
913
};
1014

11-
module.exports = function (obj, path) {
12-
forEach(path, function (part, isBracket, isArray) {
13-
if (isArray) obj = obj._subType;else {
14-
if (obj._subType) // we skipped an array
15-
obj = obj._subType;
15+
module.exports = function (obj, path, value, context) {
16+
var parent = undefined,
17+
lastPart = undefined;
18+
19+
// if only one "value" arg then use it for both
20+
context = context || value;
21+
22+
forEach(path, function (_part, isBracket, isArray) {
23+
var part = isBracket ? trim(_part) : _part;
24+
25+
if (isArray || has(obj, '_subType')) {
26+
// we skipped an array
27+
obj = obj._resolve(context, parent)._subType;
28+
value = value && value[0];
29+
}
30+
31+
if (!isArray) {
32+
obj = obj._resolve(context, parent);
33+
34+
if (!has(obj, 'fields') || !has(obj.fields, part)) throw new Error('The schema does not contain the path: ' + path + '. ' + ('(failed at: ' + lastPart + ' which is a type: "' + obj._type + '") '));
35+
36+
obj = obj.fields[part];
1637

17-
obj = obj.fields[isBracket ? trim(part) : part];
38+
parent = value;
39+
value = value && value[part];
40+
lastPart = isBracket ? '[' + _part + ']' : '.' + _part;
1841
}
1942
});
2043

21-
return obj;
44+
return obj && obj._resolve(parent);
2245
};

lib/util/reference.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,15 @@ var Ref = (function () {
2727
this.prefix = prefix;
2828
this.isContext = key.indexOf(prefix) === 0;
2929
this.path = this.isContext ? this.key.slice(this.prefix.length) : this.key;
30+
this._get = getter(this.path, true);
3031
this.map = mapFn || function (value) {
3132
return value;
3233
};
3334
}
3435

3536
Ref.prototype.getValue = function getValue(parent, context) {
3637
var isContext = this.isContext;
37-
38-
if (isContext && !context || !isContext && !context && !parent) throw new Error('missing the context necessary to cast this value');
39-
40-
var value = getter(this.path)(isContext ? context : parent || context);
41-
38+
var value = this._get(isContext ? context : parent || context || {});
4239
return this.map(value);
4340
};
4441

0 commit comments

Comments
 (0)