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.

Allow Custom Errors on any value?

See original GitHub issue

Any reason not to allow defining the last argument for any value as an error message to allow overriding the error message for that property easily? The current method is not really ideal, especially when you have many string props but need different errors.

A couple nice ways to handle it:

Allow Last Param to be Error Message

Would also accept a custom error that it would throw instead of ZodError (similar to Joi)

const TestSchema = z.object({
	three: z.literal('hi').optional(),
	one: z.number(),
	two: z.literal(3, 'that isnt 3 dummy'),
	four: z.string('string not whatever you gave!'),
	five: z.date()
})

Add a .error() option like Joi

const TestSchema = z.object({
	three: z.literal('hi').optional(),
	one: z.number(),
	two: z.literal(3).error(new Error('that isnt 3 dummy')),
	four: z.string().optional().error(new Error('string not whatever you gave!')),
	five: z.date()
})

For the VSCode extension I am working on, these styles add the benefit that jsdoc can be used to transfer over error messaging easily:

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:32
  • Comments:32 (9 by maintainers)

github_iconTop GitHub Comments

36reactions
colinhackscommented, Jul 27, 2020

This is on the roadmap. Though it’ll likely be an object instead of a string to leave room for future API additions.

z.string({ message: "That's not a string!" });
13reactions
amankkgcommented, Mar 18, 2021

What about yup#setLocale API? It seems to be easier to handle global translations for both primitives and their methods

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use web.config customErrors in ASP.NET MVC?
The first step is to enable customErrors using its mode attribute that can have one of the following three values: On: Specifies that...
Read more >
Custom errors, extending Error - The Modern JavaScript Tutorial
JavaScript allows to use throw with any argument, so technically our custom error classes don't need to inherit from Error .
Read more >
How to Use Web.Config customErrors in ASP.Net: Log All Errors
To do this, you will want to set customErrors mode to “Off” as shown below. Be careful as this could expose sensitive information...
Read more >
Web.config customErrors element with ASP.NET explained
NET and MVC using the customErrors element in your web.config file. ... To enable an error page, set the value to On and...
Read more >
CustomErrors mode="Off" - asp.net - Stack Overflow
1. try add @Model.Exception.Message to the Shared/Error.cshtml · In general, watch out for config transforms (e.g. Web.Debug.config which could change that value) ...
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