-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path10_schema_validation.mojo
More file actions
187 lines (155 loc) · 6.18 KB
/
10_schema_validation.mojo
File metadata and controls
187 lines (155 loc) · 6.18 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
# Example 10: JSON Schema Validation
#
# Validate JSON documents against schemas to ensure data quality.
# Supports a subset of JSON Schema draft-07.
from json import loads, validate, is_valid
def main() raises:
print("JSON Schema Validation Examples")
print("=" * 50)
print()
# ==========================================================
# 1. Basic type validation
# ==========================================================
print("1. Basic type validation:")
var type_schema = loads('{"type": "string"}')
print(" Schema: type=string")
print(" 'hello' valid?", is_valid(loads('"hello"'), type_schema))
print(" 42 valid?", is_valid(loads("42"), type_schema))
print()
# ==========================================================
# 2. Object with required fields
# ==========================================================
print("2. Object with required fields:")
var user_schema = loads('''
{
"type": "object",
"required": ["name", "email"],
"properties": {
"name": {"type": "string", "minLength": 1},
"email": {"type": "string"},
"age": {"type": "integer", "minimum": 0}
}
}
''')
var valid_user = loads('{"name": "Alice", "email": "alice@example.com", "age": 30}')
var missing_email = loads('{"name": "Bob"}')
var invalid_age = loads('{"name": "Charlie", "email": "c@x.com", "age": -5}')
print(" Complete user valid?", is_valid(valid_user, user_schema))
print(" Missing email valid?", is_valid(missing_email, user_schema))
print(" Negative age valid?", is_valid(invalid_age, user_schema))
print()
# ==========================================================
# 3. Detailed error messages
# ==========================================================
print("3. Detailed error messages:")
var result = validate(missing_email, user_schema)
print(" Validation result: valid=", result.valid)
if not result.valid:
print(" Errors:")
for i in range(len(result.errors)):
print(" - Path:", result.errors[i].path, "| Message:", result.errors[i].message)
print()
# ==========================================================
# 4. Number constraints
# ==========================================================
print("4. Number constraints:")
var number_schema = loads('''
{
"type": "number",
"minimum": 0,
"maximum": 100
}
''')
print(" Schema: 0 <= number <= 100")
print(" 50 valid?", is_valid(loads("50"), number_schema))
print(" -10 valid?", is_valid(loads("-10"), number_schema))
print(" 150 valid?", is_valid(loads("150"), number_schema))
print()
# ==========================================================
# 5. String constraints
# ==========================================================
print("5. String constraints:")
var string_schema = loads('''
{
"type": "string",
"minLength": 3,
"maxLength": 10
}
''')
print(" Schema: 3 <= length <= 10")
print(" 'hello' valid?", is_valid(loads('"hello"'), string_schema))
print(" 'hi' valid?", is_valid(loads('"hi"'), string_schema))
print(" 'verylongstring' valid?", is_valid(loads('"verylongstring"'), string_schema))
print()
# ==========================================================
# 6. Array validation
# ==========================================================
print("6. Array validation:")
var array_schema = loads('''
{
"type": "array",
"items": {"type": "integer"},
"minItems": 1,
"maxItems": 5
}
''')
print(" Schema: array of integers, 1-5 items")
print(" [1,2,3] valid?", is_valid(loads("[1,2,3]"), array_schema))
print(" [] valid?", is_valid(loads("[]"), array_schema))
print(" [1,'two'] valid?", is_valid(loads('[1,"two"]'), array_schema))
print()
# ==========================================================
# 7. Enum values
# ==========================================================
print("7. Enum values:")
var enum_schema = loads('''
{
"enum": ["pending", "active", "completed"]
}
''')
print(" Schema: one of [pending, active, completed]")
print(" 'active' valid?", is_valid(loads('"active"'), enum_schema))
print(" 'deleted' valid?", is_valid(loads('"deleted"'), enum_schema))
print()
# ==========================================================
# 8. Composition (allOf, anyOf, oneOf)
# ==========================================================
print("8. Schema composition:")
var composed_schema = loads('''
{
"allOf": [
{"type": "object"},
{"required": ["id"]},
{"properties": {"id": {"type": "integer"}}}
]
}
''')
print(" Schema: allOf [object, has id, id is integer]")
print(" {id: 1} valid?", is_valid(loads('{"id": 1}'), composed_schema))
print(" {id: 'a'} valid?", is_valid(loads('{"id": "a"}'), composed_schema))
print(" {name: 'x'} valid?", is_valid(loads('{"name": "x"}'), composed_schema))
print()
# ==========================================================
# 9. Practical example - API request validation
# ==========================================================
print("9. Practical example - API request:")
var api_schema = loads('''
{
"type": "object",
"required": ["action", "payload"],
"properties": {
"action": {"enum": ["create", "update", "delete"]},
"payload": {"type": "object"},
"timestamp": {"type": "string"}
},
"additionalProperties": false
}
''')
var good_request = loads('{"action": "create", "payload": {"name": "test"}}')
var bad_action = loads('{"action": "invalid", "payload": {}}')
var extra_field = loads('{"action": "create", "payload": {}, "extra": true}')
print(" Valid request:", is_valid(good_request, api_schema))
print(" Invalid action:", is_valid(bad_action, api_schema))
print(" Extra field:", is_valid(extra_field, api_schema))
print()
print("Done!")