Comparing version 1.0.5 to 1.0.6
25
main.js
@@ -9,2 +9,3 @@ 'use strict'; | ||
const isUndefined = require('./validators/isUndefined'); | ||
const isNullOrUndefined = require('./validators/isNullOrUndefined'); | ||
const isInteger = require('./validators/isInteger'); | ||
@@ -21,2 +22,3 @@ const isFinite = require('./validators/isFinite'); | ||
isUndefined, | ||
isNullOrUndefined, | ||
isInteger, | ||
@@ -27,4 +29,5 @@ isFinite, | ||
const api = { every: {} }; | ||
const api = { every: {}, optional: {} }; | ||
// Build API | ||
Object.getOwnPropertyNames(validators).forEach((validatorName) => { | ||
@@ -34,2 +37,3 @@ const validator = validators[validatorName]; | ||
api.every[validatorName] = (values, options) => validateEvery(values, validator, options); | ||
api.optional[validatorName] = (value, options) => validateOptional(value, validator, options); | ||
}); | ||
@@ -50,9 +54,22 @@ | ||
if (!(options && options.allowEmpty) && !values.length) { | ||
return false; | ||
return values.every((value) => validator.validate(value, options)); | ||
} | ||
/** | ||
* Validates an optional values | ||
* An optional value will always validate to true if null/undefined | ||
* | ||
* @param {*} value - Value | ||
* @param {object} validator - Validator | ||
* @param {object} options - Options | ||
* @returns {Boolean} | ||
*/ | ||
function validateOptional(value, validator, options) { | ||
if (isNullOrUndefined.validate(value)) { | ||
return true; | ||
} | ||
return values.every((value) => validator.validate(value, options)); | ||
return validator.validate(value, options); | ||
} | ||
module.exports = api; |
{ | ||
"name": "valido", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "Validation provider", | ||
@@ -5,0 +5,0 @@ "main": "main.js", |
@@ -18,5 +18,9 @@ 'use strict'; | ||
const validator = require(filePath); | ||
const validValues = _.pluck(_.where(validator.tests, { result: true }), 'value'); | ||
const invalidValues = _.pluck(_.where(validator.tests, { result: false }), 'value'); | ||
const notNullOrUndefinedTestValue = (test) => !valido.isNullOrUndefined(test.value); | ||
const optionalValueTests = _.filter(validator.tests, notNullOrUndefinedTestValue); | ||
describe('Validators - ' + validatorName, () => { | ||
@@ -33,25 +37,25 @@ it('should have tests (valid samples)', () => { | ||
validator.tests.forEach((test) => { | ||
return expect(valido[validatorName](test.value, test.options)) | ||
.to.equal(test.result); | ||
return expect(valido[validatorName](test.value, test.options)).to.equal(test.result); | ||
}); | ||
}); | ||
it('should validate an optional value', () => { | ||
optionalValueTests.forEach((test) => { | ||
expect(valido.optional[validatorName](null, test.options)).to.equal(true); | ||
expect(valido.optional[validatorName](undefined, test.options)).to.equal(true); | ||
expect(valido.optional[validatorName](test.value, test.options)).to.equal(test.result); | ||
}); | ||
}); | ||
it('should validate a list of values', () => { | ||
validator.tests.forEach((test) => { | ||
const values = [test.value, test.value]; | ||
return expect(valido.every[validatorName](values, test.options)) | ||
.to.equal(test.result); | ||
return expect(valido.every[validatorName](values, test.options)).to.equal(test.result); | ||
}); | ||
}); | ||
it('should validate an empty list to false', () => { | ||
return expect(valido.every[validatorName]([])) | ||
.to.equal(false); | ||
it('should validate an empty list to true', () => { | ||
return expect(valido.every[validatorName]([])).to.equal(true); | ||
}); | ||
it('should validate an empty list to true if "allowEmpty" is true', () => { | ||
return expect(valido.every[validatorName]([], { allowEmpty: true })) | ||
.to.equal(true); | ||
}); | ||
}); | ||
}); |
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
18099
21
534