Skip to content

Commit bc0ffd2

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

File tree

2 files changed

+40
-30
lines changed

2 files changed

+40
-30
lines changed

deno/lib/types.ts

+20-15
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;
@@ -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,10 +1268,11 @@ 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<
@@ -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,10 +1558,11 @@ 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<
@@ -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,9 +1765,10 @@ 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<
@@ -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,9 +1815,10 @@ 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

@@ -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

+20-15
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;
@@ -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,10 +1268,11 @@ 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<
@@ -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,10 +1558,11 @@ 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<
@@ -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,9 +1765,10 @@ 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<
@@ -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,9 +1815,10 @@ 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

@@ -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)