Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit 792dcd2

Browse files
MaximeMRFRomainLanz
authored andcommitted
fix(validation/bigint): cast bigint like string to bigint
1 parent d51a7d3 commit 792dcd2

2 files changed

Lines changed: 9 additions & 22 deletions

File tree

src/Validations/primitives/bigint.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,15 @@ export const bigint: SyncValidation = {
3636
* Attempt to cast bigint like string to a bigint. In case of
3737
* failure report the validation error
3838
*/
39-
const castedValue = Number(value)
40-
if (isNaN(castedValue)) {
41-
errorReporter.report(pointer, RULE_NAME, DEFAULT_MESSAGE, arrayExpressionPointer)
42-
return
43-
}
39+
try {
40+
const castedValue = BigInt(value)
4441

45-
if (castedValue === Infinity || castedValue === -Infinity) {
42+
/**
43+
* Mutate the value
44+
*/
45+
mutate(castedValue)
46+
} catch (e) {
4647
errorReporter.report(pointer, RULE_NAME, DEFAULT_MESSAGE, arrayExpressionPointer)
47-
return
4848
}
49-
50-
/**
51-
* Mutate the value
52-
*/
53-
mutate(castedValue)
5449
},
5550
}

test/validations/bigint.spec.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function compile() {
2121
test.group('BigInt', () => {
2222
validate(bigint, test, 'helloworld', 10n, compile())
2323

24-
test('report error when value is near Infinity', ({ assert }) => {
24+
test('work fine when value is near Infinity', ({ assert }) => {
2525
const reporter = new ApiErrorReporter(new MessagesBag({}), false)
2626
bigint.validate(
2727
'-3177777777777777777777777777777777777777777777777777777777777777777777777770000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999991111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111',
@@ -37,15 +37,7 @@ test.group('BigInt', () => {
3737
}
3838
)
3939

40-
assert.deepEqual(reporter.toJSON(), {
41-
errors: [
42-
{
43-
field: 'age',
44-
rule: 'bigint',
45-
message: 'bigint validation failed',
46-
},
47-
],
48-
})
40+
assert.deepEqual(reporter.toJSON(), { errors: [] })
4941
})
5042

5143
test('report error when value is not a valid bigint', ({ assert }) => {

0 commit comments

Comments
 (0)