Skip to content

Commit

Permalink
fix(guard) add coercion to Defs
Browse files Browse the repository at this point in the history
  • Loading branch information
oberbeck committed Nov 19, 2024
1 parent 826ed00 commit bc0ffd2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 30 deletions.
35 changes: 20 additions & 15 deletions deno/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TCoerce extends boolean = false>
extends ZodTypeDef {
checks: ZodStringCheck[];
typeName: ZodFirstPartyTypeKind.ZodString;
coerce: boolean;
coerce: TCoerce;
}

const cuidRegex = /^c[^\s-]{8,}$/i;
Expand Down Expand Up @@ -1237,7 +1238,7 @@ export class ZodString<TCoerce extends boolean = false> extends ZodType<
return new ZodString<TParam["coerce"] extends true ? true : false>({
checks: [],
typeName: ZodFirstPartyTypeKind.ZodString,
coerce: params?.coerce ?? false,
coerce: params?.coerce ?? (false as any), // TODO remove need for 'as any'
...processCreateParams(params),
});
};
Expand Down Expand Up @@ -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<TCoerce extends boolean = false>
extends ZodTypeDef {
checks: ZodNumberCheck[];
typeName: ZodFirstPartyTypeKind.ZodNumber;
coerce: boolean;
coerce: TCoerce;
}

export class ZodNumber<TCoerce extends boolean = false> extends ZodType<
Expand Down Expand Up @@ -1373,7 +1375,7 @@ export class ZodNumber<TCoerce extends boolean = false> extends ZodType<
return new ZodNumber<TParam["coerce"] extends true ? true : false>({
checks: [],
typeName: ZodFirstPartyTypeKind.ZodNumber,
coerce: params?.coerce || false,
coerce: params?.coerce ?? (false as any), // TODO remove need for 'as any'
...processCreateParams(params),
});
};
Expand Down Expand Up @@ -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<TCoerce extends boolean = false>
extends ZodTypeDef {
checks: ZodBigIntCheck[];
typeName: ZodFirstPartyTypeKind.ZodBigInt;
coerce: boolean;
coerce: TCoerce;
}

export class ZodBigInt<TCoerce extends boolean = false> extends ZodType<
Expand Down Expand Up @@ -1640,7 +1643,7 @@ export class ZodBigInt<TCoerce extends boolean = false> extends ZodType<
return new ZodBigInt<TParam["coerce"] extends true ? true : false>({
checks: [],
typeName: ZodFirstPartyTypeKind.ZodBigInt,
coerce: params?.coerce ?? false,
coerce: params?.coerce ?? (false as any), // TODO remove need for 'as any'
...processCreateParams(params),
});
};
Expand Down Expand Up @@ -1762,9 +1765,10 @@ export class ZodBigInt<TCoerce extends boolean = false> extends ZodType<
////////// ///////////
//////////////////////////////////////////
//////////////////////////////////////////
export interface ZodBooleanDef extends ZodTypeDef {
export interface ZodBooleanDef<TCoerce extends boolean = false>
extends ZodTypeDef {
typeName: ZodFirstPartyTypeKind.ZodBoolean;
coerce: boolean;
coerce: TCoerce;
}

export class ZodBoolean<TCoerce extends boolean = false> extends ZodType<
Expand Down Expand Up @@ -1795,7 +1799,7 @@ export class ZodBoolean<TCoerce extends boolean = false> extends ZodType<
): ZodBoolean<TParam["coerce"] extends true ? true : false> => {
return new ZodBoolean<TParam["coerce"] extends true ? true : false>({
typeName: ZodFirstPartyTypeKind.ZodBoolean,
coerce: params?.coerce || false,
coerce: params?.coerce ?? (false as any), // TODO remove need for 'as any'
...processCreateParams(params),
});
};
Expand All @@ -1811,9 +1815,10 @@ export class ZodBoolean<TCoerce extends boolean = false> 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<TCoerce extends boolean = false>
extends ZodTypeDef {
checks: ZodDateCheck[];
coerce: boolean;
coerce: TCoerce;
typeName: ZodFirstPartyTypeKind.ZodDate;
}

Expand Down Expand Up @@ -1937,7 +1942,7 @@ export class ZodDate<TCoerce extends boolean = false> extends ZodType<
): ZodDate<TParam["coerce"] extends true ? true : false> => {
return new ZodDate<TParam["coerce"] extends true ? true : false>({
checks: [],
coerce: params?.coerce || false,
coerce: params?.coerce ?? (false as any), // TODO remove need for 'as any'
typeName: ZodFirstPartyTypeKind.ZodDate,
...processCreateParams(params),
});
Expand Down
35 changes: 20 additions & 15 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TCoerce extends boolean = false>
extends ZodTypeDef {
checks: ZodStringCheck[];
typeName: ZodFirstPartyTypeKind.ZodString;
coerce: boolean;
coerce: TCoerce;
}

const cuidRegex = /^c[^\s-]{8,}$/i;
Expand Down Expand Up @@ -1237,7 +1238,7 @@ export class ZodString<TCoerce extends boolean = false> extends ZodType<
return new ZodString<TParam["coerce"] extends true ? true : false>({
checks: [],
typeName: ZodFirstPartyTypeKind.ZodString,
coerce: params?.coerce ?? false,
coerce: params?.coerce ?? (false as any), // TODO remove need for 'as any'
...processCreateParams(params),
});
};
Expand Down Expand Up @@ -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<TCoerce extends boolean = false>
extends ZodTypeDef {
checks: ZodNumberCheck[];
typeName: ZodFirstPartyTypeKind.ZodNumber;
coerce: boolean;
coerce: TCoerce;
}

export class ZodNumber<TCoerce extends boolean = false> extends ZodType<
Expand Down Expand Up @@ -1373,7 +1375,7 @@ export class ZodNumber<TCoerce extends boolean = false> extends ZodType<
return new ZodNumber<TParam["coerce"] extends true ? true : false>({
checks: [],
typeName: ZodFirstPartyTypeKind.ZodNumber,
coerce: params?.coerce || false,
coerce: params?.coerce ?? (false as any), // TODO remove need for 'as any'
...processCreateParams(params),
});
};
Expand Down Expand Up @@ -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<TCoerce extends boolean = false>
extends ZodTypeDef {
checks: ZodBigIntCheck[];
typeName: ZodFirstPartyTypeKind.ZodBigInt;
coerce: boolean;
coerce: TCoerce;
}

export class ZodBigInt<TCoerce extends boolean = false> extends ZodType<
Expand Down Expand Up @@ -1640,7 +1643,7 @@ export class ZodBigInt<TCoerce extends boolean = false> extends ZodType<
return new ZodBigInt<TParam["coerce"] extends true ? true : false>({
checks: [],
typeName: ZodFirstPartyTypeKind.ZodBigInt,
coerce: params?.coerce ?? false,
coerce: params?.coerce ?? (false as any), // TODO remove need for 'as any'
...processCreateParams(params),
});
};
Expand Down Expand Up @@ -1762,9 +1765,10 @@ export class ZodBigInt<TCoerce extends boolean = false> extends ZodType<
////////// ///////////
//////////////////////////////////////////
//////////////////////////////////////////
export interface ZodBooleanDef extends ZodTypeDef {
export interface ZodBooleanDef<TCoerce extends boolean = false>
extends ZodTypeDef {
typeName: ZodFirstPartyTypeKind.ZodBoolean;
coerce: boolean;
coerce: TCoerce;
}

export class ZodBoolean<TCoerce extends boolean = false> extends ZodType<
Expand Down Expand Up @@ -1795,7 +1799,7 @@ export class ZodBoolean<TCoerce extends boolean = false> extends ZodType<
): ZodBoolean<TParam["coerce"] extends true ? true : false> => {
return new ZodBoolean<TParam["coerce"] extends true ? true : false>({
typeName: ZodFirstPartyTypeKind.ZodBoolean,
coerce: params?.coerce || false,
coerce: params?.coerce ?? (false as any), // TODO remove need for 'as any'
...processCreateParams(params),
});
};
Expand All @@ -1811,9 +1815,10 @@ export class ZodBoolean<TCoerce extends boolean = false> 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<TCoerce extends boolean = false>
extends ZodTypeDef {
checks: ZodDateCheck[];
coerce: boolean;
coerce: TCoerce;
typeName: ZodFirstPartyTypeKind.ZodDate;
}

Expand Down Expand Up @@ -1937,7 +1942,7 @@ export class ZodDate<TCoerce extends boolean = false> extends ZodType<
): ZodDate<TParam["coerce"] extends true ? true : false> => {
return new ZodDate<TParam["coerce"] extends true ? true : false>({
checks: [],
coerce: params?.coerce || false,
coerce: params?.coerce ?? (false as any), // TODO remove need for 'as any'
typeName: ZodFirstPartyTypeKind.ZodDate,
...processCreateParams(params),
});
Expand Down

0 comments on commit bc0ffd2

Please sign in to comment.