Skip to content

Commit

Permalink
feat(validation/bigint): allow to cast a number 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 792dcd2 commit 233d206
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Validations/primitives/bigint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ export const bigint: SyncValidation = {
}

/**
* Report error when value is not a bigint and neither a string
* Report error when value is not a bigint and neither a string or a number
*/
if (typeof value !== 'string') {
if (typeof value !== 'string' && typeof value !== 'number') {
errorReporter.report(pointer, RULE_NAME, DEFAULT_MESSAGE, arrayExpressionPointer)
return
}

/**
* Attempt to cast bigint like string to a bigint. In case of
* Attempt to cast string or number to a bigint. In case of
* failure report the validation error
*/
try {
Expand Down
20 changes: 20 additions & 0 deletions test/validations/bigint.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,24 @@ test.group('BigInt', () => {
],
})
})

test('cast number to a valid bigint', ({ assert }) => {
const reporter = new ApiErrorReporter(new MessagesBag({}), false)
let value: any = 21

bigint.validate(value, compile().compiledOptions, {
errorReporter: reporter,
field: 'age',
pointer: 'age',
tip: {},
root: {},
refs: {},
mutate: (newValue) => {
value = newValue
},
})

assert.deepEqual(reporter.toJSON(), { errors: [] })
assert.equal(value, 21n)
})
})

0 comments on commit 233d206

Please sign in to comment.