Comparing version 0.6.1 to 0.6.2
@@ -44,3 +44,3 @@ "use strict"; | ||
if (!subType || !Array.isArray(value)) return value; | ||
if (!subType || !schema._typeCheck(value)) return value; | ||
@@ -95,5 +95,5 @@ return Promise.all(value.map(function (item, key) { | ||
return this.transform(function (values) { | ||
return values.filter(reject); | ||
return values != null ? values.filter(reject) : values; | ||
}); | ||
} | ||
}); |
@@ -100,3 +100,3 @@ "use strict"; | ||
if (value !== undefined && !schema.isType(value)) { | ||
errors.push("value: " + value + " is must be a " + schema._type + " type"); | ||
errors.push("value: " + value + " must be a " + schema._type + " type"); | ||
return Promise.reject(new ValidationError(errors)); | ||
@@ -103,0 +103,0 @@ } |
@@ -41,3 +41,3 @@ "use strict"; | ||
return this.validation({ hashKey: "min", params: { min: min }, message: msg || locale.min }, function (value) { | ||
return value >= min; | ||
return value == null || value >= min; | ||
}); | ||
@@ -48,3 +48,3 @@ }, | ||
return this.validation({ hashKey: "max", params: { max: max }, message: msg || locale.max }, function (value) { | ||
return value <= max; | ||
return value == null || value <= max; | ||
}); | ||
@@ -65,3 +65,3 @@ }, | ||
return this.transform(function (v) { | ||
return v | 0; | ||
return v != null ? v | 0 : v; | ||
}).validation(msg, function (val) { | ||
@@ -79,5 +79,5 @@ return val === (val | 0); | ||
return this.transform(function (v) { | ||
return Math[method](v); | ||
return v != null ? Math[method](v) : v; | ||
}); | ||
} | ||
}); |
@@ -59,3 +59,3 @@ "use strict"; | ||
_typeCheck: function (value) { | ||
return value && typeof value === "object" && !Array.isArray(value); | ||
return isObject(value) || typeof value === "function"; | ||
}, | ||
@@ -67,3 +67,4 @@ | ||
if (schema.isType(value)) { | ||
//should ignore nulls here | ||
if (schema._typeCheck(value)) { | ||
var fields = schema.fields, | ||
@@ -132,3 +133,3 @@ strip = schema._option("stripUnknown", _opts) === true, | ||
return this.validation({ hashKey: "required", message: msg || locale.required }, function (value) { | ||
return !!value && isPlainObject(value); | ||
return value != null; | ||
}); | ||
@@ -139,2 +140,4 @@ }, | ||
return this.transform(function (obj) { | ||
if (obj == null) return obj; | ||
var newObj = transform(obj, function (o, val, key) { | ||
@@ -153,3 +156,3 @@ return key !== from && (o[key] = val); | ||
return this.transform(function (obj) { | ||
return transform(obj, function (newobj, val, key) { | ||
return obj == null ? obj : transform(obj, function (newobj, val, key) { | ||
return newobj[c.camel(key)] = val; | ||
@@ -162,3 +165,3 @@ }); | ||
return this.transform(function (obj) { | ||
return transform(obj, function (newobj, val, key) { | ||
return obj == null ? obj : transform(obj, function (newobj, val, key) { | ||
return newobj[c.constant(key)] = val; | ||
@@ -165,0 +168,0 @@ }); |
@@ -37,4 +37,4 @@ "use strict"; | ||
return this.validation({ message: msg, hashKey: "min", params: { min: min } }, function (value) { | ||
return value && value.length >= min; | ||
return this.validation({ hashKey: "min", message: msg, params: { min: min } }, function (value) { | ||
return value == null || value.length >= min; | ||
}); | ||
@@ -45,4 +45,4 @@ }, | ||
msg = msg || locale.max; | ||
return this.validation({ message: msg, hashKey: "max", params: { max: max } }, function (value) { | ||
return value && value.length <= max; | ||
return this.validation({ hashKey: "max", message: msg, params: { max: max } }, function (value) { | ||
return value == null || value.length <= max; | ||
}); | ||
@@ -55,3 +55,3 @@ }, | ||
return this.validation({ message: msg, params: { regex: regex } }, function (value) { | ||
return regex.test(value); | ||
return value == null || regex.test(value); | ||
}); | ||
@@ -76,6 +76,6 @@ }, | ||
return this.transform(function (v) { | ||
return v.trim(); | ||
return this.transform(function (val) { | ||
return val != null ? val.trim() : val; | ||
}).validation(msg, function (val) { | ||
return val === val.trim(); | ||
return val == null || val === val.trim(); | ||
}); | ||
@@ -87,6 +87,6 @@ }, | ||
return this.validation(msg, function (val) { | ||
return val === val.toLowerCase(); | ||
}).transform(function (v) { | ||
return v.toLowerCase(); | ||
return this.transform(function (val) { | ||
return val != null ? val.toLowerCase() : val; | ||
}).validation(msg, function (val) { | ||
return val == null || val === val.toLowerCase(); | ||
}); | ||
@@ -98,8 +98,8 @@ }, | ||
return this.validation(msg, function (val) { | ||
return val === val.toUpperCase(); | ||
}).transform(function (v) { | ||
return v.toUpperCase(); | ||
return this.transform(function (val) { | ||
return val != null ? val.toUpperCase() : val; | ||
}).validation(msg, function (val) { | ||
return val == null || val === val.toUpperCase(); | ||
}); | ||
} | ||
}); |
{ | ||
"name": "yup", | ||
"version": "0.6.1", | ||
"version": "0.6.2", | ||
"description": "Dead simple Object schema validation", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -194,3 +194,3 @@ Yup | ||
#### `mixed.required(msg)` | ||
#### `mixed.required([msg])` | ||
@@ -290,11 +290,11 @@ Mark the schema as required. All field values apart from `undefined` meet this requirement. | ||
#### `string.min(limit, message)` | ||
#### `string.min(limit, [message])` | ||
Set an minimum length limit for the string value. The `${min}` interpolation can be used in the `message` argument | ||
#### `string.max(limit, message)` | ||
#### `string.max(limit, [message])` | ||
Set an maximum length limit for the string value. The `${max}` interpolation can be used in the `message` argument | ||
#### `string.matches(regex, message)` | ||
#### `string.matches(regex, [message])` | ||
@@ -309,3 +309,3 @@ Provide an arbitrary `regex` to match the value against. | ||
#### `string.email(message)` | ||
#### `string.email([message])` | ||
@@ -319,3 +319,3 @@ Validates the value as an email address via a regex. | ||
#### `string.trim(msg)` | ||
#### `string.trim([message])` | ||
@@ -325,7 +325,7 @@ Transforms string values by removing leading and trailing whitespace. If | ||
#### `string.lowercase(msg)` | ||
#### `string.lowercase([message])` | ||
Transforms the string value to lowercase. If `strict()` is set it will only validate that the value is lowercase. | ||
#### `string.uppercase(msg)` | ||
#### `string.uppercase([message])` | ||
@@ -343,3 +343,3 @@ Transforms the string value to uppercase. If `strict()` is set it will only validate that the value is uppercase. | ||
#### `number.min(limit, message)` | ||
#### `number.min(limit, [message])` | ||
@@ -349,3 +349,3 @@ Set the minimum value allowed. The `${min}` interpolation can be used in the | ||
#### `number.max(limit, message)` | ||
#### `number.max(limit, [message])` | ||
@@ -355,11 +355,11 @@ Set the maximum value allowed. The `${max}` interpolation can be used in the | ||
#### `number.positive(message)` | ||
#### `number.positive([message])` | ||
Value must be a positive number. | ||
#### `number.negative(message)` | ||
#### `number.negative([message])` | ||
Value mut be a negative number. | ||
#### `number.integer(message)` | ||
#### `number.integer([message])` | ||
@@ -392,7 +392,7 @@ Transformation that coerces the value into an integer via truncation | ||
#### `date.min(limit, message)` | ||
#### `date.min(limit, [message])` | ||
Set the minimum date allowed. | ||
#### `date.max(limit, message)` | ||
#### `date.max(limit, [message])` | ||
@@ -417,7 +417,7 @@ Set the maximum date allowed. | ||
#### `array.min(limit, message)` | ||
#### `array.min(limit, [message])` | ||
Set an minimum length limit for the array. The `${min}` interpolation can be used in the `message` argument. | ||
#### `array.max(limit, message)` | ||
#### `array.max(limit, [message])` | ||
@@ -507,3 +507,3 @@ Set an maximum length limit for the array. The `${max}` interpolation can be used in the `message` argument. | ||
function MomentDateSchemaType(){ | ||
var date = function MomentDateSchemaType(){ | ||
// so we don't need to use the `new` keyword | ||
@@ -523,3 +523,3 @@ if ( !(this instanceof MomentDateSchemaType)) | ||
value = Moment(originalValue, this._formats) | ||
return date.isValid() ? date.toDate() : invalidDate | ||
return value.isValid() ? value.toDate() : invalidDate | ||
}) | ||
@@ -534,6 +534,9 @@ } | ||
var next = this.clone(); //never mutate a schema | ||
next = [next._formats].concat(format) | ||
next._formats = next._formats.concat(format) | ||
return next | ||
} | ||
var schema = MomentDateSchemaType().format('YYYY-MM-DD') | ||
schema.cast('It is 2012-05-25') // Fri May 25 2012 00:00:00 GMT-0400 (Eastern Daylight Time) | ||
``` |
@@ -47,5 +47,5 @@ 'use strict'; | ||
.then(function(value){ | ||
if ( !subType || !schema._typeCheck(value) ) return value | ||
if ( !subType || !Array.isArray(value) ) return value | ||
return Promise | ||
@@ -94,4 +94,4 @@ .all(value.map((item, key) => { | ||
return this.transform(values => values.filter(reject)) | ||
return this.transform(values => values != null ? values.filter(reject) : values) | ||
} | ||
}) |
@@ -98,3 +98,3 @@ 'use strict'; | ||
if ( value !== undefined && !schema.isType(value) ){ | ||
errors.push(`value: ${value} is must be a ${schema._type} type`) | ||
errors.push(`value: ${value} must be a ${schema._type} type`) | ||
return Promise.reject(new ValidationError(errors)) | ||
@@ -276,2 +276,2 @@ } | ||
promise.then(val => cb(null, val), err => cb(err)) | ||
} | ||
} |
@@ -37,3 +37,3 @@ 'use strict'; | ||
{ hashKey: 'min', params: { min: min }, message: msg || locale.min } | ||
, value => value >= min) | ||
, value => value == null || value >= min) | ||
}, | ||
@@ -44,3 +44,3 @@ | ||
{ hashKey: 'max', params: { max: max }, message: msg || locale.max } | ||
, value => value <= max) | ||
, value => value == null || value <= max) | ||
}, | ||
@@ -60,3 +60,3 @@ | ||
return this | ||
.transform( v => v | 0) | ||
.transform( v => v != null ? (v | 0) : v) | ||
.validation(msg, val => val === (val | 0)) | ||
@@ -72,6 +72,4 @@ }, | ||
return this.transform(function(v){ | ||
return Math[method](v); | ||
}) | ||
return this.transform(v => v != null ? Math[method](v) : v) | ||
} | ||
}) |
@@ -55,3 +55,3 @@ 'use strict'; | ||
_typeCheck(value) { | ||
return value && typeof value === 'object' && !Array.isArray(value); | ||
return isObject(value) || typeof value === 'function'; | ||
}, | ||
@@ -63,3 +63,4 @@ | ||
if( schema.isType(value) ) { | ||
//should ignore nulls here | ||
if( schema._typeCheck(value) ) { | ||
var fields = schema.fields | ||
@@ -139,7 +140,10 @@ , strip = schema._option('stripUnknown', _opts) === true | ||
{ hashKey: 'required', message: msg || locale.required }, | ||
value => !!value && isPlainObject(value)) | ||
value => value != null) | ||
}, | ||
from(from, to, alias) { | ||
return this.transform(function(obj){ | ||
return this.transform( obj => { | ||
if ( obj == null) | ||
return obj | ||
var newObj = transform(obj, (o, val, key) => key !== from && (o[key] = val), {}) | ||
@@ -155,11 +159,11 @@ | ||
camelcase(){ | ||
return this.transform(obj => | ||
transform(obj, (newobj, val, key ) => newobj[c.camel(key)] = val)) | ||
return this.transform(obj => obj == null ? obj | ||
: transform(obj, (newobj, val, key ) => newobj[c.camel(key)] = val)) | ||
}, | ||
constantcase(){ | ||
return this.transform( obj => | ||
transform(obj, (newobj, val, key ) => newobj[c.constant(key)] = val)) | ||
return this.transform( obj => obj == null ? obj | ||
: transform(obj, (newobj, val, key ) => newobj[c.constant(key)] = val)) | ||
} | ||
}) | ||
@@ -32,3 +32,3 @@ 'use strict'; | ||
return this.validation( | ||
{ hashKey: 'required', message: msg || locale.required }, | ||
{ hashKey: 'required', message: msg || locale.required }, | ||
value => value && !!value.length) | ||
@@ -41,4 +41,4 @@ }, | ||
return this.validation( | ||
{ message: msg, hashKey: 'min', params: { min: min } } | ||
, value => value && value.length >= min) | ||
{ hashKey: 'min', message: msg, params: { min: min } } | ||
, value => value == null || value.length >= min) | ||
}, | ||
@@ -49,4 +49,4 @@ | ||
return this.validation( | ||
{ message: msg, hashKey: 'max', params: { max: max } } | ||
, value => value && value.length <= max) | ||
{ hashKey: 'max', message: msg, params: { max: max } } | ||
, value => value == null || value.length <= max) | ||
}, | ||
@@ -59,3 +59,3 @@ | ||
{ message: msg, params: { regex: regex } } | ||
, value => regex.test(value)) | ||
, value => value == null || regex.test(value)) | ||
}, | ||
@@ -80,4 +80,4 @@ | ||
return this | ||
.transform( v => v.trim()) | ||
.validation(msg, val => val === val.trim()) | ||
.transform( val => val != null ? val.trim() : val) | ||
.validation(msg, val => val == null || val === val.trim()) | ||
}, | ||
@@ -89,4 +89,4 @@ | ||
return this | ||
.validation(msg, val => val === val.toLowerCase()) | ||
.transform(v => v.toLowerCase()) | ||
.transform(val => val != null ? val.toLowerCase() : val) | ||
.validation(msg, val => val == null || val === val.toLowerCase()) | ||
}, | ||
@@ -98,5 +98,5 @@ | ||
return this | ||
.validation(msg, val => val === val.toUpperCase()) | ||
.transform(v => v.toUpperCase()) | ||
.transform(val => val != null ? val.toUpperCase(): val) | ||
.validation(msg, val => val == null || val === val.toUpperCase()) | ||
} | ||
}) |
@@ -27,2 +27,6 @@ 'use strict'; | ||
inst.cast(null)).to.equal(null) | ||
chai.expect(inst.nullable() | ||
.compact() | ||
.cast(null)).to.equal(null) | ||
}) | ||
@@ -29,0 +33,0 @@ |
@@ -33,2 +33,7 @@ 'use strict'; | ||
;(function(){ inst.round('fasf') }).should.throw(TypeError) | ||
chai.expect(inst.nullable() | ||
.integer() | ||
.round() | ||
.cast(null)).to.equal(null) | ||
}) | ||
@@ -78,3 +83,7 @@ | ||
v.isValid(35738787838).should.eventually.equal(true), | ||
v.isValid(new Date).should.eventually.equal(true) | ||
v.isValid(new Date).should.eventually.equal(true), | ||
v.isValid(null).should.eventually.equal(false), // -> NaN fails type check | ||
v.nullable().isValid(null).should.eventually.equal(true), | ||
]) | ||
@@ -93,2 +102,6 @@ }) | ||
v.isValid(new Date).should.eventually.equal(false), | ||
v.isValid(null).should.eventually.equal(false), // null -> NaN fails type check | ||
v.nullable().isValid(null).should.eventually.equal(true), | ||
]) | ||
@@ -95,0 +108,0 @@ }) |
@@ -265,2 +265,6 @@ 'use strict'; | ||
.should.eql({ conStat: 5, caseStatus: 6, hiJohn: 4 }) | ||
chai.expect(inst | ||
.nullable() | ||
.cast(null)).to.equal(null) | ||
}) | ||
@@ -278,2 +282,6 @@ | ||
.should.eql({ CON_STAT: 5, CASE_STATUS: 6, HI_JOHN: 4 }) | ||
chai.expect(inst | ||
.nullable() | ||
.cast(null)).to.equal(null) | ||
}) | ||
@@ -280,0 +288,0 @@ |
@@ -34,4 +34,11 @@ 'use strict'; | ||
inst.trim().cast(' 3 ').should.equal('3') | ||
inst.lowercase().cast('HellO JohN').should.equal('hello john') | ||
inst.uppercase().cast('HellO JohN').should.equal('HELLO JOHN') | ||
chai.expect(inst.nullable() | ||
.trim() | ||
.lowercase() | ||
.uppercase() | ||
.cast(null)).to.equal(null) | ||
}) | ||
@@ -90,3 +97,6 @@ | ||
v.isValid('big').should.eventually.equal(false), | ||
v.isValid('noffasfasfasf saf').should.eventually.equal(true) | ||
v.isValid('noffasfasfasf saf').should.eventually.equal(true), | ||
v.isValid(null).should.eventually.equal(false), // null -> '' | ||
v.nullable().isValid(null).should.eventually.equal(true) // null -> null | ||
]) | ||
@@ -106,2 +116,5 @@ | ||
v.isValid(new Date).should.eventually.equal(false), | ||
v.isValid(null).should.eventually.equal(true), | ||
v.nullable().isValid(null).should.eventually.equal(true), | ||
]) | ||
@@ -108,0 +121,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
134681
2979
529