valite
TypeScript icon, indicating that this package has built-in type declarations

0.7.0 • Public • Published

valite

Build Status

Concurrently execute your validators in a simple, practical and light validator engine.

Motivation

I spent some time looking for a validation module that was simple, practical and light. All I found were modules that promise simplicity, but deliver complex APIs; they promise lightness, but deliver very heavy dependencies; promise practicality, but deliver ready-made functions that do not meet our needs, and it is necessary to download new modules and configure messages to change language and validation behavior.

So I wrote valite, unlike all of them, it's just the core needed to build your validations. It is asynchronous by default, as everything should be in JavaScript, and has an extremely simple and concise API.

Install

valite is published under NPM registry, so you can install from any package manager.

npm install valite --save
 
# Use this command for Yarn. 
yarn add valite

API

The API is composed by a validation function, validate, a validation object function validateObject and isValid which is a simple error checker.

Validator

Validators are functions that receives a value and returns a message or true.

const isName = (name) => Boolean(name.trim()) || 'Name shouldn\'t be empty.';

For TypeScript, valite exports Validator type to improve your code safety.

import { Validator } from 'valite';
 
const isName: Validator = (name: string) => Boolean(name.trim()) || 'Name shouldn\'t be empty.';

validate

Executes validators and returns first obtained message or null.

const mail = 'hi@capiwara.com.br';
 
validate(mail, [
  (mail) => Boolean(mail.trim()) || 'Mail is required.',
  (mail) => /^.+@.+\..+$/.test(mail) || 'Mail is invalid',
 
  // You can use async validators.
  (mail) => (
    services.isMailRegistered(mail)
      .then((isRegistered) => isRegistered || 'Mail is already registered.')
      .catch(() => 'Can\'t even verify if mail is already registered.')
  )
]);
//=> Promise { 'E-Mail is already registered.' };

validateObject

Concurrently validates an object using validators from a schema and returns them in same structure.

Structure supports dot notation for deep properties.

const entries = {
  answer: document.querySelector('.answer').checked,
  user: {
    mail: document.querySelector('.mail').value,
    password: document.querySelector('.password').value,
  }
};
 
validateObject(entries, {
  'answer': [
    (answer) => Boolean(answer) || 'Terms should be accepted.',
  ],
  'user.mail': [
    (mail) => Boolean(value.trim()) || 'E-Mail is required.',
  ],
  'user.password': [
    (password) => Boolean(password.trim()) || 'Password is required.',
  ]
});
//=> Promise {{
//     'answer': null,
//     'user.mail': 'E-Mail is required',
//     'user.password': null
//   }}

isValid

Is a easy way to check if validate / validateObject payload has no errors.

const payload = await validateObject(/* ... */);
 
isValid(payload);
//=> true

License

Released under MIT License.

Package Sidebar

Install

npm i valite

Weekly Downloads

37

Version

0.7.0

License

MIT

Unpacked Size

29.4 kB

Total Files

11

Last publish

Collaborators

  • vitorluizc