Skip to content

Jeffrey-Kimani/wino-validator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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+

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published