-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathconstructor_test.go
More file actions
274 lines (237 loc) · 7.1 KB
/
Copy pathconstructor_test.go
File metadata and controls
274 lines (237 loc) · 7.1 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package jsonschema_test
import (
"fmt"
"log"
"github.com/kaptinlin/jsonschema"
)
func Example_object() {
// Simple object schema using constructor API
schema := jsonschema.Object(
jsonschema.Prop("name", jsonschema.String(jsonschema.MinLength(1))),
jsonschema.Prop("age", jsonschema.Integer(jsonschema.Min(0))),
jsonschema.Required("name"),
)
// Valid data
data := map[string]any{
"name": "Alice",
"age": 30,
}
result := schema.Validate(data)
fmt.Println("Valid:", result.IsValid())
// Output: Valid: true
}
func Example_complexSchema() {
// Complex nested schema with validation keywords
userSchema := jsonschema.Object(
jsonschema.Prop("name", jsonschema.String(
jsonschema.MinLength(1),
jsonschema.MaxLength(100),
)),
jsonschema.Prop("age", jsonschema.Integer(
jsonschema.Min(0),
jsonschema.Max(150),
)),
jsonschema.Prop("email", jsonschema.Email()),
jsonschema.Prop("address", jsonschema.Object(
jsonschema.Prop("street", jsonschema.String(jsonschema.MinLength(1))),
jsonschema.Prop("city", jsonschema.String(jsonschema.MinLength(1))),
jsonschema.Prop("zip", jsonschema.String(jsonschema.Pattern(`^\d{5}$`))),
jsonschema.Required("street", "city"),
)),
jsonschema.Prop("tags", jsonschema.Array(
jsonschema.Items(jsonschema.String()),
jsonschema.MinItems(1),
jsonschema.UniqueItems(true),
)),
jsonschema.Required("name", "email"),
)
// Test data
userData := map[string]any{
"name": "Alice",
"age": 30,
"email": "alice@example.com",
"address": map[string]any{
"street": "123 Main St",
"city": "Anytown",
"zip": "12345",
},
"tags": []any{"developer", "go"},
}
result := userSchema.Validate(userData)
if result.IsValid() {
fmt.Println("User data is valid")
} else {
for field, err := range result.Errors {
fmt.Printf("Error in %s: %s\n", field, err.Message)
}
}
// Output: User data is valid
}
func Example_arraySchema() {
// Array schema with validation keywords
numbersSchema := jsonschema.Array(
jsonschema.Items(jsonschema.Number(
jsonschema.Min(0),
jsonschema.Max(100),
)),
jsonschema.MinItems(1),
jsonschema.MaxItems(10),
)
validData := []any{10, 20, 30}
result := numbersSchema.Validate(validData)
fmt.Println("Numbers valid:", result.IsValid())
invalidData := []any{-5, 150} // Out of range
result = numbersSchema.Validate(invalidData)
fmt.Println("Invalid numbers valid:", result.IsValid())
// Output:
// Numbers valid: true
// Invalid numbers valid: false
}
func Example_enumAndConst() {
// Enum schema using enum keyword
statusSchema := jsonschema.Enum("active", "inactive", "pending")
result := statusSchema.Validate("active")
fmt.Println("Status valid:", result.IsValid())
// Const schema using const keyword
versionSchema := jsonschema.Const("1.0.0")
result = versionSchema.Validate("1.0.0")
fmt.Println("Version valid:", result.IsValid())
// Output:
// Status valid: true
// Version valid: true
}
func Example_oneOfAnyOf() {
// OneOf: exactly one schema must match
oneOfSchema := jsonschema.OneOf(
jsonschema.String(),
jsonschema.Integer(),
)
result := oneOfSchema.Validate("hello")
fmt.Println("OneOf string valid:", result.IsValid())
// AnyOf: at least one schema must match
anyOfSchema := jsonschema.AnyOf(
jsonschema.String(jsonschema.MinLength(5)),
jsonschema.Integer(jsonschema.Min(0)),
)
result = anyOfSchema.Validate("hi") // Matches integer rule (length < 5 but is string)
fmt.Println("AnyOf short string valid:", result.IsValid())
// Output:
// OneOf string valid: true
// AnyOf short string valid: false
}
func Example_conditionalSchema() {
// Conditional schema using if/then/else keywords
conditionalSchema := jsonschema.If(
jsonschema.Object(
jsonschema.Prop("type", jsonschema.Const("premium")),
),
).Then(
jsonschema.Object(
jsonschema.Prop("features", jsonschema.Array(jsonschema.MinItems(5))),
),
).Else(
jsonschema.Object(
jsonschema.Prop("features", jsonschema.Array(jsonschema.MaxItems(3))),
),
)
// Basic plan object
basicPlan := map[string]any{
"type": "basic",
"features": []any{"feature1", "feature2"},
}
result := conditionalSchema.Validate(basicPlan)
fmt.Println("Basic plan valid:", result.IsValid())
// Output: Basic plan valid: true
}
func Example_convenienceFunctions() {
// Using convenience functions that apply format keywords
profileSchema := jsonschema.Object(
jsonschema.Prop("id", jsonschema.UUID()),
jsonschema.Prop("email", jsonschema.Email()),
jsonschema.Prop("website", jsonschema.URI()),
jsonschema.Prop("created", jsonschema.DateTime()),
jsonschema.Prop("score", jsonschema.PositiveInt()),
)
data := map[string]any{
"id": "550e8400-e29b-41d4-a716-446655440000",
"email": "user@example.com",
"website": "https://example.com",
"created": "2023-01-01T00:00:00Z",
"score": 95,
}
result := profileSchema.Validate(data)
fmt.Println("Profile valid:", result.IsValid())
// Output: Profile valid: true
}
func Example_compatibilityWithJSON() {
// New code construction approach
codeSchema := jsonschema.Object(
jsonschema.Prop("name", jsonschema.String()),
jsonschema.Prop("age", jsonschema.Integer()),
)
// Existing JSON compilation approach
compiler := jsonschema.NewCompiler()
jsonSchema, err := compiler.Compile([]byte(`{
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"}
}
}`))
if err != nil {
log.Fatal(err)
}
data := map[string]any{
"name": "Bob",
"age": 25,
}
// Both approaches work identically
result1 := codeSchema.Validate(data)
result2 := jsonSchema.Validate(data)
fmt.Println("Code schema valid:", result1.IsValid())
fmt.Println("JSON schema valid:", result2.IsValid())
// Output:
// Code schema valid: true
// JSON schema valid: true
}
func Example_schemaRegistration() {
// Create compiler for schema registration
compiler := jsonschema.NewCompiler()
// Create User schema with Constructor API
userSchema := jsonschema.Object(
jsonschema.ID("https://example.com/schemas/user"),
jsonschema.Prop("id", jsonschema.UUID()),
jsonschema.Prop("name", jsonschema.String(jsonschema.MinLength(1))),
jsonschema.Prop("email", jsonschema.Email()),
jsonschema.Required("id", "name", "email"),
)
// Register the schema
compiler.SetSchema("https://example.com/schemas/user", userSchema)
// Create Profile schema that references User schema
profileJSON := `{
"type": "object",
"properties": {
"user": {"$ref": "https://example.com/schemas/user"},
"bio": {"type": "string"},
"website": {"type": "string", "format": "uri"}
},
"required": ["user"]
}`
profileSchema, err := compiler.Compile([]byte(profileJSON))
if err != nil {
log.Fatal(err)
}
// Test with valid data
profileData := map[string]any{
"user": map[string]any{
"id": "550e8400-e29b-41d4-a716-446655440000",
"name": "Alice Johnson",
"email": "alice@example.com",
},
"bio": "Software engineer",
"website": "https://alice.dev",
}
result := profileSchema.Validate(profileData)
fmt.Println("Profile with registered user schema valid:", result.IsValid())
// Output: Profile with registered user schema valid: true
}