Skip to content

Commit 1d0a4b9

Browse files
authored
fix: bigint coerce crash (#3822)
* fix: cast invalid bigint inputs to undefined * fix: cast invalid bigint values to undefined * refactor: return invalid type on coerce error
1 parent fba6d02 commit 1d0a4b9

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

deno/lib/types.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1551,17 +1551,15 @@ export interface ZodBigIntDef extends ZodTypeDef {
15511551
export class ZodBigInt extends ZodType<bigint, ZodBigIntDef, bigint> {
15521552
_parse(input: ParseInput): ParseReturnType<bigint> {
15531553
if (this._def.coerce) {
1554-
input.data = BigInt(input.data);
1554+
try {
1555+
input.data = BigInt(input.data);
1556+
} catch {
1557+
return this._getInvalidInput(input);
1558+
}
15551559
}
15561560
const parsedType = this._getType(input);
15571561
if (parsedType !== ZodParsedType.bigint) {
1558-
const ctx = this._getOrReturnCtx(input);
1559-
addIssueToContext(ctx, {
1560-
code: ZodIssueCode.invalid_type,
1561-
expected: ZodParsedType.bigint,
1562-
received: ctx.parsedType,
1563-
});
1564-
return INVALID;
1562+
return this._getInvalidInput(input);
15651563
}
15661564

15671565
let ctx: undefined | ParseContext = undefined;
@@ -1616,6 +1614,16 @@ export class ZodBigInt extends ZodType<bigint, ZodBigIntDef, bigint> {
16161614
return { status: status.value, value: input.data };
16171615
}
16181616

1617+
_getInvalidInput(input: ParseInput) {
1618+
const ctx = this._getOrReturnCtx(input);
1619+
addIssueToContext(ctx, {
1620+
code: ZodIssueCode.invalid_type,
1621+
expected: ZodParsedType.bigint,
1622+
received: ctx.parsedType,
1623+
});
1624+
return INVALID;
1625+
}
1626+
16191627
static create = (
16201628
params?: RawCreateParams & { coerce?: boolean }
16211629
): ZodBigInt => {

src/types.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1551,17 +1551,15 @@ export interface ZodBigIntDef extends ZodTypeDef {
15511551
export class ZodBigInt extends ZodType<bigint, ZodBigIntDef, bigint> {
15521552
_parse(input: ParseInput): ParseReturnType<bigint> {
15531553
if (this._def.coerce) {
1554-
input.data = BigInt(input.data);
1554+
try {
1555+
input.data = BigInt(input.data);
1556+
} catch {
1557+
return this._getInvalidInput(input);
1558+
}
15551559
}
15561560
const parsedType = this._getType(input);
15571561
if (parsedType !== ZodParsedType.bigint) {
1558-
const ctx = this._getOrReturnCtx(input);
1559-
addIssueToContext(ctx, {
1560-
code: ZodIssueCode.invalid_type,
1561-
expected: ZodParsedType.bigint,
1562-
received: ctx.parsedType,
1563-
});
1564-
return INVALID;
1562+
return this._getInvalidInput(input);
15651563
}
15661564

15671565
let ctx: undefined | ParseContext = undefined;
@@ -1616,6 +1614,16 @@ export class ZodBigInt extends ZodType<bigint, ZodBigIntDef, bigint> {
16161614
return { status: status.value, value: input.data };
16171615
}
16181616

1617+
_getInvalidInput(input: ParseInput) {
1618+
const ctx = this._getOrReturnCtx(input);
1619+
addIssueToContext(ctx, {
1620+
code: ZodIssueCode.invalid_type,
1621+
expected: ZodParsedType.bigint,
1622+
received: ctx.parsedType,
1623+
});
1624+
return INVALID;
1625+
}
1626+
16191627
static create = (
16201628
params?: RawCreateParams & { coerce?: boolean }
16211629
): ZodBigInt => {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy