question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Please use paths instead of array indices for nested schema objects

See original GitHub issue
const 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:closed
  • Created 3 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
colinhackscommented, Nov 27, 2020

@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 raw ZodError.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 a flattening 😛 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. Maybe treeify or denormalize — 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.

Screen Shot 2020-11-27 at 9 38 43 AM

0reactions
cybervaldezcommented, Nov 28, 2020

@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 raw ZodError.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 a flattening 😛 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. Maybe treeify or denormalize — 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.

Screen Shot 2020-11-27 at 9 38 43 AM

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”"

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found