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.

[Feature Request]: zod2 access to refinementData

See original GitHub issue

in zod2 there’s no way anymore to access the refinementData.

This can be very convenient when we want to reuse the info to display hints in the GUI (e.g. The password must have between 5 and 10 characters, …)

We could simply add the refinement data as a property to the check function: e.g. in base.ts:

refinement = (
    check: (arg: Output) => any,
    refinementData: MakeErrorData | ((arg: Output) => MakeErrorData),
  ) => {
    const augmentedCheck = (val, ctx) => {
      if (!check(val)) {
        ctx.addError(
          typeof refinementData === 'function'
            ? refinementData(val)
            : refinementData,
        );
      }
    };
    augmentedCheck.refinementData = refinementData;
    return this._refinement(augmentedCheck);
  };

Then we can simply loop over all ZodTypeDef.checks and check the refinementData property is set

Issue Analytics

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

github_iconTop GitHub Comments

3reactions
tmtroncommented, Oct 16, 2020

As pointed out here in https://github.com/vriad/zod/issues/138#issuecomment-685433879 this approach causes a lot of code-duplication and a maintenance nightmare.

Code-duplication

Even in a mid-sized business app, that has only 20 entities with 10 properties each, that would mean to have 20 redundant meta-objects with, a total of 200 redundant meta-fields plus 200 input definitions in HTML and many more constants.
There is no need for this redundant code. It does not add any additional value. All the information is already defined on the zod object, so we can simply loop over the zod-fields and generate the meta-data and HTML form.
Even a default helpText can easily be generated and tweaked per use case: e.g. different messages when the field has min/max or both.

Maintenance

In many apps you will have a frontend (e.g. react or angular), some shared typescript code and a node-backend. The zod definition will be in shared code and can be used by the frontend and the backend.
Now if someone of the backend development team adds a new validation (e.g. the field just had min and now they add max), it is possible (even likely) that they forget to update the metadata or form.

2reactions
tmtroncommented, Sep 28, 2020

I don’t think custom error maps are useful in this case. I am happy with the individual error-messages. What I want is to get a meaningful description of what the user should input, This should be displayed before they even enter a value.

Example:

  • temp = z.number().int().min(-10).max(30)
  • When the form is shown, I want to display: “Enter an integer between -10 and 20”

So you suggest that I first check which type it is (string, number, etc.) and per type pass all possibly invalid values to safeParse() before I show the form?

e.g. when a field is an integer, execute ALL of these:

  • temp.safeParse(Number.MIN_VALUE) to check for a minimum
  • temp.safeParse(Number.MAX_VALUE) to check for a the maximum
  • temp.safeParse(3.14) to check for integer

Then what should we pass for strings to check the max. length??

Sounds like a very bad idea to me - compared to just looping over the refinements directly…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Developers - [Feature Request]: zod2 access to refinementData -
in zod2 there's no way anymore to access the refinementData. This can be very convenient when we want to reuse the info to...
Read more >
Refining data requests - IBM
Go to Tasks > Unassigned Tasks and assign the appropriate refinement task to you. Under Objects for Review, click the data request to...
Read more >
zod - npm
Introduction. Zod is a TypeScript-first schema declaration and validation library. I'm using the term "schema" to broadly refer to any data type ...
Read more >
Feature Requests: What are they and how to manage them
Feature requests are a form of product feedback you may frequently encounter as a SaaS product manager. They typically come in the form...
Read more >
How we handle Feature Requests - WHMCS Documentation
While we do not implement features based solely on their popularity or age, requests that get a lot of traction in our feature...
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