GithubHelp home page GithubHelp logo

wino-validator's Introduction

Wino Validator

Wino validator is a template based validation framework for Javascript that allows you to validate named data and return errors in a nice organized format.

Wino validator is inspired by PHP's Framework laravel validation

Table of contents

Introduction

To install run npm install wino-validator. Here is an example of how to use it

const validator = require('wino-validator')

let validation_errors = validator.validate([
   ['ip_address-> required|ip:4', '123.257.123.25']
])

console.log(validation_errors)

The output should be

[ { field: 'ip', message: [ 'the \'ip\' field should be a version 4 ip address' ] } ]

because the ip address is not valid

Examples

Validation is easy. There are two validation options

Option One - Array

Here is an example of an object array validation type:

let errors = validator.validate([
   ['email-> required|char_between: 50, 52|email', undefined]
])

console.log(errors)

Output:

[{ 
   field: 'email',
   message:[ 
      'the \'email\' field is required',
      'the \'email\' field should fall between 50 and 52 characters',
      'the \'email\' field is not a valid email' 
   ] 
}]

Option Two - Object Array

Here is an example of an object array validation type:

let errors = validator.validateObject([
    {
        field: 'email',
        data: 'company-a@gmail',
        rules: 'required|char_between: 50, 52|email'
    }
]);

console.log(errors)

Output:

[{ field: 'email',
   message: [ 
      'the \'email\' field should fall between 50 and 52 characters',
      'the \'email\' field is not a valid email' 
   ] 
}]

Multiple Field Validation

Validation can be for more than one field as in the example below

let errors = validator.validate([
   ['email-> required|char_between: 50, 52|email', undefined],
   ['mobile_number-> required|min_char: 10|max_char: 10|numeric', '072398760']
])

console.log(errors)

Output:

[
   { 
      field: 'email',
      message:[ 
         'the \'email\' field is required',
         'the \'email\' field should fall between 50 and 52 characters',
         'the \'email\' field is not a valid email' 
      ] 
   },
   { 
      field: 'mobile_number',
      message:[ 
         'the \'mobile_number\' field should be greater than or equal to 10 characters' 
      ]
   }
]

Validation Rules

wino-validate supports a number of validation rules, below is a list of all the rules supported

required

Validates that a field is not empty. A field is considered "empty" if one of the following conditions are true:

  • The value is undefined.
  • The value is null.
  • The value is an empty string.

Licence

As of September 29, 2019 wino-validator is licensed under the GPLv3+

wino-validator's People

Contributors

jeffrey-kimani avatar jeffrey003 avatar

Watchers

 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.