All Projects → viczam → easevalidation

viczam / easevalidation

Licence: MIT license
javascript validation library

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to easevalidation

constant-time-js
Constant-time JavaScript functions
Stars: ✭ 43 (+207.14%)
Mutual labels:  javascript-library
iFrameX
Iframe generator with dynamic content injection like HTML, Javascript, CSS, etc. and two ways communication, parent <-> iframe.
Stars: ✭ 18 (+28.57%)
Mutual labels:  javascript-library
prime.js
Prime JS is a different kind of JavaScript framework. Prime is written in 100% standard, explicit, and namespaced Object Oriented JavaScript.
Stars: ✭ 13 (-7.14%)
Mutual labels:  javascript-library
menu-hamburger
🍔 A clean, simple and easy to use library to create a Menu Hamburger
Stars: ✭ 17 (+21.43%)
Mutual labels:  javascript-library
html-to-react
A lightweight library that converts raw HTML to a React DOM structure.
Stars: ✭ 696 (+4871.43%)
Mutual labels:  javascript-library
puddle.js
An ASCII/Node based fluid simulation library.
Stars: ✭ 102 (+628.57%)
Mutual labels:  javascript-library
rutils
ruitls.js 涵盖了前端开发常用的工具方法,有字符串、数字、数组、缓存、文件等,尽可能的避免前端在开发中重复造轮子
Stars: ✭ 14 (+0%)
Mutual labels:  javascript-library
blaver
A JavaScript library built on top of the Faker.JS library. It generates massive amounts of fake data in the browser and node.js.
Stars: ✭ 112 (+700%)
Mutual labels:  javascript-library
staballoy
Reactive UI framework for Titanium Alloy
Stars: ✭ 18 (+28.57%)
Mutual labels:  javascript-library
placekey-js
placekey.io
Stars: ✭ 19 (+35.71%)
Mutual labels:  javascript-library
costa-rica-iban
Funciones utiles para extraer y validar información general de números de cuenta IBAN de Costa Rica
Stars: ✭ 16 (+14.29%)
Mutual labels:  javascript-library
xGallerify
A lightweight, responsive, smart gallery based on jQuery
Stars: ✭ 52 (+271.43%)
Mutual labels:  javascript-library
animusjs
🎆 AnimusJS is the solution for combine JS and CSS animations.
Stars: ✭ 42 (+200%)
Mutual labels:  javascript-library
animate
PixiJS runtime library for content from Adobe Animate CC
Stars: ✭ 142 (+914.29%)
Mutual labels:  javascript-library
harsh
Hashids implementation in Rust
Stars: ✭ 48 (+242.86%)
Mutual labels:  javascript-library
example-typescript-package
Example TypeScript Package ready to be published on npm & Tutorial / Instruction / Workflow for 2021
Stars: ✭ 71 (+407.14%)
Mutual labels:  javascript-library
Amino.JS
A powerful JavaScript library for interacting with the Amino API 🌟
Stars: ✭ 25 (+78.57%)
Mutual labels:  javascript-library
Glize
📚 Glize is a clean and robust pure Javascript library.
Stars: ✭ 16 (+14.29%)
Mutual labels:  javascript-library
tarballjs
Javascript library to create or read tar files in the browser
Stars: ✭ 24 (+71.43%)
Mutual labels:  javascript-library
html-chain
🔗 A super small javascript library to make html by chaining javascript functions
Stars: ✭ 32 (+128.57%)
Mutual labels:  javascript-library

easevalidation

Install

npm install easevalidation

easevalidation is an easy to extend javascript validation library that supports multiple types of validators: functional validation, schema based, chained validators etc.

It comes bundled with all lodash is* validators (like isPlainObject, isFinite etc), all the validator.js validators and the date-related validators date-fns comes with. On top of that, it features some commonly used validators you can find here.

You test the input data using the test function:

import { test, validators } from 'easevalidation';

const { isNumber, isMin, isMax } = validators;

const isValid = test(isNumber(), isMin(10), isMax(15))(13); // true

Or you can use a chained validator:

import { test, number } from 'easevalidation';

const isValid1 = test(
  number()
    .isMin(10)
    .isMax(15),
)(13);

// or

const isValid2 = number()
  .isMin(10)
  .isMax(15)
  .test(13);

// isValid1 === isValid2 === true

You can also validate an object by a schema:

import { test, validators } from 'easevalidation';

const { isSchema, isEmail, isRequired, isString, isLength } = validators;

const schema = isSchema({
  email: [isEmail()],
  password: [isRequired(), isString(), isLength({ min: 5 })],
});

const isValid = test(schema)({
  email: '[email protected]',
  password: '12345',
});

Or:

import { test, validators } from 'easevalidation';

const { isPlainObject, isProperty, isEmail, isRequired, isString, isLength } = validators;

const schema = [
  isPlainObject(),
  isProperty('email', isEmail()),
  isProperty('password', isRequired(), isString(), isLength({ min: 5 })),
];

const isValid = test(schema)({
  email: '[email protected]',
  password: '12345',
});

While easevalidation comes prebuilt with many validators, creating your own validators is an easy job.

import { createValidator, test } from 'easevalidation';

const isBetween = createValidator('isBetween', (value, min, max) => min <= value && value <= max);

const isValid = test(isBetween(10, 15))(13); // true

Validators can also update the value they receive for testing.

import { createValidator, test } from 'easevalidation';
import { ObjectID as objectId } from 'mongodb';

const isObjectId = createValidator(
  'isObjectId',
  value => objectId.isValid(value),
  value => objectId(value),
);

const isValid = test(isObjectId())('5bf6cd3e766582a5bf892519');

As you can see, the signature of createValidator is:

createValidator(String code, Function validate, [Function updateValue, Function validateConfig])

Keep in mind that updateValue will run after validate function tests the value and only if it returns true (value passes validation).

Only code and validate are required, the rest of arguments are optional. validateConfig is a function that can be used to validate the configuration the validate function will receive.

Another way to update the value is by returning an object from validate:

import { createValidator, test } from 'easevalidation';
import { ObjectID as objectId } from 'mongodb';

const isObjectId = createValidator('isObjectId', value => ({
  isValid: objectId.isValid(value),
  value: objectId(value),
}));

const isValid = test(isObjectId())('5bf6cd3e766582a5bf892519');

Sometimes you may want to get access to the final updated value, besides just testing it.

import { createValidator, test, validators } from 'easevalidation';
import { ObjectID as objectId } from 'mongodb';

const { isSchema, isString, isNumber, isMin } = validators;

const isObjectId = createValidator('isObjectId', value => ({
  isValid: objectId.isValid(value),
  value: objectId(value),
}));

const validate = test(
  isSchema({
    name: isString(),
    age: [isNumber(), isMin(20)],
    id: isObjectId(),
  }),
);

const isValid = validate({
  name: 'John Doe',
  age: '22',
  id: '5bf6cd3e766582a5bf892519',
});

const { value } = validate;

// In this case `isValid` will be `true` and `value` will be:

{
  name: 'John Doe',
  age: 22, // number
  id: ObjectId('5bf6cd3e766582a5bf892519') // object
}

Instead of building a validation function like we did above, you can use validate:

import { createValidator, validate, validators } from 'easevalidation';
import { ObjectID as objectId } from 'mongodb';

const { isSchema, isString, isNumber, isMin } = validators;

try {
  const value = validate(
    isSchema({
      name: isString(),
      age: [isNumber(), isMin(20)],
      id: isObjectId(),
    }),
  )({
    name: 'John Doe',
    age: '22',
    id: '5bf6cd3e766582a5bf892519',
  });
} catch (err) {
  // won't get here, because it passes validation
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].