Skip to content

Commit 5d65038

Browse files
committed
add type to error
1 parent 055707d commit 5d65038

File tree

19 files changed

+1595
-2
lines changed

19 files changed

+1595
-2
lines changed

lib/array.js

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
'use strict';
2+
3+
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
4+
5+
var MixedSchema = require('./mixed');
6+
var Promise = require('promise/lib/es6-extensions');
7+
8+
var _require = require('./locale.js');
9+
10+
var mixed = _require.mixed;
11+
var locale = _require.array;
12+
13+
var _require2 = require('./util/_');
14+
15+
var inherits = _require2.inherits;
16+
var collectErrors = _require2.collectErrors;
17+
18+
var scopeError = function scopeError(value) {
19+
return function (err) {
20+
err.value = value;
21+
throw err;
22+
};
23+
};
24+
25+
module.exports = ArraySchema;
26+
27+
function ArraySchema() {
28+
if (!(this instanceof ArraySchema)) return new ArraySchema();
29+
30+
MixedSchema.call(this, { type: 'array' });
31+
32+
this.transforms.push(function (values) {
33+
if (typeof values === 'string') try {
34+
values = JSON.parse(values);
35+
} catch (err) {
36+
values = null;
37+
}
38+
39+
if (Array.isArray(values)) return this._subType ? values.map(this._subType.cast, this._subType) : values;
40+
41+
return this.isType(values) ? values : null;
42+
});
43+
}
44+
45+
inherits(ArraySchema, MixedSchema, {
46+
47+
_typeCheck: function _typeCheck(v) {
48+
return Array.isArray(v);
49+
},
50+
51+
_validate: function _validate(_value, _opts, _state) {
52+
var errors = [],
53+
context,
54+
subType,
55+
schema,
56+
endEarly,
57+
recursive;
58+
59+
_state = _state || {};
60+
context = _state.parent || (_opts || {}).context;
61+
schema = this._resolve(context);
62+
subType = schema._subType;
63+
endEarly = schema._option('abortEarly', _opts);
64+
recursive = schema._option('recursive', _opts);
65+
66+
return MixedSchema.prototype._validate.call(this, _value, _opts, _state)['catch'](endEarly ? null : function (err) {
67+
errors = err;
68+
return err.value;
69+
}).then(function (value) {
70+
if (!recursive || !subType || !schema._typeCheck(value)) {
71+
if (errors.length) throw errors[0];
72+
return value;
73+
}
74+
75+
var result = value.map(function (item, key) {
76+
var path = (_state.path || '') + '[' + key + ']',
77+
state = _extends({}, _state, { path: path, key: key, parent: value });
78+
79+
return subType._validate(item, _opts, state);
80+
});
81+
82+
result = endEarly ? Promise.all(result)['catch'](scopeError(value)) : collectErrors(result, value, _state.path, errors);
83+
84+
return result.then(function () {
85+
return value;
86+
});
87+
});
88+
},
89+
90+
of: function of(schema) {
91+
var next = this.clone();
92+
next._subType = schema;
93+
return next;
94+
},
95+
96+
required: function required(msg) {
97+
var next = MixedSchema.prototype.required.call(this, msg || mixed.required);
98+
99+
return next.min(1, msg || mixed.required);
100+
},
101+
102+
min: function min(_min, message) {
103+
message = message || locale.min;
104+
105+
return this.test({
106+
message: message,
107+
name: 'min',
108+
exclusive: true,
109+
params: { min: _min },
110+
test: function test(value) {
111+
return value && value.length >= _min;
112+
}
113+
});
114+
},
115+
116+
max: function max(_max, message) {
117+
message = message || locale.max;
118+
return this.test({
119+
message: message,
120+
name: 'max',
121+
exclusive: true,
122+
params: { max: _max },
123+
test: function test(value) {
124+
return value && value.length <= _max;
125+
}
126+
});
127+
},
128+
129+
compact: function compact(rejector) {
130+
var reject = !rejector ? function (v) {
131+
return !!v;
132+
} : function (v, i, a) {
133+
return !rejector(v, i, a);
134+
};
135+
136+
return this.transform(function (values) {
137+
return values != null ? values.filter(reject) : values;
138+
});
139+
}
140+
});

lib/boolean.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
var MixedSchema = require('./mixed'),
3+
inherits = require('./util/_').inherits;
4+
5+
module.exports = BooleanSchema;
6+
7+
function BooleanSchema() {
8+
if (!(this instanceof BooleanSchema)) return new BooleanSchema();
9+
10+
MixedSchema.call(this, { type: 'boolean' });
11+
12+
this.transforms.push(function (value) {
13+
if (this.isType(value)) return value;
14+
return /true|1/i.test(value);
15+
});
16+
}
17+
18+
inherits(BooleanSchema, MixedSchema, {
19+
20+
_typeCheck: function _typeCheck(v) {
21+
return typeof v === 'boolean';
22+
}
23+
});

lib/date.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
'use strict';
2+
var MixedSchema = require('./mixed');
3+
var isoParse = require('./util/isodate');
4+
var locale = require('./locale.js').date;
5+
6+
var _require = require('./util/_');
7+
8+
var isDate = _require.isDate;
9+
var inherits = _require.inherits;
10+
11+
var invalidDate = new Date('');
12+
13+
module.exports = DateSchema;
14+
15+
function DateSchema() {
16+
if (!(this instanceof DateSchema)) return new DateSchema();
17+
18+
MixedSchema.call(this, { type: 'date' });
19+
20+
this.transforms.push(function (value) {
21+
if (this.isType(value)) return isDate(value) ? new Date(value) : value;
22+
23+
value = isoParse(value);
24+
return value ? new Date(value) : invalidDate;
25+
});
26+
}
27+
28+
inherits(DateSchema, MixedSchema, {
29+
30+
_typeCheck: function _typeCheck(v) {
31+
return isDate(v) && !isNaN(v.getTime());
32+
},
33+
34+
min: function min(_min, msg) {
35+
var limit = this.cast(_min);
36+
37+
if (!this._typeCheck(limit)) throw new TypeError('`min` must be a Date or a value that can be `cast()` to a Date');
38+
39+
return this.test({
40+
name: 'min',
41+
exclusive: true,
42+
message: msg || locale.min,
43+
params: { min: _min },
44+
test: function test(value) {
45+
return value && value >= limit;
46+
}
47+
});
48+
},
49+
50+
max: function max(_max, msg) {
51+
var limit = this.cast(_max);
52+
53+
if (!this._typeCheck(limit)) throw new TypeError('`max` must be a Date or a value that can be `cast()` to a Date');
54+
55+
return this.test({
56+
name: 'max',
57+
exclusive: true,
58+
message: msg || locale.max,
59+
params: { max: _max },
60+
test: function test(value) {
61+
return !value || value <= limit;
62+
}
63+
});
64+
}
65+
66+
});

lib/index.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
var mixed = require('./mixed'),
3+
bool = require('./boolean');
4+
5+
var isSchema = function isSchema(schema) {
6+
return schema && !!schema.__isYupSchema__;
7+
};
8+
9+
module.exports = {
10+
mixed: mixed,
11+
string: require('./string'),
12+
number: require('./number'),
13+
boolean: bool,
14+
bool: bool,
15+
date: require('./date'),
16+
object: require('./object'),
17+
array: require('./array'),
18+
19+
reach: require('./util/reach'),
20+
21+
ValidationError: require('./util/validation-error'),
22+
23+
isSchema: isSchema,
24+
25+
addMethod: function addMethod(schemaType, name, fn) {
26+
if (!schemaType || !isSchema(schemaType.prototype)) throw new TypeError('You must provide a yup schema constructor function');
27+
28+
if (typeof name !== 'string') throw new TypeError('A Method name must be provided');
29+
if (typeof fn !== 'function') throw new TypeError('Method function must be provided');
30+
31+
schemaType.prototype[name] = fn;
32+
}
33+
};

lib/locale.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use strict';
2+
3+
module.exports = {
4+
mixed: {
5+
'default': '${path} is invalid',
6+
notType: '${path} (value: `${value}`) must be a `${type}` type',
7+
required: '${path} is a required field',
8+
oneOf: '${path} must be one the following values: ${values}',
9+
notOneOf: '${path} must not be one the following values: ${values}'
10+
},
11+
12+
string: {
13+
required: '${path} is a required field',
14+
min: '${path} must be at least ${min} characters',
15+
max: '${path} must be less than ${max} characters',
16+
matches: '${path} must match the following: "${regex}"',
17+
email: '${path} must be a valid email',
18+
url: '${path} must be a valid URL',
19+
trim: '${path} must be a trimmed string',
20+
lowercase: '${path} must be a lowercase string',
21+
uppercase: '${path} must be a uppercase string'
22+
},
23+
24+
number: {
25+
min: '${path} must be at least ${min}',
26+
max: '${path} must be less than or equal to ${max}',
27+
positive: '${path} must be a positive number',
28+
negative: '${path} must be a negative number',
29+
integer: '${path} must be an integer'
30+
},
31+
32+
date: {
33+
min: '${path} field must be later than ${min}',
34+
max: '${path} field must be at earlier than ${max}'
35+
},
36+
37+
boolean: {},
38+
39+
object: {
40+
noUnknown: '${path} field cannot have keys not specified in the objcet shape'
41+
},
42+
43+
array: {
44+
required: '${path} is a required field',
45+
min: '${path} field must have at least ${min} items',
46+
max: '${path} field must have less than ${max} items'
47+
}
48+
};

0 commit comments

Comments
 (0)