aladov3.13.0

Docs/Validation

Validator Basics

Alado ships its own chainable validator, with sync and async support: nothing else to install.

Basic usage

TypeScript
import { validate } from 'alado';

const isEmail = validate.string().email().required().build();
const isUsername = validate
  .string()
  .min(3)
  .max(20)
  .regex(/^[a-z0-9_]+$/)
  .build();
const isRole = validate.string().enum('admin', 'user', 'guest').required().build();
const isAge = validate.number().integer().min(18).max(120).required().build();
const isId = validate.string().objectId().required().build();

Each chain ends with .build() which returns (input: any) => Promise<boolean> — build once, reuse anywhere. For sync-only rules use .buildSync() to get a plain boolean.


Entry points

TypeScript
validate.string(); // starts a string validator
validate.number(); // starts a number validator
validate.bigint(); // starts a bigint validator
validate.boolean(); // starts a boolean validator
validate.array(); // starts an array validator
validate.object(); // starts an object validator
validate.custom(fn); // starts with a custom rule

Modifiers

Method Description
.required() Input must be present and not "" (and not null, unless .nullable())
.optional() Input may be absent (undefined): then all rules are skipped (default)
.nullable() null is accepted and skips the other rules; without it null fails

Only a missing value counts as "not provided". A null or "" that is sent goes through the rules like any other value: an optional validate.number().min(18) refuses null, an optional validate.string().email() refuses "", while a plain optional validate.string() accepts "". This also applies to items() and shape(): a null array item or property needs a nullable() validator.

TypeScript
const nickname = validate.string().min(3).build(); // missing: ok; null, "" or "ab": refused
const middleName = validate.string().min(1).nullable().build(); // missing or null: ok; "": refused