Skip to content

Commit 66826e7

Browse files
committed
rebuild
1 parent 577ffb6 commit 66826e7

File tree

7 files changed

+55
-31
lines changed

7 files changed

+55
-31
lines changed

lib/boolean.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ function BooleanSchema() {
1919
inherits(BooleanSchema, MixedSchema, {
2020

2121
_typeCheck: function _typeCheck(v) {
22-
return typeof v === 'boolean';
22+
return typeof v === 'boolean' || typeof v === 'object' && v instanceof Boolean;
2323
}
2424
});

lib/mixed.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,18 @@ SchemaType.prototype = {
3939
constructor: SchemaType,
4040

4141
clone: function clone() {
42+
if (this._mutate) return this;
43+
4244
return cloneDeep(this);
4345
},
4446

47+
withMutation: function withMutation(fn) {
48+
this._mutate = true;
49+
var result = fn(this);
50+
this._mutate = false;
51+
return result;
52+
},
53+
4554
concat: function concat(schema) {
4655
if (!schema) return this;
4756

@@ -85,22 +94,25 @@ SchemaType.prototype = {
8594
},
8695

8796
_resolve: function _resolve(context, parent) {
88-
var schema = this;
97+
if (this._deps.length) {
98+
return this._deps.reduce(function (schema, match) {
99+
return match.resolve(schema, match.getValue(parent, context));
100+
}, this);
101+
}
89102

90-
return this._deps.reduce(function (schema, match) {
91-
return match.resolve(schema, match.getValue(parent, context));
92-
}, schema);
103+
return this;
93104
},
94105

95106
//-- tests
96-
_validate: function _validate(value) {
107+
_validate: function _validate(_value) {
97108
var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
98109
var state = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
99110

100111
var valids = this._whitelist,
101112
invalids = this._blacklist,
102113
context = options.context,
103114
parent = state.parent,
115+
value = _value,
104116
schema = undefined,
105117
endEarly = undefined,
106118
isStrict = undefined;

lib/number.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ function NumberSchema() {
2525
inherits(NumberSchema, SchemaObject, {
2626

2727
_typeCheck: function _typeCheck(v) {
28-
return typeof v === 'number' && !(v !== +v); //isNaN check
28+
if (typeof v === 'number' && !(v !== +v)) return true;
29+
if (typeof v === 'object' && v instanceof Number) return true;
30+
31+
return false;
2932
},
3033

3134
min: function min(_min, msg) {

lib/object.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -83,46 +83,50 @@ inherits(ObjectSchema, MixedSchema, {
8383
value = MixedSchema.prototype._cast.call(schema, _value);
8484

8585
//should ignore nulls here
86-
if (schema._typeCheck(value)) {
87-
var fields = schema.fields,
88-
strip = schema._option('stripUnknown', _opts) === true,
89-
extra = Object.keys(value).filter(function (v) {
90-
return schema._nodes.indexOf(v) === -1;
91-
}),
92-
props = schema._nodes.concat(extra);
93-
94-
return transform(props, function (obj, prop) {
86+
if (!schema._typeCheck(value)) return value;
87+
88+
var fields = schema.fields,
89+
strip = schema._option('stripUnknown', _opts) === true,
90+
extra = Object.keys(value).filter(function (v) {
91+
return schema._nodes.indexOf(v) === -1;
92+
}),
93+
props = schema._nodes.concat(extra);
94+
95+
schema.withMutation(function () {
96+
value = transform(props, function (obj, prop) {
9597
var exists = has(value, prop);
9698

9799
if (exists && fields[prop]) {
98100
var fieldSchema = childSchema(fields[prop], schema['default'](undefined));
99101

100102
obj[prop] = fieldSchema.cast(value[prop], { context: obj });
101-
} else if (exists && !strip) obj[prop] = cloneDeep(value[prop]);else if (fields[prop]) {
103+
} else if (exists && !strip) obj[prop] = value[prop];else if (fields[prop]) {
102104
var fieldDefault = fields[prop]['default'] ? fields[prop]['default']() : undefined;
103105

104106
if (fieldDefault !== undefined) obj[prop] = fieldDefault;
105107
}
106108
}, {});
107-
}
109+
110+
delete schema._default;
111+
});
108112

109113
return value;
110114
},
111115

112116
_validate: function _validate(_value, _opts, _state) {
113117
var errors = [],
118+
state = _state || {},
114119
context,
115120
schema,
116121
endEarly,
117122
recursive;
118123

119-
_state = _state || {};
120-
context = _state.parent || (_opts || {}).context;
124+
context = state.parent || (_opts || {}).context;
121125
schema = this._resolve(context);
122126
endEarly = schema._option('abortEarly', _opts);
123127
recursive = schema._option('recursive', _opts);
124128

125-
return MixedSchema.prototype._validate.call(this, _value, _opts, _state)['catch'](endEarly ? null : function (err) {
129+
return MixedSchema.prototype._validate.call(this, _value, _opts, state)['catch'](endEarly ? null : function (err) {
126130
errors.push(err);
127131
return err.value;
128132
}).then(function (value) {
@@ -133,13 +137,13 @@ inherits(ObjectSchema, MixedSchema, {
133137
}
134138

135139
var result = schema._nodes.map(function (key) {
136-
var path = (_state.path ? _state.path + '.' : '') + key,
140+
var path = (state.path ? state.path + '.' : '') + key,
137141
field = childSchema(schema.fields[key], schema);
138142

139-
return field._validate(value[key], _opts, _extends({}, _state, { key: key, path: path, parent: value }));
143+
return field._validate(value[key], _opts, _extends({}, state, { key: key, path: path, parent: value }));
140144
});
141145

142-
result = endEarly ? Promise.all(result)['catch'](scopeError(value)) : collectErrors(result, value, _state.path, errors);
146+
result = endEarly ? Promise.all(result)['catch'](scopeError(value)) : collectErrors(result, value, state.path, errors);
143147

144148
return result.then(function () {
145149
return value;

lib/string.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function StringSchema() {
2626
inherits(StringSchema, MixedSchema, {
2727

2828
_typeCheck: function _typeCheck(value) {
29-
return typeof value === 'string';
29+
return typeof value === 'string' || typeof value === 'object' && value instanceof String;
3030
},
3131

3232
required: function required(msg) {

lib/util/clone.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,18 @@
44

55
'use strict';
66

7+
var isSchema = function isSchema(schema) {
8+
return schema && !!schema.__isYupSchema__;
9+
};
10+
711
module.exports = function clone(obj, seen) {
8-
var isFirst = !seen;
12+
var isFirst = !seen,
13+
isImmutable = isSchema(obj) && !isFirst;
14+
15+
if (typeof obj !== 'object' || obj === null || isImmutable) return obj;
916

10-
if (typeof obj !== 'object' || obj === null) return obj;
17+
// if (global.REPORT_CLONE && isFirst)
18+
// throw new Error() //console.log('clone')
1119

1220
seen = seen || { orig: [], copy: [] };
1321

@@ -26,7 +34,7 @@ module.exports = function clone(obj, seen) {
2634
} else {
2735
var proto = Object.getPrototypeOf(obj);
2836

29-
if (proto !== null && (!proto || proto.__isYupSchema__ && !isFirst)) {
37+
if (proto !== null && !proto) {
3038
newObj = obj;
3139
} else {
3240
newObj = Object.create(proto);

lib/util/condition.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ var Conditional = (function () {
2828

2929
if (!options.then && !options.otherwise) throw new TypeError('either `then:` or `otherwise:` is required for `when()` conditions');
3030

31-
// if( options.then && options.then._type !== type || options.otherwise && options.otherwise._type !== type)
32-
// throw new TypeError(`cannot create polymorphic conditionals, \`then\` and \`otherwise\` must be the same type: ${type}`)
33-
3431
is = typeof is === 'function' ? is : (function (is, value) {
3532
return is === value;
3633
}).bind(null, is);

0 commit comments

Comments
 (0)