Any way to tell if a custom description has been provided?
Is there any way to tell if a custom description has been created for a type?
For example, let's say I have this, which I'm using to validate some user input:
If the name or age are incorrect I want to display the normal composite messages (the .message property)
If email is incorrect, I want to display only the description I've entered (the .description property), not including the "must be" and "was" parts.
The problem is that .description is populated even if I havent provided a custom description, so I can't make the choice of which to display dynamically.
Is there any property/method in ArkErrors that will tell me?
8 Replies
The concept of description is the condition that fails
So if you want to customize an entire message like this, what you'd really want is to configure
problem
instead. That's an open issue:
https://github.com/arktypeio/arktype/issues/977
That said, an alternative would be to use .narrow
:
GitHub
Allow other error config like
expected
, actual
, problem
and `...This would allow more granular customization e.g. of a field like password to omit actual.
The difference between problem and message is that message also includes the path the error occurred at
Thanks David.
I think as a temporary workaround I can prefix any such custom descriptions (e.g. With #CUST) and check for its presence in description. If #CUST is there then I remove the prefix and display the description. If not, I display the message.
Are you sure that's better than what I suggested? You could even create a wrapper like
customDescription
or whatever for the narrowing logic
It's kind of misusing description
My challenge is that I'm dealing with dynamic forms and I need to be able to store the types in a database.
As soon as I start to need code changes for specific types l, rather than something that's easily serializable, it becomes a problem.
Hopefully it's only short term anyway!
Okay just keep in mind that description in general is supposed to mean just the expected condition, so if that type is ever used in e.g. a union, the composed message won't be correct
Understood, thanks.