TypeScript-first schema validation with static type inference
This changes the default generics back to any
to prevent breakages with common packager like @hookform/resolvers
:
- class ZodType<Output = unknown, Def extends ZodTypeDef = ZodTypeDef, Input = unknown> {}
+ class ZodType<Output = any, Def extends ZodTypeDef = ZodTypeDef, Input = any> {}
Zod 3.23 is now available. This is the final 3.x
release before Zod 4.0. To try it out:
npm install zod
z.string().date()
Zod can now validate ISO 8601 date strings. Thanks @igalklebanov! https://github.com/colinhacks/zod/pull/1766
const schema = z.string().date();
schema.parse("2022-01-01"); // OK
z.string().time()
Zod can now validate ISO 8601 time strings. Thanks @igalklebanov! https://github.com/colinhacks/zod/pull/1766
const schema = z.string().time();
schema.parse("12:00:00"); // OK
You can specify sub-second precision using the precision
option:
const schema = z.string().time({ precision: 3 });
schema.parse("12:00:00.123"); // OK
schema.parse("12:00:00.123456"); // Error
schema.parse("12:00:00"); // Error
z.string().duration()
Zod can now validate ISO 8601 duration strings. Thanks @mastermatt! https://github.com/colinhacks/zod/pull/3265
const schema = z.string().duration();
schema.parse("P3Y6M4DT12H30M5S"); // OK
z.string().datetime()
Thanks @bchrobot https://github.com/colinhacks/zod/pull/2522
You can now allow unqualified (timezone-less) datetimes using the local: true
flag.
const schema = z.string().datetime({ local: true });
schema.parse("2022-01-01T12:00:00"); // OK
Plus, Zod now validates the day-of-month correctly to ensure no invalid dates (e.g. February 30th) pass validation. Thanks @szamanr! https://github.com/colinhacks/zod/pull/3391
z.string().base64()
Zod can now validate base64 strings. Thanks @StefanTerdell! https://github.com/colinhacks/zod/pull/3047
const schema = z.string().base64();
schema.parse("SGVsbG8gV29ybGQ="); // OK
The following can now be used as discriminator keys in z.discriminatedUnion()
:
ZodOptional
ZodNullable
ZodReadonly
ZodBranded
ZodCatch
const schema = z.discriminatedUnion("type", [
z.object({ type: z.literal("A").optional(), value: z.number() }),
z.object({ type: z.literal("B").nullable(), value: z.string() }),
z.object({ type: z.literal("C").readonly(), value: z.boolean() }),
z.object({ type: z.literal("D").brand<"D">(), value: z.boolean() }),
z.object({ type: z.literal("E").catch("E"), value: z.unknown() }),
]);
There are no breaking changes to the public API of Zod. However some changes can impact ecosystem tools that rely on Zod internals.
ZodFirstPartySchemaTypes
Three new types have been added to the ZodFirstPartySchemaTypes
union. This may impact some codegen libraries. https://github.com/colinhacks/zod/pull/3247
+ | ZodPipeline<any, any>
+ | ZodReadonly<any>
+ | ZodSymbol;
.pick()
and .omit()
This version fixes a bug where unknown keys were accidentally accepted in .pick()
and omit()
. This has been fixed, which could cause compiler errors in some user code. https://github.com/colinhacks/zod/pull/3255
z.object({
name: z.string()
}).pick({
notAKey: true // no longer allowed
})
ZodFirstPartySchemaTypes
by @MatthijsMud in https://github.com/colinhacks/zod/pull/3247
input
of .required()
readonly by @KATT in https://github.com/colinhacks/zod/pull/3301
addQuestionMarks
, fix #2184 by @colinhacks in https://github.com/colinhacks/zod/pull/3352
src
for bun test
by @rotu in https://github.com/colinhacks/zod/pull/3038
zod-dev
utility to eco-system section by @schalkventer in https://github.com/colinhacks/zod/pull/3113
orval
to "X to Zod" ecosystems by @soartec-lab in https://github.com/colinhacks/zod/pull/3397
Full Changelog: https://github.com/colinhacks/zod/compare/v3.22.4...v3.23.0
Zod 3.23 is now in beta! This is the final 3.x
release before Zod 4.0. To try it out:
npm install zod@beta
z.string().date()
Zod can now validate ISO 8601 date strings. Thanks @igalklebanov! https://github.com/colinhacks/zod/pull/1766
const schema = z.string().date();
schema.parse("2022-01-01"); // OK
z.string().time()
Zod can now validate ISO 8601 time strings. Thanks @igalklebanov! https://github.com/colinhacks/zod/pull/1766
const schema = z.string().time();
schema.parse("12:00:00"); // OK
You can specify sub-second precision using the precision
option:
const schema = z.string().time({ precision: 3 });
schema.parse("12:00:00.123"); // OK
schema.parse("12:00:00.123456"); // Error
schema.parse("12:00:00"); // Error
z.string().duration()
Zod can now validate ISO 8601 duration strings. Thanks @mastermatt! https://github.com/colinhacks/zod/pull/3265
const schema = z.string().duration();
schema.parse("P3Y6M4DT12H30M5S"); // OK
z.string().datetime()
Thanks @bchrobot https://github.com/colinhacks/zod/pull/2522
You can now allow unqualified (timezone-less) datetimes using the local: true
flag.
const schema = z.string().datetime({ local: true });
schema.parse("2022-01-01T12:00:00"); // OK
Plus, Zod now validates the day-of-month correctly to ensure no invalid dates (e.g. February 30th) pass validation. Thanks @szamanr! https://github.com/colinhacks/zod/pull/3391
z.string().base64()
Zod can now validate base64 strings. Thanks @StefanTerdell! https://github.com/colinhacks/zod/pull/3047
const schema = z.string().base64();
schema.parse("SGVsbG8gV29ybGQ="); // OK
The following can now be used as discriminator keys in z.discriminatedUnion()
:
ZodOptional
ZodNullable
ZodReadonly
ZodBranded
ZodCatch
const schema = z.discriminatedUnion("type", [
z.object({ type: z.literal("A").optional(), value: z.number() }),
z.object({ type: z.literal("B").nullable(), value: z.string() }),
z.object({ type: z.literal("C").readonly(), value: z.boolean() }),
z.object({ type: z.literal("D").readonly(), value: z.boolean() }),
z.object({ type: z.literal("E").catch("E"), value: z.unknown() }),
]);
There are no breaking changes to the public API of Zod. However some changes can impact ecosystem tools that rely on Zod internals.
ZodFirstPartySchemaTypes
Three new types have been added to the ZodFirstPartySchemaTypes
union. This may impact some codegen libraries. https://github.com/colinhacks/zod/pull/3247
+ | ZodPipeline<any, any>
+ | ZodReadonly<any>
+ | ZodSymbol;
ZodType
The third argument of the ZodType
base class now defaults to unknown
. This makes it easier to define recursive schemas and write generic functions that accept Zod schemas.
- class ZodType<Output = any, Def extends ZodTypeDef = ZodTypeDef, Input = Output> {}
+ class ZodType<Output = unknown, Def extends ZodTypeDef = ZodTypeDef, Input = unknown> {}
.pick()
and .omit()
This version fixes a bug where unknown keys were accidentally accepted in .pick()
and omit()
. This has been fixed, which could cause compiler errors in some user code. https://github.com/colinhacks/zod/pull/3255
z.object({
name: z.string()
}).pick({
notAKey: true // no longer allowed
})
ZodFirstPartySchemaTypes
by @MatthijsMud in https://github.com/colinhacks/zod/pull/3247
input
of .required()
readonly by @KATT in https://github.com/colinhacks/zod/pull/3301
addQuestionMarks
, fix #2184 by @colinhacks in https://github.com/colinhacks/zod/pull/3352
src
for bun test
by @rotu in https://github.com/colinhacks/zod/pull/3038
zod-dev
utility to eco-system section by @schalkventer in https://github.com/colinhacks/zod/pull/3113
orval
to "X to Zod" ecosystems by @soartec-lab in https://github.com/colinhacks/zod/pull/3397
Full Changelog: https://github.com/colinhacks/zod/compare/v3.22.4...v3.23.0-beta
zod-sandbox
to README ecosystem links (#2707)@deprecated
when building (#2717)