Files
gemini-cli/packages/core/src/utils/schemaValidator.test.ts
geoffdowns 4f49341ce9 relax JSON schema validation (#9332)
Co-authored-by: Tommaso Sciortino <sciortino@gmail.com>
2025-09-24 16:55:32 +00:00

126 lines
2.9 KiB
TypeScript

/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { describe, expect, it } from 'vitest';
import { SchemaValidator } from './schemaValidator.js';
describe('SchemaValidator', () => {
it('should allow any params if schema is undefined', () => {
const params = {
foo: 'bar',
};
expect(SchemaValidator.validate(undefined, params)).toBeNull();
});
it('rejects null params', () => {
const schema = {
type: 'object',
properties: {
foo: {
type: 'string',
},
},
};
expect(SchemaValidator.validate(schema, null)).toBe(
'Value of params must be an object',
);
});
it('rejects params that are not objects', () => {
const schema = {
type: 'object',
properties: {
foo: {
type: 'string',
},
},
};
expect(SchemaValidator.validate(schema, 'not an object')).toBe(
'Value of params must be an object',
);
});
it('allows schema with extra properties', () => {
const schema = {
type: 'object',
properties: {
example_enum: {
type: 'string',
enum: ['FOO', 'BAR'],
// enum-descriptions is not part of the JSON schema spec.
// This test verifies that the SchemaValidator allows the
// use of extra keywords, like this one, in the schema.
'enum-descriptions': ['a foo', 'a bar'],
},
},
};
const params = {
example_enum: 'BAR',
};
expect(SchemaValidator.validate(schema, params)).toBeNull();
});
it('allows custom format values', () => {
const schema = {
type: 'object',
properties: {
duration: {
type: 'string',
// See: https://cloud.google.com/docs/discovery/type-format
format: 'google-duration',
},
mask: {
type: 'string',
format: 'google-fieldmask',
},
foo: {
type: 'string',
format: 'something-totally-custom',
},
},
};
const params = {
duration: '10s',
mask: 'foo.bar,biz.baz',
foo: 'some value',
};
expect(SchemaValidator.validate(schema, params)).toBeNull();
});
it('allows valid values for known formats', () => {
const schema = {
type: 'object',
properties: {
today: {
type: 'string',
format: 'date',
},
},
};
const params = {
today: '2025-04-08',
};
expect(SchemaValidator.validate(schema, params)).toBeNull();
});
it('rejects invalid values for known formats', () => {
const schema = {
type: 'object',
properties: {
today: {
type: 'string',
format: 'date',
},
},
};
const params = {
today: 'this is not a date',
};
expect(SchemaValidator.validate(schema, params)).not.toBeNull();
});
});