Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

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.1 to 1.0.2

10

main.js

@@ -20,3 +20,3 @@ 'use strict';

api[validatorName] = validator.validate;
api.array[validatorName] = (values, allowEmpty) => validateArray(values, validator, allowEmpty);
api.array[validatorName] = (values, options) => validateArray(values, validator, options);
});

@@ -29,6 +29,6 @@

* @param {object} validator - Validator
* @param {Boolean} allowEmpty - Allow empty array
* @param {object} options - Options
* @returns {Boolean}
*/
function validateArray(values, validator, allowEmpty) {
function validateArray(values, validator, options) {
if (!isArray.validate(values)) {

@@ -38,9 +38,9 @@ return false;

if (!allowEmpty && !values.length) {
if (!(options && options.allowEmpty) && !values.length) {
return false;
}
return values.every(validator.validate);
return values.every((value) => validator.validate(value, options));
}
module.exports = api;
{
"name": "valido",
"version": "1.0.1",
"version": "1.0.2",
"description": "Validation provider",

@@ -5,0 +5,0 @@ "main": "main.js",

@@ -37,12 +37,10 @@ 'use strict';

it('should validate a list of values (valid samples)', () => {
return expect(valido.array[validatorName](validValues))
.to.equal(true);
it('should validate a list of values', () => {
validator.tests.forEach((test) => {
const values = [test.value, test.value];
return expect(valido.array[validatorName](values, test.options))
.to.equal(test.result);
});
});
it('should validate a list of invalid values (invalid samples)', () => {
return expect(valido.array[validatorName](invalidValues))
.to.equal(false);
});
it('should validate an empty list to false', () => {

@@ -54,3 +52,3 @@ return expect(valido.array[validatorName]([]))

it('should validate an empty list to true if "allowEmpty" is true', () => {
return expect(valido.array[validatorName]([], true))
return expect(valido.array[validatorName]([], { allowEmpty: true }))
.to.equal(true);

@@ -57,0 +55,0 @@ });

@@ -7,7 +7,23 @@ 'use strict';

* @param {String} value - Value
* @param {object} options - Options
* @returns {Boolean}
*/
function validate(value) {
return value instanceof String || typeof value === 'string';
function validate(value, options) {
let isString = value instanceof String || typeof value === 'string';
if (!options) {
return isString;
}
// Optional checks
if (options.endsWith && !value.endsWith(options.endsWith)) {
isString = false;
}
if (options.startsWith && !value.startsWith(options.startsWith)) {
isString = false;
}
return isString;
}

@@ -26,5 +42,12 @@

{ value: 'abc', result: true },
{ value: 'abc' + 123, result: true }
{ value: 'abc' + 123, result: true },
{ value: 'abc', options: { startsWith: 'a' }, result: true },
{ value: 'abc', options: { startsWith: 'b' }, result: false },
{ value: 'abc', options: { endsWith: 'c' }, result: true },
{ value: 'abc', options: { endsWith: 'd' }, result: false },
{ value: 'abc', options: { startsWith: 'b', endsWith: 'c' }, result: false },
{ value: 'abc', options: { startsWith: 'a', endsWith: 'd' }, result: false },
{ value: 'abc', options: { startsWith: 'a', endsWith: 'c' }, result: true }
];
module.exports = { validate, tests };

@@ -69,7 +69,15 @@ 'use strict';

// Check for trailing slash
if (options && options.trailingSlash && !value.endsWith('/')) {
if (!options) {
return isWebUrl;
}
// Optional checks
if (options.endsWith && !value.endsWith(options.endsWith)) {
isWebUrl = false;
}
if (options.startsWith && !value.startsWith(options.startsWith)) {
isWebUrl = false;
}
return isWebUrl;

@@ -100,5 +108,12 @@ }

{ value: 'https://8.8.8.8:3128', result: true },
{ value: 'https://pass:bob@www.google.com:8080/index.html?param=2&yy=abc', result: true }
{ value: 'https://pass:bob@www.google.com:8080/index.html?param=2&yy=abc', result: true },
{ value: 'https://www.google.com', options: { endsWith: '/' }, result: false },
{ value: 'https://www.google.com/', options: { endsWith: '/' }, result: true },
{ value: 'https://www.google.com', options: { startsWith: 'https://www.ebay' }, result: false },
{ value: 'https://www.google.com/', options: { startsWith: 'https://www.google' }, result: true },
{ value: 'https://www.google.com/', options: { startsWith: 'https://www.google', endsWith: '/' }, result: true },
{ value: 'https://www.google.com', options: { startsWith: 'https://www.google', endsWith: '/' }, result: false },
{ value: 'https://www.google.com/', options: { startsWith: 'https://www.ebay', endsWith: '/' }, result: false }
];
module.exports = { validate, tests };
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