import { Ajv2019 as Ajv } from 'ajv/dist/2019.js'; import { describe, expect, it } from 'vitest'; import { inputSchema } from '@apify/input_schema'; /** * Temporarily replace console.warn with implementation that throws error instead * of logging the warning. * * @returns {() => void} Function that will turn this behavior off. */ const setThrowErrorOnConsoleWarn = (): (() => void) => { const consoleWarn = console.warn; console.warn = () => { throw new Error('Console.warn has been called!'); }; const turnOff = () => { console.warn = consoleWarn; }; return turnOff; }; describe('input_schema.json', () => { const ajv = new Ajv({ strict: false }); describe('type any', () => { it('should allow to compile only a valid type any', () => { // Valid one. if ( !ajv.validate(inputSchema, { title: 'Test input schema', type: 'object', schemaVersion: 1, properties: { myField: { title: 'Field title', type: ['object', 'array', 'string', 'integer', 'number', 'boolean'], nullable: false, description: 'Some description ...', editor: 'json', }, }, }) ) throw new Error(ajv.errorsText()); // Nonexisting type. if ( ajv.validate(inputSchema, { title: 'Test input schema', type: 'object', schemaVersion: 1, properties: { myField: { title: 'Field title', type: ['array', 'nonexisting'], nullable: false, description: 'Some description ...', editor: 'json', }, }, }) ) throw new Error('Input field definition with onexisting type should have failed!'); // Missing type. if ( ajv.validate(inputSchema, { title: 'Test input schema', type: 'object', schemaVersion: 1, properties: { myField: { title: 'Field title', type: [], nullable: false, description: 'Some description ...', editor: 'json', }, }, }) ) throw new Error('Input field definition that misses type should have failed!'); // Duplicate types. if ( ajv.validate(inputSchema, { title: 'Test input schema', type: 'object', schemaVersion: 1, properties: { myField: { title: 'Field title', type: ['string', 'integer', 'integer'], nullable: false, description: 'Some description ...', editor: 'json', }, }, }) ) throw new Error('Input field definition that duplicate types should have failed!'); }); it('should allow all the needed types', () => { const schema = { title: 'Test input schema', type: 'object', schemaVersion: 1, properties: { myField: { title: 'Field title', type: ['object', 'array', 'string', 'integer', 'number', 'boolean'], nullable: false, description: 'Some description ...', editor: 'json', default: { foo: 'bar' }, prefill: [1, 2, 3], example: true, }, }, }; const test = (val: unknown) => { if (!ajv.validate(schema, { myField: val })) throw new Error(ajv.errorsText()); }; test({ foo: 'bar' }); test([1, 2, 'foo']); test('something'); test(324567); test(true); }); it('should work with a subset (integer and array is not allowed)', () => { const schema = { title: 'Test input schema', type: 'object', schemaVersion: 1, properties: { myField: { title: 'Field title', type: ['object', 'string', 'boolean'], nullable: false, description: 'Some description ...', editor: 'json', }, }, }; const test = (val: unknown) => { if (!ajv.validate(schema, { myField: val })) throw new Error(ajv.errorsText()); }; test({ foo: 'bar' }); expect(() => test([1, 2, 'foo'])).toThrow('data/myField must be object,string,boolean'); test('something'); expect(() => test(324567)).toThrow('data/myField must be object,string,boolean'); test(true); }); it('should not generate console warnings for schema containing `id` (1)', () => { const turnOffConsoleWarnErrors = setThrowErrorOnConsoleWarn(); expect(() => console.warn('OK')).toThrow('Console.warn has been called!'); const schema = { $id: 'http://mydomain/schemas/node.json', type: 'object', properties: { id: { description: 'The unique identifier for a node', type: 'string', }, }, required: ['id'], example: { id: 'test', }, }; const test = (data: unknown) => { if (!ajv.validate(schema, data)) throw new Error(ajv.errorsText()); }; test({ id: 'test' }); turnOffConsoleWarnErrors(); }); it('should not generate console warnings for schema containing `id` (2)', () => { const turnOffConsoleWarnErrors = setThrowErrorOnConsoleWarn(); expect(() => console.warn('OK')).toThrow('Console.warn has been called!'); const schema = { title: 'Status dashboard', type: 'object', schemaVersion: 1, properties: { test: { title: 'Test', description: '', type: 'object', editor: 'json', prefill: { id: 'KubaTestPrefill', apiKey: 'aaa', workspace: 'aaa', title: 'aaaa', }, }, }, }; const test = (data: unknown) => { if (!ajv.validate(schema, data)) throw new Error(ajv.errorsText()); }; test({ test: { id: 1 } }); turnOffConsoleWarnErrors(); }); describe('special cases for number and integer types', () => { const isSchemaValid = (fields: object, type: 'number' | 'integer') => { return ajv.validate(inputSchema, { title: 'Test input schema', type: 'object', schemaVersion: 1, properties: { myField: { title: 'Field title', description: 'My test field', type, editor: 'number', ...fields, }, }, }); }; it('should allow all number/integer specific fields', () => { ['minimum', 'maximum', 'default', 'prefill', 'example'].forEach((intField) => { expect(isSchemaValid({ [intField]: 10 }, 'integer')).toBe(true); expect(isSchemaValid({ [intField]: 10.0 }, 'integer')).toBe(true); expect(isSchemaValid({ [intField]: 10.5 }, 'integer')).toBe(false); expect(isSchemaValid({ [intField]: 10 }, 'number')).toBe(true); expect(isSchemaValid({ [intField]: 10.0 }, 'number')).toBe(true); expect(isSchemaValid({ [intField]: 10.5 }, 'number')).toBe(true); }); }); it('should allow only number editor', () => { expect(isSchemaValid({ editor: 'number' }, 'integer')).toBe(true); expect(isSchemaValid({ editor: 'textfield' }, 'integer')).toBe(false); expect(isSchemaValid({ editor: 'number' }, 'number')).toBe(true); expect(isSchemaValid({ editor: 'textfield' }, 'number')).toBe(false); }); }); describe('special cases for isSecret property', () => { const isSchemaValid = (fields: object, isSecret?: boolean) => { return ajv.validate(inputSchema, { title: 'Test input schema', type: 'object', schemaVersion: 1, properties: { myField: { title: 'Field title', description: 'My test field', type: 'string', editor: 'textfield', isSecret, ...fields, }, }, }); }; it('string field should not allow all editors', () => { ['textfield', 'textarea', 'hidden'].forEach((editor) => { expect(isSchemaValid({ editor }, true)).toBe(true); }); ['javascript', 'python'].forEach((editor) => { expect(isSchemaValid({ editor }, true)).toBe(false); }); }); it('string field should not allow some fields', () => { ['minLength', 'maxLength'].forEach((intField) => { expect(isSchemaValid({ [intField]: 10 }, true)).toBe(false); }); ['default', 'pattern'].forEach((stringField) => { expect(isSchemaValid({ [stringField]: 'bla' }, true)).toBe(false); }); }); it('should allow only string, object and array type', () => { [ { type: 'string', editor: 'textfield' }, { type: 'object', editor: 'json' }, { type: 'array', editor: 'json' }, ].forEach((fields) => { expect(isSchemaValid(fields, true)).toBe(true); }); [{ type: 'boolean' }, { type: 'integer' }, { type: 'number' }].forEach((fields) => { expect(isSchemaValid(fields, true)).toBe(false); }); }); it('should work without isSecret with all editors and properties', () => { expect( ajv.validate(inputSchema, { title: 'Test input schema', type: 'object', schemaVersion: 1, properties: { myField: { title: 'Field title', description: 'My test field', type: 'string', editor: 'textarea', isSecret: false, minLength: 2, maxLength: 100, default: 'blablablablabla', prefill: 'blablablablablablablablablablabla', }, }, }), ).toBe(true); expect( ajv.validate(inputSchema, { title: 'Test input schema', type: 'object', schemaVersion: 1, properties: { myField: { title: 'Field title', description: 'My test field', type: 'string', editor: 'textarea', isSecret: false, minLength: 2, maxLength: 100, default: 'blablablablabla', prefill: 'blablablablablablablablablablabla', bla: 'bla', // Validation failed because additional property }, }, }), ).toBe(false); }); }); describe('special cases for isSecret object type', () => { const isSchemaValid = (fields: object, isSecret?: boolean) => { return ajv.validate(inputSchema, { title: 'Test input schema', type: 'object', schemaVersion: 1, properties: { myField: { title: 'Field title', description: 'My test field', type: 'object', isSecret, ...fields, }, }, }); }; it('should not allow all editors', () => { ['json', 'hidden'].forEach((editor) => { expect(isSchemaValid({ editor }, true)).toBe(true); }); ['proxy'].forEach((editor) => { expect(isSchemaValid({ editor }, true)).toBe(false); }); }); it('should not allow some fields', () => { ['minProperties', 'maxProperties'].forEach((intField) => { expect(isSchemaValid({ [intField]: 10 }, true)).toBe(false); }); ['prefill', 'example'].forEach((stringField) => { expect(isSchemaValid({ [stringField]: 'bla' }, true)).toBe(false); }); }); it('should work without isSecret with all editors and properties', () => { expect( ajv.validate(inputSchema, { title: 'Test input schema', type: 'object', schemaVersion: 1, properties: { myField: { title: 'Field title', description: 'My test field', type: 'object', editor: 'json', isSecret: false, minProperties: 2, maxProperties: 100, default: { key: 'value' }, prefill: { key: 'value', key2: 'value2' }, }, }, }), ).toBe(true); expect( ajv.validate(inputSchema, { title: 'Test input schema', type: 'object', schemaVersion: 1, properties: { myField: { title: 'Field title', description: 'My test field', type: 'object', editor: 'json', isSecret: false, minProperties: 2, maxProperties: 100, default: { key: 'value' }, prefill: { key: 'value', key2: 'value2' }, bla: 'bla', // Validation failed because additional property }, }, }), ).toBe(false); }); }); describe('special cases for datepicker editor type', () => { it('should accept dateType field omitted', () => { expect( ajv.validate(inputSchema, { title: 'Test input schema', type: 'object', schemaVersion: 1, properties: { myField: { title: 'Field title', description: 'My test field', type: 'string', editor: 'datepicker', }, }, }), ).toBe(true); }); const isSchemaValid = (dateType: string) => { return ajv.validate(inputSchema, { title: 'Test input schema', type: 'object', schemaVersion: 1, properties: { myField: { title: 'Field title', description: 'My test field', type: 'string', editor: 'datepicker', dateType, }, }, }); }; it('should accept valid dateType', () => { ['absolute', 'relative', 'absoluteOrRelative'].forEach((dateType) => { expect(isSchemaValid(dateType)).toBe(true); }); }); it('should not accept invalid dateType', () => { ['xxx', 'invalid'].forEach((dateType) => { expect(isSchemaValid(dateType)).toBe(false); }); }); }); describe('special cases for resourceProperty', () => { it('should accept resourceType with editor', () => { expect( ajv.validate(inputSchema, { title: 'Test input schema', type: 'object', schemaVersion: 1, properties: { myField: { title: 'Field title', description: 'My test field', type: 'string', resourceType: 'dataset', editor: 'resourcePicker', }, }, }), ).toBe(true); }); it('should accept resourceType without editor', () => { expect( ajv.validate(inputSchema, { title: 'Test input schema', type: 'object', schemaVersion: 1, properties: { myField: { title: 'Field title', description: 'My test field', type: 'string', resourceType: 'dataset', }, }, }), ).toBe(true); }); it('should accept array resourceType', () => { expect( ajv.validate(inputSchema, { title: 'Test input schema', type: 'object', schemaVersion: 1, properties: { myField: { title: 'Field title', description: 'My test field', type: 'array', resourceType: 'dataset', }, }, }), ).toBe(true); }); it('should reject mcpConnector resourceType without mcpServers', () => { expect( ajv.validate(inputSchema, { title: 'Test input schema', type: 'object', schemaVersion: 1, properties: { myField: { title: 'Field title', description: 'My test field', type: 'string', resourceType: 'mcpConnector', }, }, }), ).toBe(false); }); it('should accept mcpConnector with mcpServers', () => { expect( ajv.validate(inputSchema, { title: 'Test input schema', type: 'object', schemaVersion: 1, properties: { myField: { title: 'Field title', description: 'My test field', type: 'string', resourceType: 'mcpConnector', mcpServers: [ { url: 'https://example.com/*', tools: { required: ['tool1'] }, }, ], }, }, }), ).toBe(true); }); it('should accept mcpConnector array with mcpServers', () => { expect( ajv.validate(inputSchema, { title: 'Test input schema', type: 'object', schemaVersion: 1, properties: { myField: { title: 'Field title', description: 'My test field', type: 'array', resourceType: 'mcpConnector', mcpServers: [{ url: 'https://example.com/*' }], }, }, }), ).toBe(true); }); it('should reject mcpServers on storage resourceType', () => { expect( ajv.validate(inputSchema, { title: 'Test input schema', type: 'object', schemaVersion: 1, properties: { myField: { title: 'Field title', description: 'My test field', type: 'string', resourceType: 'dataset', mcpServers: [{ url: 'https://example.com/*' }], }, }, }), ).toBe(false); }); it('should reject resourcePermissions on mcpConnector', () => { expect( ajv.validate(inputSchema, { title: 'Test input schema', type: 'object', schemaVersion: 1, properties: { myField: { title: 'Field title', description: 'My test field', type: 'string', resourceType: 'mcpConnector', resourcePermissions: ['READ'], }, }, }), ).toBe(false); }); it('should accept storage resourceType without resourcePermissions', () => { expect( ajv.validate(inputSchema, { title: 'Test input schema', type: 'object', schemaVersion: 1, properties: { myField: { title: 'Field title', description: 'My test field', type: 'string', resourceType: 'dataset', }, }, }), ).toBe(true); }); it('should accept storage resourceType with resourcePermissions', () => { expect( ajv.validate(inputSchema, { title: 'Test input schema', type: 'object', schemaVersion: 1, properties: { myField: { title: 'Field title', description: 'My test field', type: 'string', resourceType: 'dataset', resourcePermissions: ['READ'], }, }, }), ).toBe(true); }); it('should accept mcpServers with tools containing only hint booleans', () => { expect( ajv.validate(inputSchema, { title: 'Test input schema', type: 'object', schemaVersion: 1, properties: { myField: { title: 'Field title', description: 'My test field', type: 'string', resourceType: 'mcpConnector', mcpServers: [ { url: '*', tools: { readOnly: true, idempotent: true, }, }, ], }, }, }), ).toBe(true); }); it('should reject unknown properties in tools object', () => { expect( ajv.validate(inputSchema, { title: 'Test input schema', type: 'object', schemaVersion: 1, properties: { myField: { title: 'Field title', description: 'My test field', type: 'string', resourceType: 'mcpConnector', mcpServers: [ { url: '*', tools: { unknownHint: true }, }, ], }, }, }), ).toBe(false); }); it('should reject unknown properties in mcpServers entry', () => { expect( ajv.validate(inputSchema, { title: 'Test input schema', type: 'object', schemaVersion: 1, properties: { myField: { title: 'Field title', description: 'My test field', type: 'string', resourceType: 'mcpConnector', mcpServers: [ { url: '*', unknownProp: 'value', }, ], }, }, }), ).toBe(false); }); }); }); });