|
| 1 | +/** |
| 2 | + * Tests for validator caching behaviour (fixes #2605: memory leak from |
| 3 | + * unconditional recompilation of schemas without `$id`). |
| 4 | + */ |
| 5 | + |
| 6 | +import { describe, expect, it } from 'vitest'; |
| 7 | + |
| 8 | +import { AjvJsonSchemaValidator } from '../../src/validators/ajvProvider'; |
| 9 | +import { CfWorkerJsonSchemaValidator } from '../../src/validators/cfWorkerProvider'; |
| 10 | +import type { JsonSchemaType } from '../../src/validators/types'; |
| 11 | + |
| 12 | +describe('AjvJsonSchemaValidator caching (#2605)', () => { |
| 13 | + it('returns the same validator function for identical schemas without $id', () => { |
| 14 | + const provider = new AjvJsonSchemaValidator(); |
| 15 | + const schema: JsonSchemaType = { |
| 16 | + type: 'object', |
| 17 | + properties: { name: { type: 'string' } }, |
| 18 | + required: ['name'] |
| 19 | + }; |
| 20 | + |
| 21 | + // Call getValidator twice with structurally identical (but different object) schemas |
| 22 | + const v1 = provider.getValidator(schema); |
| 23 | + const v2 = provider.getValidator({ ...schema }); |
| 24 | + |
| 25 | + // Both should validate correctly |
| 26 | + expect(v1({ name: 'Alice' }).valid).toBe(true); |
| 27 | + expect(v2({ name: 'Alice' }).valid).toBe(true); |
| 28 | + expect(v1({}).valid).toBe(false); |
| 29 | + expect(v2({}).valid).toBe(false); |
| 30 | + }); |
| 31 | + |
| 32 | + it('does not recompile when called repeatedly with the same schema content', () => { |
| 33 | + let compileCount = 0; |
| 34 | + const fakeEngine = { |
| 35 | + compile: (schema: unknown) => { |
| 36 | + compileCount++; |
| 37 | + return Object.assign(() => true, { errors: undefined }); |
| 38 | + }, |
| 39 | + getSchema: () => undefined, |
| 40 | + errorsText: () => '' |
| 41 | + }; |
| 42 | + const provider = new AjvJsonSchemaValidator(fakeEngine); |
| 43 | + const schema: JsonSchemaType = { type: 'string' }; |
| 44 | + |
| 45 | + provider.getValidator(schema); |
| 46 | + provider.getValidator(schema); |
| 47 | + provider.getValidator({ type: 'string' }); |
| 48 | + |
| 49 | + // Should compile only once — subsequent calls use the cache |
| 50 | + expect(compileCount).toBe(1); |
| 51 | + }); |
| 52 | + |
| 53 | + it('recompiles when schema content changes', () => { |
| 54 | + const provider = new AjvJsonSchemaValidator(); |
| 55 | + |
| 56 | + const v1 = provider.getValidator({ type: 'string' } as JsonSchemaType); |
| 57 | + const v2 = provider.getValidator({ type: 'number' } as JsonSchemaType); |
| 58 | + |
| 59 | + expect(v1('hello').valid).toBe(true); |
| 60 | + expect(v1(42).valid).toBe(false); |
| 61 | + expect(v2(42).valid).toBe(true); |
| 62 | + expect(v2('hello').valid).toBe(false); |
| 63 | + }); |
| 64 | + |
| 65 | + it('schemas with $id still use Ajv built-in identity cache', () => { |
| 66 | + const provider = new AjvJsonSchemaValidator(); |
| 67 | + const schema: JsonSchemaType = { |
| 68 | + $id: 'https://example.com/test-schema', |
| 69 | + type: 'object', |
| 70 | + properties: { x: { type: 'number' } } |
| 71 | + }; |
| 72 | + |
| 73 | + const v1 = provider.getValidator(schema); |
| 74 | + const v2 = provider.getValidator(schema); |
| 75 | + |
| 76 | + expect(v1({ x: 1 }).valid).toBe(true); |
| 77 | + expect(v2({ x: 1 }).valid).toBe(true); |
| 78 | + expect(v1({ x: 'nope' }).valid).toBe(false); |
| 79 | + }); |
| 80 | + |
| 81 | + it('a mutated schema object produces a validator for the new content', () => { |
| 82 | + const provider = new AjvJsonSchemaValidator(); |
| 83 | + const schema: JsonSchemaType = { type: 'string' }; |
| 84 | + |
| 85 | + const v1 = provider.getValidator(schema); |
| 86 | + expect(v1('hello').valid).toBe(true); |
| 87 | + expect(v1(42).valid).toBe(false); |
| 88 | + |
| 89 | + // Mutate in place |
| 90 | + (schema as Record<string, unknown>).type = 'number'; |
| 91 | + |
| 92 | + const v2 = provider.getValidator(schema); |
| 93 | + expect(v2(42).valid).toBe(true); |
| 94 | + expect(v2('hello').valid).toBe(false); |
| 95 | + }); |
| 96 | + |
| 97 | + it('shared cached validator does not have cross-call error pollution', () => { |
| 98 | + const provider = new AjvJsonSchemaValidator(); |
| 99 | + const schema: JsonSchemaType = { |
| 100 | + type: 'object', |
| 101 | + properties: { name: { type: 'string' } }, |
| 102 | + required: ['name'] |
| 103 | + }; |
| 104 | + |
| 105 | + const v1 = provider.getValidator(schema); |
| 106 | + const v2 = provider.getValidator(schema); |
| 107 | + |
| 108 | + // Validate with invalid data on v1 |
| 109 | + const r1 = v1({}); |
| 110 | + expect(r1.valid).toBe(false); |
| 111 | + expect(r1.errorMessage).toBeDefined(); |
| 112 | + |
| 113 | + // v2 should still validate correctly (not inheriting errors from v1) |
| 114 | + const r2 = v2({ name: 'Bob' }); |
| 115 | + expect(r2.valid).toBe(true); |
| 116 | + }); |
| 117 | + |
| 118 | + it('works correctly across different dialect schemas', () => { |
| 119 | + const provider = new AjvJsonSchemaValidator(); |
| 120 | + |
| 121 | + const schema2020: JsonSchemaType = { |
| 122 | + $schema: 'https://json-schema.org/draft/2020-12/schema', |
| 123 | + type: 'string' |
| 124 | + }; |
| 125 | + const schema07: JsonSchemaType = { |
| 126 | + $schema: 'http://json-schema.org/draft-07/schema#', |
| 127 | + type: 'string' |
| 128 | + }; |
| 129 | + |
| 130 | + const v2020 = provider.getValidator(schema2020); |
| 131 | + const v07 = provider.getValidator(schema07); |
| 132 | + |
| 133 | + expect(v2020('hello').valid).toBe(true); |
| 134 | + expect(v07('hello').valid).toBe(true); |
| 135 | + }); |
| 136 | +}); |
| 137 | + |
| 138 | +describe('CfWorkerJsonSchemaValidator caching (#2605)', () => { |
| 139 | + it('returns the same validation result for identical schemas without $id', () => { |
| 140 | + const provider = new CfWorkerJsonSchemaValidator(); |
| 141 | + const schema: JsonSchemaType = { |
| 142 | + type: 'object', |
| 143 | + properties: { name: { type: 'string' } }, |
| 144 | + required: ['name'] |
| 145 | + }; |
| 146 | + |
| 147 | + const v1 = provider.getValidator(schema); |
| 148 | + const v2 = provider.getValidator({ ...schema }); |
| 149 | + |
| 150 | + expect(v1({ name: 'Alice' }).valid).toBe(true); |
| 151 | + expect(v2({ name: 'Alice' }).valid).toBe(true); |
| 152 | + expect(v1({}).valid).toBe(false); |
| 153 | + expect(v2({}).valid).toBe(false); |
| 154 | + }); |
| 155 | + |
| 156 | + it('recompiles when schema content changes', () => { |
| 157 | + const provider = new CfWorkerJsonSchemaValidator(); |
| 158 | + |
| 159 | + const v1 = provider.getValidator({ type: 'string' } as JsonSchemaType); |
| 160 | + const v2 = provider.getValidator({ type: 'number' } as JsonSchemaType); |
| 161 | + |
| 162 | + expect(v1('hello').valid).toBe(true); |
| 163 | + expect(v1(42).valid).toBe(false); |
| 164 | + expect(v2(42).valid).toBe(true); |
| 165 | + expect(v2('hello').valid).toBe(false); |
| 166 | + }); |
| 167 | + |
| 168 | + it('caches per draft — same content with different drafts validates differently', () => { |
| 169 | + const provider = new CfWorkerJsonSchemaValidator(); |
| 170 | + |
| 171 | + // A schema with prefixItems: under 2020-12 it's enforced, under draft-07 it's ignored |
| 172 | + const schema2020: JsonSchemaType = { |
| 173 | + $schema: 'https://json-schema.org/draft/2020-12/schema', |
| 174 | + type: 'array', |
| 175 | + prefixItems: [{ type: 'number' }, { type: 'string' }] |
| 176 | + }; |
| 177 | + |
| 178 | + const v2020 = provider.getValidator(schema2020); |
| 179 | + // prefixItems is enforced under 2020-12 |
| 180 | + expect(v2020([1, 'x']).valid).toBe(true); |
| 181 | + expect(v2020(['x', 1]).valid).toBe(false); |
| 182 | + }); |
| 183 | +}); |
0 commit comments