Multiple Error Message using Arktype

plateDefinition: { rows: type('number>0').and(type('number<33')).configure({ message: 'Please enter an integer between 1 and 32.', }), How can I have separate messages for integer must be greater than 1 And a different error messgae for integer must be less 32?
1 Reply
ssalbdivad
ssalbdivad2mo ago
Hey! The best way to do this would be to define those custom messages as you're defining the constraints:
import { type } from "arktype"

const Thing = type({
plateDefinition: {
rows: type.number
.moreThan({ rule: 0, meta: { message: "too smol" } })
.lessThan({ rule: 33, meta: { message: "too lorge" } })
}
})

// ArkErrors: too smol
const out = Thing({
plateDefinition: { rows: 0 }
})
import { type } from "arktype"

const Thing = type({
plateDefinition: {
rows: type.number
.moreThan({ rule: 0, meta: { message: "too smol" } })
.lessThan({ rule: 33, meta: { message: "too lorge" } })
}
})

// ArkErrors: too smol
const out = Thing({
plateDefinition: { rows: 0 }
})
Playground
ArkType
ArkType Playground
TypeScript's 1:1 validator, optimized from editor to runtime

Did you find this page helpful?