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.

Parse only throws error for single field, if outer refine is present

See original GitHub issue

Consider Following Code:

const User = z
  .object({
    email: z
      .string()
      .min(1, { message: 'Email is required.' })
      .email({ message: 'Email is invalid.' }),
    password: z.string().min(8, { message: 'Password must have minimum 8 characters.' }),
    confirmPassword: z.string().min(1, { message: 'Confirm password required.' }),
  })
  .refine((data) => data.password === data.confirmPassword, {
    message: "Passwords don't match.",
    path: ['confirmPassword'], // path of error
  });

const parseResult = User2.parse({
  email: 'sds@',
  password: 'Ak111@202902',
  confirmPassword: 'Ak111@kj32l',
});

console.log({ parseResult });

Above code throws following error:

[
  {
    "validation": "email",
    "code": "invalid_string",
    "message": "Email is invalid.",
    "path": [
      "email"
    ]
  }
]

Expected Output: It should also return error for Passwords don't match. Because of this issue all errors are not shown at once in react-hook-form.

Versions Zod: 3.1.0

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
colinhackscommented, Oct 19, 2021

Yes, actually. Zod 3.10 just landed and now surfaces more errors simultaneously. Basically refinement issues (and a few other issue types) are considered “continuable”, in that parsing continues even after they occur. Previously Zod would “short circuit” as soon as any issue occurred.

The OPs example now returns two errors:

 ZodError: [
    {
      "validation": "email",
      "code": "invalid_string",
      "message": "Email is invalid.",
      "path": [
        "email"
      ]
    },
    {
      "code": "custom",
      "message": "Passwords don't match.",
      "path": [
        "confirmPassword"
      ]
    }
  ]

Keep in mind, this approach has limits. If base object type is missing a required field, for instance, that issue is considered “fatal”/non-continuable, so the outer refine won’t execute. (Though setting default values on the keys would solve this.)

0reactions
aksdevaccommented, Oct 20, 2021

Hello, it seems issue is now fixed (even with acceptTerms boolean & refine) in 3.10.3, earlier I was checking with 3.9.8, Thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Validation rule Error message not displaying in LWC ...
I have a record which I'm updating via the UpdateRecord method from LWC, using Toast events to display success or error messages, which...
Read more >
Check whether a string is parsable into Long without try- ...
Exception should be thrown and handled properly. If you look inside parseLong code, you'll see that there are many different verifications and ...
Read more >
Control flow and error handling - JavaScript - MDN Web Docs
The try...catch statement marks a block of statements to try, and specifies one or more responses should an exception be thrown. If an...
Read more >
Database Engine events and errors - SQL Server
For a full list of all errors, query the sys.messages catalog view with ... to target a sparse column set instead of single...
Read more >
Failed to parse field of type in document with id ''.
In a nutshell, this error just means that you provided a value which could not be parsed by Elasticsearch for the field's type...
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