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.

Ability to add label to be shown in error instead of generic like: String must contain at least 3 character(s)

See original GitHub issue

For example if I have this validation

const schema = z.object({
   firstName: z.string().min(3).max(12),
   isActive: z.boolean(),
   role: z.enum(['Admin', 'User']),
});

const result = schema.safeParse({
   "firstName": '',
   "isActive": null,
   "role": "min",
});

console.log(JSON.stringify(result, null, 2));
{
  "success": false,
  "error": {
    "issues": [
      {
        "code": "too_small",
        "minimum": 3,
        "type": "string",
        "inclusive": true,
        "message": "String must contain at least 3 character(s)",
        "path": [
          "firstName"
        ]
      },
      {
        "code": "invalid_type",
        "expected": "boolean",
        "received": "null",
        "path": [
          "isActive"
        ],
        "message": "Expected boolean, received null"
      },
      {
        "received": "min",
        "code": "invalid_enum_value",
        "options": [
          "Admin",
          "User"
        ],
        "path": [
          "role"
        ],
        "message": "Invalid enum value. Expected 'Admin' | 'User', received 'min'"
      }
    ],
    "name": "ZodError"
  }
}

Error message will be

firstName must contain at least 3 character(s) instead of String must contain at least 3 character(s)

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:11

github_iconTop GitHub Comments

7reactions
ITwrxcommented, Aug 13, 2022

i think by default it should use the field name, and also have a convenient override like label().

2reactions
ITwrxcommented, Oct 12, 2022

so it seems like you’re asking why users can’t just use path as alluded to in the ERROR_HANDLING docs. There is also this package as another option.

IIRC, (i haven’t used any of this in 2 months) I think some of us just didn’t see why manual customization/workaround or another package should be necessary to use path by default and preferably even a label() option. If it needs to stay hard-coded with a generic type label for other use cases, then that’s understandable.

If what i have listed above is the stance of devs, than maybe closing as wontfix, optionally with a brief explanation, would be preferable to the stalebot being unleashed, which i find to be a somewhat disagreeable way to close an issue. 😃

thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshoot Shipment Issues - ShipStation Help
The shipment detail shown in the error has not been set yet. ShipStation requires all four shipment details to create a label successfully....
Read more >
ios - Simplest way to throw an error/exception with a custom ...
The simplest approach is probably to define one custom enum with just one case that has a String attached to it: enum MyError:...
Read more >
Error Explanations for The W3C Markup Validation Service
An attribute name (and some attribute values) must start with one of a restricted set of characters. This error usually indicates that you...
Read more >
Troubleshooting Common Error Messages in Shippo
Error messages can be confusing, frustrating, and can happen for various reasons. Thats why we have put together this list of common...
Read more >
Improve Validation Errors with Adaptive Messages
In this article we'll go over findings from our usability studies on how the wording of validation error messages largely determines the user's ......
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