forked from langgenius/dify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidators.spec.ts
More file actions
139 lines (128 loc) · 3.74 KB
/
validators.spec.ts
File metadata and controls
139 lines (128 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { draft07Validator, forbidBooleanProperties } from './validators'
describe('Validators', () => {
describe('draft07Validator', () => {
it('should validate a valid JSON schema', () => {
const validSchema = {
type: 'object',
properties: {
name: { type: 'string' },
age: { type: 'number' },
},
}
const result = draft07Validator(validSchema)
expect(result.valid).toBe(true)
expect(result.errors).toHaveLength(0)
})
it('should invalidate schema with unknown type', () => {
const invalidSchema = {
type: 'invalid_type',
}
const result = draft07Validator(invalidSchema)
expect(result.valid).toBe(false)
expect(result.errors.length).toBeGreaterThan(0)
})
it('should validate nested schemas', () => {
const nestedSchema = {
type: 'object',
properties: {
user: {
type: 'object',
properties: {
name: { type: 'string' },
address: {
type: 'object',
properties: {
street: { type: 'string' },
city: { type: 'string' },
},
},
},
},
},
}
const result = draft07Validator(nestedSchema)
expect(result.valid).toBe(true)
})
it('should validate array schemas', () => {
const arraySchema = {
type: 'array',
items: { type: 'string' },
}
const result = draft07Validator(arraySchema)
expect(result.valid).toBe(true)
})
})
describe('forbidBooleanProperties', () => {
it('should return empty array for schema without boolean properties', () => {
const schema = {
properties: {
name: { type: 'string' },
age: { type: 'number' },
},
}
const errors = forbidBooleanProperties(schema)
expect(errors).toHaveLength(0)
})
it('should detect boolean property at root level', () => {
const schema = {
properties: {
name: true,
age: { type: 'number' },
},
}
const errors = forbidBooleanProperties(schema)
expect(errors).toHaveLength(1)
expect(errors[0]).toContain('name')
})
it('should detect boolean properties in nested objects', () => {
const schema = {
properties: {
user: {
properties: {
name: true,
profile: {
properties: {
bio: false,
},
},
},
},
},
}
const errors = forbidBooleanProperties(schema)
expect(errors).toHaveLength(2)
expect(errors.some(e => e.includes('user.name'))).toBe(true)
expect(errors.some(e => e.includes('user.profile.bio'))).toBe(true)
})
it('should handle schema without properties', () => {
const schema = { type: 'string' }
const errors = forbidBooleanProperties(schema)
expect(errors).toHaveLength(0)
})
it('should handle null schema', () => {
const errors = forbidBooleanProperties(null)
expect(errors).toHaveLength(0)
})
it('should handle empty schema', () => {
const errors = forbidBooleanProperties({})
expect(errors).toHaveLength(0)
})
it('should provide correct path in error messages', () => {
const schema = {
properties: {
level1: {
properties: {
level2: {
properties: {
level3: true,
},
},
},
},
},
}
const errors = forbidBooleanProperties(schema)
expect(errors[0]).toContain('level1.level2.level3')
})
})
})