0.28.2 • Published 4 years ago

easevalidation v0.28.2

Weekly downloads
8
License
MIT
Repository
github
Last release
4 years ago

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: 'me@gmail.com',
  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: 'me@gmail.com',
  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
}
0.28.2

4 years ago

0.28.1

4 years ago

0.28.0

5 years ago

0.27.0

5 years ago

0.26.0

5 years ago

0.25.0

5 years ago

0.24.0

5 years ago

0.23.0

5 years ago

0.22.1

5 years ago

0.22.0

5 years ago

0.21.1

5 years ago

0.21.0

5 years ago

0.20.0

5 years ago

0.19.0

5 years ago

0.18.0

5 years ago

0.17.0

5 years ago

0.16.0

5 years ago

0.15.0

5 years ago

0.14.0

5 years ago

0.13.0

5 years ago

0.12.0

5 years ago

0.11.0

5 years ago

0.10.0

5 years ago

0.9.0

5 years ago

0.8.0

5 years ago

0.7.0

5 years ago

0.6.0

5 years ago

0.5.0

5 years ago

0.4.0

5 years ago

0.3.0

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago