Features
z.string().emoji()
Thanks @joseph-lozano for #2045! To validate that all characters in a string are emoji:
z.string().emoji()
...if that's something you want to do for some reason.
z.string().cuid2()
Thanks @joulev for #1813! To validate CUIDv2:
z.string().cuid2()
z.string().ip()
Thanks @fvckDesa for #2066. To validate that a string is a valid IP address:
const v4IP = "122.122.122.122";
const v6IP = "6097:adfa:6f0b:220d:db08:5021:6191:7990";
// matches both IPv4 and IPv6 by default
const ipSchema = z.string().ip();
ipSchema.parse(v4IP) // pass
ipSchema.parse(v6IP) // pass
To specify a particular version
:
const ipv4Schema = z.string().ip({ version: "v4" });
const ipv6Schema = z.string().ip({ version: "v6" });
z.bigint().{gt|gte|lt|lte}()
Thanks @igalklebanov for #1711
! ZodBigInt
gets the same set of methods found on ZodNumber
:
z.bigint().gt(BigInt(5));
z.bigint().gte(BigInt(5));
z.bigint().lt(BigInt(5));
z.bigint().lte(BigInt(5));
z.bigint().positive();
z.bigint().negative();
z.bigint().nonnegative();
z.bigint().nonpositive();
z.bigint().multipleOf(BigInt(5));
z.enum(...).extract()
and z.enum(...).exclude()
Thanks @santosmarco-caribou for #1652! To add or remove elements from a ZodEnum
:
const FoodEnum = z.enum(["Pasta", "Pizza", "Tacos", "Burgers", "Salad"]);
const ItalianEnum = FoodEnum.extract(["Pasta", "Pizza"]); // ZodEnum<["Pasta", "Pizza"]>
const UnhealthyEnum = FoodEnum.exclude(["Salad"]); // ZodEnum<["Pasta", "Pizza", "Tacos", "Burgers"]>
This API is inspired by the Exclude
and Extract
TypeScript built-ins.
Pass a function to .catch()
Thanks @0xWryth for #2087! The .catch()
method now accepts a function that receives the caught error:
const numberWithErrorCatch = z.number().catch((ctx) => {
ctx.error; // ZodError
return 42;
});
Compiler performance
Zod 3.20.2 introduced an accidental type recursion that caused long compilation times for some users. These kinds of bugs are very hard to diagnose. Big shoutout to @gydroperit for some heroic efforts here: #2107 Zod 3.21 resolves these issues:
- #2142
- #1741
- https://stackoverflow.com/questions/74881472/slow-typescript-autocompletion-in-vs-code-for-zod
Commits:
- 3c54461 fix typo in readme
- c244fb6 feat: z.string().emoji() (#2045)
- 39cbb69 Fix emoji validation, fix lint
- d8f07bb Fix emoji
- 9b7dd81 Improve variable name clarity (#2048)
- 5cec187 Add documentation for the param parameter of z.custom
- 654f529 Merge pull request #2057 from trygveaa/add-documentation-for-z-custom-params
- 981af65 Merge pull request #2019 from vbud/patch-1
- a7c2969 Update error_handling
- 8f3d028 BRAND Record to Non Partial (#2097)
- 5ec98e1 Fix email issues in pull request #1982 (#2058)
- 7d40ba5 feat(#2059): z.string.ip() - add support for IP address (#2066)
- e559605 feat: add
.catch
error (#2087) - defdab9 Fix record tests
- 8de36eb FIX: emoji regex and tests (#2090)
- 16beeb5 lowercase method for ZodString (#2038)
- 75cb9e8 add checks @
ZodBigInt
. (#1711) - c4d4e49 Update ERROR_HANDLING.md (#2022)
- d6f0890 added link to deno land
- 4cf1960 Refactoring of ZodFormattedError type to improve tsc check time (#2107)
- 867a921 Bump http-cache-semantics from 4.1.0 to 4.1.1 (#1985)
- edc3a67 Deprecate deepPartial
- e59f639 Add custom tests
- a6b44ed Remove logging
- a1fc3fb commented out
toLowerCase
andtoUpperCase
- e71cc52 Update README_ZH.md (#2139)
- 3af38fb add
ZodNumber.safe()
&ZodNumber.isSafe
. (#1753) - 6ef82ee Add benchmark flags
- 5463593 Support brands in recursive types
- 8074523 Update readme
- b6794a4 Add index signature for passthrough
- 3c6cdd2 Make generic optional in objectOutputType
- bc43ad1 Fix rollup build
- 6a0545a 3.21.0
- 7c07339 Fix brand
- 0aa6021 Clean up tests