diff --git a/index.js b/index.js index e692d5e9..aa087805 100644 --- a/index.js +++ b/index.js @@ -946,7 +946,7 @@ function buildMultiTypeSerializer (context, location, input) { } case 'integer': { code += ` - ${statement}(Number.isInteger(${input}) || ${input} === null) { + ${statement}(Number.isInteger(${input}) || typeof ${input} === 'bigint' || ${input} === null) { ${nestedResult} } ` @@ -1214,8 +1214,17 @@ function buildOneOf (context, location, input) { const schemaRef = getValidatorSchemaRef(context, optionLocation) context.validatorSchemaRefs.add(schemaRef) + // AJV does not recognise BigInt as type "integer". + // Pre-route BigInt to the first integer-typed schema so it reaches + // asInteger() (which correctly handles bigint) instead of falling + // through to the TypeError branch. + const isIntegerSchema = optionSchema.type === 'integer' + const condition = isIntegerSchema + ? `typeof ${input} === 'bigint' || validator.validate("${schemaRef}", ${input})` + : `validator.validate("${schemaRef}", ${input})` + code += ` - ${index === 0 ? 'if' : 'else if'}(validator.validate("${schemaRef}", ${input})) { + ${index === 0 ? 'if' : 'else if'}(${condition}) { ${nestedResult} } ` diff --git a/test/typesArray.test.js b/test/typesArray.test.js index dc09d9ba..a9e0b86a 100644 --- a/test/typesArray.test.js +++ b/test/typesArray.test.js @@ -66,6 +66,25 @@ test('possibly nullable integer primitive alternative with null value', (t) => { t.assert.equal(value, '{"data":0}') }) + +test('bigint serialized as integer in type array [integer, null]', (t) => { + t.plan(3) + const schema = { type: 'object', properties: { data: { type: ['integer', 'null'] } } } + const stringify = build(schema) + t.assert.equal(stringify({ data: 12n }), '{"data":12}') + t.assert.equal(stringify({ data: -5n }), '{"data":-5}') + t.assert.equal(stringify({ data: null }), '{"data":null}') +}) + +test('bigint serialized as integer in anyOf union', (t) => { + t.plan(3) + const schema = { type: 'object', properties: { data: { anyOf: [{ type: 'integer' }, { type: 'null' }] } } } + const stringify = build(schema) + t.assert.equal(stringify({ data: 12n }), '{"data":12}') + t.assert.equal(stringify({ data: -5n }), '{"data":-5}') + t.assert.equal(stringify({ data: null }), '{"data":null}') +}) + test('possibly nullable number primitive alternative with null value', (t) => { t.plan(1)