|
1 | 1 | import yaml from 'js-yaml'
|
2 |
| -import { validateYamlSchema } from './validateYamlSchema' |
| 2 | +import { validateYamlSchema, validateSchema } from './validateYamlSchema' |
3 | 3 |
|
4 | 4 | const schema = {
|
5 | 5 | type: 'object',
|
@@ -97,3 +97,168 @@ describe('validateYamlSchema', () => {
|
97 | 97 | jest.restoreAllMocks()
|
98 | 98 | })
|
99 | 99 | })
|
| 100 | + |
| 101 | +describe('validateSchema with ValidationConfig', () => { |
| 102 | + const testSchema = { |
| 103 | + type: 'object', |
| 104 | + properties: { |
| 105 | + name: { type: 'string' }, |
| 106 | + nested: { |
| 107 | + type: 'object', |
| 108 | + properties: { |
| 109 | + value: { type: 'number' } |
| 110 | + }, |
| 111 | + required: ['value'] |
| 112 | + } |
| 113 | + }, |
| 114 | + required: ['name'] |
| 115 | + } |
| 116 | + |
| 117 | + const invalidData = { |
| 118 | + nested: { |
| 119 | + value: 'not-a-number' |
| 120 | + } |
| 121 | + // missing required 'name' field |
| 122 | + } |
| 123 | + |
| 124 | + describe('default ValidationConfig', () => { |
| 125 | + it('should use default error message prefix "Error:"', () => { |
| 126 | + const result = validateSchema(invalidData, testSchema) |
| 127 | + |
| 128 | + expect(result.valid).toBe(false) |
| 129 | + expect(result.errors).toEqual( |
| 130 | + expect.arrayContaining([ |
| 131 | + expect.stringContaining('Error:') |
| 132 | + ]) |
| 133 | + ) |
| 134 | + }) |
| 135 | + |
| 136 | + it('should include path information by default', () => { |
| 137 | + const result = validateSchema(invalidData, testSchema) |
| 138 | + |
| 139 | + expect(result.valid).toBe(false) |
| 140 | + expect(result.errors).toEqual( |
| 141 | + expect.arrayContaining([ |
| 142 | + expect.stringContaining('(at root)'), |
| 143 | + expect.stringContaining('(at /nested/value)') |
| 144 | + ]) |
| 145 | + ) |
| 146 | + }) |
| 147 | + }) |
| 148 | + |
| 149 | + describe('custom ValidationConfig', () => { |
| 150 | + it('should use custom error message prefix', () => { |
| 151 | + const config = { errorMessagePrefix: 'Custom Prefix:' } |
| 152 | + const result = validateSchema(invalidData, testSchema, config) |
| 153 | + |
| 154 | + expect(result.valid).toBe(false) |
| 155 | + expect(result.errors).toEqual( |
| 156 | + expect.arrayContaining([ |
| 157 | + expect.stringContaining('Custom Prefix:') |
| 158 | + ]) |
| 159 | + ) |
| 160 | + expect(result.errors).not.toEqual( |
| 161 | + expect.arrayContaining([ |
| 162 | + expect.stringContaining('Error:') |
| 163 | + ]) |
| 164 | + ) |
| 165 | + }) |
| 166 | + |
| 167 | + it('should exclude path information when includePathIntoErrorMessage is false', () => { |
| 168 | + const config = { includePathIntoErrorMessage: false } |
| 169 | + const result = validateSchema(invalidData, testSchema, config) |
| 170 | + |
| 171 | + expect(result.valid).toBe(false) |
| 172 | + expect(result.errors).not.toEqual( |
| 173 | + expect.arrayContaining([ |
| 174 | + expect.stringContaining('(at ') |
| 175 | + ]) |
| 176 | + ) |
| 177 | + }) |
| 178 | + |
| 179 | + it('should use both custom prefix and exclude path information', () => { |
| 180 | + const config = { |
| 181 | + errorMessagePrefix: 'Custom Error:', |
| 182 | + includePathIntoErrorMessage: false |
| 183 | + } |
| 184 | + const result = validateSchema(invalidData, testSchema, config) |
| 185 | + |
| 186 | + expect(result.valid).toBe(false) |
| 187 | + expect(result.errors).toEqual( |
| 188 | + expect.arrayContaining([ |
| 189 | + expect.stringContaining('Custom Error:') |
| 190 | + ]) |
| 191 | + ) |
| 192 | + expect(result.errors).not.toEqual( |
| 193 | + expect.arrayContaining([ |
| 194 | + expect.stringContaining('(at ') |
| 195 | + ]) |
| 196 | + ) |
| 197 | + }) |
| 198 | + |
| 199 | + it('should handle empty string as error message prefix', () => { |
| 200 | + const config = { errorMessagePrefix: '' } |
| 201 | + const result = validateSchema(invalidData, testSchema, config) |
| 202 | + |
| 203 | + expect(result.valid).toBe(false) |
| 204 | + expect(result.errors.length).toBeGreaterThan(0) |
| 205 | + // Should not start with "Error:" but with the actual error message |
| 206 | + expect(result.errors[0]).not.toMatch(/^Error:/) |
| 207 | + }) |
| 208 | + }) |
| 209 | + |
| 210 | + describe('ValidationConfig with exceptions', () => { |
| 211 | + it('should use custom error prefix for unknown errors', () => { |
| 212 | + const mockSchema = null // This will cause an error in AJV |
| 213 | + const config = { errorMessagePrefix: 'Schema Error:' } |
| 214 | + |
| 215 | + const result = validateSchema({}, mockSchema, config) |
| 216 | + |
| 217 | + expect(result.valid).toBe(false) |
| 218 | + expect(result.errors).toEqual(['Schema Error: unknown error']) |
| 219 | + }) |
| 220 | + |
| 221 | + it('should use default error prefix for unknown errors when no config provided', () => { |
| 222 | + const mockSchema = null // This will cause an error in AJV |
| 223 | + |
| 224 | + const result = validateSchema({}, mockSchema) |
| 225 | + |
| 226 | + expect(result.valid).toBe(false) |
| 227 | + expect(result.errors).toEqual(['Error: unknown error']) |
| 228 | + }) |
| 229 | + }) |
| 230 | + |
| 231 | + describe('edge cases', () => { |
| 232 | + it('should handle valid data with custom config', () => { |
| 233 | + const validData = { name: 'test', nested: { value: 42 } } |
| 234 | + const config = { |
| 235 | + errorMessagePrefix: 'Custom Error:', |
| 236 | + includePathIntoErrorMessage: false |
| 237 | + } |
| 238 | + |
| 239 | + const result = validateSchema(validData, testSchema, config) |
| 240 | + |
| 241 | + expect(result).toEqual({ |
| 242 | + valid: true, |
| 243 | + errors: [] |
| 244 | + }) |
| 245 | + }) |
| 246 | + |
| 247 | + it('should handle undefined config properties gracefully', () => { |
| 248 | + const config = { |
| 249 | + errorMessagePrefix: undefined, |
| 250 | + includePathIntoErrorMessage: undefined |
| 251 | + } |
| 252 | + const result = validateSchema(invalidData, testSchema, config) |
| 253 | + |
| 254 | + expect(result.valid).toBe(false) |
| 255 | + // Should use defaults when undefined |
| 256 | + expect(result.errors).toEqual( |
| 257 | + expect.arrayContaining([ |
| 258 | + expect.stringContaining('Error:'), |
| 259 | + expect.stringContaining('(at ') |
| 260 | + ]) |
| 261 | + ) |
| 262 | + }) |
| 263 | + }) |
| 264 | +}) |
0 commit comments