customize errors

i am quite new to this library and knowing that arktype support regexp validation, i am surprised, but now my question is how do i even explain this regexp to the user? i know that i can do
import {type} from "arktype";
const regexp1 = type("[a-z]").configure({
problem: ctx => `${ctx.actual} does not match ${ctx.expected}`
});
import {type} from "arktype";
const regexp1 = type("[a-z]").configure({
problem: ctx => `${ctx.actual} does not match ${ctx.expected}`
});
but that doesnt explain much to the user that has no regexp experience, is there a way to return a more reasonable errors? like for example the regexp: "/^\\s*[a-zA-Z0-9_]{3,16}\\s*$/" when the user put less than 3 characters in the username field then return the username must be atleast 3 character thanks in advance
3 Replies
TizzySaurus
TizzySaurus4d ago
You can set the message instead of the problem And then just provide a better error message yourself
anispwyn
anispwynOP4d ago
thank you, but how about specific case like the user put too much characters i assume i can use .narrow for that?
TizzySaurus
TizzySaurus4d ago
Just use an if statement :Shrug: Per your example it's a function, so you can just return different things based on what the user entered

Did you find this page helpful?