Skip to content

Commit 1afd010

Browse files
committed
fix(guard) add coercion to Defs
1 parent 826ed00 commit 1afd010

File tree

2 files changed

+50
-40
lines changed

2 files changed

+50
-40
lines changed

deno/lib/types.ts

+25-20
Original file line numberDiff line numberDiff line change
@@ -571,10 +571,11 @@ export type ZodStringCheck =
571571
| { kind: "ip"; version?: IpVersion; message?: string }
572572
| { kind: "base64"; message?: string };
573573

574-
export interface ZodStringDef extends ZodTypeDef {
574+
export interface ZodStringDef<TCoerce extends boolean = false>
575+
extends ZodTypeDef {
575576
checks: ZodStringCheck[];
576577
typeName: ZodFirstPartyTypeKind.ZodString;
577-
coerce: boolean;
578+
coerce: TCoerce;
578579
}
579580

580581
const cuidRegex = /^c[^\s-]{8,}$/i;
@@ -677,7 +678,7 @@ function isValidIP(ip: string, version?: IpVersion) {
677678

678679
export class ZodString<TCoerce extends boolean = false> extends ZodType<
679680
string,
680-
ZodStringDef,
681+
ZodStringDef<TCoerce>,
681682
TCoerce extends true ? unknown : string
682683
> {
683684
_parse(input: ParseInput): ParseReturnType<string> {
@@ -1237,7 +1238,7 @@ export class ZodString<TCoerce extends boolean = false> extends ZodType<
12371238
return new ZodString<TParam["coerce"] extends true ? true : false>({
12381239
checks: [],
12391240
typeName: ZodFirstPartyTypeKind.ZodString,
1240-
coerce: params?.coerce ?? false,
1241+
coerce: params?.coerce ?? (false as any), // TODO remove need for 'as any'
12411242
...processCreateParams(params),
12421243
});
12431244
};
@@ -1267,15 +1268,16 @@ function floatSafeRemainder(val: number, step: number) {
12671268
return (valInt % stepInt) / Math.pow(10, decCount);
12681269
}
12691270

1270-
export interface ZodNumberDef extends ZodTypeDef {
1271+
export interface ZodNumberDef<TCoerce extends boolean = false>
1272+
extends ZodTypeDef {
12711273
checks: ZodNumberCheck[];
12721274
typeName: ZodFirstPartyTypeKind.ZodNumber;
1273-
coerce: boolean;
1275+
coerce: TCoerce;
12741276
}
12751277

12761278
export class ZodNumber<TCoerce extends boolean = false> extends ZodType<
12771279
number,
1278-
ZodNumberDef,
1280+
ZodNumberDef<TCoerce>,
12791281
TCoerce extends true ? unknown : number
12801282
> {
12811283
_parse(input: ParseInput): ParseReturnType<number> {
@@ -1373,7 +1375,7 @@ export class ZodNumber<TCoerce extends boolean = false> extends ZodType<
13731375
return new ZodNumber<TParam["coerce"] extends true ? true : false>({
13741376
checks: [],
13751377
typeName: ZodFirstPartyTypeKind.ZodNumber,
1376-
coerce: params?.coerce || false,
1378+
coerce: params?.coerce ?? (false as any), // TODO remove need for 'as any'
13771379
...processCreateParams(params),
13781380
});
13791381
};
@@ -1556,15 +1558,16 @@ export type ZodBigIntCheck =
15561558
| { kind: "max"; value: bigint; inclusive: boolean; message?: string }
15571559
| { kind: "multipleOf"; value: bigint; message?: string };
15581560

1559-
export interface ZodBigIntDef extends ZodTypeDef {
1561+
export interface ZodBigIntDef<TCoerce extends boolean = false>
1562+
extends ZodTypeDef {
15601563
checks: ZodBigIntCheck[];
15611564
typeName: ZodFirstPartyTypeKind.ZodBigInt;
1562-
coerce: boolean;
1565+
coerce: TCoerce;
15631566
}
15641567

15651568
export class ZodBigInt<TCoerce extends boolean = false> extends ZodType<
15661569
bigint,
1567-
ZodBigIntDef,
1570+
ZodBigIntDef<TCoerce>,
15681571
TCoerce extends true ? unknown : bigint
15691572
> {
15701573
_parse(input: ParseInput): ParseReturnType<bigint> {
@@ -1640,7 +1643,7 @@ export class ZodBigInt<TCoerce extends boolean = false> extends ZodType<
16401643
return new ZodBigInt<TParam["coerce"] extends true ? true : false>({
16411644
checks: [],
16421645
typeName: ZodFirstPartyTypeKind.ZodBigInt,
1643-
coerce: params?.coerce ?? false,
1646+
coerce: params?.coerce ?? (false as any), // TODO remove need for 'as any'
16441647
...processCreateParams(params),
16451648
});
16461649
};
@@ -1762,14 +1765,15 @@ export class ZodBigInt<TCoerce extends boolean = false> extends ZodType<
17621765
////////// ///////////
17631766
//////////////////////////////////////////
17641767
//////////////////////////////////////////
1765-
export interface ZodBooleanDef extends ZodTypeDef {
1768+
export interface ZodBooleanDef<TCoerce extends boolean = false>
1769+
extends ZodTypeDef {
17661770
typeName: ZodFirstPartyTypeKind.ZodBoolean;
1767-
coerce: boolean;
1771+
coerce: TCoerce;
17681772
}
17691773

17701774
export class ZodBoolean<TCoerce extends boolean = false> extends ZodType<
17711775
boolean,
1772-
ZodBooleanDef,
1776+
ZodBooleanDef<TCoerce>,
17731777
TCoerce extends true ? unknown : boolean
17741778
> {
17751779
_parse(input: ParseInput): ParseReturnType<boolean> {
@@ -1795,7 +1799,7 @@ export class ZodBoolean<TCoerce extends boolean = false> extends ZodType<
17951799
): ZodBoolean<TParam["coerce"] extends true ? true : false> => {
17961800
return new ZodBoolean<TParam["coerce"] extends true ? true : false>({
17971801
typeName: ZodFirstPartyTypeKind.ZodBoolean,
1798-
coerce: params?.coerce || false,
1802+
coerce: params?.coerce ?? (false as any), // TODO remove need for 'as any'
17991803
...processCreateParams(params),
18001804
});
18011805
};
@@ -1811,15 +1815,16 @@ export class ZodBoolean<TCoerce extends boolean = false> extends ZodType<
18111815
export type ZodDateCheck =
18121816
| { kind: "min"; value: number; message?: string }
18131817
| { kind: "max"; value: number; message?: string };
1814-
export interface ZodDateDef extends ZodTypeDef {
1818+
export interface ZodDateDef<TCoerce extends boolean = false>
1819+
extends ZodTypeDef {
18151820
checks: ZodDateCheck[];
1816-
coerce: boolean;
1821+
coerce: TCoerce;
18171822
typeName: ZodFirstPartyTypeKind.ZodDate;
18181823
}
18191824

18201825
export class ZodDate<TCoerce extends boolean = false> extends ZodType<
18211826
Date,
1822-
ZodDateDef,
1827+
ZodDateDef<TCoerce>,
18231828
TCoerce extends true ? unknown : Date
18241829
> {
18251830
_parse(input: ParseInput): ParseReturnType<this["_output"]> {
@@ -1937,7 +1942,7 @@ export class ZodDate<TCoerce extends boolean = false> extends ZodType<
19371942
): ZodDate<TParam["coerce"] extends true ? true : false> => {
19381943
return new ZodDate<TParam["coerce"] extends true ? true : false>({
19391944
checks: [],
1940-
coerce: params?.coerce || false,
1945+
coerce: params?.coerce ?? (false as any), // TODO remove need for 'as any'
19411946
typeName: ZodFirstPartyTypeKind.ZodDate,
19421947
...processCreateParams(params),
19431948
});

src/types.ts

+25-20
Original file line numberDiff line numberDiff line change
@@ -571,10 +571,11 @@ export type ZodStringCheck =
571571
| { kind: "ip"; version?: IpVersion; message?: string }
572572
| { kind: "base64"; message?: string };
573573

574-
export interface ZodStringDef extends ZodTypeDef {
574+
export interface ZodStringDef<TCoerce extends boolean = false>
575+
extends ZodTypeDef {
575576
checks: ZodStringCheck[];
576577
typeName: ZodFirstPartyTypeKind.ZodString;
577-
coerce: boolean;
578+
coerce: TCoerce;
578579
}
579580

580581
const cuidRegex = /^c[^\s-]{8,}$/i;
@@ -677,7 +678,7 @@ function isValidIP(ip: string, version?: IpVersion) {
677678

678679
export class ZodString<TCoerce extends boolean = false> extends ZodType<
679680
string,
680-
ZodStringDef,
681+
ZodStringDef<TCoerce>,
681682
TCoerce extends true ? unknown : string
682683
> {
683684
_parse(input: ParseInput): ParseReturnType<string> {
@@ -1237,7 +1238,7 @@ export class ZodString<TCoerce extends boolean = false> extends ZodType<
12371238
return new ZodString<TParam["coerce"] extends true ? true : false>({
12381239
checks: [],
12391240
typeName: ZodFirstPartyTypeKind.ZodString,
1240-
coerce: params?.coerce ?? false,
1241+
coerce: params?.coerce ?? (false as any), // TODO remove need for 'as any'
12411242
...processCreateParams(params),
12421243
});
12431244
};
@@ -1267,15 +1268,16 @@ function floatSafeRemainder(val: number, step: number) {
12671268
return (valInt % stepInt) / Math.pow(10, decCount);
12681269
}
12691270

1270-
export interface ZodNumberDef extends ZodTypeDef {
1271+
export interface ZodNumberDef<TCoerce extends boolean = false>
1272+
extends ZodTypeDef {
12711273
checks: ZodNumberCheck[];
12721274
typeName: ZodFirstPartyTypeKind.ZodNumber;
1273-
coerce: boolean;
1275+
coerce: TCoerce;
12741276
}
12751277

12761278
export class ZodNumber<TCoerce extends boolean = false> extends ZodType<
12771279
number,
1278-
ZodNumberDef,
1280+
ZodNumberDef<TCoerce>,
12791281
TCoerce extends true ? unknown : number
12801282
> {
12811283
_parse(input: ParseInput): ParseReturnType<number> {
@@ -1373,7 +1375,7 @@ export class ZodNumber<TCoerce extends boolean = false> extends ZodType<
13731375
return new ZodNumber<TParam["coerce"] extends true ? true : false>({
13741376
checks: [],
13751377
typeName: ZodFirstPartyTypeKind.ZodNumber,
1376-
coerce: params?.coerce || false,
1378+
coerce: params?.coerce ?? (false as any), // TODO remove need for 'as any'
13771379
...processCreateParams(params),
13781380
});
13791381
};
@@ -1556,15 +1558,16 @@ export type ZodBigIntCheck =
15561558
| { kind: "max"; value: bigint; inclusive: boolean; message?: string }
15571559
| { kind: "multipleOf"; value: bigint; message?: string };
15581560

1559-
export interface ZodBigIntDef extends ZodTypeDef {
1561+
export interface ZodBigIntDef<TCoerce extends boolean = false>
1562+
extends ZodTypeDef {
15601563
checks: ZodBigIntCheck[];
15611564
typeName: ZodFirstPartyTypeKind.ZodBigInt;
1562-
coerce: boolean;
1565+
coerce: TCoerce;
15631566
}
15641567

15651568
export class ZodBigInt<TCoerce extends boolean = false> extends ZodType<
15661569
bigint,
1567-
ZodBigIntDef,
1570+
ZodBigIntDef<TCoerce>,
15681571
TCoerce extends true ? unknown : bigint
15691572
> {
15701573
_parse(input: ParseInput): ParseReturnType<bigint> {
@@ -1640,7 +1643,7 @@ export class ZodBigInt<TCoerce extends boolean = false> extends ZodType<
16401643
return new ZodBigInt<TParam["coerce"] extends true ? true : false>({
16411644
checks: [],
16421645
typeName: ZodFirstPartyTypeKind.ZodBigInt,
1643-
coerce: params?.coerce ?? false,
1646+
coerce: params?.coerce ?? (false as any), // TODO remove need for 'as any'
16441647
...processCreateParams(params),
16451648
});
16461649
};
@@ -1762,14 +1765,15 @@ export class ZodBigInt<TCoerce extends boolean = false> extends ZodType<
17621765
////////// ///////////
17631766
//////////////////////////////////////////
17641767
//////////////////////////////////////////
1765-
export interface ZodBooleanDef extends ZodTypeDef {
1768+
export interface ZodBooleanDef<TCoerce extends boolean = false>
1769+
extends ZodTypeDef {
17661770
typeName: ZodFirstPartyTypeKind.ZodBoolean;
1767-
coerce: boolean;
1771+
coerce: TCoerce;
17681772
}
17691773

17701774
export class ZodBoolean<TCoerce extends boolean = false> extends ZodType<
17711775
boolean,
1772-
ZodBooleanDef,
1776+
ZodBooleanDef<TCoerce>,
17731777
TCoerce extends true ? unknown : boolean
17741778
> {
17751779
_parse(input: ParseInput): ParseReturnType<boolean> {
@@ -1795,7 +1799,7 @@ export class ZodBoolean<TCoerce extends boolean = false> extends ZodType<
17951799
): ZodBoolean<TParam["coerce"] extends true ? true : false> => {
17961800
return new ZodBoolean<TParam["coerce"] extends true ? true : false>({
17971801
typeName: ZodFirstPartyTypeKind.ZodBoolean,
1798-
coerce: params?.coerce || false,
1802+
coerce: params?.coerce ?? (false as any), // TODO remove need for 'as any'
17991803
...processCreateParams(params),
18001804
});
18011805
};
@@ -1811,15 +1815,16 @@ export class ZodBoolean<TCoerce extends boolean = false> extends ZodType<
18111815
export type ZodDateCheck =
18121816
| { kind: "min"; value: number; message?: string }
18131817
| { kind: "max"; value: number; message?: string };
1814-
export interface ZodDateDef extends ZodTypeDef {
1818+
export interface ZodDateDef<TCoerce extends boolean = false>
1819+
extends ZodTypeDef {
18151820
checks: ZodDateCheck[];
1816-
coerce: boolean;
1821+
coerce: TCoerce;
18171822
typeName: ZodFirstPartyTypeKind.ZodDate;
18181823
}
18191824

18201825
export class ZodDate<TCoerce extends boolean = false> extends ZodType<
18211826
Date,
1822-
ZodDateDef,
1827+
ZodDateDef<TCoerce>,
18231828
TCoerce extends true ? unknown : Date
18241829
> {
18251830
_parse(input: ParseInput): ParseReturnType<this["_output"]> {
@@ -1937,7 +1942,7 @@ export class ZodDate<TCoerce extends boolean = false> extends ZodType<
19371942
): ZodDate<TParam["coerce"] extends true ? true : false> => {
19381943
return new ZodDate<TParam["coerce"] extends true ? true : false>({
19391944
checks: [],
1940-
coerce: params?.coerce || false,
1945+
coerce: params?.coerce ?? (false as any), // TODO remove need for 'as any'
19411946
typeName: ZodFirstPartyTypeKind.ZodDate,
19421947
...processCreateParams(params),
19431948
});

0 commit comments

Comments
 (0)