aladov3.13.0

Docs/Validation

Objects & Shapes

TypeScript
const isAddress = validate
  .object()
  .shape({
    street: validate.string().min(2).required().build(),
    city: validate.string().min(2).required().build(),
    country: validate.string().length(2).required().build(),
    zip: validate
      .string()
      .regex(/^\d{4,10}$/)
      .build(),
  })
  .required()
  .build();

A shape refuses keys it does not name. { "city": "Kyiv", "isVerified": true } fails a shape without isVerified, at any depth. Top-level request fields are stripped when unknown, but a validator cannot remove a key from a nested object, so an extra key would otherwise reach the handler and whatever stores the object. Accepting extra keys is an explicit choice: shape(schema, { allowUnknownKeys: true }) checks the named keys only. The keys the XML parser adds to elements (@ns, @attrs) never count as unknown.

Shapes can be nested to any depth:

TypeScript
const isUser = validate
  .object()
  .shape({
    id: validate.string().objectId().required().build(),
    email: validate.string().email().required().build(),
    role: validate.string().enum('admin', 'user').required().build(),
    address: validate
      .object()
      .shape({
        city: validate.string().required().build(),
        country: validate.string().length(2).required().build(),
      })
      .build(),
  })
  .required()
  .build();

Object key constraints and mutual exclusion

TypeScript
// at least one contact method required
const isContact = validate.object().or('email', 'phone').build();

// exactly one payment method
const isPayment = validate.object().xor('cardId', 'bankAccount').build();

// bounded key count
const isMeta = validate.object().minKeys(1).maxKeys(10).build();