forked from colinhacks/zod
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add new ZodType definitions for
z.coerce
types
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
1 parent
579cf75
commit 35e228f
Showing
1 changed file
with
26 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -897,6 +897,10 @@ export class ZodString extends ZodType<string, ZodStringDef> { | |
}; | ||
} | ||
|
||
export declare class ZodCoercedString extends ZodString { | ||
readonly _input: any | ||
} | ||
|
||
///////////////////////////////////////// | ||
///////////////////////////////////////// | ||
////////// ////////// | ||
|
@@ -1181,6 +1185,10 @@ export class ZodNumber extends ZodType<number, ZodNumberDef> { | |
} | ||
} | ||
|
||
export declare class ZodCoercedNumber extends ZodNumber { | ||
readonly _input: any | ||
} | ||
|
||
///////////////////////////////////////// | ||
///////////////////////////////////////// | ||
////////// ////////// | ||
|
@@ -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.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
aleclarson
Author
Owner
|
||
|
||
////////////////////////////////////////// | ||
////////////////////////////////////////// | ||
////////// /////////// | ||
|
@@ -1265,6 +1275,10 @@ export class ZodBoolean extends ZodType<boolean, ZodBooleanDef> { | |
}; | ||
} | ||
|
||
export declare class ZodCoercedBoolean extends ZodBoolean { | ||
readonly _input: any | ||
} | ||
|
||
/////////////////////////////////////// | ||
/////////////////////////////////////// | ||
////////// //////// | ||
|
@@ -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.
Sorry, something went wrong.
aleclarson
Author
Owner
|
||
|
||
//////////////////////////////////////////// | ||
//////////////////////////////////////////// | ||
////////// ////////// | ||
|
@@ -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 { | ||
|
Unfortunately, a
ZodBigInt
type is not assignable to aZodCoercedBigInt
type, or vice versa. This is why I have to use aninterface
type, instead of a subclass.