diff --git a/src/Validations/primitives/bigint.ts b/src/Validations/primitives/bigint.ts index ab93693..476b064 100644 --- a/src/Validations/primitives/bigint.ts +++ b/src/Validations/primitives/bigint.ts @@ -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 { diff --git a/test/validations/bigint.spec.ts b/test/validations/bigint.spec.ts index 6837d4d..22ffac3 100644 --- a/test/validations/bigint.spec.ts +++ b/test/validations/bigint.spec.ts @@ -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) + }) })