A
arktype4mo 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
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
ArkType Playground
TypeScript's 1:1 validator, optimized from editor to runtime
3 Replies
Robb
RobbOP2mo ago
Hey @ssalbdivad ! Hope everything's well Is my issue related to this question ? I tried the workaround you mentionned in here and tried to tweak it, but couldn't manage to get the custom error
GitHub
Unable to directly configure top-level intersection error message ...
import { type } from &quot;arktype&quot;; const Thing = type(&quot;string.email &gt; 0&quot;).configure({ message: &quot;Email is not valid&quot;, }); // standard bulleted message- config seemingly...
ssalbdivad
ssalbdivad2mo ago
Hey thanks for the patience! This is another nuance of how configuration applies to a type with an input and output. You can work around it by passing the config to both endpoints individually if you want it to apply to both:
import { type } from "arktype"

const output = type("0<number<=65535").configure({
message: ctx => {
return "This is a custom error"
}
})

const Thing = type("string.numeric.parse | number")
// currently configures input only
.configure({
message: ctx => {
return "This is a custom error"
}
})
.to(output)

const out2 = Thing("abcd") // Shows custom error
const out = Thing("999999") // Shows custom error
import { type } from "arktype"

const output = type("0<number<=65535").configure({
message: ctx => {
return "This is a custom error"
}
})

const Thing = type("string.numeric.parse | number")
// currently configures input only
.configure({
message: ctx => {
return "This is a custom error"
}
})
.to(output)

const out2 = Thing("abcd") // Shows custom error
const out = Thing("999999") // Shows custom error
Playground I'd actually be interested in revisiting the default behavior here as I think it may be reasonable for common cases like this one to propagate the config across all intermediate transforms in the Type. If you could create a GitHub issue summarizing this case and possibly mention it on the existing issue for intersections, that would be greatly appreciated!
Robb
RobbOP2mo ago
Done in here
GitHub
Custom error messages don't work on morph · Issue #1479 · arktype...
Report a bug 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. 🔎 Search Terms morph, error, configur...

Did you find this page helpful?