arktypea
arktype7mo ago
Robb

Custom errors with morphs

Hello,
I am trying to have custom error messages on morphs. Here is an example for a Port type.

The error message is still the original one from Arktype.

import { type } from "arktype"

const Thing = type("string.numeric.parse | number")
    .to("0<number<=65535")
    .configure({
        message: (ctx) => {
            return "This is a custom error"
        }
    })

// const out = Thing("abcd") // Shows custom error
const out = Thing("999999") // Shows default error


Here is the playground link:
https://arktype.io/playground?code=import%2520%257B%2520type%2520%257D%2520from%2520%2522arktype%2522%250A%250Aconst%2520Thing%2520%253D%2520type%28%2522string.numeric.parse%2520%257C%2520number%2522%29%250A%2509.to%28%25220%253Cnumber%253C%253D65535%2522%29%250A%2509.configure%28%257B%250A%2509%2509message%253A%2520%28ctx%29%2520%253D%253E%2520%257B%250A%2509%2509%2509return%2520%2522This%2520is%2520a%2520custom%2520error%2522%250A%2509%2509%257D%250A%2509%257D%29%250A%250A%252F%252F%2520const%2520out%2520%253D%2520Thing%28%2522abcd%2522%29%2520%252F%252F%2520Shows%2520custom%2520error%250Aconst%2520out%2520%253D%2520Thing%28%2522999999%2522%29%2520%252F%252F%2520Shows%2520default%2520error%250A%250A
ArkType
TypeScript's 1:1 validator, optimized from editor to runtime
Was this page helpful?