Coder Social home page Coder Social logo

easevalidation's Introduction

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
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.