Thanks to visit codestin.com
Credit goes to github.com

Skip to content

How might this support .transform()? #18

@morgs32

Description

@morgs32

Here's an example:

const stringToNumber: toZod<string> = z.string().transform((val) => val.length);
const a = stringToNumber.parse('string'); // => 6

It'd be nice to know that the value passed to the schema WAS a string, but that the parsed object would return a number? Have you run into this?

My use case is that:

  • All my endpoints that take dates (in queries or request bodies) hit my backend as strings.
  • I'd like to .refine() the schema to make sure that they are ISO strings even
  • But after parsing with zod I'd love to use Date objects again.

So this is my example:

const stringToNumber: toZod<string> = z.string().transform((val) => new Date(val));
const a = stringToNumber.parse('2022-01-01'); => // Date('2022-01-01')

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions