coerceTypes option interfere in the validation with the not keyword
Author: FerNumbersCreated Sep 24, 2026Updated Sep 24, 2026
Labelsbug report
What version of Ajv are you using? Does the issue happen if you use the latest version?
8.16.0
Ajv options object
coerceTypesJSON Schema
{
type: 'object',
properties: { foo: { not: { type: 'string' } } }
}Sample data
{ foo: 123 }Your code
import { describe, test } from 'node:test'
import assert from 'node:assert/strict'
import Ajv from 'ajv/dist/2020.js'
const schema = {
type: 'object',
properties: {
foo: { not: { type: 'string' } }
}
}
describe("Works when coerceTypes isn't around", () => {
const ajv = new Ajv({ /* coerceTypes: true */ })
const validate = ajv.compile(schema)
test('Number is valid', () => {
const data = { foo: 123 }
assert.equal(validate(data), true)
assert.equal(validate.errors, null)
assert.equal(data.foo, 123)
})
test('Boolean is valid', () => {
const data = { foo: true }
assert.equal(validate(data), true)
assert.equal(validate.errors, null)
assert.equal(data.foo, true)
})
})
describe('BUG when coerceTypes is enabled', () => {
const ajv = new Ajv({ coerceTypes: true })
const validate = ajv.compile(schema)
test('Number is valid', () => {
const data = { foo: 123 }
assert.equal(validate(data), true)
assert.equal(validate.errors, null)
assert.equal(data.foo, 123)
})
test('Boolean is valid', () => {
const data = { foo: true }
assert.equal(validate(data), true)
assert.equal(validate.errors, null)
assert.equal(data.foo, true)
})
})What results did you expect?
I expect that coerceTypes don't interfere in the validation with the not keyword
Source: ajv-validator/ajv