Skip to content

Commit 139dd24

Browse files
committed
[changed] lazy qualifies as a yup schema
1 parent c8fbe3d commit 139dd24

File tree

5 files changed

+26
-16
lines changed

5 files changed

+26
-16
lines changed

lib/mixed.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,10 @@ SchemaType.prototype = {
103103
if (this._nullable && v === null) return true;
104104
return !this._typeCheck || this._typeCheck(v);
105105
},
106-
resolve: function resolve(context, parent) {
106+
resolve: function resolve(_ref) {
107+
var context = _ref.context;
108+
var parent = _ref.parent;
109+
107110
if (this._conditions.length) {
108111
return this._conditions.reduce(function (schema, match) {
109112
return match.resolve(schema, match.getValue(parent, context));
@@ -115,7 +118,7 @@ SchemaType.prototype = {
115118
cast: function cast(value) {
116119
var opts = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
117120

118-
var schema = this.resolve(opts.context, opts.parent);
121+
var schema = this.resolve(opts);
119122

120123
return schema._cast(value, opts);
121124
},
@@ -138,7 +141,7 @@ SchemaType.prototype = {
138141

139142
if (typeof options === 'function') cb = options, options = {};
140143

141-
var schema = this.resolve(options.context, options.parent);
144+
var schema = this.resolve(options);
142145

143146
return nodeify(schema._validate(value, options), cb);
144147
},
@@ -194,9 +197,9 @@ SchemaType.prototype = {
194197
throw err;
195198
}), cb);
196199
},
197-
getDefault: function getDefault(_ref) {
198-
var context = _ref.context;
199-
var parent = _ref.parent;
200+
getDefault: function getDefault(_ref2) {
201+
var context = _ref2.context;
202+
var parent = _ref2.parent;
200203

201204
return this._resolve(context, parent).default();
202205
},

lib/util/lazy.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
exports.__esModule = true;
44

5+
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; }
6+
57
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
68

79
var _require = require('./_');
@@ -12,24 +14,28 @@ var Lazy = function () {
1214
function Lazy(mapFn) {
1315
_classCallCheck(this, Lazy);
1416

15-
this._resolve = function (value) {
16-
var schema = mapFn(value);
17+
this._resolve = function () {
18+
var schema = mapFn.apply(undefined, arguments);
1719
if (!isSchema(schema)) throw new TypeError('lazy() functions must return a valid schema');
1820

1921
return schema;
2022
};
2123
}
2224

23-
Lazy.prototype.resolve = function resolve(context, parent, value) {
24-
return this._resolve(value);
25+
Lazy.prototype.resolve = function resolve(_ref) {
26+
var value = _ref.value;
27+
28+
var rest = _objectWithoutProperties(_ref, ['value']);
29+
30+
return this._resolve(value, rest);
2531
};
2632

2733
Lazy.prototype.cast = function cast(value, options) {
28-
return this._resolve(value).cast(value, options);
34+
return this._resolve(value, options).cast(value, options);
2935
};
3036

3137
Lazy.prototype.validate = function validate(value, options) {
32-
return this._resolve(value).validate(value, options);
38+
return this._resolve(value, options).validate(value, options);
3339
};
3440

3541
return Lazy;

lib/util/reach.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module.exports = function (obj, path, value, context) {
2626
if (isArray || has(obj, '_subType')) {
2727
// we skipped an array
2828
var idx = isArray ? parseInt(part, 10) : 0;
29-
obj = obj.resolve(context, parent, value)._subType;
29+
obj = obj.resolve({ context: context, parent: parent, value: value })._subType;
3030

3131
if (value) {
3232

@@ -38,7 +38,7 @@ module.exports = function (obj, path, value, context) {
3838
}
3939

4040
if (!isArray) {
41-
obj = obj.resolve(context, parent, value);
41+
obj = obj.resolve({ context: context, parent: parent, value: value });
4242

4343
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 + '") '));
4444

@@ -50,5 +50,5 @@ module.exports = function (obj, path, value, context) {
5050
}
5151
});
5252

53-
return obj && obj.resolve(value, parent, value);
53+
return obj && obj.resolve({ context: context, parent: parent, value: value });
5454
};

src/object.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ function sortFields(fields, excludes = []){
268268

269269
if (Ref.isRef(value) && !value.isContext)
270270
addNode(value.path)
271-
else if (isSchema(value))
271+
else if (isSchema(value) && value._deps)
272272
value._deps.forEach(addNode)
273273
}
274274

src/util/lazy.js

+1
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@ class Lazy {
2626
}
2727
}
2828

29+
Lazy.prototype.__isYupSchema__ = true;
2930

3031
export default Lazy

0 commit comments

Comments
 (0)