Socket
Socket
Sign inDemoInstall

valido

Package Overview
Dependencies
Maintainers
1
Versions
40
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

valido - npm Package Compare versions

Comparing version 1.0.5 to 1.0.6

validators/isNullOrUndefined.js

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;

2

package.json
{
"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);
});
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc