Skip to content

Commit

Permalink
refactor: return invalid type on coerce error
Browse files Browse the repository at this point in the history
  • Loading branch information
kodemon committed Nov 24, 2024
1 parent 4d05e52 commit 644af27
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
20 changes: 12 additions & 8 deletions deno/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1552,18 +1552,12 @@ export class ZodBigInt extends ZodType<bigint, ZodBigIntDef, bigint> {
try {
input.data = BigInt(input.data);
} catch {
input.data = undefined;
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 @@ -1618,6 +1612,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
20 changes: 12 additions & 8 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1552,18 +1552,12 @@ export class ZodBigInt extends ZodType<bigint, ZodBigIntDef, bigint> {
try {
input.data = BigInt(input.data);
} catch {
input.data = undefined;
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 @@ -1618,6 +1612,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 644af27

Please sign in to comment.