1.0.4 • Published 2 years ago

js-validation-tool v1.0.4

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

js-validation-tool

Uses validator.js under the hood.

Installation and usage

Install the library with npm i js-validation-tool

Import

const jsValidator = require('js-validator-tool');
// or
const validate = require('js-validator-tool/core/validate');
const validations = require('js-validator-tool/core/validations);
// or
const { validate, validateModel, validations } = require('js-validator-tool);

Validation models

Models are functions that returns an object, each property has a collection of validations.

const ModelValidations = require('js-validator-tool/core/modelValidations');
const userModel = () => ({
  name: [ModelValidations.string.isNotEmpty()],
  age: [ModelValidations.number.isBetween({ from: 18, to: 30 })],
  isNew: [ModelValidations.boolean.isBool()]
});

Single validations

Every validation takes the following parameters

ParameterDescriptionOptional
keyProperty name being validatedNo
valueValue to validateNo
configurationValidation additional parametersYes
errorMessageCustom error message when validation is failedYes

returns a Promise<{ fields: String, isValid: Boolean }>

Available validations

Validations are separated in types (string, boolean, number and common)

String validations

ValidationDescriptionParams
isNotEmptyReturns true if the given string value is not empty
isStringReturns true if the given value is a string
isMongoIdReturns true if the given value is a valid mongo id
isDateReturns true if the given value is a date
isEmailReturns true if the given value is a valid email address
isLengthReturns true if the given string matches the provided min - max length{min: number, max: number}

Common validations

ValidationDescription
isOptionalSet the property as optional to be validated
isOneOfReturns true when any value matches the provided array

Number validations

ValidationDescription
isNumericReturns true when the given value is a valid number
isNotZeroReturns true when the provided value is different to 0
isBetweenReturns true when the provided value matches with the provided range (config)

Bool validations

ValidationDescription
isBoolReturns true if the given value is boolean ('true' is valid)

Example

const validationResult = await validate([
  validations.string.isString('name', 'Jhon'),
  validations.number.isNotZero('age', 0),
]);
console.log(validationResult); // prints: { isValid: true, fields: ["Field 'age', expected not to be Zero. Got 0"] }

Add your own validations

const customValidator = (prop, value) => {
  const onFailureMessage = `Field '${prop}', is not valid`;
  return {
    validation: 'myCustomValidation', // validation name
    property: prop,
    onFailureMessage,
    execute: () => value !== undefined,
  };
};

const customValResult = await validate([customValidator('name', undefined)]);
console.log(customValResult); // prints { isValid: false, fields: [ "Field 'name', is not valid" ] }

Full example

const { validate, validateModel, validations, modelValidations } = require('js-validator-tool');

(async () => {
  // model validator example
  const productModel = () => ({
    name: [modelValidations.string.isNotEmpty('name', entity.name)],
    description: [
      modelValidations.common.isOptional('description', entity.description),
      modelValidations.string.isNotEmpty('description', entity.description),
    ],
    isNew: [modelValidations.boolean.isBool('isNew', entity.isNew)],
  });

  const product = {
    name: 'Super box',
    // description is marked as optional, so can be omitted
    isNew: false,
  };

  const modelValidationResult = await validateModel(productModel, product);
  console.log(modelValidationResult); // prints {isValid: true, fields: []}

  // single validations example
  const validationResult = await validate([
    validations.string.isString('foo', 'fooValue'),
    validations.number.isNotZero('bar', 0),
  ]);
  console.log(validationResult); // prints { isValid: true, fields: ["Field 'bar', expected not to be Zero. Got 0"] }

  // custom validation example
  const customValidator = (prop, value) => {
    const onFailureMessage = `Field '${prop}', is not valid`;
    return {
      validation: 'myCustomValidation', // validation name
      property: prop,
      onFailureMessage,
      execute: () => value !== undefined,
    };
  };

  const customValResult = await validate([customValidator('name', undefined)]);
  console.log(customValResult); // prints { isValid: false, fields: [ "Field 'name', is not valid" ] }
})();
1.0.2

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.2.0

2 years ago

0.1.7

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago