valit

An optimized version of Joi. A very simple input validation by creating a schema and comparing it to the given data.

Usage no npm install needed!

<script type="module">
  import valit from 'https://cdn.skypack.dev/valit';
</script>

README

Valit

an optimized version of Joi, if all what you need is to check data types, min and max length, and a bunch of others then you don't need a 70KB library! Valit will be perfect for you.

install

npm i valit

import valit from valit
//or
const valit = require('valit')

Usage

1. First create your schema

const schema = {
    username: valit.createField(valit.string, {
        required: true,
        min: 5,
        max: 25,
        label: 'Username'
    }),
    email: valit.createField(valit.string, {
        required: true,
        email: true
    }),
    age: valit.createField(valit.number, {
        min: 10,
        max: 100,
    }),
    password1: valit.createField(valit.string, {
        required: true,
        match: 'password2',
    }),
    password2: valit.createField(valit.string, { required: true }),
};

the first parameter in valit.createField is the data type of that field. required

  • valit.string valit.number valit.boolean valit.object valit.function

the second parameter is an object of extra options

  • required min max label email match

Note: required and email default value is false

2. Get the data you want to validate

const data = {
    email: 'officialvalit@gmail.com',
    age: -17,
    password1: 1234,
    password2: 'myPassword'
};

3. Validate and get errors

const errors = valit.validate(data, schema)

valit.validate will return an object of all errors found, the key will be the name of the field and the value will be the first error found for that field.

So in this example errors will look like this

{
  username: 'Username is required',
  age: 'Age must be at least 10',
  password1: 'Password1 must be a string'
}

There is a third parameter for valit.validate: returnAllErrors type: boolean, default: false.

if set to true it will return all errors found for that field in an array.

so if we did

const errors = valit.validate(data, schema, true)

The errors will look like this:

{
  username: [ 'Username is required' ],
  age: [ 'Age must be at least 10' ],
  password1: [ 'Password1 must be a string', 'Password1 must match password2' ]
}

Resources

If you have any ideas to improve the app or this documentation please open an issue on the github page.