Z
Zod•11mo ago
addamsson

addamsson - Is there a way to fine-tune error m...

Is there a way to fine-tune error messages in Zod to include the path (or the last element of the path)? I'm looking at my errors and the only thing I'd change is to have the field name instead of String/Number :
[
{
code: 'too_small',
minimum: 1,
type: 'string',
inclusive: true,
exact: false,
message: 'String must contain at least 1 character(s)',
path: [ 'name' ]
},
{
code: 'too_small',
minimum: 0,
type: 'number',
inclusive: true,
exact: false,
message: 'Number must be greater than or equal to 0',
path: [ 'age' ]
},
{
validation: 'email',
code: 'invalid_string',
message: 'Invalid email',
path: [ 'email' ]
},
{
code: 'invalid_type',
expected: 'string',
received: 'undefined',
path: [ 'addresses', 0, 'country' ],
message: 'Required'
}
]
[
{
code: 'too_small',
minimum: 1,
type: 'string',
inclusive: true,
exact: false,
message: 'String must contain at least 1 character(s)',
path: [ 'name' ]
},
{
code: 'too_small',
minimum: 0,
type: 'number',
inclusive: true,
exact: false,
message: 'Number must be greater than or equal to 0',
path: [ 'age' ]
},
{
validation: 'email',
code: 'invalid_string',
message: 'Invalid email',
path: [ 'email' ]
},
{
code: 'invalid_type',
expected: 'string',
received: 'undefined',
path: [ 'addresses', 0, 'country' ],
message: 'Required'
}
]
vs
[
{
code: 'too_small',
minimum: 1,
type: 'string',
inclusive: true,
exact: false,
message: 'name must contain at least 1 character(s)',
path: [ 'name' ]
},
{
code: 'too_small',
minimum: 0,
type: 'number',
inclusive: true,
exact: false,
message: 'age must be greater than or equal to 0',
path: [ 'age' ]
},
{
validation: 'email',
code: 'invalid_string',
message: 'Invalid email',
path: [ 'email' ]
},
{
code: 'invalid_type',
expected: 'string',
received: 'undefined',
path: [ 'addresses', 0, 'country' ],
message: 'country is Required'
}
]
[
{
code: 'too_small',
minimum: 1,
type: 'string',
inclusive: true,
exact: false,
message: 'name must contain at least 1 character(s)',
path: [ 'name' ]
},
{
code: 'too_small',
minimum: 0,
type: 'number',
inclusive: true,
exact: false,
message: 'age must be greater than or equal to 0',
path: [ 'age' ]
},
{
validation: 'email',
code: 'invalid_string',
message: 'Invalid email',
path: [ 'email' ]
},
{
code: 'invalid_type',
expected: 'string',
received: 'undefined',
path: [ 'addresses', 0, 'country' ],
message: 'country is Required'
}
]
Solution:
ok, I just realized I can do this: ```ts const Address = z.object({ street: z .string({...
Jump to solution
2 Replies
Solution
addamsson
addamsson•11mo ago
ok, I just realized I can do this:
const Address = z.object({
street: z
.string({
invalid_type_error: "Street must be a string.",
required_error: "Street is required.",
})
.min(1, { message: "Street can't be empty." }),
city: z
.string({
invalid_type_error: "City must be a string.",
required_error: "City is required.",
})
.min(1, { message: "City can't be empty." }),
zip: z
.number({
invalid_type_error: "Zip must be a number.",
required_error: "Zip is required.",
})
.gt(0, { message: "Zip must be greater than zero." }),
country: z
.string({
invalid_type_error: "Country must be a string.",
required_error: "Country is required.",
})
.min(1, { message: "Country can't be empty." }),
});
const Address = z.object({
street: z
.string({
invalid_type_error: "Street must be a string.",
required_error: "Street is required.",
})
.min(1, { message: "Street can't be empty." }),
city: z
.string({
invalid_type_error: "City must be a string.",
required_error: "City is required.",
})
.min(1, { message: "City can't be empty." }),
zip: z
.number({
invalid_type_error: "Zip must be a number.",
required_error: "Zip is required.",
})
.gt(0, { message: "Zip must be greater than zero." }),
country: z
.string({
invalid_type_error: "Country must be a string.",
required_error: "Country is required.",
})
.min(1, { message: "Country can't be empty." }),
});
Svish
Svish•11mo ago
There's no way to "automate" it as far as I know, no. In our project we've worked around it by writing all error messages in a way that the error message component (which does know the field label) can just put the error message after the label and have it make sense. For example must be greater than zero, can easily be appended to a label and make a string like "Age" must be greater than zero. Definitely not optimal, but works OK for now 🙂
Want results from more Discord servers?
Add your server