Skip to content
This repository has been archived by the owner on Aug 26, 2022. It is now read-only.

Commit

Permalink
[changed] lazy qualifies as a yup schema
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense committed Apr 21, 2016
1 parent c8fbe3d commit 139dd24
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 16 deletions.
15 changes: 9 additions & 6 deletions lib/mixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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);
},
Expand All @@ -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);
},
Expand Down Expand Up @@ -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();
},
Expand Down
18 changes: 12 additions & 6 deletions lib/util/lazy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('./_');
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions lib/util/reach.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand All @@ -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 + '") '));

Expand All @@ -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 });
};
2 changes: 1 addition & 1 deletion src/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
1 change: 1 addition & 0 deletions src/util/lazy.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ class Lazy {
}
}

Lazy.prototype.__isYupSchema__ = true;

export default Lazy

0 comments on commit 139dd24

Please sign in to comment.