zod 3.21.0
Features
z.string().emoji()
Thanks @joseph-lozano for https://github.com/colinhacks/zod/pull/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 https://github.com/colinhacks/zod/pull/1813! To validate CUIDv2:
z.string().cuid2()
z.string().ip()
Thanks @fvckDesa for https://github.com/colinhacks/zod/pull/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 https://github.com/colinhacks/zod/pull/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 https://github.com/colinhacks/zod/pull/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: https://github.com/colinhacks/zod/pull/2107 Zod 3.21 resolves these issues:
- https://github.com/colinhacks/zod/issues/2142
- https://github.com/colinhacks/zod/issues/1741
- https://stackoverflow.com/questions/74881472/slow-typescript-autocompletion-in-vs-code-for-zod
Commits:
- 3c54461d7a649bf727aec59367eefb214ffe24fe fix typo in readme
- c244fb6c57506fd11609106960639de26ddf9b6d feat: z.string().emoji() (#2045)
- 39cbb6971c3dca09cbc0acdca2d3995dfd26aab2 Fix emoji validation, fix lint
- d8f07bbfffd255b0aee6b5fe9bb6aa2bce6586af Fix emoji
- 9b7dd816e92e16ca735e488b77e280a92a84ed64 Improve variable name clarity (#2048)
- 5cec1871ac21627608af6c07585d5e50ff30f281 Add documentation for the param parameter of z.custom
- 654f52969a968c630af816dcad0d8f3721f9001a Merge pull request #2057 from trygveaa/add-documentation-for-z-custom-params
- 981af6503ee1be530fe525ac77ba95e1904ce24a Merge pull request #2019 from vbud/patch-1
- a7c2969b9125df0fbfa65e8541a974eeaf53b6a6 Update error_handling
- 8f3d0283ba75d146d5199fe3dc140eeb5402352c BRAND Record to Non Partial (#2097)
- 5ec98e1c4420957f93a7388bf49fb01d91bcbcd0 Fix email issues in pull request #1982 (#2058)
- 7d40ba58931130ec965be9d65d14e0665ee9c379 feat(#2059): z.string.ip() - add support for IP address (#2066)
- e5596054cfddf8aa1ba8d7d3bad63552a2bf9b6a feat: add
.catcherror (#2087) - defdab9c163d9a994f927a0644ab4ec172513fcb Fix record tests
- 8de36eb17d9477ada75cea2164d6b47079dd0444 FIX: emoji regex and tests (#2090)
- 16beeb598039b33bc5a209956042d858abacca34 lowercase method for ZodString (#2038)
- 75cb9e814115a35c0b4cc2e970f6aaae90e1a13c add checks @
ZodBigInt. (#1711) - c4d4e494227aadc752d4d5a5a317ec16bb6f5a45 Update ERROR_HANDLING.md (#2022)
- d6f08908b17ea10456befc2231a867d5cea02a0e added link to deno land
- 4cf19606870e66bf4307984bf99a4bef495c7818 Refactoring of ZodFormattedError type to improve tsc check time (#2107)
- 867a921a93160780d9f3aeddabf1ca49758a3c1e Bump http-cache-semantics from 4.1.0 to 4.1.1 (#1985)
- edc3a67e77d33979b3ee9e071557b4ca53298c55 Deprecate deepPartial
- e59f639d3bb5cca65ef239d5dd5c93e74ce4a801 Add custom tests
- a6b44ed8e346af2fabbb62b5164b99a387accd32 Remove logging
- a1fc3fb815051beb4b7030969828aa6abe469c2c commented out
toLowerCaseandtoUpperCase - e71cc523b1865568c4db45f82789e5c86bed0f12 Update README_ZH.md (#2139)
- 3af38fbe2cb114219e688103e41e382e28ec7a80 add
ZodNumber.safe()&ZodNumber.isSafe. (#1753) - 6ef82ee1e45dbcfc92a2ed359b1d2ae87d54c43d Add benchmark flags
- 5463593385549452768e060a98cc0fd8988760d2 Support brands in recursive types
- 80745238937adc06c5b4eab19ef273ca9f7f2de8 Update readme
- b6794a4981b8621fee84642f5313464f5e8f9a22 Add index signature for passthrough
- 3c6cdd245ab73d7ca1461a18b5be2da07abe6b6b Make generic optional in objectOutputType
- bc43ad18e64cf7add23e90bc6b07d5bba8370e5e Fix rollup build
- 6a0545acd5b9cfc40b0b3f04e64bfc5dd789d1b1 3.21.0
- 7c0733968d64c7d7aebc1ae54ca90aadcb2bffc3 Fix brand
- 0aa6021ef3628bb39715bdda416be2c6a4951141 Clean up tests