aladov3.13.0

Docs/Validation

Array Items

TypeScript
const isTagList = validate
  .array()
  .items(validate.string().min(1).max(30).required().build())
  .minItems(1)
  .maxItems(10)
  .unique()
  .required()
  .build();

const isOrderItems = validate
  .array()
  .items(
    validate
      .object()
      .shape({
        productId: validate.string().objectId().required().build(),
        quantity: validate.number().integer().positive().required().build(),
        price: validate.number().positive().required().build(),
      })
      .required()
      .build(),
  )
  .minItems(1)
  .required()
  .build();

Items are checked one after the other, and the check stops at the first item that fails. An item validator built with build() costs no promise per item unless one of its rules is async, so a large array is as cheap as with buildSync(). Rules run in the order they are chained: put maxItems() before items() to refuse an oversized array before any item is looked at.