@jetvil/validate
@jetvil
packages.
Features
-
🚀 Easy to use: Easy to install in your project. -
✅ ES6+ && TS: TypeScript and ES6+ support(JS). -
🐭 Small footprint: With less then 20kb, you won't even notice. -
📦 No dependencies: You don't depend on anything else. -
💵 Free: It's free and always will be, the beauty of open source.
Getting Started
Installation
To use this package, install using npm
, yarn
or pnpm
# npm
npm install @jetvil/validate
# yarn
yarn add @jetvil/validate
# pnpm
pnpm install @jetvil/validate
Usage
// ES6+ JavaScript CommonsJs
const validate = require("@jetvil/validate");
// TypeScript || ES6+ JavaScript module
import * as validate from "@jetvil/validate";
Documentation
VListener
Adding validation over your array before your program starts.
parameters:
-
array
: array to validate. -
callback
: function to validate each item in the array, must return boolean, can only take 1 parameter(for now!).
Use @jetvil/validateisTruthy
orisFalsy
for example.
options-vlistener:
-
strict
:boolean
, default:false
, strict mode; skip push if validation fails in 1 or more items. -
condition
:boolean
, default:true
, condition to validate; check if callback withcondition
is met.
functions:
-
register
: register listeners for given arrays. -
remove
: remove listeners for given arrays.
const { VListener } = require("@jetvil/validate");
const vListener = new VListener();
const arr1 = [];
const arr2 = [{ a: 1 }, { b: 2 }]; // items already added will not be validated.
const customCallback = (value) => {
...
}; // create your own callback
const customCallbackWithDefaultValues = (value, checkOnThis = {id:1}) => {
...
}; // create your own callback with default values to check against
vListener.register([
{ array: arr1, callback: isTruthy, options: { strict: true } },
{ array: arr2, callback: isFalsyExtended, options: { condition: false } },
{ array: arr2, callback: [isTruthy, customCallback, customCallbackWithDefaultValues], options: { strict: true } },
]);
arr1.push(1); // should be pushed, [1]
arr2.push({ c: 3 }, { d: 4 }, null); // should not be pushed, strict mode is on, [{ a: 1 }, { b: 2 }]
arr1.push(0); // should not be pushed, [1]
arr2.push({}); // should not be pushed, [{ a: 1 }, { b: 2 }]
vListener.remove([{ array: arr1 }]);
arr1.push(0); // can now be pushed again, [1, 0]
vListener.remove([{ array: arr2 }]);
arr2.push({}); // can now be pushed again, [{ a: 1 }, { b: 2 }, {}]
Matchers
Matchers are functions that check if a value matches a certain pattern or value.
isUnique:
Check if value is unique in array or object(key mostly).
const { isUnique } = require("@jetvil/validate");
isUnique([1, 2, 3, 4], 1); // false
isUnique([1, 2, 3, 4], { a: 1 }); // throws error
isUnique({ a: 1, b: 2 }, { a: 1 }); // false
isUnique({ a: 1, b: 2 }, { c: 1 }); // true
isSoftMatch:
Check if two values soft match with each other.
const { isSoftMatch } = require("@jetvil/validate");
isSoftMatch("string", "STRING"); // true
isSoftMatch("string", "abc"); // false
isSoftMatch(1, 1.0); // true
isDeepMatch:
Check if two values deep match with each other.
const { isDeepMatch } = require("@jetvil/validate");
isDeepMatch({ a: 1 }, { a: 1 }); // true
isDeepMatch({ a: 1 }, { a: 2 }); // false
isDeepMatch([1, 2, { a: 3 }], [1, 2, { a: 3 }]); // true
// without the await it will return a promise holding the boolean.
await isDeepMatch(Promise.resolve(1), Promise.resolve(1)); // true
isValidEmail:
Check if value is a valid email.
const { isValidEmail } = require("@jetvil/validate");
isValidEmail("test@test.test"); // true
isValidEmail(""); // false
isValidEmail("test@test"); // false
isValidEmail("test@test.t"); // false
isValidEmail(123); // throws error
isValidPassword:
Check if value is a valid password.
options-isvalidpassword:
-
minLength
: number, default8
-
maxLength
: number, default32
-
numbers
: number, default1
-
specialChars
: number, default1
const { isValidPassword } = require("@jetvil/validate");
isValidPassword("test"); // false
isValidPassword("te!1", { minLength: 4 }); // true
isValidPassword(123); // throws error
isValidPassword("t", { minLength: 1, numbers: 0, specialChars: 0 }); // true
isValidUrl:
Check if value is a valid url.
IPV4 and IPV6 are not supported.
const { isValidUrl } = require("@jetvil/validate");
isValidUrl("https://google.com"); // true
isValidUrl("google.com"); // true
isValidUrl("google"); // false
isValidUrl(123); // throws error
isValidDate:
Check if value is a valid date.
This function takes any input and will parse it to a Date, -1
and 1
will work because they are valid dates.
const { isValidDate } = require("@jetvil/validate");
isValidDate(new Date()); // true
isValidDate("1"); //true: because it will be parsed to a date starting from 1970.
isValidDate(-1); //true : because it will be parsed to a date starting from 1970, moving backwards.
isValidDate("string"); // false
Contributing
Found a bug
Feel free to open an issue or a pull request.
Contributions are always welcome!
- Fork the project here.
- Create a new branch like this:
git checkout -b feature/featureName
. - Commit your changes to your branch:
git commit -m 'Create AwesomeFeature'
⚙️ . - Push your branch:
git push origin feature/featureName
. - Open a pull request on the
dev
branch here🔃 .