Skip to content

Commit

Permalink
fix: bigint coerce crash (#3822)
Browse files Browse the repository at this point in the history
* fix: cast invalid bigint inputs to undefined

* fix: cast invalid bigint values to undefined

* refactor: return invalid type on coerce error
  • Loading branch information
kodemon authored Dec 10, 2024
1 parent fba6d02 commit 1d0a4b9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
24 changes: 16 additions & 8 deletions deno/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1551,17 +1551,15 @@ export interface ZodBigIntDef extends ZodTypeDef {
export class ZodBigInt extends ZodType<bigint, ZodBigIntDef, bigint> {
_parse(input: ParseInput): ParseReturnType<bigint> {
if (this._def.coerce) {
input.data = BigInt(input.data);
try {
input.data = BigInt(input.data);
} catch {
return this._getInvalidInput(input);
}
}
const parsedType = this._getType(input);
if (parsedType !== ZodParsedType.bigint) {
const ctx = this._getOrReturnCtx(input);
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_type,
expected: ZodParsedType.bigint,
received: ctx.parsedType,
});
return INVALID;
return this._getInvalidInput(input);
}

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

_getInvalidInput(input: ParseInput) {
const ctx = this._getOrReturnCtx(input);
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_type,
expected: ZodParsedType.bigint,
received: ctx.parsedType,
});
return INVALID;
}

static create = (
params?: RawCreateParams & { coerce?: boolean }
): ZodBigInt => {
Expand Down
24 changes: 16 additions & 8 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1551,17 +1551,15 @@ export interface ZodBigIntDef extends ZodTypeDef {
export class ZodBigInt extends ZodType<bigint, ZodBigIntDef, bigint> {
_parse(input: ParseInput): ParseReturnType<bigint> {
if (this._def.coerce) {
input.data = BigInt(input.data);
try {
input.data = BigInt(input.data);
} catch {
return this._getInvalidInput(input);
}
}
const parsedType = this._getType(input);
if (parsedType !== ZodParsedType.bigint) {
const ctx = this._getOrReturnCtx(input);
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_type,
expected: ZodParsedType.bigint,
received: ctx.parsedType,
});
return INVALID;
return this._getInvalidInput(input);
}

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

_getInvalidInput(input: ParseInput) {
const ctx = this._getOrReturnCtx(input);
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_type,
expected: ZodParsedType.bigint,
received: ctx.parsedType,
});
return INVALID;
}

static create = (
params?: RawCreateParams & { coerce?: boolean }
): ZodBigInt => {
Expand Down

0 comments on commit 1d0a4b9

Please sign in to comment.