Skip to content

Commit

Permalink
fix(validation/bigint): cast bigint like string to bigint
Browse files Browse the repository at this point in the history
  • Loading branch information
MaximeMRF authored and RomainLanz committed Nov 16, 2023
1 parent d51a7d3 commit 792dcd2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 22 deletions.
19 changes: 7 additions & 12 deletions src/Validations/primitives/bigint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,15 @@ export const bigint: SyncValidation = {
* Attempt to cast bigint like string to a bigint. In case of
* failure report the validation error
*/
const castedValue = Number(value)
if (isNaN(castedValue)) {
errorReporter.report(pointer, RULE_NAME, DEFAULT_MESSAGE, arrayExpressionPointer)
return
}
try {
const castedValue = BigInt(value)

if (castedValue === Infinity || castedValue === -Infinity) {
/**
* Mutate the value
*/
mutate(castedValue)
} catch (e) {
errorReporter.report(pointer, RULE_NAME, DEFAULT_MESSAGE, arrayExpressionPointer)
return
}

/**
* Mutate the value
*/
mutate(castedValue)
},
}
12 changes: 2 additions & 10 deletions test/validations/bigint.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function compile() {
test.group('BigInt', () => {
validate(bigint, test, 'helloworld', 10n, compile())

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

assert.deepEqual(reporter.toJSON(), {
errors: [
{
field: 'age',
rule: 'bigint',
message: 'bigint validation failed',
},
],
})
assert.deepEqual(reporter.toJSON(), { errors: [] })
})

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

0 comments on commit 792dcd2

Please sign in to comment.