|
| 1 | +import { describe, expect, test } from 'vitest'; |
| 2 | +import { |
| 3 | + compileMcpJsonSchemaValidator, |
| 4 | + UnsupportedMcpJsonSchemaDialectError, |
| 5 | + UnsupportedMcpJsonSchemaFeatureError, |
| 6 | +} from './mcpJsonSchemaValidator'; |
| 7 | + |
| 8 | +describe('compileMcpJsonSchemaValidator', () => { |
| 9 | + test('uses draft-07 when it is explicitly declared', () => { |
| 10 | + const validate = compileMcpJsonSchemaValidator({ |
| 11 | + $schema: 'http://json-schema.org/draft-07/schema#', |
| 12 | + type: 'array', |
| 13 | + items: [{ type: 'string' }], |
| 14 | + additionalItems: false, |
| 15 | + }); |
| 16 | + |
| 17 | + expect(validate(['valid'])).toBe(true); |
| 18 | + expect(validate(['valid', 'extra'])).toBe(false); |
| 19 | + }); |
| 20 | + |
| 21 | + test('uses draft 2019-09 for its declared meta-schema', () => { |
| 22 | + const validate = compileMcpJsonSchemaValidator({ |
| 23 | + $schema: 'https://json-schema.org/draft/2019-09/schema', |
| 24 | + type: 'object', |
| 25 | + properties: { |
| 26 | + known: { type: 'string' }, |
| 27 | + }, |
| 28 | + unevaluatedProperties: false, |
| 29 | + }); |
| 30 | + |
| 31 | + expect(validate({ known: 'value' })).toBe(true); |
| 32 | + expect(validate({ known: 'value', extra: true })).toBe(false); |
| 33 | + expect(validate.errors).toEqual(expect.arrayContaining([ |
| 34 | + expect.objectContaining({ keyword: 'unevaluatedProperties' }), |
| 35 | + ])); |
| 36 | + }); |
| 37 | + |
| 38 | + test.each([ |
| 39 | + ['when explicitly declared', 'https://json-schema.org/draft/2020-12/schema'], |
| 40 | + ['by default when omitted', undefined], |
| 41 | + ])('uses draft 2020-12 %s', (_name, dialect) => { |
| 42 | + const validate = compileMcpJsonSchemaValidator({ |
| 43 | + ...(dialect ? { $schema: dialect } : {}), |
| 44 | + type: 'array', |
| 45 | + prefixItems: [{ type: 'string' }], |
| 46 | + items: false, |
| 47 | + }); |
| 48 | + |
| 49 | + expect(validate(['valid'])).toBe(true); |
| 50 | + expect(validate([42])).toBe(false); |
| 51 | + expect(validate(['valid', 'extra'])).toBe(false); |
| 52 | + }); |
| 53 | + |
| 54 | + test('preserves all-errors validation and permits unknown keywords', () => { |
| 55 | + const validate = compileMcpJsonSchemaValidator({ |
| 56 | + type: 'object', |
| 57 | + required: ['first', 'second'], |
| 58 | + unknownServerKeyword: true, |
| 59 | + }); |
| 60 | + |
| 61 | + expect(validate({})).toBe(false); |
| 62 | + expect(validate.errors?.filter(error => error.keyword === 'required')).toHaveLength(2); |
| 63 | + }); |
| 64 | + |
| 65 | + test('does not conflict when separate server schemas reuse the same root ID', () => { |
| 66 | + const first = compileMcpJsonSchemaValidator({ |
| 67 | + $id: 'https://schemas.example.com/tool-input', |
| 68 | + type: 'string', |
| 69 | + }); |
| 70 | + const second = compileMcpJsonSchemaValidator({ |
| 71 | + $id: 'https://schemas.example.com/tool-input', |
| 72 | + type: 'number', |
| 73 | + }); |
| 74 | + |
| 75 | + expect(first('value')).toBe(true); |
| 76 | + expect(second(42)).toBe(true); |
| 77 | + }); |
| 78 | + |
| 79 | + test.each([ |
| 80 | + 'http://json-schema.org/draft-07/schema#', |
| 81 | + 'https://json-schema.org/draft/2019-09/schema', |
| 82 | + 'https://json-schema.org/draft/2020-12/schema', |
| 83 | + ])('does not let a remote $id remove the %s meta-schema', (dialect) => { |
| 84 | + const collidingSchema = compileMcpJsonSchemaValidator({ |
| 85 | + $schema: dialect, |
| 86 | + $id: dialect, |
| 87 | + type: 'object', |
| 88 | + }); |
| 89 | + |
| 90 | + expect(collidingSchema({})).toBe(true); |
| 91 | + expect(() => compileMcpJsonSchemaValidator({ |
| 92 | + $schema: dialect, |
| 93 | + type: 'object', |
| 94 | + })).not.toThrow(); |
| 95 | + }); |
| 96 | + |
| 97 | + test('resolves document-local 2020-12 references after compilation', () => { |
| 98 | + const validate = compileMcpJsonSchemaValidator({ |
| 99 | + $schema: 'https://json-schema.org/draft/2020-12/schema', |
| 100 | + $defs: { |
| 101 | + identifier: { type: 'string', minLength: 1 }, |
| 102 | + }, |
| 103 | + type: 'object', |
| 104 | + properties: { |
| 105 | + id: { $ref: '#/$defs/identifier' }, |
| 106 | + }, |
| 107 | + required: ['id'], |
| 108 | + }); |
| 109 | + |
| 110 | + expect(validate({ id: 'issue-id' })).toBe(true); |
| 111 | + expect(validate({ id: '' })).toBe(false); |
| 112 | + }); |
| 113 | + |
| 114 | + test('rejects Ajv asynchronous schemas instead of bypassing validation', () => { |
| 115 | + expect(() => compileMcpJsonSchemaValidator({ |
| 116 | + $async: true, |
| 117 | + type: 'object', |
| 118 | + })).toThrow(UnsupportedMcpJsonSchemaFeatureError); |
| 119 | + }); |
| 120 | + |
| 121 | + test.each([ |
| 122 | + 'http://json-schema.org/draft-04/schema#', |
| 123 | + 'https://malicious.example/schema?access_token=secret-token', |
| 124 | + ])('rejects unsupported dialects without echoing the declaration', (declaredDialect) => { |
| 125 | + let thrown: unknown; |
| 126 | + try { |
| 127 | + compileMcpJsonSchemaValidator({ |
| 128 | + $schema: declaredDialect, |
| 129 | + type: 'object', |
| 130 | + }); |
| 131 | + } catch (error) { |
| 132 | + thrown = error; |
| 133 | + } |
| 134 | + |
| 135 | + expect(thrown).toBeInstanceOf(UnsupportedMcpJsonSchemaDialectError); |
| 136 | + expect(thrown).toMatchObject({ |
| 137 | + name: 'UnsupportedMcpJsonSchemaDialectError', |
| 138 | + reason: 'unsupported_json_schema_dialect', |
| 139 | + }); |
| 140 | + expect((thrown as Error).message).not.toContain(declaredDialect); |
| 141 | + expect((thrown as Error).message).not.toContain('secret-token'); |
| 142 | + }); |
| 143 | + |
| 144 | + test('rejects a non-string declared dialect safely', () => { |
| 145 | + expect(() => compileMcpJsonSchemaValidator({ |
| 146 | + $schema: 202012, |
| 147 | + type: 'object', |
| 148 | + })).toThrow(UnsupportedMcpJsonSchemaDialectError); |
| 149 | + }); |
| 150 | +}); |
0 commit comments