A
arktype4mo ago
GreggOD

"string.email>0" custom message not working

Hey for most types i use .configure({message: "Some error message"}) to customize error messages for fields. When i use it on email type it seems configure does not work the same:
const stringEmail = type("string.email>0")
.configure({message: "You need to fill in your email"})

stringEmail.from("")

// error: email ("") must be... ◦ an email address ◦ non-empty
const stringEmail = type("string.email>0")
.configure({message: "You need to fill in your email"})

stringEmail.from("")

// error: email ("") must be... ◦ an email address ◦ non-empty
What should I do to get a custom message in this case?
3 Replies
Claus
Claus4mo ago
configure only works with scopes:
configure({
required: {
message: (ctx) => `The field ${ctx.path} is required`,
},
});

const $ = type.scope({});

const schema = $.type({
name: "string",
});
configure({
required: {
message: (ctx) => `The field ${ctx.path} is required`,
},
});

const $ = type.scope({});

const schema = $.type({
name: "string",
});
GreggOD
GreggODOP3mo ago
Thanks for the response. that code seems a bit complex just to overwrite the validation message no?
ssalbdivad
ssalbdivad3mo ago
@Claus @GreggOD configure does work outside scopes, but there is an issue specifically for customizing errors like this one that include multiple parts. Here's the relevant GitHub issue: https://github.com/arktypeio/arktype/issues/1412 The workaround would be to use a global or scope config to avoid this behavior for now as mentioned in the GitHub issue. We'll work on fixing this ASAP so the workaround isn't required!
GitHub
Unable to directly configure top-level intersection error message ...
import { type } from "arktype"; const Thing = type("string.email > 0").configure({ message: "Email is not valid", }); // standard bulleted message- config seemingly...

Did you find this page helpful?