diff --git a/lib/mixed.js b/lib/mixed.js index 581ed8e..b2300c8 100644 --- a/lib/mixed.js +++ b/lib/mixed.js @@ -103,7 +103,10 @@ SchemaType.prototype = { if (this._nullable && v === null) return true; return !this._typeCheck || this._typeCheck(v); }, - resolve: function resolve(context, parent) { + resolve: function resolve(_ref) { + var context = _ref.context; + var parent = _ref.parent; + if (this._conditions.length) { return this._conditions.reduce(function (schema, match) { return match.resolve(schema, match.getValue(parent, context)); @@ -115,7 +118,7 @@ SchemaType.prototype = { cast: function cast(value) { var opts = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; - var schema = this.resolve(opts.context, opts.parent); + var schema = this.resolve(opts); return schema._cast(value, opts); }, @@ -138,7 +141,7 @@ SchemaType.prototype = { if (typeof options === 'function') cb = options, options = {}; - var schema = this.resolve(options.context, options.parent); + var schema = this.resolve(options); return nodeify(schema._validate(value, options), cb); }, @@ -194,9 +197,9 @@ SchemaType.prototype = { throw err; }), cb); }, - getDefault: function getDefault(_ref) { - var context = _ref.context; - var parent = _ref.parent; + getDefault: function getDefault(_ref2) { + var context = _ref2.context; + var parent = _ref2.parent; return this._resolve(context, parent).default(); }, diff --git a/lib/util/lazy.js b/lib/util/lazy.js index 47cd342..0025480 100644 --- a/lib/util/lazy.js +++ b/lib/util/lazy.js @@ -2,6 +2,8 @@ exports.__esModule = true; +function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } + function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var _require = require('./_'); @@ -12,24 +14,28 @@ var Lazy = function () { function Lazy(mapFn) { _classCallCheck(this, Lazy); - this._resolve = function (value) { - var schema = mapFn(value); + this._resolve = function () { + var schema = mapFn.apply(undefined, arguments); if (!isSchema(schema)) throw new TypeError('lazy() functions must return a valid schema'); return schema; }; } - Lazy.prototype.resolve = function resolve(context, parent, value) { - return this._resolve(value); + Lazy.prototype.resolve = function resolve(_ref) { + var value = _ref.value; + + var rest = _objectWithoutProperties(_ref, ['value']); + + return this._resolve(value, rest); }; Lazy.prototype.cast = function cast(value, options) { - return this._resolve(value).cast(value, options); + return this._resolve(value, options).cast(value, options); }; Lazy.prototype.validate = function validate(value, options) { - return this._resolve(value).validate(value, options); + return this._resolve(value, options).validate(value, options); }; return Lazy; diff --git a/lib/util/reach.js b/lib/util/reach.js index 5703f0b..b15405e 100644 --- a/lib/util/reach.js +++ b/lib/util/reach.js @@ -26,7 +26,7 @@ module.exports = function (obj, path, value, context) { if (isArray || has(obj, '_subType')) { // we skipped an array var idx = isArray ? parseInt(part, 10) : 0; - obj = obj.resolve(context, parent, value)._subType; + obj = obj.resolve({ context: context, parent: parent, value: value })._subType; if (value) { @@ -38,7 +38,7 @@ module.exports = function (obj, path, value, context) { } if (!isArray) { - obj = obj.resolve(context, parent, value); + obj = obj.resolve({ context: context, parent: parent, value: value }); 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 + '") ')); @@ -50,5 +50,5 @@ module.exports = function (obj, path, value, context) { } }); - return obj && obj.resolve(value, parent, value); + return obj && obj.resolve({ context: context, parent: parent, value: value }); }; \ No newline at end of file diff --git a/src/object.js b/src/object.js index 2e706dc..85af273 100644 --- a/src/object.js +++ b/src/object.js @@ -268,7 +268,7 @@ function sortFields(fields, excludes = []){ if (Ref.isRef(value) && !value.isContext) addNode(value.path) - else if (isSchema(value)) + else if (isSchema(value) && value._deps) value._deps.forEach(addNode) } diff --git a/src/util/lazy.js b/src/util/lazy.js index 15d26a3..d9ed062 100644 --- a/src/util/lazy.js +++ b/src/util/lazy.js @@ -26,5 +26,6 @@ class Lazy { } } +Lazy.prototype.__isYupSchema__ = true; export default Lazy