Skip to content

Commit bf5f0ae

Browse files
authored
Schema: Fix BigIntFromNumber to enforce upper and lower bounds (#4301)
1 parent f474678 commit bf5f0ae

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

.changeset/stupid-days-hear.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
"effect": patch
3+
---
4+
5+
Schema: Fix `BigIntFromNumber` to enforce upper and lower bounds.
6+
7+
This update ensures the `BigIntFromNumber` schema adheres to safe integer limits by applying the following bounds:
8+
9+
```diff
10+
BigIntFromSelf
11+
+ .pipe(
12+
+ betweenBigInt(
13+
+ BigInt(Number.MIN_SAFE_INTEGER),
14+
+ BigInt(Number.MAX_SAFE_INTEGER)
15+
+ )
16+
+ )
17+
```

packages/effect/src/Schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5546,7 +5546,7 @@ export const NonNegativeBigInt: filter<Schema<bigint, string>> = BigInt$.pipe(
55465546
*/
55475547
export class BigIntFromNumber extends transformOrFail(
55485548
Number$.annotations({ description: "a number to be decoded into a bigint" }),
5549-
BigIntFromSelf,
5549+
BigIntFromSelf.pipe(betweenBigInt(BigInt(Number.MIN_SAFE_INTEGER), BigInt(Number.MAX_SAFE_INTEGER))),
55505550
{
55515551
strict: true,
55525552
decode: (n, _, ast) =>

packages/effect/test/Schema/Schema/BigInt/BigIntFromNumber.test.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import { describe, it } from "vitest"
55
describe("BigIntFromNumber", () => {
66
const schema = S.BigIntFromNumber
77

8-
it.todo("test roundtrip consistency", () => {
8+
it("test roundtrip consistency", () => {
99
Util.assertions.testRoundtripConsistency(schema)
1010
})
1111

12-
it("Decoder", async () => {
12+
it("decoding", async () => {
1313
await Util.assertions.decoding.succeed(schema, 0, 0n)
1414
await Util.assertions.decoding.succeed(schema, -0, -0n)
1515
await Util.assertions.decoding.succeed(schema, 1, 1n)
@@ -44,22 +44,26 @@ describe("BigIntFromNumber", () => {
4444
)
4545
})
4646

47-
it("Encoder", async () => {
47+
it("encoding", async () => {
4848
await Util.assertions.encoding.succeed(schema, 1n, 1)
4949

5050
await Util.assertions.encoding.fail(
5151
schema,
5252
BigInt(Number.MAX_SAFE_INTEGER) + 1n,
5353
`BigIntFromNumber
54-
└─ Transformation process failure
55-
└─ Unable to encode 9007199254740992n into a number`
54+
└─ Type side transformation failure
55+
└─ betweenBigInt(-9007199254740991, 9007199254740991)
56+
└─ Predicate refinement failure
57+
└─ Expected a bigint between -9007199254740991n and 9007199254740991n, actual 9007199254740992n`
5658
)
5759
await Util.assertions.encoding.fail(
5860
schema,
5961
BigInt(Number.MIN_SAFE_INTEGER) - 1n,
6062
`BigIntFromNumber
61-
└─ Transformation process failure
62-
└─ Unable to encode -9007199254740992n into a number`
63+
└─ Type side transformation failure
64+
└─ betweenBigInt(-9007199254740991, 9007199254740991)
65+
└─ Predicate refinement failure
66+
└─ Expected a bigint between -9007199254740991n and 9007199254740991n, actual -9007199254740992n`
6367
)
6468
})
6569
})

0 commit comments

Comments
 (0)