diff --git a/deno/lib/types.ts b/deno/lib/types.ts index bb2f08519..ed4fdc71a 100644 --- a/deno/lib/types.ts +++ b/deno/lib/types.ts @@ -1549,17 +1549,15 @@ export interface ZodBigIntDef extends ZodTypeDef { export class ZodBigInt extends ZodType { _parse(input: ParseInput): ParseReturnType { 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; @@ -1614,6 +1612,16 @@ export class ZodBigInt extends ZodType { 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 => { diff --git a/src/types.ts b/src/types.ts index 5aa30b900..149f0f749 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1549,17 +1549,15 @@ export interface ZodBigIntDef extends ZodTypeDef { export class ZodBigInt extends ZodType { _parse(input: ParseInput): ParseReturnType { 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; @@ -1614,6 +1612,16 @@ export class ZodBigInt extends ZodType { 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 => {