Schemas
Object

Object

Define objects with nested schemas. By default, unknown keys are stripped; use :passthrough() to retain extra keys.

Parameters

  • object: table
    • Contains the schemas that will be validated.
  • options?: table
    • required_error?: string
      • Custom required message
    • type_error?: string
      • Custom invalid type message
local user = calid:object({
    id = calid:number(),
    name = calid:string(),
    super = calid:boolean():default(false),
}, {
    required_error = "User is required",
    type_error = "User must be a object"
})

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.
  • :passthrough() – Keeps extra object properties beyond those defined in the schema.

Examples

local userSchema = calid:object({
    id = calid:number():int():min(1),
    name = calid:string():min(5):max(30),
    email = calid:string():email(),
    super = calid:boolean():default(false)
})
 
local validUser = {
    id = 1001,
    name = "John Doe",
    email = "johndoe@email.com",
}
 
userSchema:parse(validUser)
--{
--    success = true,
--    data = {
--        id = 1001,
--        name = "John Doe",
--        email = "johndoe@email.com",
--        super = false
--    }
--}
 
local invalidUser = {
    id = 1002,
    name = "Jane",
    email = "jane@email.com",
    super = false
}
 
userSchema:parse(invalidUser)
--{
--    success = false,
--    error = {
--        code = "min",
--        message = "Must be at least 5 characters"
--        path = "name"
--    }
--}