Schemas
Array

Array

Validate arrays of items against a schema, with length constraints.

Parameters

  • schema: CalidSchema
    • The schema that will be used to validate all array values.
  • options?: table
    • required_error?: string
      • Custom required message
    • type_error?: string
      • Custom invalid type message
local items = calid:array(calid:string(), {
    required_error = "Items is required",
    type_error = "Items must be a array"
})

Options

  • :default(value) – Specifies a fallback value when the input is nil. Useful for optional fields with defaults.
  • :optional() – Allows nil as a valid value without causing a validation error.
  • :length(n) – Requires the array to have exactly n elements.
  • :min(n) – Sets minimum element counts.
  • :max(n) – Sets maximum element counts.

Examples

local itemsSchema = calid:array(calid:string():min(5))
 
local validItems = { "sandwich", "water", "backpack" }
 
itemsSchema:parse(validRole)
-- {
--     success = true,
--     data = { "sandwich", "water", "backpack" }
-- }
 
local invalidItems = { "apple", "kiwi", "banana"  }
 
itemsSchema:parse(invalidItems)
--{
--    success = false,
--    error = {
--        code = "min",
--        message = "Must be at least 5 characters",
--        path = "[2]"
--    }
--}