diff --git a/deno/lib/README.md b/deno/lib/README.md index 97cf7a725..b5d982240 100644 --- a/deno/lib/README.md +++ b/deno/lib/README.md @@ -617,6 +617,7 @@ There are a growing number of tools that are built atop or support Zod natively! - [`quicktype`](https://app.quicktype.io/): Convert JSON objects and JSON schemas into Zod schemas. - [`@sanity-typed/zod`](https://github.com/saiichihashimoto/sanity-typed/tree/main/packages/zod): Generate Zod Schemas from [Sanity Schemas](https://www.sanity.io/docs/schema-types). - [`java-to-zod`](https://github.com/ivangreene/java-to-zod): Convert POJOs to Zod schemas +- [`Orval`](https://github.com/anymaniax/orval): Generate Zod schemas from OpenAPI schemas #### Mocking diff --git a/deno/lib/types.ts b/deno/lib/types.ts index 0714f2004..a202ceeda 100644 --- a/deno/lib/types.ts +++ b/deno/lib/types.ts @@ -166,9 +166,9 @@ export type SafeParseReturnType = | SafeParseError; export abstract class ZodType< - Output = any, + Output = unknown, Def extends ZodTypeDef = ZodTypeDef, - Input = Output + Input = unknown > { readonly _type!: Output; readonly _output!: Output; @@ -669,7 +669,7 @@ function isValidIP(ip: string, version?: IpVersion) { return false; } -export class ZodString extends ZodType { +export class ZodString extends ZodType { _parse(input: ParseInput): ParseReturnType { if (this._def.coerce) { input.data = String(input.data); @@ -1261,7 +1261,7 @@ export interface ZodNumberDef extends ZodTypeDef { coerce: boolean; } -export class ZodNumber extends ZodType { +export class ZodNumber extends ZodType { _parse(input: ParseInput): ParseReturnType { if (this._def.coerce) { input.data = Number(input.data); @@ -1546,7 +1546,7 @@ export interface ZodBigIntDef extends ZodTypeDef { coerce: boolean; } -export class ZodBigInt extends ZodType { +export class ZodBigInt extends ZodType { _parse(input: ParseInput): ParseReturnType { if (this._def.coerce) { input.data = BigInt(input.data); @@ -1747,7 +1747,7 @@ export interface ZodBooleanDef extends ZodTypeDef { coerce: boolean; } -export class ZodBoolean extends ZodType { +export class ZodBoolean extends ZodType { _parse(input: ParseInput): ParseReturnType { if (this._def.coerce) { input.data = Boolean(input.data); @@ -1793,7 +1793,7 @@ export interface ZodDateDef extends ZodTypeDef { typeName: ZodFirstPartyTypeKind.ZodDate; } -export class ZodDate extends ZodType { +export class ZodDate extends ZodType { _parse(input: ParseInput): ParseReturnType { if (this._def.coerce) { input.data = new Date(input.data); @@ -1962,7 +1962,11 @@ export interface ZodUndefinedDef extends ZodTypeDef { typeName: ZodFirstPartyTypeKind.ZodUndefined; } -export class ZodUndefined extends ZodType { +export class ZodUndefined extends ZodType< + undefined, + ZodUndefinedDef, + undefined +> { _parse(input: ParseInput): ParseReturnType { const parsedType = this._getType(input); if (parsedType !== ZodParsedType.undefined) { @@ -1997,7 +2001,7 @@ export interface ZodNullDef extends ZodTypeDef { typeName: ZodFirstPartyTypeKind.ZodNull; } -export class ZodNull extends ZodType { +export class ZodNull extends ZodType { _parse(input: ParseInput): ParseReturnType { const parsedType = this._getType(input); if (parsedType !== ZodParsedType.null) { @@ -2030,7 +2034,7 @@ export interface ZodAnyDef extends ZodTypeDef { typeName: ZodFirstPartyTypeKind.ZodAny; } -export class ZodAny extends ZodType { +export class ZodAny extends ZodType { // to prevent instances of other classes from extending ZodAny. this causes issues with catchall in ZodObject. _any = true as const; _parse(input: ParseInput): ParseReturnType { @@ -2055,7 +2059,7 @@ export interface ZodUnknownDef extends ZodTypeDef { typeName: ZodFirstPartyTypeKind.ZodUnknown; } -export class ZodUnknown extends ZodType { +export class ZodUnknown extends ZodType { // required _unknown = true as const; _parse(input: ParseInput): ParseReturnType { @@ -2081,7 +2085,7 @@ export interface ZodNeverDef extends ZodTypeDef { typeName: ZodFirstPartyTypeKind.ZodNever; } -export class ZodNever extends ZodType { +export class ZodNever extends ZodType { _parse(input: ParseInput): ParseReturnType { const ctx = this._getOrReturnCtx(input); addIssueToContext(ctx, { @@ -2110,7 +2114,7 @@ export interface ZodVoidDef extends ZodTypeDef { typeName: ZodFirstPartyTypeKind.ZodVoid; } -export class ZodVoid extends ZodType { +export class ZodVoid extends ZodType { _parse(input: ParseInput): ParseReturnType { const parsedType = this._getType(input); if (parsedType !== ZodParsedType.undefined) { @@ -3374,7 +3378,7 @@ export class ZodIntersection< export type ZodTupleItems = [ZodTypeAny, ...ZodTypeAny[]]; export type AssertArray = T extends any[] ? T : never; export type OutputTypeOfTuple = AssertArray<{ - [k in keyof T]: T[k] extends ZodType ? T[k]["_output"] : never; + [k in keyof T]: T[k] extends ZodType ? T[k]["_output"] : never; }>; export type OutputTypeOfTupleWithRest< T extends ZodTupleItems | [], @@ -3384,7 +3388,7 @@ export type OutputTypeOfTupleWithRest< : OutputTypeOfTuple; export type InputTypeOfTuple = AssertArray<{ - [k in keyof T]: T[k] extends ZodType ? T[k]["_input"] : never; + [k in keyof T]: T[k] extends ZodType ? T[k]["_input"] : never; }>; export type InputTypeOfTupleWithRest< T extends ZodTupleItems | [], @@ -3982,7 +3986,7 @@ export class ZodFunction< }); } - returns>( + returns>( returnType: NewReturnType ): ZodFunction { return new ZodFunction({ @@ -4089,7 +4093,7 @@ export interface ZodLiteralDef extends ZodTypeDef { typeName: ZodFirstPartyTypeKind.ZodLiteral; } -export class ZodLiteral extends ZodType> { +export class ZodLiteral extends ZodType, T> { _parse(input: ParseInput): ParseReturnType { if (input.data !== this._def.value) { const ctx = this._getOrReturnCtx(input); @@ -4174,7 +4178,8 @@ function createZodEnum( export class ZodEnum extends ZodType< T[number], - ZodEnumDef + ZodEnumDef, + T[number] > { #cache: Set | undefined; @@ -4284,7 +4289,8 @@ export type EnumLike = { [k: string]: string | number; [nu: number]: string }; export class ZodNativeEnum extends ZodType< T[keyof T], - ZodNativeEnumDef + ZodNativeEnumDef, + T[keyof T] > { #cache: Set | undefined; _parse(input: ParseInput): ParseReturnType { @@ -4851,7 +4857,7 @@ export interface ZodNaNDef extends ZodTypeDef { typeName: ZodFirstPartyTypeKind.ZodNaN; } -export class ZodNaN extends ZodType { +export class ZodNaN extends ZodType { _parse(input: ParseInput): ParseReturnType { const parsedType = this._getType(input); if (parsedType !== ZodParsedType.nan) { @@ -5073,7 +5079,7 @@ export function custom( * */ fatal?: boolean -): ZodType { +): ZodType { if (check) return ZodAny.create().superRefine((data, ctx) => { if (!check(data)) { diff --git a/playground.ts b/playground.ts index aa1eec445..d0c205e65 100644 --- a/playground.ts +++ b/playground.ts @@ -2,6 +2,4 @@ import { z } from "./src"; z; -const dateRegexSource = `((\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-((0[13578]|1[02])-(0[1-9]|[12]\\d|3[01])|(0[469]|11)-(0[1-9]|[12]\\d|30)|(02)-(0[1-9]|1\\d|2[0-8])))`; -const dateRegex = new RegExp(`^${dateRegexSource}$`); -console.log(dateRegex.test("2400-02-29")); +console.log(z.string().ip().parse("255.255.255.255")); diff --git a/src/types.ts b/src/types.ts index aee5869b7..f98c0993a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -166,9 +166,9 @@ export type SafeParseReturnType = | SafeParseError; export abstract class ZodType< - Output = any, + Output = unknown, Def extends ZodTypeDef = ZodTypeDef, - Input = Output + Input = unknown > { readonly _type!: Output; readonly _output!: Output; @@ -669,7 +669,7 @@ function isValidIP(ip: string, version?: IpVersion) { return false; } -export class ZodString extends ZodType { +export class ZodString extends ZodType { _parse(input: ParseInput): ParseReturnType { if (this._def.coerce) { input.data = String(input.data); @@ -1261,7 +1261,7 @@ export interface ZodNumberDef extends ZodTypeDef { coerce: boolean; } -export class ZodNumber extends ZodType { +export class ZodNumber extends ZodType { _parse(input: ParseInput): ParseReturnType { if (this._def.coerce) { input.data = Number(input.data); @@ -1546,7 +1546,7 @@ export interface ZodBigIntDef extends ZodTypeDef { coerce: boolean; } -export class ZodBigInt extends ZodType { +export class ZodBigInt extends ZodType { _parse(input: ParseInput): ParseReturnType { if (this._def.coerce) { input.data = BigInt(input.data); @@ -1747,7 +1747,7 @@ export interface ZodBooleanDef extends ZodTypeDef { coerce: boolean; } -export class ZodBoolean extends ZodType { +export class ZodBoolean extends ZodType { _parse(input: ParseInput): ParseReturnType { if (this._def.coerce) { input.data = Boolean(input.data); @@ -1793,7 +1793,7 @@ export interface ZodDateDef extends ZodTypeDef { typeName: ZodFirstPartyTypeKind.ZodDate; } -export class ZodDate extends ZodType { +export class ZodDate extends ZodType { _parse(input: ParseInput): ParseReturnType { if (this._def.coerce) { input.data = new Date(input.data); @@ -1962,7 +1962,11 @@ export interface ZodUndefinedDef extends ZodTypeDef { typeName: ZodFirstPartyTypeKind.ZodUndefined; } -export class ZodUndefined extends ZodType { +export class ZodUndefined extends ZodType< + undefined, + ZodUndefinedDef, + undefined +> { _parse(input: ParseInput): ParseReturnType { const parsedType = this._getType(input); if (parsedType !== ZodParsedType.undefined) { @@ -1997,7 +2001,7 @@ export interface ZodNullDef extends ZodTypeDef { typeName: ZodFirstPartyTypeKind.ZodNull; } -export class ZodNull extends ZodType { +export class ZodNull extends ZodType { _parse(input: ParseInput): ParseReturnType { const parsedType = this._getType(input); if (parsedType !== ZodParsedType.null) { @@ -2030,7 +2034,7 @@ export interface ZodAnyDef extends ZodTypeDef { typeName: ZodFirstPartyTypeKind.ZodAny; } -export class ZodAny extends ZodType { +export class ZodAny extends ZodType { // to prevent instances of other classes from extending ZodAny. this causes issues with catchall in ZodObject. _any = true as const; _parse(input: ParseInput): ParseReturnType { @@ -2055,7 +2059,7 @@ export interface ZodUnknownDef extends ZodTypeDef { typeName: ZodFirstPartyTypeKind.ZodUnknown; } -export class ZodUnknown extends ZodType { +export class ZodUnknown extends ZodType { // required _unknown = true as const; _parse(input: ParseInput): ParseReturnType { @@ -2081,7 +2085,7 @@ export interface ZodNeverDef extends ZodTypeDef { typeName: ZodFirstPartyTypeKind.ZodNever; } -export class ZodNever extends ZodType { +export class ZodNever extends ZodType { _parse(input: ParseInput): ParseReturnType { const ctx = this._getOrReturnCtx(input); addIssueToContext(ctx, { @@ -2110,7 +2114,7 @@ export interface ZodVoidDef extends ZodTypeDef { typeName: ZodFirstPartyTypeKind.ZodVoid; } -export class ZodVoid extends ZodType { +export class ZodVoid extends ZodType { _parse(input: ParseInput): ParseReturnType { const parsedType = this._getType(input); if (parsedType !== ZodParsedType.undefined) { @@ -3374,7 +3378,7 @@ export class ZodIntersection< export type ZodTupleItems = [ZodTypeAny, ...ZodTypeAny[]]; export type AssertArray = T extends any[] ? T : never; export type OutputTypeOfTuple = AssertArray<{ - [k in keyof T]: T[k] extends ZodType ? T[k]["_output"] : never; + [k in keyof T]: T[k] extends ZodType ? T[k]["_output"] : never; }>; export type OutputTypeOfTupleWithRest< T extends ZodTupleItems | [], @@ -3384,7 +3388,7 @@ export type OutputTypeOfTupleWithRest< : OutputTypeOfTuple; export type InputTypeOfTuple = AssertArray<{ - [k in keyof T]: T[k] extends ZodType ? T[k]["_input"] : never; + [k in keyof T]: T[k] extends ZodType ? T[k]["_input"] : never; }>; export type InputTypeOfTupleWithRest< T extends ZodTupleItems | [], @@ -3982,7 +3986,7 @@ export class ZodFunction< }); } - returns>( + returns>( returnType: NewReturnType ): ZodFunction { return new ZodFunction({ @@ -4089,7 +4093,7 @@ export interface ZodLiteralDef extends ZodTypeDef { typeName: ZodFirstPartyTypeKind.ZodLiteral; } -export class ZodLiteral extends ZodType> { +export class ZodLiteral extends ZodType, T> { _parse(input: ParseInput): ParseReturnType { if (input.data !== this._def.value) { const ctx = this._getOrReturnCtx(input); @@ -4174,7 +4178,8 @@ function createZodEnum( export class ZodEnum extends ZodType< T[number], - ZodEnumDef + ZodEnumDef, + T[number] > { #cache: Set | undefined; @@ -4284,7 +4289,8 @@ export type EnumLike = { [k: string]: string | number; [nu: number]: string }; export class ZodNativeEnum extends ZodType< T[keyof T], - ZodNativeEnumDef + ZodNativeEnumDef, + T[keyof T] > { #cache: Set | undefined; _parse(input: ParseInput): ParseReturnType { @@ -4851,7 +4857,7 @@ export interface ZodNaNDef extends ZodTypeDef { typeName: ZodFirstPartyTypeKind.ZodNaN; } -export class ZodNaN extends ZodType { +export class ZodNaN extends ZodType { _parse(input: ParseInput): ParseReturnType { const parsedType = this._getType(input); if (parsedType !== ZodParsedType.nan) { @@ -5073,7 +5079,7 @@ export function custom( * */ fatal?: boolean -): ZodType { +): ZodType { if (check) return ZodAny.create().superRefine((data, ctx) => { if (!check(data)) {