0.2.8 • Published 6 years ago

valid8tor v0.2.8

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

Build Status

valid8tor

A dead simple JS validation library inspired by Laravel rules.

Installation

npm i valid8tor --save

Usage

Valid8tor takes an object (the data) and validate using set of rules define in object containing the same keys and validation rules. Example:

import { validate, validateSync } from 'valid8tor';

// You can combine several rules
const rules = {
  firstname: 'required|alpha_dash|min:3',
  lastname: 'alpha_dash|nullable',
  email: 'required|email',
  jobs: 'array|min:1|max:5',
  website: 'required|url',
};

const data = {
  firstname: 'John',
  lastname: 'Doe',
  email: 'john@doe.io',
  jobs: ['Web Developer', 'Traveler', 'Lifehacker'],
  website: 'https://askthomas.co.uk',
};

validate(data, rules).then(() => {
  // data is valid
}).catch((errors) => {
  // not valid
  console.log(errors);
});

// Or if you want synchronous validation
validateSync(data, rules) // => Will return either [] or example: [{ field: 'firstname', error: 'The firstname field is required' }]

valid8tor provides 3 functions:

  • validate(dataObj, rulesObj) Validate an object for a given set of rules and return a promise.
  • validateSync(dataObj, rulesObj) Validate and object and return an array of error or empty array if the data is valid.
  • isValid(dataObj, rulesObj) Validate the object and return true or false if valid or not.

Validation rules

RuleDescription
requiredMake sure a value is defined.
nullableAccept a value as null or undefined.
min:3String Check that string has minimum length (3 characters in this example). Number: Check that a number is at least equal to 3. Array: Check that an array has at leat 3 elements.
max:10String: Check that string has a maximum length (10 characters in this example). Number: Check that a number is at 10 at max. Array: Check that an array has at max 10 elements.
size:5String: Check that the length of the string is equal to 5 (in the example). Array Check that the size of an array is equal to 5.
between:1,5Number Check that a number is in the interval (inclusive). Array: Check that an array has a number at least one and at max 5 elements (in this example). String: Check that the length of the string is in that interval
emailCheck that a value is a correct email address.
urlCheck that a value is a valid a URL (http or https only for now)
ipCheck that a value is a valid IP v4 address
ipv6Check that a value is a valid IP v6 address
alphaCheck that the value only contains alphabetic characters (a to z and A to Z)
alpha_dashCheck that the value only contains alphabetic characters, hyphens and underscores (a-z, A-Z, - and _ )
alpha_numCheck that the value only contains alphabetic characters, hyphens and underscores (a-z, A-Z and 0-9 )
alphanum_dashCheck that the value only contains alphabetic characters, numbers, hyphens and underscores (a-z, A-Z, 0-9, - and _ )
integerCheck that the value is an integer (positive, negative, null and 0x0)
decimalCheck that the value is a decimal (positive, negative or null)
numericCheck that the value is numeric (not null, not decimal)
base64Check that the value is base64 format
arrayCheck that the value is an array (empty or not)
acceptedCheck acceptance, example when you have terms and conditions, etc. This rule accept: 1, true, yes and on.
stringCheck that the value is a string
booleanCheck that value is a boolean
dateCheck that the value is a Date or a moment instance.
afterComing soon
after_or_equalComing soon
beforeComing soon
before_or_equalComing soon

Testing

Testing with Mocha: npm run test

Contributions

You can use dev.js as a playground using Parcel parcel index.html

To do

  • Allow override of error messages
  • Allow to pass a object containing custom validation functions
  • Add Date/time related rules (after, before, etc)
  • Add more tests
0.2.8

6 years ago

0.2.7

6 years ago

0.2.6

6 years ago

0.2.5

6 years ago

0.2.4

6 years ago

0.2.3

6 years ago

0.2.2

6 years ago

0.2.0

6 years ago

0.1.0

6 years ago