Skip to content

Commit

Permalink
fix: add new ZodType definitions for z.coerce types
Browse files Browse the repository at this point in the history
The new ZodType definitions allow for `z.input` to work as expected with coerced types. I have aligned the `z.input` results to match the respective coercion expressions. For example, z.input<ZodCoercedBigInt> resolves to "string | number | bigint | boolean" because that is what the BigInt constructor accepts, according to its TypeScript definition.
  • Loading branch information
aleclarson committed Jan 16, 2023
1 parent 579cf75 commit 35e228f
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,10 @@ export class ZodString extends ZodType<string, ZodStringDef> {
};
}

export declare class ZodCoercedString extends ZodString {
readonly _input: any
}

/////////////////////////////////////////
/////////////////////////////////////////
////////// //////////
Expand Down Expand Up @@ -1181,6 +1185,10 @@ export class ZodNumber extends ZodType<number, ZodNumberDef> {
}
}

export declare class ZodCoercedNumber extends ZodNumber {
readonly _input: any
}

/////////////////////////////////////////
/////////////////////////////////////////
////////// //////////
Expand Down Expand Up @@ -1223,6 +1231,8 @@ export class ZodBigInt extends ZodType<bigint, ZodBigIntDef> {
};
}

export interface ZodCoercedBigInt extends Omit<ZodBigInt, keyof ZodType>, ZodType<bigint, ZodBigIntDef, string | number | bigint | boolean> {}

This comment has been minimized.

Copy link
@aleclarson

aleclarson Jan 16, 2023

Author Owner

Unfortunately, a ZodBigInt type is not assignable to a ZodCoercedBigInt type, or vice versa. This is why I have to use an interface type, instead of a subclass.

This comment has been minimized.

Copy link
@aleclarson

aleclarson Jan 16, 2023

Author Owner

The Omit<ZodBigInt, keyof ZodType> part is future-proofing, in case methods are added to the ZodBigInt class later on.


//////////////////////////////////////////
//////////////////////////////////////////
////////// ///////////
Expand Down Expand Up @@ -1265,6 +1275,10 @@ export class ZodBoolean extends ZodType<boolean, ZodBooleanDef> {
};
}

export declare class ZodCoercedBoolean extends ZodBoolean {
readonly _input: any
}

///////////////////////////////////////
///////////////////////////////////////
////////// ////////
Expand Down Expand Up @@ -1404,6 +1418,8 @@ export class ZodDate extends ZodType<Date, ZodDateDef> {
};
}

export interface ZodCoercedDate extends Omit<ZodDate, keyof ZodType>, ZodType<Date, ZodDateDef, string | number | Date> {}

This comment has been minimized.

Copy link
@aleclarson

aleclarson Jan 16, 2023

Author Owner

In this case, ZodCoercedDate is assignable to ZodDate, but not vice versa.


////////////////////////////////////////////
////////////////////////////////////////////
////////// //////////
Expand Down Expand Up @@ -4437,16 +4453,16 @@ const onumber = () => numberType().optional();
const oboolean = () => booleanType().optional();

export const coerce = {
string: ((arg) =>
ZodString.create({ ...arg, coerce: true })) as typeof ZodString["create"],
number: ((arg) =>
ZodNumber.create({ ...arg, coerce: true })) as typeof ZodNumber["create"],
boolean: ((arg) =>
ZodBoolean.create({ ...arg, coerce: true })) as typeof ZodBoolean["create"],
bigint: ((arg) =>
ZodBigInt.create({ ...arg, coerce: true })) as typeof ZodBigInt["create"],
date: ((arg) =>
ZodDate.create({ ...arg, coerce: true })) as typeof ZodDate["create"],
string: ((params?: RawCreateParams) =>
ZodString.create({ ...params, coerce: true }) as ZodCoercedString),
number: ((params?: RawCreateParams) =>
ZodNumber.create({ ...params, coerce: true }) as ZodCoercedNumber),
boolean: ((params?: RawCreateParams) =>
ZodBoolean.create({ ...params, coerce: true }) as ZodCoercedBoolean),
bigint: ((params?: RawCreateParams) =>
ZodBigInt.create({ ...params, coerce: true }) as ZodCoercedBigInt),
date: ((params?: RawCreateParams) =>
ZodDate.create({ ...params, coerce: true }) as ZodCoercedDate),
};

export {
Expand Down

0 comments on commit 35e228f

Please sign in to comment.