From bc0ffd248a1118da14f5fa8e9bfd0a3acdc7bb79 Mon Sep 17 00:00:00 2001 From: oberbeck <20164654+oberbeck@users.noreply.github.com> Date: Tue, 19 Nov 2024 21:08:11 +0100 Subject: [PATCH] fix(guard) add coercion to Defs --- deno/lib/types.ts | 35 ++++++++++++++++++++--------------- src/types.ts | 35 ++++++++++++++++++++--------------- 2 files changed, 40 insertions(+), 30 deletions(-) diff --git a/deno/lib/types.ts b/deno/lib/types.ts index 6ed30845a..9c0cc3416 100644 --- a/deno/lib/types.ts +++ b/deno/lib/types.ts @@ -571,10 +571,11 @@ export type ZodStringCheck = | { kind: "ip"; version?: IpVersion; message?: string } | { kind: "base64"; message?: string }; -export interface ZodStringDef extends ZodTypeDef { +export interface ZodStringDef + extends ZodTypeDef { checks: ZodStringCheck[]; typeName: ZodFirstPartyTypeKind.ZodString; - coerce: boolean; + coerce: TCoerce; } const cuidRegex = /^c[^\s-]{8,}$/i; @@ -1237,7 +1238,7 @@ export class ZodString extends ZodType< return new ZodString({ checks: [], typeName: ZodFirstPartyTypeKind.ZodString, - coerce: params?.coerce ?? false, + coerce: params?.coerce ?? (false as any), // TODO remove need for 'as any' ...processCreateParams(params), }); }; @@ -1267,10 +1268,11 @@ function floatSafeRemainder(val: number, step: number) { return (valInt % stepInt) / Math.pow(10, decCount); } -export interface ZodNumberDef extends ZodTypeDef { +export interface ZodNumberDef + extends ZodTypeDef { checks: ZodNumberCheck[]; typeName: ZodFirstPartyTypeKind.ZodNumber; - coerce: boolean; + coerce: TCoerce; } export class ZodNumber extends ZodType< @@ -1373,7 +1375,7 @@ export class ZodNumber extends ZodType< return new ZodNumber({ checks: [], typeName: ZodFirstPartyTypeKind.ZodNumber, - coerce: params?.coerce || false, + coerce: params?.coerce ?? (false as any), // TODO remove need for 'as any' ...processCreateParams(params), }); }; @@ -1556,10 +1558,11 @@ export type ZodBigIntCheck = | { kind: "max"; value: bigint; inclusive: boolean; message?: string } | { kind: "multipleOf"; value: bigint; message?: string }; -export interface ZodBigIntDef extends ZodTypeDef { +export interface ZodBigIntDef + extends ZodTypeDef { checks: ZodBigIntCheck[]; typeName: ZodFirstPartyTypeKind.ZodBigInt; - coerce: boolean; + coerce: TCoerce; } export class ZodBigInt extends ZodType< @@ -1640,7 +1643,7 @@ export class ZodBigInt extends ZodType< return new ZodBigInt({ checks: [], typeName: ZodFirstPartyTypeKind.ZodBigInt, - coerce: params?.coerce ?? false, + coerce: params?.coerce ?? (false as any), // TODO remove need for 'as any' ...processCreateParams(params), }); }; @@ -1762,9 +1765,10 @@ export class ZodBigInt extends ZodType< ////////// /////////// ////////////////////////////////////////// ////////////////////////////////////////// -export interface ZodBooleanDef extends ZodTypeDef { +export interface ZodBooleanDef + extends ZodTypeDef { typeName: ZodFirstPartyTypeKind.ZodBoolean; - coerce: boolean; + coerce: TCoerce; } export class ZodBoolean extends ZodType< @@ -1795,7 +1799,7 @@ export class ZodBoolean extends ZodType< ): ZodBoolean => { return new ZodBoolean({ typeName: ZodFirstPartyTypeKind.ZodBoolean, - coerce: params?.coerce || false, + coerce: params?.coerce ?? (false as any), // TODO remove need for 'as any' ...processCreateParams(params), }); }; @@ -1811,9 +1815,10 @@ export class ZodBoolean extends ZodType< export type ZodDateCheck = | { kind: "min"; value: number; message?: string } | { kind: "max"; value: number; message?: string }; -export interface ZodDateDef extends ZodTypeDef { +export interface ZodDateDef + extends ZodTypeDef { checks: ZodDateCheck[]; - coerce: boolean; + coerce: TCoerce; typeName: ZodFirstPartyTypeKind.ZodDate; } @@ -1937,7 +1942,7 @@ export class ZodDate extends ZodType< ): ZodDate => { return new ZodDate({ checks: [], - coerce: params?.coerce || false, + coerce: params?.coerce ?? (false as any), // TODO remove need for 'as any' typeName: ZodFirstPartyTypeKind.ZodDate, ...processCreateParams(params), }); diff --git a/src/types.ts b/src/types.ts index 668e91f78..404c7130e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -571,10 +571,11 @@ export type ZodStringCheck = | { kind: "ip"; version?: IpVersion; message?: string } | { kind: "base64"; message?: string }; -export interface ZodStringDef extends ZodTypeDef { +export interface ZodStringDef + extends ZodTypeDef { checks: ZodStringCheck[]; typeName: ZodFirstPartyTypeKind.ZodString; - coerce: boolean; + coerce: TCoerce; } const cuidRegex = /^c[^\s-]{8,}$/i; @@ -1237,7 +1238,7 @@ export class ZodString extends ZodType< return new ZodString({ checks: [], typeName: ZodFirstPartyTypeKind.ZodString, - coerce: params?.coerce ?? false, + coerce: params?.coerce ?? (false as any), // TODO remove need for 'as any' ...processCreateParams(params), }); }; @@ -1267,10 +1268,11 @@ function floatSafeRemainder(val: number, step: number) { return (valInt % stepInt) / Math.pow(10, decCount); } -export interface ZodNumberDef extends ZodTypeDef { +export interface ZodNumberDef + extends ZodTypeDef { checks: ZodNumberCheck[]; typeName: ZodFirstPartyTypeKind.ZodNumber; - coerce: boolean; + coerce: TCoerce; } export class ZodNumber extends ZodType< @@ -1373,7 +1375,7 @@ export class ZodNumber extends ZodType< return new ZodNumber({ checks: [], typeName: ZodFirstPartyTypeKind.ZodNumber, - coerce: params?.coerce || false, + coerce: params?.coerce ?? (false as any), // TODO remove need for 'as any' ...processCreateParams(params), }); }; @@ -1556,10 +1558,11 @@ export type ZodBigIntCheck = | { kind: "max"; value: bigint; inclusive: boolean; message?: string } | { kind: "multipleOf"; value: bigint; message?: string }; -export interface ZodBigIntDef extends ZodTypeDef { +export interface ZodBigIntDef + extends ZodTypeDef { checks: ZodBigIntCheck[]; typeName: ZodFirstPartyTypeKind.ZodBigInt; - coerce: boolean; + coerce: TCoerce; } export class ZodBigInt extends ZodType< @@ -1640,7 +1643,7 @@ export class ZodBigInt extends ZodType< return new ZodBigInt({ checks: [], typeName: ZodFirstPartyTypeKind.ZodBigInt, - coerce: params?.coerce ?? false, + coerce: params?.coerce ?? (false as any), // TODO remove need for 'as any' ...processCreateParams(params), }); }; @@ -1762,9 +1765,10 @@ export class ZodBigInt extends ZodType< ////////// /////////// ////////////////////////////////////////// ////////////////////////////////////////// -export interface ZodBooleanDef extends ZodTypeDef { +export interface ZodBooleanDef + extends ZodTypeDef { typeName: ZodFirstPartyTypeKind.ZodBoolean; - coerce: boolean; + coerce: TCoerce; } export class ZodBoolean extends ZodType< @@ -1795,7 +1799,7 @@ export class ZodBoolean extends ZodType< ): ZodBoolean => { return new ZodBoolean({ typeName: ZodFirstPartyTypeKind.ZodBoolean, - coerce: params?.coerce || false, + coerce: params?.coerce ?? (false as any), // TODO remove need for 'as any' ...processCreateParams(params), }); }; @@ -1811,9 +1815,10 @@ export class ZodBoolean extends ZodType< export type ZodDateCheck = | { kind: "min"; value: number; message?: string } | { kind: "max"; value: number; message?: string }; -export interface ZodDateDef extends ZodTypeDef { +export interface ZodDateDef + extends ZodTypeDef { checks: ZodDateCheck[]; - coerce: boolean; + coerce: TCoerce; typeName: ZodFirstPartyTypeKind.ZodDate; } @@ -1937,7 +1942,7 @@ export class ZodDate extends ZodType< ): ZodDate => { return new ZodDate({ checks: [], - coerce: params?.coerce || false, + coerce: params?.coerce ?? (false as any), // TODO remove need for 'as any' typeName: ZodFirstPartyTypeKind.ZodDate, ...processCreateParams(params), });