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.

Specify path for object refinement

See original GitHub issue

refine is the recommended way for validating related properties on an object https://github.com/vriad/zod/issues/61 Consider this schema

const validationSchema = z
  .object({
    firstName: z.string().optional(),
    lastName: z.string().optional(),
    email: z.string().email(),
    password: z.string(),
    confirmPassword: z.string(),
  })
  .refine((data) => data.password === data.confirmPassword, 'Both password and confirmation must match')

with this input data

{
  firstName: 'zod',
  lastName: '',
  email: 'theba@zod.c',
  password: 'thetetathea',
  confirmPassword: 'thethtbet',
}

we get this error

[
  {
    message: 'Both password and confirmation must match',
    path: [],
  },
]

The path is empty. Ideally, the path should contain “confirmPassword” so that the error can be easily displayed on the UI

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
colinhackscommented, Jun 15, 2020

This is the desired default behavior. The custom check is associated with the entire object schema. There’s no way for Zod to know that the error should be associated with confirmPassword in the code you provided.

Though there should be a way to support your use case. My proposal: the second argument to .refine can also accept an object, including an optional path key where you can specify the error path you’d like to be associated with the error.

Something like this:

const validationSchema = z
  .object({
    firstName: z.string().optional(),
    lastName: z.string().optional(),
    email: z.string().email(),
    password: z.string(),
    confirmPassword: z.string(),
  })
  .refine((data) => data.password === data.confirmPassword, {
    path: ['confirmPassword'],
    message: 'Both password and confirmation must match'
  })

Which results in:

validationSchema.parse({
  firstName: 'zod',
  lastName: '',
  email: 'theba@zod.c',
  password: 'thetetathea',
  confirmPassword: 'thethtbet',
});

/*
[
  {
    message: 'Both password and confirmation must match',
    path: ['confirmPassword'],
  },
]
*/

Would that work?

1reaction
colinhackscommented, Jul 9, 2020

@rossholdway @etienne-dldc Tagging for interest.

Read more comments on GitHub >

github_iconTop Results From Across the Web

3D Object Pose Refinement — ISAAC 2020.1 documentation
A rendering of the CAD model in the scene, which requires the path to the object CAD model and file names. These correspond...
Read more >
Learning to Refine Object Segments - Ronan Collobert
Abstract. Object segmentation requires both object-level information and low-level pixel data. This presents a challenge for feedforward net-.
Read more >
LookML refinements | Looker - Google Cloud
This refinement can go in any LookML file in the project, such as a ... Values from the extends specified in refinements of...
Read more >
towards general object understanding through recursive ...
In our model, a refinement module recursively develops understanding across space and semantics: our model gradually classifies the region into finer categories ...
Read more >
RefineNet — Multi-path Refinement Network (Semantic ...
It includes 20 object categories and one background class. It is split into a training set, a validation set and a test set,...
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