Please use paths instead of array indices for nested schema objects
See original GitHub issueconst test = zod
.object({
name: zod
.object({
firstName: zod.string().nonempty({ message: "First name can not be empty." }),
middleName: zod.string(),
lastName: zod.string().nonempty(),
})
.refine((name) => name.firstName === name.middleName, {
message: "First and Middle Name must be the same", // refinement test.
}),
})
.safeParse({
name: {
firstName: "",
middleName: "estrada",
lastName: "",
},
}).error.flatten();
console.log:
fieldErrors: {
name: (3) ["First Name can not be empty.", "Should be at least 1 characters", "First and Middle Name must be the same"]
}
I have a component for each field and the lack of path prevents me from passing the error message to their respective components (ex: <FirstNameInput error={ {error.fieldErrors?.name?} }) />). We can always use object.keys to achieve the current behavior as well.
Another issue is the error message becomes very unclear since they don’t reference any paths in their message (ex: Should be at least 1 characters). The workaround here is to manually type the name inside the custom error message but that just isn’t elegant especially if we’ll be doing that per field.
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (5 by maintainers)
Top Results From Across the Web
Change `formErrors` to produce normalized, deep object ...
const test = zod .object({ name: zod .object({ firstName: ... Please use paths instead of array indices for nested schema objects #223.
Read more >Javascript: how to dynamically create nested objects using ...
Set your path into an array. first, you have to reverse the array, to start filling the object. let obj = ['a', ...
Read more >Nested arrays how to do query and update? - MongoDB
Hi Team, I have a collection with 5000k documents with multiple nested arrays. I want to update the prodId key from old to...
Read more >ElasticSearch - nested mappings and filters - Joel Abrahamsson
Luckily ElasticSearch provides a way for us to be able to filter on multiple fields within the same objects in arrays; mapping such...
Read more >How To Use Indexes in MongoDB - DigitalOcean
This field stores values as an array to allow for mountains located in more than one country. ascents : this field's value is...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@flybayer I don’t think your proposal is what the OP is asking for. @cybervaldez wants a full
path
array associated with each issue (correct me if I’m wrong). If you need that granularity of information, then you should use the rawZodError.issues
array.The idea behind
flatten
is to easily collapse everything down to an object of depth one. I see now that this isn’t particularly useful. Your proposal is essentially the opposite of aflattening
😛 It takes the.issues
array and converts it into a nested keyed object. Seems like a really useful utility, but it should be a separate method. Maybetreeify
ordenormalize
— any thoughts?BTW you’re using the
.formErrors
getter, which just delegates to.flatten()
. I removed.formErrors
from the documentation because.flatten()
is more explicit.I was referring to the index and @flybayer 's solution is exactly what I’m looking for since it tells us what fields is causing the error. Without that the error becomes very unclear forcing us to set a custom message for each. Just look at the error produced by the middleName which doesn’t have any custom message: “Should be at least 1 characters”"