feat(core): add draft-2020-12 JSON Schema support with lenient fallback (#15060)

Co-authored-by: A.K.M. Adib <adibakm@google.com>
Co-authored-by: Jack Wotherspoon <jackwoth@google.com>
This commit is contained in:
Alexander Farber
2026-02-03 16:49:08 +01:00
committed by GitHub
parent 1d045792ce
commit 19b1a74c99
2 changed files with 176 additions and 18 deletions
@@ -122,4 +122,93 @@ describe('SchemaValidator', () => {
};
expect(SchemaValidator.validate(schema, params)).not.toBeNull();
});
it('allows schemas with draft-07 $schema property', () => {
const schema = {
type: 'object',
properties: { name: { type: 'string' } },
$schema: 'http://json-schema.org/draft-07/schema#',
};
const params = { name: 'test' };
expect(SchemaValidator.validate(schema, params)).toBeNull();
});
it('allows schemas with unrecognized $schema versions (lenient fallback)', () => {
// Future-proof: any unrecognized schema version should skip validation
// with a warning rather than failing
const schema = {
type: 'object',
properties: { name: { type: 'string' } },
$schema: 'https://json-schema.org/draft/2030-99/schema',
};
const params = { name: 'test' };
expect(SchemaValidator.validate(schema, params)).toBeNull();
});
describe('JSON Schema draft-2020-12 support', () => {
it('validates params against draft-2020-12 schema', () => {
const schema = {
$schema: 'https://json-schema.org/draft/2020-12/schema',
type: 'object',
properties: {
message: {
type: 'string',
},
},
required: ['message'],
};
// Valid data should pass
expect(SchemaValidator.validate(schema, { message: 'hello' })).toBeNull();
// Invalid data should fail (proves validation actually works)
expect(SchemaValidator.validate(schema, { message: 123 })).not.toBeNull();
});
it('validates draft-2020-12 schema with prefixItems', () => {
// prefixItems is a draft-2020-12 feature (replaces tuple validation)
const schema = {
$schema: 'https://json-schema.org/draft/2020-12/schema',
type: 'object',
properties: {
coords: {
type: 'array',
prefixItems: [{ type: 'number' }, { type: 'number' }],
items: false,
},
},
};
// Valid: exactly 2 numbers
expect(SchemaValidator.validate(schema, { coords: [1, 2] })).toBeNull();
// Invalid: 3 items when items: false
expect(
SchemaValidator.validate(schema, { coords: [1, 2, 3] }),
).not.toBeNull();
});
it('validates draft-2020-12 schema with $defs', () => {
// draft-2020-12 uses $defs instead of definitions
const schema = {
$schema: 'https://json-schema.org/draft/2020-12/schema',
type: 'object',
$defs: {
ChatRole: {
type: 'string',
enum: ['System', 'User', 'Assistant'],
},
},
properties: {
role: { $ref: '#/$defs/ChatRole' },
},
required: ['role'],
};
// Valid enum value
expect(SchemaValidator.validate(schema, { role: 'User' })).toBeNull();
// Invalid enum value (proves validation works)
expect(
SchemaValidator.validate(schema, { role: 'InvalidRole' }),
).not.toBeNull();
});
});
});