Package Exports
- validatees
- validatees/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (validatees) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
validatees
Validation package for ES6+, TypeScript and JavaScript(CommonJS and Module) ready.
Features
- πEasy to use: Easy to install in your project.
- β ES6+ && TS: TypeScript and ES6+ support(JS).
- πSmall footprint: With less then 10kb, you won't even notice.
- π¦No dependencies: You don't depend on anything else.
Getting Started
Installation
To use this package, install using npm
, yarn
or pnpm
π₯:
# npm
npm install validatees
# yarn
yarn add validatees
# pnpm
pnpm install validatees
Usage
// ES6+ JavaScript CommonsJs
const validatees = require("validatees");
// TypeScript || ES6+ JavaScript module
import validatees from "validatees";
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 validateesisTruthy
orisFalsy
for example.
options:
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:
registerArrayListeners
: register listeners for given arrays.unregisterArrayListeners
: unregister listeners for given arrays.
const { VListener } = require("validatees");
const vListener = new VListener();
const arr1 = [];
const arr2 = [{ a: 1 }, { b: 2 }]; // items already added will not be validated.
vListener.registerArrayListeners([
{ array: arr1, callback: isTruthy, options: { condition: true, strict: false } },
{ array: arr2, callback: isFalsyExtended, options: { condition: false } },
]);
arr1.push(1); // should be pushed
arr2.push({ c: 3 }, { d: 4 }, null); // null should not be pushed
arr1.push(0); // should not be pushed
arr2.push({}); // should not be pushed
console.log(arr1); // [1]
console.log(arr2); // [{ a: 1 }, { b: 2 }, { c: 3 }, { d: 4 }]
vListener.unregisterArrayListeners([{ array: arr1 }]);
arr1.push(0); // can now be pushed again.
console.log(arr1); // [1, 0]
vListener.unregisterArrayListeners([{ array: arr2 }]);
arr2.push({}); // can now be pushed again.
console.log(arr2); // [{ a: 1 }, { b: 2 }, { c: 3 }, { d: 4 }, {}]
Types
Type checking can be difficult, but with validatees
types, it's easy.
isFalsy:
Made from 'Falsy MDN defenition'.
const { isFalsy } = require("validatees");
isFalsy(0); // true
isFalsy(1); // false
isFalsyExtended:
Made from 'Falsy MDN defenition'. Also includes Array and object checking.
const { isFalsyExtended } = require("validatees");
isFalsyExtended(1); // false
isFalsyExtended(0); // true
isFalsyExtended([]); // true
isFalsyExtended({}); // true
isTruthy:
Everything not falsy is truthy. Made from 'Truthy MDN defenition'
const { isTruthy } = require("validatees");
isTruthy(1); // true
isTruthy(0); // false
isTruthyExtended:
Everything not falsy is truthy. Made from 'Truthy MDN defenition' Also includes Array and object checking.
const { isTruthyExtended } = require("validatees");
isTruthyExtended(1); // true
isTruthyExtended(0); // false
isTruthyExtended([]); // false
isTruthyExtended({}); // false
isNullish:
Check if value is null or undefined.
const { isNullish } = require("validatees");
isNullish(null); // true
isNullish(undefined); // true
isNullish(0); // false
isString:
Check if value is a string.
const { isString } = require("validatees");
isString("string"); // true
isString(1); // false
isNumber:
Check if value is a number.
const { isNumber } = require("validatees");
isNumber(1); // true
isNumber(Infinity); // true
isNumber("string"); // false
isBoolean:
Check if value is a boolean.
const { isBoolean } = require("validatees");
isBoolean(true); // true
isBoolean(false); // true
isBoolean("1"); // false
isExtendable:
Check if value is extendable for modification.
const { isExtendable } = require("validatees");
isExtendable({}); // true
isExtendable([]); // true
const prevent = {};
Object.preventExtensions(prevent);
isExtendable(obj); // false
const sealed = {};
Object.seal(sealed);
isExtendable(sealed); // false
const frozen = {};
Object.freeze(frozen);
isExtendable(frozen); // false
const frozenArray = [];
Object.freeze(frozenArray);
isExtendable(frozenArray); // false
isExtendable(1 /**anything else then object or array */); // throws error
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("validatees");
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("validatees");
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("validatees");
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
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("validatees");
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π¦? or want to suggest a new featureπ? or just want to helpπ? 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π.
πNote: Make sure to add tests for your changes β .