Comparing version 0.14.1 to 0.14.2
@@ -0,1 +1,8 @@ | ||
v0.14.2 - Tue, 29 Mar 2016 14:48:37 GMT | ||
--------------------------------------- | ||
v0.14.1 - Tue, 16 Feb 2016 19:51:25 GMT | ||
@@ -2,0 +9,0 @@ --------------------------------------- |
@@ -15,3 +15,3 @@ 'use strict'; | ||
min: '${path} must be at least ${min} characters', | ||
max: '${path} must be less than ${max} characters', | ||
max: '${path} must be at most ${max} characters', | ||
matches: '${path} must match the following: "${regex}"', | ||
@@ -26,3 +26,3 @@ email: '${path} must be a valid email', | ||
number: { | ||
min: '${path} must be at least ${min}', | ||
min: '${path} must be greater than or equal to ${min}', | ||
max: '${path} must be less than or equal to ${max}', | ||
@@ -29,0 +29,0 @@ positive: '${path} must be a positive number', |
@@ -59,2 +59,8 @@ 'use strict'; | ||
label: function label(_label) { | ||
var next = this.clone(); | ||
next._label = _label; | ||
return next; | ||
}, | ||
withMutation: function withMutation(fn) { | ||
@@ -140,2 +146,3 @@ this._mutate = true; | ||
var path = state.path; | ||
var label = this._label; | ||
@@ -145,3 +152,3 @@ if (!state.isCast && !isStrict) value = schema._cast(value, options); | ||
// value is cast, we can check if it meets type requirements | ||
var validationParams = { value: value, path: path, state: state, schema: schema, options: options }; | ||
var validationParams = { value: value, path: path, state: state, schema: schema, options: options, label: label }; | ||
var initialTests = []; | ||
@@ -238,4 +245,2 @@ | ||
if (next._whitelist.length) throw new Error('Cannot add tests when specific valid values are specified'); | ||
var validate = createValidation(opts); | ||
@@ -242,0 +247,0 @@ |
@@ -12,3 +12,3 @@ 'use strict'; | ||
function createErrorFactory(orginalMessage, orginalPath, value, orginalParams, originalType) { | ||
function createErrorFactory(orginalMessage, orginalPath, value, orginalParams, originalType, label) { | ||
return function createError() { | ||
@@ -25,3 +25,3 @@ var _ref = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
return new ValidationError(formatError(message, _extends({ path: path, value: value }, orginalParams, params)), value, path, type); | ||
return new ValidationError(formatError(message, _extends({ path: path, value: value, label: label }, orginalParams, params)), value, path, type); | ||
}; | ||
@@ -40,7 +40,8 @@ } | ||
var path = _ref2.path; | ||
var label = _ref2.label; | ||
var parent = _ref2.state.parent; | ||
var rest = _objectWithoutProperties(_ref2, ['value', 'path', 'state']); | ||
var rest = _objectWithoutProperties(_ref2, ['value', 'path', 'label', 'state']); | ||
var createError = createErrorFactory(message, path, value, params, name); | ||
var createError = createErrorFactory(message, path, value, params, name, label); | ||
var ctx = _extends({ path: path, parent: parent, createError: createError, type: name }, rest); | ||
@@ -47,0 +48,0 @@ |
@@ -1,10 +0,9 @@ | ||
'use strict'; | ||
"use strict"; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } | ||
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); | ||
var toString = Object.prototype.toString; | ||
var isDate = function isDate(obj) { | ||
return toString.call(obj) === '[object Date]'; | ||
}; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var hasOwnProperty = Object.prototype.hasOwnProperty; | ||
module.exports = (function () { | ||
@@ -14,36 +13,37 @@ function BadSet() { | ||
this._array = []; | ||
this.length = 0; | ||
this._map = Object.create(null); | ||
} | ||
BadSet.prototype.values = function values() { | ||
return this._array; | ||
var _this = this; | ||
return Object.keys(this._map).map(function (v) { | ||
return _this._map[v]; | ||
}); | ||
}; | ||
BadSet.prototype.add = function add(item) { | ||
if (!this.has(item)) this._array.push(item); | ||
this.length = this._array.length; | ||
this._map[stringify(item)] = item; | ||
}; | ||
BadSet.prototype['delete'] = function _delete(item) { | ||
var idx = indexOf(this._array, item); | ||
if (idx !== -1) this._array.splice(idx, 1); | ||
this.length = this._array.length; | ||
BadSet.prototype["delete"] = function _delete(item) { | ||
delete this._map[stringify(item)]; | ||
}; | ||
BadSet.prototype.has = function has(val) { | ||
return indexOf(this._array, val) !== -1; | ||
BadSet.prototype.has = function has(item) { | ||
return hasOwnProperty.call(this._map, stringify(item)); | ||
}; | ||
_createClass(BadSet, [{ | ||
key: "length", | ||
get: function get() { | ||
return Object.keys(this._map).length; | ||
} | ||
}]); | ||
return BadSet; | ||
})(); | ||
function indexOf(arr, val) { | ||
for (var i = 0; i < arr.length; i++) { | ||
var item = arr[i]; | ||
if (item === val || isDate(item) && +val === +item) return i; | ||
} | ||
return -1; | ||
function stringify(item) { | ||
return JSON.stringify(item); | ||
} |
@@ -51,6 +51,7 @@ 'use strict'; | ||
var path = _ref2.path; | ||
var label = _ref2.label; | ||
var params = _objectWithoutProperties(_ref2, ['path']); | ||
var params = _objectWithoutProperties(_ref2, ['path', 'label']); | ||
params.path = path || 'this'; | ||
params.path = label || path || 'this'; | ||
@@ -57,0 +58,0 @@ return message(params); |
{ | ||
"name": "yup", | ||
"version": "0.14.1", | ||
"version": "0.14.2", | ||
"description": "Dead simple Object schema validation", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -27,2 +27,3 @@ | ||
- [`mixed.clone()`](#mixedclone) | ||
- [`mixed.label(String label)`](#mixedlabelstring-label) | ||
- [`mixed.concat(Schema schema)`](#mixedconcatschema-schema) | ||
@@ -204,2 +205,6 @@ - [`mixed.validate(Any value, [Object options, Function callback])`](#mixedvalidateany-value-object-options-function-callback) | ||
#### `mixed.label(String label)` | ||
Overrides the key name which is used in error messages. | ||
#### `mixed.concat(Schema schema)` | ||
@@ -206,0 +211,0 @@ |
@@ -15,3 +15,3 @@ | ||
min: '${path} must be at least ${min} characters', | ||
max: '${path} must be less than ${max} characters', | ||
max: '${path} must be at most ${max} characters', | ||
matches: '${path} must match the following: "${regex}"', | ||
@@ -26,3 +26,3 @@ email: '${path} must be a valid email', | ||
number: { | ||
min: '${path} must be at least ${min}', | ||
min: '${path} must be greater than or equal to ${min}', | ||
max: '${path} must be less than or equal to ${max}', | ||
@@ -29,0 +29,0 @@ positive: '${path} must be a positive number', |
@@ -58,2 +58,8 @@ 'use strict'; | ||
label: function(label){ | ||
var next = this.clone(); | ||
next._label = label; | ||
return next; | ||
}, | ||
withMutation(fn) { | ||
@@ -135,2 +141,3 @@ this._mutate = true | ||
let path = state.path | ||
let label = this._label | ||
@@ -141,3 +148,3 @@ if (!state.isCast && !isStrict) | ||
// value is cast, we can check if it meets type requirements | ||
let validationParams = { value, path, state, schema, options } | ||
let validationParams = { value, path, state, schema, options, label } | ||
let initialTests = [] | ||
@@ -249,5 +256,2 @@ | ||
if (next._whitelist.length) | ||
throw new Error('Cannot add tests when specific valid values are specified') | ||
var validate = createValidation(opts); | ||
@@ -254,0 +258,0 @@ |
@@ -7,6 +7,6 @@ 'use strict'; | ||
function createErrorFactory(orginalMessage, orginalPath, value, orginalParams, originalType) { | ||
function createErrorFactory(orginalMessage, orginalPath, value, orginalParams, originalType, label) { | ||
return function createError({ path = orginalPath, message = orginalMessage, type = originalType, params } = {}) { | ||
return new ValidationError( | ||
formatError(message, { path, value, ...orginalParams, ...params }), value, path, type) | ||
formatError(message, { path, value, label, ...orginalParams, ...params }), value, path, type) | ||
} | ||
@@ -18,4 +18,4 @@ } | ||
function validate({ value, path, state: { parent }, ...rest }) { | ||
var createError = createErrorFactory(message, path, value, params, name) | ||
function validate({ value, path, label, state: { parent }, ...rest }) { | ||
var createError = createErrorFactory(message, path, value, params, name, label) | ||
var ctx = { path, parent, createError, type: name, ...rest } | ||
@@ -22,0 +22,0 @@ |
@@ -1,43 +0,32 @@ | ||
var toString = Object.prototype.toString | ||
var isDate = obj => toString.call(obj) === '[object Date]' | ||
var hasOwnProperty = Object.prototype.hasOwnProperty | ||
module.exports = class BadSet { | ||
module.exports = class BadSet { | ||
constructor(){ | ||
this._array = [] | ||
this.length = 0 | ||
this._map = Object.create(null) | ||
} | ||
values(){ | ||
return this._array | ||
return Object.keys(this._map).map(v => this._map[v]) | ||
} | ||
add(item) { | ||
if(!this.has(item)) | ||
this._array.push(item) | ||
get length(){ | ||
return Object.keys(this._map).length | ||
} | ||
this.length = this._array.length | ||
add(item){ | ||
this._map[stringify(item)] = item | ||
} | ||
delete(item){ | ||
var idx = indexOf(this._array, item) | ||
if( idx !== -1) | ||
this._array.splice(idx, 1) | ||
this.length = this._array.length | ||
delete this._map[stringify(item)] | ||
} | ||
has(val){ | ||
return indexOf(this._array, val) !== -1 | ||
has(item){ | ||
return hasOwnProperty.call(this._map, stringify(item)) | ||
} | ||
} | ||
function indexOf(arr, val){ | ||
for (var i = 0; i < arr.length; i++) { | ||
var item = arr[i] | ||
if (item === val || (isDate(item) && +val === +item)) | ||
return i | ||
} | ||
return -1 | ||
} | ||
function stringify(item){ | ||
return JSON.stringify(item) | ||
} |
@@ -45,4 +45,4 @@ 'use strict'; | ||
let fn = ({ path, ...params }) => { | ||
params.path = path || 'this' | ||
let fn = ({ path, label, ...params }) => { | ||
params.path = label || path || 'this' | ||
@@ -49,0 +49,0 @@ return message(params) |
@@ -61,3 +61,3 @@ 'use strict'; | ||
it('should limit values', function(){ | ||
var inst = mixed().oneOf(['hello', 5]) | ||
var inst = mixed().oneOf([5, 'hello']) | ||
@@ -69,3 +69,3 @@ return Promise.all([ | ||
inst.validate(6).should.be.rejected.then(function(err) { | ||
err.errors[0].should.equal('this must be one the following values: hello, 5') | ||
err.errors[0].should.equal('this must be one the following values: 5, hello') | ||
}) | ||
@@ -81,2 +81,7 @@ ]) | ||
.should.eventually.equal(true), | ||
mixed() | ||
.oneOf(['hello']) | ||
.required() | ||
.isValid(null) | ||
.should.eventually.equal(false), | ||
string() | ||
@@ -91,3 +96,3 @@ .nullable() | ||
it('should exclude values', function(){ | ||
var inst = mixed().notOneOf(['hello', 5]) | ||
var inst = mixed().notOneOf([5, 'hello']) | ||
@@ -101,5 +106,8 @@ return Promise.all([ | ||
inst.validate(5).should.be.rejected.then(function(err){ | ||
err.errors[0].should.equal('this must not be one the following values: hello, 5') | ||
err.errors[0].should.equal('this must not be one the following values: 5, hello') | ||
}), | ||
inst.oneOf([5]).isValid(5).should.eventually.equal(true) | ||
inst.oneOf([5]).isValid(5).should.eventually.equal(true), | ||
inst.isValid(null).should.eventually.equal(true), | ||
inst.required().isValid(null).should.eventually.equal(false) | ||
]) | ||
@@ -442,2 +450,15 @@ }) | ||
it('should use label in error message', function () { | ||
var label = 'Label' | ||
var inst = object({ | ||
prop: string().required().label(label) | ||
}) | ||
return Promise.all([ | ||
inst.validate({}).should.be.rejected.then(function (err) { | ||
err.message.should.equal(`${label} is a required field`) | ||
}) | ||
]) | ||
}) | ||
}) |
@@ -445,3 +445,3 @@ 'use strict'; | ||
.then(function(err){ | ||
err.errors[0].should.contain('must be at least 5') | ||
err.errors[0].should.contain('must be greater than or equal to 5') | ||
}), | ||
@@ -448,0 +448,0 @@ |
130
test/yup.js
@@ -7,2 +7,3 @@ 'use strict'; | ||
, reach = require('../src/util/reach') | ||
, BadSet = require('../src/util/set') | ||
, number = require('../src/number') | ||
@@ -88,4 +89,131 @@ , array = require('../src/array') | ||
describe('BadSet', function(){ | ||
it('should preserve primitive types', function(){ | ||
var set = new BadSet() | ||
set.add(2) | ||
set.has(2).should.be.true | ||
set.has('2').should.be.false | ||
set.values().should.eql([2]) | ||
set.add('3') | ||
set.has('3').should.be.true | ||
set.has(3).should.be.false | ||
set.values().should.eql([2, '3']) | ||
set.add(false) | ||
set.has(false).should.be.true | ||
set.has('false').should.be.false | ||
set.values().should.eql([2, '3', false]) | ||
set.add('true') | ||
set.has('true').should.be.true | ||
set.has(true).should.be.false | ||
set.values().should.eql([2, '3', false, 'true']) | ||
set.add(null) | ||
set.has(null).should.be.true | ||
set.has('null').should.be.false | ||
set.values().should.eql([2, '3', false, 'true', null]) | ||
set.add(undefined) | ||
set.has(undefined).should.be.true | ||
set.has('undefined').should.be.false | ||
set.values().should.eql([2, '3', false, 'true', null, undefined]) | ||
}) | ||
it('should perform value equality for arrays and objects', function(){ | ||
var set = new BadSet() | ||
var oneTwoThree = [1, '2', 3] | ||
set.add(oneTwoThree) | ||
set.has(oneTwoThree).should.be.true | ||
set.has([1, '2', 3]).should.be.true | ||
set.values().should.eql([[1, '2', 3]]) | ||
set.length.should.equal(1) | ||
set.add([1, '2', 3]) | ||
set.has(oneTwoThree).should.be.true | ||
set.has([1, '2', 3]).should.be.true | ||
set.values().should.eql([[1, '2', 3]]) | ||
set.length.should.equal(1) | ||
var aOnebTwo = { a: 1, b: '2'} | ||
set.add(aOnebTwo) | ||
set.has(aOnebTwo).should.be.true | ||
set.has({ a: 1, b: '2'}).should.be.true | ||
set.values().should.eql([[1, '2', 3], { a: 1, b: '2'}]) | ||
set.length.should.equal(2) | ||
set.add({ a: 1, b: '2'}) | ||
set.has(aOnebTwo).should.be.true | ||
set.has({ a: 1, b: '2'}).should.be.true | ||
set.values().should.eql([[1, '2', 3], { a: 1, b: '2'}]) | ||
set.length.should.equal(2) | ||
}) | ||
it('should perform value equality for dates', function(){ | ||
var set = new BadSet() | ||
var someDate = new Date('12-12-12') | ||
set.add(someDate) | ||
set.has(someDate).should.be.true | ||
set.has(new Date('12-12-12')).should.be.true | ||
set.values().should.eql([new Date('12-12-12')]) | ||
set.length.should.equal(1) | ||
set.add(new Date('12-12-12')) | ||
set.has(someDate).should.be.true | ||
set.has(new Date('12-12-12')).should.be.true | ||
set.values().should.eql([new Date('12-12-12')]) | ||
set.length.should.equal(1) | ||
}) | ||
it('should not contain the same value twice', function(){ | ||
var set = new BadSet() | ||
var arrayWithDuplicates = [ | ||
1, | ||
2, | ||
3, | ||
'2', | ||
3, | ||
'abc', | ||
new Date('12-12-12'), | ||
4, | ||
new Date('12-12-12') | ||
] | ||
arrayWithDuplicates.forEach(item => set.add(item)) | ||
set.values().sort().should.eql( | ||
[1, 2, 3, '2', 'abc', new Date('12-12-12'), 4].sort()) | ||
}) | ||
it('should delete values', function(){ | ||
var set = new BadSet() | ||
set.add(2) | ||
set.has(2).should.be.true | ||
set.length.should.equal(1) | ||
set.values().should.eql([2]) | ||
set.delete('2') | ||
set.has(2).should.be.true | ||
set.length.should.equal(1) | ||
set.values().should.eql([2]) | ||
set.add('3') | ||
set.has(2).should.be.true | ||
set.has('3').should.be.true | ||
set.length.should.equal(2) | ||
set.values().should.eql([2, '3']) | ||
set.delete('3') | ||
set.has(2).should.be.true | ||
set.has('3').should.be.false | ||
set.length.should.equal(1) | ||
set.values().should.eql([2]) | ||
}) | ||
}) | ||
// it.only('should REACH with conditions', function(){ | ||
@@ -120,2 +248,2 @@ // var num = number() | ||
}) | ||
}) |
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
354951
9301
838