Skip to content

Commit 5252948

Browse files
committed
rebuild
1 parent 6fd19be commit 5252948

9 files changed

+186
-119
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ json separate from validating it, via the `cast` method.
2222
- [`yup.reach(schema: Schema, path: string, value: ?object, context: ?object): Schema`](#yupreachschema-schema-path-string-value-object-context-object-schema)
2323
- [`yup.addMethod(schemaType: Schema, name: string, method: ()=> Schema): void`](#yupaddmethodschematype-schema-name-string-method--schema-void)
2424
- [`yup.ref(path: string, options: { contextPrefix: string }): Ref`](#yuprefpath-string-options--contextprefix-string--ref)
25+
- [`yup.lazy((value: any) => Schema): Lazy`](#yuplazyvalue-any--schema-lazy)
2526
- [`ValidationError(errors: string | Array<string>, value: any, path: string)`](#validationerrorerrors-string--arraystring-value-any-path-string)
2627
- [mixed](#mixed)
2728
- [`mixed.clone(): Schema`](#mixedclone-schema)

lib/array.js

+19-16
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ inherits(ArraySchema, MixedSchema, {
6161
_cast: function _cast(_value, _opts) {
6262
var _this2 = this;
6363

64-
var value = MixedSchema.prototype._cast.call(this, _value);
64+
var value = MixedSchema.prototype._cast.call(this, _value, _opts);
6565

6666
//should ignore nulls here
6767
if (!this._typeCheck(value) || !this._subType) return value;
@@ -70,38 +70,41 @@ inherits(ArraySchema, MixedSchema, {
7070
return _this2._subType.cast(v, _opts);
7171
});
7272
},
73-
_validate: function _validate(_value, _opts, _state) {
73+
_validate: function _validate(_value) {
74+
var _this3 = this;
75+
76+
var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
77+
7478
var errors = [],
75-
context,
7679
subType,
77-
schema,
7880
endEarly,
7981
recursive;
8082

81-
_state = _state || {};
82-
context = _state.parent || (_opts || {}).context;
83-
schema = this._resolve(context);
84-
subType = schema._subType;
85-
endEarly = schema._option('abortEarly', _opts);
86-
recursive = schema._option('recursive', _opts);
83+
subType = this._subType;
84+
endEarly = this._option('abortEarly', options);
85+
recursive = this._option('recursive', options);
8786

88-
return MixedSchema.prototype._validate.call(this, _value, _opts, _state).catch(endEarly ? null : function (err) {
87+
return MixedSchema.prototype._validate.call(this, _value, options).catch(endEarly ? null : function (err) {
8988
errors = err;
9089
return err.value;
9190
}).then(function (value) {
92-
if (!recursive || !subType || !schema._typeCheck(value)) {
91+
if (!recursive || !subType || !_this3._typeCheck(value)) {
9392
if (errors.length) throw errors[0];
9493
return value;
9594
}
9695

9796
var result = value.map(function (item, key) {
98-
var path = (_state.path || '') + '[' + key + ']',
99-
state = _extends({}, _state, { path: path, key: key, parent: value });
97+
var path = (options.path || '') + '[' + key + ']';
98+
99+
// object._validate note for isStrict explanation
100+
var innerOptions = _extends({}, options, { path: path, key: key, strict: true, parent: value });
101+
102+
if (subType.validate) return subType.validate(item, innerOptions);
100103

101-
return subType._validate(item, _opts, state);
104+
return true;
102105
});
103106

104-
result = endEarly ? Promise.all(result).catch(scopeError(value)) : collectErrors(result, value, _state.path, errors);
107+
result = endEarly ? Promise.all(result).catch(scopeError(value)) : collectErrors(result, value, options.path, errors);
105108

106109
return result.then(function () {
107110
return value;

lib/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
var mixed = require('./mixed'),
44
bool = require('./boolean'),
5-
Ref = require('./util/reference');
5+
Ref = require('./util/reference'),
6+
Lazy = require('./util/lazy');
67

78
var isSchema = function isSchema(schema) {
89
return schema && !!schema.__isYupSchema__;
@@ -24,6 +25,9 @@ module.exports = {
2425
ref: function ref(key, options) {
2526
return new Ref(key, options);
2627
},
28+
lazy: function lazy(fn) {
29+
return new Lazy(fn);
30+
},
2731

2832
isSchema: isSchema,
2933

lib/mixed.js

+37-29
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,20 @@ SchemaType.prototype = {
103103
if (this._nullable && v === null) return true;
104104
return !this._typeCheck || this._typeCheck(v);
105105
},
106+
resolve: function resolve(context, parent) {
107+
if (this._conditions.length) {
108+
return this._conditions.reduce(function (schema, match) {
109+
return match.resolve(schema, match.getValue(parent, context));
110+
}, this);
111+
}
112+
113+
return this;
114+
},
106115
cast: function cast(value) {
107116
var opts = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
108117

109-
var schema = this._resolve(opts.context, opts.parent);
118+
var schema = this.resolve(opts.context, opts.parent);
119+
110120
return schema._cast(value, opts);
111121
},
112122
_cast: function _cast(_value) {
@@ -116,65 +126,63 @@ SchemaType.prototype = {
116126
return transform.call(_this2, value, _value);
117127
}, _value);
118128

119-
if (value === undefined && _.has(this, '_default')) value = this.default();
129+
if (value === undefined && _.has(this, '_default')) {
130+
value = this.default();
131+
}
120132

121133
return value;
122134
},
123-
_resolve: function _resolve(context, parent) {
124-
if (this._conditions.length) {
125-
return this._conditions.reduce(function (schema, match) {
126-
return match.resolve(schema, match.getValue(parent, context));
127-
}, this);
128-
}
135+
validate: function validate(value) {
136+
var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
137+
var cb = arguments[2];
129138

130-
return this;
139+
if (typeof options === 'function') cb = options, options = {};
140+
141+
var schema = this.resolve(options.context, options.parent);
142+
143+
return nodeify(schema._validate(value, options), cb);
131144
},
132145

133146

134147
//-- tests
135148
_validate: function _validate(_value) {
149+
var _this3 = this;
150+
136151
var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
137-
var state = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
138152

139-
var context = options.context,
140-
parent = state.parent,
141-
value = _value,
153+
var value = _value,
142154
schema = void 0,
143155
endEarly = void 0,
144156
isStrict = void 0;
145157

146-
schema = this._resolve(context, parent);
147-
isStrict = schema._option('strict', options);
148-
endEarly = schema._option('abortEarly', options);
158+
schema = this;
159+
isStrict = this._option('strict', options);
160+
endEarly = this._option('abortEarly', options);
149161

150-
var path = state.path;
162+
var path = options.path;
151163
var label = this._label;
152164

153-
if (!state.isCast && !isStrict) value = schema._cast(value, options);
154-
165+
if (!isStrict) {
166+
value = this._cast(value, options, options);
167+
}
155168
// value is cast, we can check if it meets type requirements
156-
var validationParams = { value: value, path: path, state: state, schema: schema, options: options, label: label };
169+
var validationParams = { value: value, path: path, schema: this, options: options, label: label };
157170
var initialTests = [];
158171

159-
if (schema._typeError) initialTests.push(schema._typeError(validationParams));
172+
if (schema._typeError) initialTests.push(this._typeError(validationParams));
160173

161-
if (schema._whitelistError) initialTests.push(schema._whitelistError(validationParams));
174+
if (this._whitelistError) initialTests.push(this._whitelistError(validationParams));
162175

163-
if (schema._blacklistError) initialTests.push(schema._blacklistError(validationParams));
176+
if (this._blacklistError) initialTests.push(this._blacklistError(validationParams));
164177

165178
return runValidations(initialTests, endEarly, value, path).then(function () {
166-
return runValidations(schema.tests.map(function (fn) {
179+
return runValidations(_this3.tests.map(function (fn) {
167180
return fn(validationParams);
168181
}), endEarly, value, path);
169182
}).then(function () {
170183
return value;
171184
});
172185
},
173-
validate: function validate(value, options, cb) {
174-
if (typeof options === 'function') cb = options, options = {};
175-
176-
return nodeify(this._validate(value, options, {}), cb);
177-
},
178186
isValid: function isValid(value, options, cb) {
179187
if (typeof options === 'function') cb = options, options = {};
180188

lib/object.js

+57-63
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,19 @@ var _require = require('./util/_');
1414

1515
var isObject = _require.isObject;
1616
var transform = _require.transform;
17-
var assign = _require.assign;
1817
var inherits = _require.inherits;
1918
var collectErrors = _require.collectErrors;
2019
var isSchema = _require.isSchema;
2120
var has = _require.has;
2221

2322

24-
var isRecursive = function isRecursive(schema) {
25-
return (schema._subType || schema) === '$this';
26-
};
27-
2823
c.type('altCamel', function (str) {
2924
var result = c.camel(str),
3025
idx = str.search(/[^_]/);
3126

3227
return idx === 0 ? result : str.substr(0, idx) + result;
3328
});
3429

35-
var childSchema = function childSchema(field, parent) {
36-
if (!isRecursive(field)) return field;
37-
38-
return field.of ? field.of(parent) : parent;
39-
};
40-
4130
var scopeError = function scopeError(value) {
4231
return function (err) {
4332
err.value = value;
@@ -89,64 +78,58 @@ inherits(ObjectSchema, MixedSchema, {
8978
return isObject(value) || typeof value === 'function';
9079
},
9180
_cast: function _cast(_value) {
92-
var _opts = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
81+
var _this3 = this;
9382

94-
var schema = this,
95-
value = MixedSchema.prototype._cast.call(schema, _value);
83+
var opts = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
84+
85+
var value = MixedSchema.prototype._cast.call(this, _value, opts);
9686

9787
//should ignore nulls here
98-
if (!schema._typeCheck(value)) return value;
88+
if (value === undefined) return this.default();
89+
90+
if (!this._typeCheck(value)) return value;
9991

100-
var fields = schema.fields,
101-
strip = schema._option('stripUnknown', _opts) === true,
92+
var fields = this.fields,
93+
strip = this._option('stripUnknown', opts) === true,
10294
extra = Object.keys(value).filter(function (v) {
103-
return schema._nodes.indexOf(v) === -1;
95+
return _this3._nodes.indexOf(v) === -1;
10496
}),
105-
props = schema._nodes.concat(extra);
97+
props = this._nodes.concat(extra);
10698

107-
schema.withMutation(function () {
108-
var innerOptions = _extends({}, _opts, { parent: {} });
99+
var innerOptions = _extends({}, opts, { parent: {} });
109100

110-
value = transform(props, function (obj, prop) {
111-
var field = fields[prop];
112-
var exists = has(value, prop);
101+
value = transform(props, function (obj, prop) {
102+
var field = fields[prop];
103+
var exists = has(value, prop);
113104

114-
if (Ref.isRef(field)) {
115-
var refValue = field.getValue(obj, innerOptions.context);
105+
if (field) {
106+
var fieldValue = void 0;
116107

117-
if (refValue !== undefined) obj[prop] = refValue;
118-
} else if (exists && field) {
119-
tempClearDefault(schema, function () {
120-
var fieldSchema = childSchema(field, schema.default(undefined));
108+
if (field._strip === true) return;
121109

122-
if (fieldSchema._strip !== true) {
123-
obj[prop] = fieldSchema.cast(value[prop], innerOptions);
124-
}
125-
});
126-
} else if (exists && !strip) obj[prop] = value[prop];else if (field) {
127-
var fieldDefault = field.default ? field.default() : undefined;
110+
fieldValue = field.cast(value[prop], innerOptions);
128111

129-
if (fieldDefault !== undefined) obj[prop] = fieldDefault;
130-
}
131-
}, innerOptions.parent);
132-
});
112+
if (fieldValue !== undefined) obj[prop] = fieldValue;
113+
} else if (exists && !strip) obj[prop] = value[prop];
114+
}, innerOptions.parent);
133115

134116
return value;
135117
},
136-
_validate: function _validate(_value, _opts, _state) {
118+
_validate: function _validate(_value) {
119+
var _this4 = this;
120+
121+
var opts = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
122+
137123
var errors = [],
138-
state = _state || {},
139-
context,
140-
schema,
141124
endEarly,
125+
isStrict,
142126
recursive;
143127

144-
context = state.parent || (_opts || {}).context;
145-
schema = this._resolve(context);
146-
endEarly = schema._option('abortEarly', _opts);
147-
recursive = schema._option('recursive', _opts);
128+
isStrict = this._option('strict', opts);
129+
endEarly = this._option('abortEarly', opts);
130+
recursive = this._option('recursive', opts);
148131

149-
return MixedSchema.prototype._validate.call(this, _value, _opts, state).catch(endEarly ? null : function (err) {
132+
return MixedSchema.prototype._validate.call(this, _value, opts).catch(endEarly ? null : function (err) {
150133
errors.push(err);
151134
return err.value;
152135
}).then(function (value) {
@@ -156,14 +139,24 @@ inherits(ObjectSchema, MixedSchema, {
156139
return value;
157140
}
158141

159-
var result = schema._nodes.map(function (key) {
160-
var path = (state.path ? state.path + '.' : '') + key,
161-
field = childSchema(schema.fields[key], schema);
142+
var result = _this4._nodes.map(function (key) {
143+
var path = (opts.path ? opts.path + '.' : '') + key,
144+
field = _this4.fields[key],
145+
innerOptions = _extends({}, opts, { key: key, path: path, parent: value });
146+
147+
if (field) {
148+
// inner fields are always strict:
149+
// 1. this isn't strict so we just cast the value leaving nested values already cast
150+
// 2. this is strict in which case the nested values weren't cast either
151+
innerOptions.strict = true;
152+
153+
if (field.validate) return field.validate(value[key], innerOptions);
154+
}
162155

163-
return field._validate(value[key], _opts, _extends({}, state, { key: key, path: path, parent: value }));
156+
return true;
164157
});
165158

166-
result = endEarly ? Promise.all(result).catch(scopeError(value)) : collectErrors(result, value, state.path, errors);
159+
result = endEarly ? Promise.all(result).catch(scopeError(value)) : collectErrors(result, value, opts.path, errors);
167160

168161
return result.then(function () {
169162
return value;
@@ -181,7 +174,7 @@ inherits(ObjectSchema, MixedSchema, {
181174
var excludes = arguments.length <= 1 || arguments[1] === undefined ? [] : arguments[1];
182175

183176
var next = this.clone(),
184-
fields = assign(next.fields, schema);
177+
fields = _extends(next.fields, schema);
185178

186179
if (!Array.isArray(excludes[0])) excludes = [excludes];
187180

@@ -257,14 +250,15 @@ function unknown(ctx, value) {
257250

258251
// ugly optimization avoiding a clone. clears default for recursive
259252
// cast and resets it below;
260-
function tempClearDefault(schema, fn) {
261-
var hasDflt = has(schema, '_default'),
262-
dflt = schema._default;
263-
264-
fn(schema);
265-
266-
if (hasDflt) schema.default(dflt);else delete schema._default;
267-
}
253+
// function tempClearDefault(schema, fn) {
254+
// let hasDflt = has(schema, '_default')
255+
// , dflt = schema._default;
256+
//
257+
// fn(schema)
258+
//
259+
// if (hasDflt) schema.default(dflt)
260+
// else delete schema._default
261+
// }
268262

269263
function sortFields(fields) {
270264
var excludes = arguments.length <= 1 || arguments[1] === undefined ? [] : arguments[1];

0 commit comments

Comments
 (0)