Comparing version 0.7.0 to 0.7.1
@@ -39,3 +39,3 @@ "use strict"; | ||
if (!this.isType(limit)) throw new TypeError("min must be a Date or a value that can be parsed to a Date"); | ||
if (!this._typeCheck(limit)) throw new TypeError("`min` must be a Date or a value that can be parsed to a Date"); | ||
@@ -56,3 +56,3 @@ return this.test({ | ||
if (!this.isType(limit)) throw new TypeError("max must be a Date or a value that can be parsed to a Date"); | ||
if (!this._typeCheck(limit)) throw new TypeError("`max` must be a Date or a value that can be parsed to a Date"); | ||
@@ -59,0 +59,0 @@ return this.test({ |
@@ -28,2 +28,3 @@ "use strict"; | ||
this.transforms = []; | ||
this._typeError = interpolate(locale.notType); | ||
@@ -99,7 +100,6 @@ if (_.has(options, "default")) this._default = options.default; | ||
context = (_opts || {}).context || _state.parent, | ||
schema, | ||
valid, | ||
state, | ||
endEarly, | ||
isStrict; | ||
schema = undefined, | ||
state = undefined, | ||
endEarly = undefined, | ||
isStrict = undefined; | ||
@@ -114,2 +114,5 @@ state = _state; | ||
var errors = []; | ||
var reject = function () { | ||
return Promise.reject(new ValidationError(errors)); | ||
}; | ||
@@ -119,16 +122,19 @@ if (!state.isCast && !isStrict) value = schema._cast(value, _opts); | ||
if (value !== undefined && !schema.isType(value)) { | ||
errors.push(interpolate(locale.notType, { value: value, type: schema._type })); | ||
return Promise.reject(new ValidationError(errors)); | ||
errors.push(schema._typeError({ value: value, type: schema._type, path: state.path })); | ||
if (endEarly) return reject(); | ||
} | ||
if (valids.length) { | ||
valid = valids.has(value); | ||
if (!valid) return Promise.reject(new ValidationError(errors.concat(schema._whitelistError({ values: valids.values().join(", "), path: state.path })))); | ||
if (valids.length && !valids.has(value)) { | ||
errors.push(schema._whitelistError(valids.values(), state.path)); | ||
if (endEarly) return reject(); | ||
} | ||
if (invalids.has(value)) { | ||
return Promise.reject(new ValidationError(errors.concat(schema._blacklistError({ values: invalids.values().join(", "), path: state.path })))); | ||
errors.push(schema._blacklistError(invalids.values(), state.path)); | ||
if (endEarly) return reject(); | ||
} | ||
// It makes no sense to validate further at this point | ||
if (errors.length) return reject(); | ||
var result = schema.tests.map(function (fn) { | ||
@@ -139,10 +145,10 @@ return fn.call(schema, value, state); | ||
result = endEarly ? Promise.all(result) : _.settled(result).then(function (results) { | ||
errors = results.reduce(function (arr, r) { | ||
var errors = results.reduce(function (arr, r) { | ||
return !r.fulfilled ? arr.concat([r.value.errors]) : arr; | ||
}, errors); | ||
}, []); | ||
if (errors.length) throw new ValidationError(errors); | ||
}); | ||
return result.then(function () { | ||
if (errors.length) throw new ValidationError(errors); | ||
return value; | ||
@@ -197,10 +203,7 @@ }); | ||
// optional() { | ||
// return this.test({ | ||
// name: 'required', | ||
// exclusive: true, | ||
// message: '', | ||
// test: () => true | ||
// }) | ||
// }, | ||
typeError: function (msg) { | ||
var next = this.clone(); | ||
next._typeError = interpolate(msg); | ||
return next; | ||
}, | ||
@@ -273,3 +276,5 @@ nullable: function (value) { | ||
next._whitelistError = interpolate(msg || locale.oneOf); | ||
next._whitelistError = function (valids, path) { | ||
return interpolate(msg || locale.oneOf, { values: valids.join(", "), path: path }); | ||
}; | ||
@@ -287,3 +292,5 @@ enums.forEach(function (val) { | ||
next._blacklistError = interpolate(msg || locale.notOneOf); | ||
next._blacklistError = function (invalids, path) { | ||
return interpolate(msg || locale.notOneOf, { values: invalids.join(", "), path: path }); | ||
}; | ||
@@ -309,3 +316,3 @@ enums.forEach(function (val) { | ||
return SchemaType.prototype[alias] = SchemaType.prototype[method]; | ||
}); | ||
}); //eslint-disable-line no-loop-func | ||
@@ -312,0 +319,0 @@ function nodeify(promise, cb) { |
{ | ||
"name": "yup", | ||
"version": "0.7.0", | ||
"version": "0.7.1", | ||
"description": "Dead simple Object schema validation", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -175,2 +175,6 @@ Yup | ||
#### `mixed.typeError(message)` (default: '${path} (value: \`${value}\`) must be a \`${type}\` type') | ||
Define an error message for failed type checks. The `${value}` and `${type}` interpolation can be used in the `message` argument. | ||
#### `mixed.nullable(isNullable)` (default: `false`) | ||
@@ -181,9 +185,9 @@ | ||
#### `mixed.required([msg])` | ||
#### `mixed.required([message])` | ||
Mark the schema as required. All field values apart from `undefined` meet this requirement. | ||
#### `mixed.oneOf(arrayOfValues)` Alias: `equals` | ||
#### `mixed.oneOf(arrayOfValues, [message])` Alias: `equals` | ||
Whitelist a set of values. Values added are automatically removed from any blacklist if they are in it. | ||
Whitelist a set of values. Values added are automatically removed from any blacklist if they are in it. The `${values}` interpolation can be used in the `message` argument. | ||
@@ -197,5 +201,5 @@ ```javascript | ||
#### `mixed.notOneOf(arrayOfValues)` | ||
#### `mixed.notOneOf(arrayOfValues, [message])` | ||
Blacklist a set of values. Values added are automatically removed from any whitelist if they are in it. | ||
Blacklist a set of values. Values added are automatically removed from any whitelist if they are in it. The `${values}` interpolation can be used in the `message` argument. | ||
@@ -532,4 +536,9 @@ ```javascript | ||
Alternatively, each schema is a normal javascript constructor function that you can mutate or delegate to using the normal methods. Generally you should not inherit from `mixed` unless you know what you are doing, better to think of it as an abastract class. The other types are fair game though. | ||
Alternatively, each schema is a normal javascript constructor function that you can mutate or delegate to using the normal patterns. Generally you should not inherit from `mixed` unless you know what you are doing, better to think of it as an abstract class. The other types are fair game though. | ||
You should keep in mind some basic guidelines when extending schemas | ||
- never mutate an existing schema, always `clone()` and then mutate the new one before returning it. Built-in methods like `test` and `transform` take care of this for you, so you can safely use them (see below) without worrying | ||
- transforms should never mutate the `value` passed in, and should return an invalid object when one exists (`NaN`, `InvalidDate`, etc) instead of `null` for bad values. | ||
- by the time validations run the `value` is gaurunteed to be the correct type, however if `nullable` is set then `null` is a valid value for that type, so don't assume that a property or method exists on the value. | ||
__Adjust core Types__ | ||
@@ -542,2 +551,3 @@ ```js | ||
// `transform` will clone the schema for us so no worry of mutation | ||
return this.transform(function(value, originalValue){ | ||
@@ -544,0 +554,0 @@ if ( this.isType(value) ) return value |
@@ -7,3 +7,2 @@ 'use strict'; | ||
let invalidDate = new Date('') | ||
@@ -21,5 +20,3 @@ | ||
if (this.isType(value)) | ||
return isDate(value) | ||
? new Date(value) | ||
: value | ||
return isDate(value) ? new Date(value) : value | ||
@@ -41,4 +38,4 @@ value = isoParse(value) | ||
if(!this.isType(limit)) | ||
throw new TypeError('min must be a Date or a value that can be parsed to a Date') | ||
if(!this._typeCheck(limit)) | ||
throw new TypeError('`min` must be a Date or a value that can be parsed to a Date') | ||
@@ -57,4 +54,4 @@ return this.test({ | ||
if(!this.isType(limit)) | ||
throw new TypeError('max must be a Date or a value that can be parsed to a Date') | ||
if(!this._typeCheck(limit)) | ||
throw new TypeError('`max` must be a Date or a value that can be parsed to a Date') | ||
@@ -61,0 +58,0 @@ return this.test({ |
@@ -28,2 +28,3 @@ 'use strict'; | ||
this.transforms = [] | ||
this._typeError = interpolate(locale.notType) | ||
@@ -98,6 +99,6 @@ if (_.has(options, 'default')) | ||
_validate(value, _opts, _state) { | ||
var valids = this._whitelist | ||
let valids = this._whitelist | ||
, invalids = this._blacklist | ||
, context = (_opts || {}).context || _state.parent | ||
, schema, valid, state, endEarly, isStrict; | ||
, schema, state, endEarly, isStrict; | ||
@@ -111,26 +112,26 @@ state = _state | ||
var errors = []; | ||
let errors = []; | ||
let reject = () => Promise.reject(new ValidationError(errors)); | ||
if( !state.isCast && !isStrict ) | ||
if ( !state.isCast && !isStrict ) | ||
value = schema._cast(value, _opts) | ||
if( value !== undefined && !schema.isType(value) ){ | ||
errors.push(interpolate(locale.notType, { value, type: schema._type })) | ||
return Promise.reject(new ValidationError(errors)) | ||
if ( value !== undefined && !schema.isType(value) ){ | ||
errors.push(schema._typeError({ value, type: schema._type, path: state.path })) | ||
if ( endEarly ) return reject() | ||
} | ||
if( valids.length ) { | ||
valid = valids.has(value) | ||
if( !valid ) | ||
return Promise.reject(new ValidationError(errors.concat( | ||
schema._whitelistError({ values: valids.values().join(', '), path: state.path })) | ||
)) | ||
if ( valids.length && !valids.has(value) ) { | ||
errors.push(schema._whitelistError(valids.values(), state.path)) | ||
if ( endEarly ) return reject() | ||
} | ||
if( invalids.has(value) ) { | ||
return Promise.reject(new ValidationError(errors.concat( | ||
schema._blacklistError({ values: invalids.values().join(', '), path: state.path })) | ||
)) | ||
if ( invalids.has(value) ){ | ||
errors.push(schema._blacklistError(invalids.values(), state.path)) | ||
if ( endEarly ) return reject() | ||
} | ||
// It makes no sense to validate further at this point | ||
if ( errors.length ) | ||
return reject() | ||
@@ -142,12 +143,10 @@ let result = schema.tests.map(fn => fn.call(schema, value, state)) | ||
: _.settled(result).then( results => { | ||
errors = results.reduce( | ||
(arr, r) => !r.fulfilled ? arr.concat([ r.value.errors ]) : arr, errors) | ||
}) | ||
let errors = results.reduce( | ||
(arr, r) => !r.fulfilled ? arr.concat([ r.value.errors ]) : arr, []) | ||
return result.then(() => { | ||
if ( errors.length ) | ||
throw new ValidationError(errors) | ||
if ( errors.length ) | ||
throw new ValidationError(errors) | ||
}) | ||
return value | ||
}); | ||
return result.then(() => value); | ||
}, | ||
@@ -202,10 +201,7 @@ | ||
// optional() { | ||
// return this.test({ | ||
// name: 'required', | ||
// exclusive: true, | ||
// message: '', | ||
// test: () => true | ||
// }) | ||
// }, | ||
typeError(msg){ | ||
var next = this.clone() | ||
next._typeError = interpolate(msg) | ||
return next | ||
}, | ||
@@ -280,3 +276,4 @@ nullable(value) { | ||
next._whitelistError = interpolate(msg || locale.oneOf) | ||
next._whitelistError = (valids, path) => | ||
interpolate(msg || locale.oneOf, { values: valids.join(', '), path }) | ||
@@ -294,3 +291,4 @@ enums.forEach( val => { | ||
next._blacklistError = interpolate(msg || locale.notOneOf) | ||
next._blacklistError = (invalids, path) => | ||
interpolate(msg || locale.notOneOf, { values: invalids.join(', '), path }) | ||
@@ -315,5 +313,6 @@ enums.forEach( val => { | ||
for( var method in aliases ) if ( _.has(aliases, method) ) | ||
aliases[method].forEach( | ||
alias => SchemaType.prototype[alias] = SchemaType.prototype[method]) | ||
for( var method in aliases ) if ( _.has(aliases, method) ) | ||
aliases[method].forEach( | ||
alias => SchemaType.prototype[alias] = SchemaType.prototype[method]) //eslint-disable-line no-loop-func | ||
@@ -320,0 +319,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
150335
3273
585