|
| 1 | +import type { NonBooleanJsfSchema } from '../../src/types' |
| 2 | +import { describe, expect, it } from '@jest/globals' |
| 3 | +import { validateConst } from '../../src/validation/const' |
| 4 | + |
| 5 | +describe('const schema validation', () => { |
| 6 | + it('should return empty array when const value matches', () => { |
| 7 | + const schema: NonBooleanJsfSchema = { const: 42 } |
| 8 | + const value = 42 |
| 9 | + expect(validateConst(value, schema)).toEqual([]) |
| 10 | + }) |
| 11 | + |
| 12 | + it('should return error when const value does not match', () => { |
| 13 | + const schema: NonBooleanJsfSchema = { const: 42 } |
| 14 | + const value = 43 |
| 15 | + const result = validateConst(value, schema) |
| 16 | + expect(result).toHaveLength(1) |
| 17 | + expect(result[0]).toEqual({ |
| 18 | + path: [], |
| 19 | + validation: 'const', |
| 20 | + schema, |
| 21 | + value: 43, |
| 22 | + }) |
| 23 | + }) |
| 24 | + |
| 25 | + it('should handle nested objects with deep equality', () => { |
| 26 | + const schema: NonBooleanJsfSchema = { |
| 27 | + const: { foo: { bar: 42 } }, |
| 28 | + } |
| 29 | + const value = { foo: { bar: 42 } } |
| 30 | + expect(validateConst(value, schema)).toEqual([]) |
| 31 | + }) |
| 32 | + |
| 33 | + it('should handle arrays with deep equality', () => { |
| 34 | + const schema: NonBooleanJsfSchema = { |
| 35 | + const: [1, 2, { foo: 'bar' }], |
| 36 | + } |
| 37 | + const value = [1, 2, { foo: 'bar' }] |
| 38 | + expect(validateConst(value, schema)).toEqual([]) |
| 39 | + }) |
| 40 | + |
| 41 | + it('should return empty array when const is not present', () => { |
| 42 | + const schema: NonBooleanJsfSchema = {} |
| 43 | + const value = 42 |
| 44 | + expect(validateConst(value, schema)).toEqual([]) |
| 45 | + }) |
| 46 | + |
| 47 | + it('should handle schema.value as fallback for const', () => { |
| 48 | + const schema: NonBooleanJsfSchema = { value: 42 } |
| 49 | + const input = 42 |
| 50 | + expect(validateConst(input, schema)).toEqual([]) |
| 51 | + }) |
| 52 | + |
| 53 | + it('should handle different types correctly', () => { |
| 54 | + const testCases = [ |
| 55 | + { schema: { const: 'string' }, value: 'string', shouldPass: true }, |
| 56 | + { schema: { const: true }, value: false, shouldPass: false }, |
| 57 | + { schema: { const: null }, value: null, shouldPass: true }, |
| 58 | + { schema: { const: 0 }, value: '0', shouldPass: false }, |
| 59 | + { schema: { const: [] }, value: [], shouldPass: true }, |
| 60 | + { schema: { const: {} }, value: {}, shouldPass: true }, |
| 61 | + ] |
| 62 | + |
| 63 | + testCases.forEach(({ schema, value, shouldPass }) => { |
| 64 | + const result = validateConst(value, schema as NonBooleanJsfSchema) |
| 65 | + expect(result.length).toBe(shouldPass ? 0 : 1) |
| 66 | + }) |
| 67 | + }) |
| 68 | + |
| 69 | + it('should handle path parameter correctly', () => { |
| 70 | + const schema: NonBooleanJsfSchema = { const: 42 } |
| 71 | + const value = 43 |
| 72 | + const path = ['foo', 'bar'] |
| 73 | + const result = validateConst(value, schema, path) |
| 74 | + expect(result[0].path).toEqual(path) |
| 75 | + }) |
| 76 | +}) |
0 commit comments