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:
const form_schema = type({
name: 'string.alpha>2',
email: ['string.email','@','Please enter your work email address']
'age': 'number>17'
});
const form_schema = type({
name: 'string.alpha>2',
email: ['string.email','@','Please enter your work email address']
'age': 'number>17'
});
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
ssalbdivad
ssalbdivad4mo ago
The concept of description is the condition that fails
ssalbdivad
ssalbdivad4mo ago
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:
const email = type("string.email")

const form_schema = type({
name: "string.alpha>2",
email: [
"string",
":",
(s, ctx) =>
email.allows(s) ? true : (
ctx.reject({
problem: "Please enter your work email address"
})
)
],
age: "number>17"
})
const out = form_schema({
name: "foo",
email: "david",
age: 20
})

if (out instanceof type.errors) {
console.log(out.byPath) //?
}
const email = type("string.email")

const form_schema = type({
name: "string.alpha>2",
email: [
"string",
":",
(s, ctx) =>
email.allows(s) ? true : (
ctx.reject({
problem: "Please enter your work email address"
})
)
],
age: "number>17"
})
const out = form_schema({
name: "foo",
email: "david",
age: 20
})

if (out instanceof type.errors) {
console.log(out.byPath) //?
}
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.
ssalbdivad
ssalbdivad4mo ago
The difference between problem and message is that message also includes the path the error occurred at
Stuart B
Stuart BOP4mo ago
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.
ssalbdivad
ssalbdivad4mo ago
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
Stuart B
Stuart BOP4mo ago
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!
ssalbdivad
ssalbdivad4mo ago
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
Stuart B
Stuart BOP4mo ago
Understood, thanks.
Want results from more Discord servers?
Add your server