Skip to content

Commit

Permalink
rebuild
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense committed Apr 5, 2016
1 parent 0448201 commit 9095d4c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ json separate from validating it, via the `cast` method.
- [Usage](#usage)
- [API](#api)
- [`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)
- [`ValidationError(String|Array<String> errors, Any value, String path)`](#validationerrorstringarraystring-errors-any-value-string-path)
- [`ref(String path, Object options)`](#refstring-path-object-options)
Expand Down
2 changes: 2 additions & 0 deletions lib/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ function ArraySchema() {

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

this._subType = null;

this.withMutation(function () {
_this.transform(function (values) {
if (typeof values === 'string') try {
Expand Down
37 changes: 30 additions & 7 deletions lib/util/reach.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,42 @@ var _require = require('property-expr');

var forEach = _require.forEach;

var _require2 = require('./_');

var has = _require2.has;

var trim = function trim(part) {
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) {
var parent = undefined,
lastPart = undefined;

// if only one "value" arg then use it for both
context = context || value;

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') || !has(obj.fields, part)) throw new Error('The schema does not contain the path: ' + path + '. ' + ('(failed at: ' + lastPart + ' which is a type: "' + obj._type + '") '));

obj = obj.fields[part];

obj = obj.fields[isBracket ? trim(part) : part];
parent = value;
value = value && value[part];
lastPart = isBracket ? '[' + _part + ']' : '.' + _part;
}
});

return obj;
return obj && obj._resolve(parent);
};
7 changes: 2 additions & 5 deletions lib/util/reference.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,15 @@ var Ref = (function () {
this.prefix = prefix;
this.isContext = key.indexOf(prefix) === 0;
this.path = this.isContext ? this.key.slice(this.prefix.length) : this.key;
this._get = getter(this.path, true);
this.map = mapFn || function (value) {
return value;
};
}

Ref.prototype.getValue = function getValue(parent, context) {
var isContext = this.isContext;

if (isContext && !context || !isContext && !context && !parent) throw new Error('missing the context necessary to cast this value');

var value = getter(this.path)(isContext ? context : parent || context);

var value = this._get(isContext ? context : parent || context || {});
return this.map(value);
};

Expand Down

0 comments on commit 9095d4c

Please sign in to comment.