|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { VariationValueSchema } from '@types'; |
| 3 | +import { |
| 4 | + createValueValidator, |
| 5 | + getSupportedSchemaTypes, |
| 6 | + isSchemaSupported, |
| 7 | + validateSchemaDefinition |
| 8 | +} from './variation-value-schema'; |
| 9 | + |
| 10 | +const enumSchema = (values: string[]): VariationValueSchema => ({ |
| 11 | + type: 'ENUM', |
| 12 | + enumValidator: { values } |
| 13 | +}); |
| 14 | + |
| 15 | +const regexSchema = (pattern: string): VariationValueSchema => ({ |
| 16 | + type: 'REGEX', |
| 17 | + regexValidator: { pattern } |
| 18 | +}); |
| 19 | + |
| 20 | +const jsonSchema = (schema: object): VariationValueSchema => ({ |
| 21 | + type: 'JSON_SCHEMA', |
| 22 | + jsonSchemaValidator: { schema: JSON.stringify(schema) } |
| 23 | +}); |
| 24 | + |
| 25 | +describe('getSupportedSchemaTypes / isSchemaSupported', () => { |
| 26 | + it('matches the backend v1 type matrix', () => { |
| 27 | + expect(getSupportedSchemaTypes('STRING')).toEqual(['ENUM', 'REGEX']); |
| 28 | + expect(getSupportedSchemaTypes('NUMBER')).toEqual(['ENUM']); |
| 29 | + expect(getSupportedSchemaTypes('JSON')).toEqual(['JSON_SCHEMA']); |
| 30 | + expect(getSupportedSchemaTypes('BOOLEAN')).toEqual([]); |
| 31 | + expect(getSupportedSchemaTypes('YAML')).toEqual([]); |
| 32 | + }); |
| 33 | + |
| 34 | + it('reports support only for types with at least one validator', () => { |
| 35 | + expect(isSchemaSupported('STRING')).toBe(true); |
| 36 | + expect(isSchemaSupported('BOOLEAN')).toBe(false); |
| 37 | + }); |
| 38 | +}); |
| 39 | + |
| 40 | +describe('validateSchemaDefinition', () => { |
| 41 | + it('rejects schema types unsupported for the variation type', () => { |
| 42 | + expect(validateSchemaDefinition(regexSchema('a+'), 'NUMBER')).toBe( |
| 43 | + 'type-unsupported' |
| 44 | + ); |
| 45 | + }); |
| 46 | + |
| 47 | + it('rejects empty enums', () => { |
| 48 | + expect(validateSchemaDefinition(enumSchema([]), 'STRING')).toBe( |
| 49 | + 'enum-empty' |
| 50 | + ); |
| 51 | + }); |
| 52 | + |
| 53 | + it('accepts plain decimal enum values for NUMBER flags', () => { |
| 54 | + expect( |
| 55 | + validateSchemaDefinition(enumSchema(['1', '-1.5', '1e3', '.5']), 'NUMBER') |
| 56 | + ).toBeNull(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('rejects non-decimal syntaxes the backend parser rejects', () => { |
| 60 | + for (const value of ['0x10', '0b10', 'Infinity', ' 1', '']) { |
| 61 | + expect(validateSchemaDefinition(enumSchema([value]), 'NUMBER')).toBe( |
| 62 | + 'enum-not-number' |
| 63 | + ); |
| 64 | + } |
| 65 | + }); |
| 66 | + |
| 67 | + it('rejects empty regex patterns', () => { |
| 68 | + expect(validateSchemaDefinition(regexSchema(''), 'STRING')).toBe( |
| 69 | + 'regex-empty' |
| 70 | + ); |
| 71 | + }); |
| 72 | + |
| 73 | + it('accepts Go-only RE2 constructs like inline flags', () => { |
| 74 | + expect( |
| 75 | + validateSchemaDefinition(regexSchema('(?i)^a+$'), 'STRING') |
| 76 | + ).toBeNull(); |
| 77 | + }); |
| 78 | + |
| 79 | + it('rejects Perl-only constructs like lookaheads, matching Go RE2', () => { |
| 80 | + expect(validateSchemaDefinition(regexSchema('(?=a)'), 'STRING')).toBe( |
| 81 | + 'regex-invalid' |
| 82 | + ); |
| 83 | + }); |
| 84 | + |
| 85 | + it('rejects empty and malformed JSON Schemas', () => { |
| 86 | + expect( |
| 87 | + validateSchemaDefinition( |
| 88 | + { type: 'JSON_SCHEMA', jsonSchemaValidator: { schema: ' ' } }, |
| 89 | + 'JSON' |
| 90 | + ) |
| 91 | + ).toBe('json-schema-empty'); |
| 92 | + expect( |
| 93 | + validateSchemaDefinition( |
| 94 | + { type: 'JSON_SCHEMA', jsonSchemaValidator: { schema: '{ not json' } }, |
| 95 | + 'JSON' |
| 96 | + ) |
| 97 | + ).toBe('json-schema-invalid'); |
| 98 | + }); |
| 99 | + |
| 100 | + it('accepts a valid JSON Schema', () => { |
| 101 | + expect( |
| 102 | + validateSchemaDefinition(jsonSchema({ type: 'object' }), 'JSON') |
| 103 | + ).toBeNull(); |
| 104 | + }); |
| 105 | +}); |
| 106 | + |
| 107 | +describe('createValueValidator: ENUM', () => { |
| 108 | + it('validates string values by exact match', () => { |
| 109 | + const validate = createValueValidator( |
| 110 | + enumSchema(['ssh', 'email']), |
| 111 | + 'STRING' |
| 112 | + )!; |
| 113 | + expect(validate('ssh').valid).toBe(true); |
| 114 | + expect(validate('SSH').valid).toBe(false); |
| 115 | + expect(validate('').valid).toBe(false); |
| 116 | + }); |
| 117 | + |
| 118 | + it('validates number values numerically but rejects non-decimal syntax', () => { |
| 119 | + const validate = createValueValidator(enumSchema(['16', '1.5']), 'NUMBER')!; |
| 120 | + expect(validate('16').valid).toBe(true); |
| 121 | + expect(validate('16.0').valid).toBe(true); |
| 122 | + // Number('0x10') === 16, but the backend's strconv.ParseFloat rejects it. |
| 123 | + expect(validate('0x10').valid).toBe(false); |
| 124 | + expect(validate('2').valid).toBe(false); |
| 125 | + }); |
| 126 | +}); |
| 127 | + |
| 128 | +describe('createValueValidator: REGEX', () => { |
| 129 | + it('returns null for patterns that do not compile as RE2', () => { |
| 130 | + expect(createValueValidator(regexSchema('(?=a)'), 'STRING')).toBeNull(); |
| 131 | + }); |
| 132 | + |
| 133 | + it('supports Go-only inline flags', () => { |
| 134 | + const validate = createValueValidator(regexSchema('(?i)^ssh$'), 'STRING')!; |
| 135 | + expect(validate('SSH').valid).toBe(true); |
| 136 | + expect(validate('email').valid).toBe(false); |
| 137 | + }); |
| 138 | + |
| 139 | + it('matches unanchored, mirroring the backend regexp.MatchString', () => { |
| 140 | + const validate = createValueValidator(regexSchema('b+'), 'STRING')!; |
| 141 | + expect(validate('abc').valid).toBe(true); |
| 142 | + expect(validate('ac').valid).toBe(false); |
| 143 | + }); |
| 144 | +}); |
| 145 | + |
| 146 | +describe('createValueValidator: JSON_SCHEMA', () => { |
| 147 | + const schema = jsonSchema({ |
| 148 | + type: 'object', |
| 149 | + required: ['name'], |
| 150 | + additionalProperties: false, |
| 151 | + properties: { |
| 152 | + name: { type: 'string' }, |
| 153 | + theme: { enum: ['light', 'dark'] } |
| 154 | + } |
| 155 | + }); |
| 156 | + |
| 157 | + it('returns null when the schema itself does not compile', () => { |
| 158 | + const invalid: VariationValueSchema = { |
| 159 | + type: 'JSON_SCHEMA', |
| 160 | + jsonSchemaValidator: { schema: '{ not json' } |
| 161 | + }; |
| 162 | + expect(createValueValidator(invalid, 'JSON')).toBeNull(); |
| 163 | + }); |
| 164 | + |
| 165 | + it('accepts a conforming value without detail', () => { |
| 166 | + const validate = createValueValidator(schema, 'JSON')!; |
| 167 | + expect(validate('{"name":"a","theme":"dark"}')).toEqual({ valid: true }); |
| 168 | + }); |
| 169 | + |
| 170 | + it('reports the failing path for missing required properties', () => { |
| 171 | + const validate = createValueValidator(schema, 'JSON')!; |
| 172 | + const result = validate('{}'); |
| 173 | + expect(result.valid).toBe(false); |
| 174 | + expect(result.detail).toBe("/ must have required property 'name'"); |
| 175 | + }); |
| 176 | + |
| 177 | + it('names the offending additional property', () => { |
| 178 | + const validate = createValueValidator(schema, 'JSON')!; |
| 179 | + const result = validate('{"name":"a","extraField":"x"}'); |
| 180 | + expect(result.valid).toBe(false); |
| 181 | + expect(result.detail).toContain( |
| 182 | + "must NOT have additional properties ('extraField')" |
| 183 | + ); |
| 184 | + }); |
| 185 | + |
| 186 | + it('lists the allowed values for enum violations', () => { |
| 187 | + const validate = createValueValidator(schema, 'JSON')!; |
| 188 | + const result = validate('{"name":"a","theme":"blue"}'); |
| 189 | + expect(result.valid).toBe(false); |
| 190 | + expect(result.detail).toContain( |
| 191 | + '/theme must be equal to one of the allowed values' |
| 192 | + ); |
| 193 | + expect(result.detail).toContain('"light", "dark"'); |
| 194 | + }); |
| 195 | + |
| 196 | + it('truncates long enum allowed-value lists', () => { |
| 197 | + const manyValues = jsonSchema({ |
| 198 | + type: 'object', |
| 199 | + properties: { size: { enum: ['a', 'b', 'c', 'd', 'e', 'f', 'g'] } } |
| 200 | + }); |
| 201 | + const validate = createValueValidator(manyValues, 'JSON')!; |
| 202 | + const result = validate('{"size":"z"}'); |
| 203 | + expect(result.valid).toBe(false); |
| 204 | + expect(result.detail).toContain('"a", "b", "c", "d", "e", +2 more'); |
| 205 | + }); |
| 206 | + |
| 207 | + it('reports multiple violations, capped with a remainder count', () => { |
| 208 | + const multi = jsonSchema({ |
| 209 | + type: 'object', |
| 210 | + required: ['a', 'b', 'c', 'd'], |
| 211 | + properties: { |
| 212 | + a: { type: 'string' }, |
| 213 | + b: { type: 'string' }, |
| 214 | + c: { type: 'string' }, |
| 215 | + d: { type: 'string' } |
| 216 | + } |
| 217 | + }); |
| 218 | + const validate = createValueValidator(multi, 'JSON')!; |
| 219 | + const result = validate('{}'); |
| 220 | + expect(result.valid).toBe(false); |
| 221 | + expect(result.detail).toBe( |
| 222 | + "/ must have required property 'a'; " + |
| 223 | + "/ must have required property 'b'; " + |
| 224 | + "/ must have required property 'c' (+1 more)" |
| 225 | + ); |
| 226 | + }); |
| 227 | + |
| 228 | + it('reports values that are not valid JSON', () => { |
| 229 | + const validate = createValueValidator(schema, 'JSON')!; |
| 230 | + expect(validate('not json')).toEqual({ |
| 231 | + valid: false, |
| 232 | + detail: 'invalid JSON' |
| 233 | + }); |
| 234 | + }); |
| 235 | +}); |
0 commit comments