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 suggestion] A way to chain/pipe validations

See original GitHub issue

Hello,

First of all, thank you for making and maintaining this awesome library.

I started using zod to validate forms on a frontend project, and found myself doing things like:

const FormData = z.object({
  age: z.string().transform((x, ctx) => {
    const value = parseInt(x)
    if (isNaN(value) || value < 0) {
      ctx.addIssue({code: z.ZodIssueCode.custom, message: 'should be a positif integer'})
      return z.NEVER
    }
    return value
  }),
  worked_days_in_month: z.string().transform((x, ctx) => {
    const value = parseInt(x)
    if (isNaN(value) || value < 0 || value > 31) {
      ctx.addIssue({code: z.ZodIssueCode.custom, message: 'should be an integer between 1 and 31'})
      return z.NEVER
    }
    return value
  }),
})

Since all values coming from the HTML inputs are strings, I have to validate them using z.string then use .transform to convert them to numbers and do additional validation.

I think it will be easier if after converting them to numbers, I could chain them to a z.number with some additional checks, something like:

const FormData = z.object({
  age: z.string().transform(x => parseInt(x)).chain(z.number().int().min(0)),
  worked_days_in_month: z.string().transform(x => parseInt(x)).chain(z.number().int().min(0).max(31)),
})

So my suggestion is to add a method .chain (or .pipe or other name …) that behaves as follows:

const a: ZodType // with types: _input = A and _output = B
const b: ZodType // with types: _input = B and _output = C

const c = a.chain(b) // gives a ZodType with _input = A and _output = C

Let me know if this is something you would be interested in adding to the library, I can try to make a PR for it if you agree.

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
timm-stelzer-e-farmcommented, Dec 12, 2022

Looks like pipe is now built-in and should resolve this issue 😃

z.string()
  .transform(val => val.length)
  .pipe(z.number().min(5))
3reactions
timm-stelzer-e-farmcommented, Nov 12, 2022

tbh if you need to do that many nests you’re probably overcomplicating your Zod type. Just my 2c.

Fair enough, though this type of composition is fundamental in functional programming, and since zod was explicitly inspired by io-ts (a library where this kind of composition is built-in), it isn’t far-fetched to expect this kind of API.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[Feature suggestion] A way to chain/pipe validations
Since all values coming from the HTML inputs are strings, I have to validate them using z.string then use .transform to convert them...
Read more >
Chain pipe construction - error? - Fusion 360 - Autodesk Forums
The pipe command doesn't allow branches, you can only select a single continuous path. Your post reminded me that I was going to...
Read more >
Guide: Quickly validate a need for a feature idea - Almanac.io
Use validation tools such as prototyping and user testing to gain a better understanding of your feature specs. Test more than one solution...
Read more >
Chain Pipe Tongs and Replacement Parts - Travers Tool
We feature a complete inventory of the following: End Mills · Taps · Drills · Calipers · Micrometers · Deburring tools · Drill...
Read more >
How to validate product or feature idea before building it
How to validate product or feature idea before building it Ideas have become a commodity, everyone has had that 1 million dollar idea....
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