aladov3.13.0

Docs/Validation

Async & Sync Modes

Any rule can return Promise<boolean>. Use .custom() for async rules.

TypeScript
const isAvailableEmail = validate
  .string()
  .email()
  .required()
  .custom(async (v) => {
    const taken = await db.users.exists({ email: v });
    return !taken;
  })
  .build();

const ok = await isAvailableEmail('new@example.com');

Rules run sequentially — cheap sync checks short-circuit before any async I/O fires. Inside .shape() and .items(), field validators run in parallel via Promise.all.


Sync mode

If you have no async rules, use .buildSync() to get a plain boolean without await.

TypeScript
const isValidName = validate.string().min(2).max(50).buildSync();

isValidName('Pablo'); // true — no await needed

.buildSync() throws at runtime if any rule in the chain is async:

Text
Error: Validator contains async rules — use .build() instead of .buildSync()

Use with Alado @validateProperty

TypeScript
import { documentProperty, validateProperty, validate } from 'alado';

export class CredentialsDto {
  @validateProperty({
    required: true,
    handler: validate.string().email().required().build(),
    error: { statusCode: 400, message: 'Invalid email' },
  })
  @documentProperty({
    schema: { type: 'string', format: 'email' },
    example: 'user@example.com',
  })
  email: string = '';

  @validateProperty({
    required: true,
    handler: validate.string().min(8).required().build(),
    error: { statusCode: 400, message: 'Password must be at least 8 characters' },
  })
  @documentProperty({
    schema: { type: 'string', minLength: 8 },
    example: 'securePassword',
  })
  password: string = '';
}