Customizing Error Messages

I've searched Github & Discord a bit and seen a few mentions of "full error customization", and am finding that I need it, but can't find any examples of it. I have a type:
const form = type({
user: {
email: 'string.email',
age: 'number>13',
}
})
const form = type({
user: {
email: 'string.email',
age: 'number>13',
}
})
When these fields are incorrect, it yields the following error messages:
{
"user.email": "user.email must be an email address (was \"test\")",
"user.age": "user.age must be more than 13 (was 12)"
}
{
"user.email": "user.email must be an email address (was \"test\")",
"user.age": "user.age must be more than 13 (was 12)"
}
I want to show these outputs in-line in a form. To do that, I need some more customization like: - Showing email instead of user.email (I can use css capitalize to capitalize it if necessary) - Getting rid of (was ___) Right now my best option is to manually manipulate the string to make these changes, which isn't ideal.
3 Replies
ssalbdivad
ssalbdivad3mo ago
Yes unfortunately this is one of the areas we're still behind on V2 documentation, but all of these things are customizable! Check out the tests here for narrowing and manually adding a custom error: https://github.com/arktypeio/arktype/blob/4e687f80904f8b58af4c969a9b2edda25b892f4d/ark/type/__tests__/narrow.test.ts#L14 Examples of adding custom metadata: https://github.com/arktypeio/arktype/blob/4e687f80904f8b58af4c969a9b2edda25b892f4d/ark/type/__tests__/config.test.ts#L6 Currently, you can't control how actual + the path are stringified through metadata for an individual type, but that is tracked here (would probably be ideal for a case like this): https://github.com/arktypeio/arktype/issues/977 At a global or scope level, you can change how errors are written based on the type of the error (e.g. by omitting was X by setting actual: null), e.g: https://github.com/arktypeio/arktype/blob/4e687f80904f8b58af4c969a9b2edda25b892f4d/ark/type/__tests__/traverse.test.ts#L46 Take a look in those places and see if you come up with something reasonable, if you're stuck I can try to help later tonight
Bobakanoosh
BobakanooshOP3mo ago
Thanks for the response - will try to take a look at this tomorrow! I tried looking at each of these examples, but could only get the “actual” part working. Even then, the code for it feels really gross and hard to reuse. I see you say actual can be applied at a global level, but I don’t see an example of that in the link.
ssalbdivad
ssalbdivad3mo ago
Sorry to hear that, I will try and publish the docs for this soon!
Want results from more Discord servers?
Add your server