valite
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 = Booleanname || 'Name shouldn\'t be empty.';
For TypeScript, valite
exports Validator
type to improve your code safety.
; ;
validate
Executes validators and returns first obtained message or null
.
const mail = 'hi@capiwara.com.br'; ;//=> 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: documentchecked user: mail: documentvalue password: documentvalue ; ;//=> 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 ; ;//=> true
License
Released under MIT License.