Docs
Getting Started

Getting Started

Installation

Cevali is available as an npm package (opens in a new tab).

npm install --save cevali
 
# or
yarn add cevali

Quick Start

To get started, first you need to create a schema type:

import { create, TYPE } from "cevali";
 
const string = create('string', TYPE.string);

Then you can create your own validators such as validators for email, minLength, maxLength, and etc.

ℹ️

We plan on adding more validators examples in the docs soon.

const email = string.create((value: string, opts: { message?: string } = {}) => {
  if (!value.includes("@"))
    throw new Error(opts.message ?? "Invalid email");
});
 
const minLength = string.create((value: string, minLength: number, opts: { message?: string } = {}) => {
  if (value.length < minLength)
    throw new Error(opts.message ?? `Must be at least ${minLength} characters`);
});
 
const maxLength = string.create((value: string, maxLength: number, opts: { message?: string } = {}) => {
  if (value.length > maxLength)
    throw new Error(opts.message ?? `Must be at most ${maxLength} characters`);
});

The first argument of string.create is the value in which you want to validate. The succeeding arguments are the optional options that you want to pass to the validator.

After that, you can combine these validators into a single validator using by:

const nameValidator = string([
  minLength(3),
  maxLength(20),
]);

Then you can validate the value:

try {
  nameValidator("hello");
} catch (error) {
  console.log(error.message); // Must be at least 3 characters
}

Code Sandbox Example