3

Validating a string can be done with a regex. That's easy.

const myString = z.string().regex(/A string/);

But what about other data types?

I guess the following could work for number, but it doesn't seem idiomatic.

const myNumber = z.number().gte(7).lte(7);

Is there a better way?

Souperman
  • 5,057
  • 1
  • 14
  • 39
Stephen Horvath
  • 5,188
  • 3
  • 24
  • 31

1 Answers1

3

You can use Zod Literals to match exact values. Inferred types will also be exactly the value specified rather than number.

For example:

const myNumber = z.literal(7);
Souperman
  • 5,057
  • 1
  • 14
  • 39