Giter VIP home page Giter VIP logo

validatr.js's Introduction

validar.js

JavaScript validation library for NodeJS.

validatr.js is written in TypeScript and compiled with ES6 target.

Usage in TypeScript projects is highly recommended since the library uses templates for smart intelisense.

Instalation

Install the library using npm install validatr.js.

Built in validators

  // check if a field is not empty
  notEmpty()

  // check if a field is not equal to value
  notEqual(value: any)

  // check if a field is equal to value
  equal(value: any)

  // check if a fields' length is within bounds
  length(min: number, max?: number)

  // check if a field is a valid email
  isEmail()

  // check if a field is a number and is above value
  min(value: number)
  
  // check if a field is a number and is below value
  max(value: number)

  // custom validator, provide your own function to validate a field.
  // signal errors by throwing exceptions
  must(predicate: {(field: FieldType) : void})

Example usage

Usage with primitive values

  import {ValidatR} from 'validatr.js';
  
  let validator = new ValidaR<string>();

  // configure validations
  validator.addRule('email', x => x).notEmpty().isEmail();

  // run validator
  let validation = validator.run('[email protected]');
  
  // check result
  if (!validation.isValid()) {
    // your validation error logic here
    console.error(validation.getErrors());
  }

Usage with JSON objects

  import {ValidatR} from 'validatr.js';
  
  interface IUser {
    email: string,
    firstname: string,
    lastname: string
  }

  let user: IUser = {
    email: '[email protected]',
    firstname: 'Dragos',
    lastname: 'Sebestin'
  };

  let validator = new ValidaR<IUser>();

  // configure validations
  validator.addRule('email', x => x.email).notEmpty().isEmail();
  validator.addRule('firstname', x => x.firstname).notEmpty().length(5);
  validator.addRule('lastname', x => x.lastname).notEmpty().length(5, 15);

  // run validator
  let validation = validator.run(user);
  
  // check result
  if (!validation.isValid()) {
    // your validation error logic here
    console.error(validation.getErrors());
  }

Full documentation

Soon to come.

validatr.js's People

Contributors

sebestindragos avatar

Stargazers

Ionut Tonita avatar  avatar

Watchers

Ionut Tonita avatar James Cloos avatar  avatar  avatar  avatar

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.