Cevali

Enabling you to create and extend your validations.

  • Small bundle
  • Extensible
  • TypeScript ready
  • Framework Agnostic

Cevali is a small library that allows you to create validations for any types of data, from simple strings to class instances, or remix existing ones to create your own. It uses two APIs: create and extend.

⚠️

Not yet ready for production use.

Installation

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

npm install --save cevali
 
# or
 
yarn add cevali

Overview

import { create, extend, TYPE } from "cevali";
 
// Create a schema type
const string = create('string', TYPE.string);
 
// Create your own validators
const email = string.create((value: string, opts: { message?: string } = {}) => {
  if (!value.includes("@"))
    throw new Error(opts.message ?? "Invalid email");
});
const blocklisted = string.create((value: string, blocklisted: string[]) => {
  if (blocklisted.includes(value))
    throw new Error("Blocklisted word");
});
 
// Extend existing validators
const extendedEmail = extend(email, (value: string, domainName: string, opts: { message?: string } = {}) => {
  if (!value.endsWith(`@${domainName}`))
    throw new Error(opts.message ?? "Invalid email");
});
 
// Create a pipe
const validate = string([
  extendedEmail(),
  blocklisted(["[email protected]", "[email protected]"]),
]);
 
// Validate
try {
  validate("hello");
} catch (error) {
  console.log(error.message); // Invalid email
}

Using create, you can create your own schema types, such as a string or number. It accepts an "id", a "type", and an optional converter function. The id is used to identify the schema type, and the type is used to validate the value.

The schema type has a create method that allows you to create your own validators. It accepts a validator function. It should throw an error if the value is invalid.

Using extend, you can extend existing validators. It accepts the validator you wish to extend and a validator function.

Using the created schema type, in this case string, you can the validator by passing it an array of validators.

With that, you can validate the value.

Custom Types

You can even create your own custom types:

import { create } from "cevali";
 
class User {
  name: string;
  access: "admin" | "user" | "guest";
 
  constructor(name: string, access: "admin" | "user" | "guest" = "guest") {
    this.name = name;
    this.access = access;
  }
}
 
// A converted function is to be used to convert the value to the desired type.
const userSchema = create("user", new User(''), (value) => {
  if (typeof value === "string")
    return new User(value);
 
  if (!(value instanceof User))
    throw new Error("Invalid user");
 
  return value;
});
 
// Validators for the user schema type
const isAdmin = userSchema.create((value: User) => {
  if (value.access !== "admin")
    throw new Error("Not admin");
});
 
const validate = userSchema([isAdmin()]);
 
try {
  validate(new User("john"));
} catch (ex) {
  console.log(ex.message); // Not admin
}

Cevali is licensed under the MIT (opens in a new tab). Created as a personal project of Joeylene (opens in a new tab).