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.

errorMessagesForSchema returns wrong data

See original GitHub issue
  const securityUpdateAction = useActionData<typeof action>() as any;
  if (securityUpdateAction) {
    const errorMessagesFormattedByDf = errorMessagesForSchema(
      securityUpdateAction.inputErrors,
      Schema
    );
  }

The securityUpdateAction returns inputErrors with this shape:

[
    {
        "path": [
            "newPassword"
        ],
        "message": "String must contain at least 8 character(s)"
    },
    {
        "path": [
            "newPasswordRepeat"
        ],
        "message": "String must contain at least 8 character(s)"
    },
    {
        "path": [
            "newPassword",
            "newPasswordRepeat"
        ],
        "message": "Passwords don't match"
    }
]

The Schema looks like this:

const Schema = z
  .object({
    oldPassword: z.string(),
    newPassword: z.string().min(8),
    newPasswordRepeat: z.string().min(8),
  })
  .refine((data) => data.newPassword === data.newPasswordRepeat, {
    message: "Passwords don't match",
    path: ["newPassword", "newPasswordRepeat"], // path of error
  })

The output after parsing looks like this in the browser console:

Bildschirmfoto 2022-12-12 um 20 01 38

In the docs it shows the shape what is to be expected: https://github.com/SeasonedSoftware/domain-functions#errormessagesforschema

errorForSchema(result.inputErrors, schema)
/*
{
  email: ['Must not be empty', 'Must be a string'],
  password: ['Must not be empty']
}
*/

Basically: Record<string,string[]>

Issue Analytics

  • State:closed
  • Created 9 months ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
phifacommented, Dec 13, 2022

i can confirm, this produces the wanted behaviour

export const UserSecuritySettingsUpdateData = z
  .object({
    oldPassword: z.string(),
    newPassword: z.string().min(8),
    newPasswordRepeat: z.string().min(8),
  })
  .refine((data) => data.newPassword === data.newPasswordRepeat, {
    message: "Passwords don't match",
    path: ["newPassword"], // path of error
  })
  .refine((data) => data.newPassword === data.newPasswordRepeat, {
    message: "Passwords don't match",
    path: ["newPasswordRepeat"], // path of error
  })
0reactions
phifacommented, Dec 13, 2022

@diogob interesting, i had used the same schema before with zod’s flatten() method, and it worked nicely. It added the refinement error for both fields, so in the UI I had the error displayed under both inputs. How can I achieve the same result with errorMessagesForSchema()? Do I have to use the refine() twice? changing the path for the second refinement?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can you get good error messages for schema validaiton, on ...
Hi there. My objective is to upgrade an existing mongo database to enforce schemas. Reading this blog, it seems like version 5 actually ......
Read more >
Warning and error messages for schema validation in ...
Warning and error messages for schema validation in Instance Data Replication (IDR) ... Some information is incorrect or missing.
Read more >
domain-functions/README.md at main - GitHub
Given a array of SchemaError be it from inputErrors or environmentErrors and a name, it returns a list of error messages with that...
Read more >
Release Notes (mandatory)
Fixed the issue where incorrect error message was returned for Appointment ... with unexpected error message and no error messages for schema validation...
Read more >
SAP ABAP Message Class 5P (Error Messages for Schema ...
SAP ABAP Message Class 5P (Error Messages for Schema, Pers.Calc.Rule and Feature Checks) - SAP ... PBAS (Package) SAP HR Master Data Application...
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