-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathjson_schema.txtar
More file actions
331 lines (296 loc) · 6.69 KB
/
json_schema.txtar
File metadata and controls
331 lines (296 loc) · 6.69 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# ============================================================
# JSON $schema validation tests (all local, no network)
# ============================================================
# --- package.json schema ---
# Valid package.json
exec validator valid_package.json
stdout '✓'
# Invalid package.json (version must be string)
! exec validator invalid_package.json
stdout '×'
stdout 'error:'
# --- tsconfig.json schema ---
# Valid tsconfig.json
exec validator valid_tsconfig.json
stdout '✓'
# Invalid tsconfig.json (target must be string)
! exec validator invalid_tsconfig.json
stdout '×'
stdout 'error:'
# --- eslintrc schema ---
# Valid eslintrc
exec validator valid_eslintrc.json
stdout '✓'
# --- workflow schema ---
# Valid workflow
exec validator valid_workflow.json
stdout '✓'
# Invalid workflow (missing required jobs)
! exec validator invalid_workflow.json
stdout '×'
stdout 'error:'
# --- Strict config schema ---
# Valid
exec validator valid_strict.json
stdout '✓'
# Missing required field
! exec validator missing_required.json
stdout '×'
stdout 'error:'
# Wrong type
! exec validator wrong_type.json
stdout '×'
stdout 'error:'
# Extra property not allowed
! exec validator extra_property.json
stdout '×'
stdout 'error:'
# Value out of range
! exec validator out_of_range.json
stdout '×'
stdout 'error:'
# Enum violation
! exec validator bad_enum.json
stdout '×'
stdout 'error:'
# --- Array schema ---
# Valid array
exec validator valid_array.json
stdout '✓'
# Too few items
! exec validator too_few_items.json
stdout '×'
stdout 'error:'
# Wrong item type
! exec validator wrong_item_type.json
stdout '×'
stdout 'error:'
# --- Mixed: files with and without $schema ---
# File without $schema passes (syntax only)
exec validator no_schema.json
stdout '✓'
# --require-schema fails file without $schema
! exec validator --require-schema no_schema.json
stdout 'no schema declared'
# --require-schema passes file with $schema
exec validator --require-schema valid_strict.json
stdout '✓'
# Multiple files: one with schema, one without
exec validator valid_strict.json no_schema.json
stdout '✓.*valid_strict'
stdout '✓.*no_schema'
# Multiple files with --require-schema: one fails
! exec validator --require-schema valid_strict.json no_schema.json
stdout '✓.*valid_strict'
stdout '×.*no_schema'
# --- Non-JSON files without SchemaValidator unaffected ---
exec validator good.ini
stdout '✓'
exec validator --require-schema good.ini
stdout '✓'
# ============================================================
# Local schema files
# ============================================================
-- package_schema.json --
{
"type": "object",
"properties": {
"name": { "type": "string" },
"version": { "type": "string" },
"description": { "type": "string" },
"main": { "type": "string" },
"scripts": { "type": "object" },
"license": { "type": "string" },
"dependencies": { "type": "object" },
"devDependencies": { "type": "object" }
}
}
-- tsconfig_schema.json --
{
"type": "object",
"properties": {
"compilerOptions": {
"type": "object",
"properties": {
"target": { "type": "string" },
"module": { "type": "string" },
"strict": { "type": "boolean" }
}
},
"include": { "type": "array", "items": { "type": "string" } },
"exclude": { "type": "array", "items": { "type": "string" } }
}
}
-- eslintrc_schema.json --
{
"type": "object",
"properties": {
"env": { "type": "object" },
"extends": {},
"rules": { "type": "object" },
"plugins": { "type": "array" },
"root": { "type": "boolean" }
}
}
-- workflow_schema.json --
{
"type": "object",
"required": ["on", "jobs"],
"properties": {
"name": { "type": "string" },
"on": {},
"jobs": { "type": "object" }
},
"additionalProperties": true
}
-- strict_schema.json --
{
"type": "object",
"required": ["host", "port", "database"],
"properties": {
"host": { "type": "string", "minLength": 1 },
"port": { "type": "integer", "minimum": 1, "maximum": 65535 },
"database": { "type": "string" },
"ssl": { "type": "boolean" },
"log_level": { "type": "string", "enum": ["debug", "info", "warn", "error"] }
},
"additionalProperties": false
}
-- array_schema.json --
{
"type": "object",
"required": ["items"],
"properties": {
"items": {
"type": "array",
"minItems": 1,
"items": { "type": "string" }
}
}
}
# ============================================================
# Test data files
# ============================================================
-- valid_package.json --
{
"$schema": "package_schema.json",
"name": "my-package",
"version": "1.0.0"
}
-- invalid_package.json --
{
"$schema": "package_schema.json",
"name": "my-package",
"version": 123
}
-- valid_tsconfig.json --
{
"$schema": "tsconfig_schema.json",
"compilerOptions": {
"target": "es2020",
"module": "commonjs",
"strict": true
}
}
-- invalid_tsconfig.json --
{
"$schema": "tsconfig_schema.json",
"compilerOptions": {
"target": 999,
"module": "commonjs"
}
}
-- valid_eslintrc.json --
{
"$schema": "eslintrc_schema.json",
"env": {
"browser": true,
"es2021": true
},
"rules": {}
}
-- valid_workflow.json --
{
"$schema": "workflow_schema.json",
"name": "CI",
"on": "push",
"jobs": {
"build": {
"runs-on": "ubuntu-latest",
"steps": [
{"uses": "actions/checkout@v4"}
]
}
}
}
-- invalid_workflow.json --
{
"$schema": "workflow_schema.json",
"name": "CI",
"on": "push"
}
-- valid_strict.json --
{
"$schema": "strict_schema.json",
"host": "db.example.com",
"port": 5432,
"database": "myapp",
"ssl": true,
"log_level": "info"
}
-- missing_required.json --
{
"$schema": "strict_schema.json",
"host": "db.example.com",
"port": 5432
}
-- wrong_type.json --
{
"$schema": "strict_schema.json",
"host": "db.example.com",
"port": "not_a_number",
"database": "myapp"
}
-- extra_property.json --
{
"$schema": "strict_schema.json",
"host": "db.example.com",
"port": 5432,
"database": "myapp",
"unknown_field": true
}
-- out_of_range.json --
{
"$schema": "strict_schema.json",
"host": "db.example.com",
"port": 99999,
"database": "myapp"
}
-- bad_enum.json --
{
"$schema": "strict_schema.json",
"host": "db.example.com",
"port": 5432,
"database": "myapp",
"log_level": "verbose"
}
-- valid_array.json --
{
"$schema": "array_schema.json",
"items": ["one", "two", "three"]
}
-- too_few_items.json --
{
"$schema": "array_schema.json",
"items": []
}
-- wrong_item_type.json --
{
"$schema": "array_schema.json",
"items": ["valid", 123, "also_valid"]
}
-- no_schema.json --
{"key": "value"}
-- good.ini --
[section]
key=value