-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_api_json.sh
82 lines (76 loc) · 2.02 KB
/
check_api_json.sh
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
#!/bin/bash
API_JSON_PATH="public/api.json"
# Check if public/api.json exists
if [ ! -f $API_JSON_PATH ]; then
echo "Error: $API_JSON_PATH does not exist."
exit 1
fi
# Check the structure of the JSON file
node -e "
const fs = require('fs');
const schema = {
type: 'object',
properties: {
domain: { type: 'string' },
categories: {
type: 'array',
items: {
type: 'object',
properties: {
category: { type: 'string' },
apis: {
type: 'array',
items: {
type: 'object',
properties: {
name: { type: 'string' },
endpoint: { type: 'string' },
method: { type: 'string' },
requiresAuth: { type: 'boolean' },
params: {
type: 'array',
items: {
type: 'object',
properties: {
name: { type: 'string' },
type: { type: 'string' },
required: { type: 'boolean' }
},
required: ['name', 'type', 'required']
}
}
},
required: ['name', 'endpoint', 'method', 'requiresAuth']
}
}
},
required: ['category', 'apis']
}
}
},
required: ['domain', 'categories']
};
fs.readFile('$API_JSON_PATH', 'utf8', (err, data) => {
if (err) {
console.error('Error reading $API_JSON_PATH:', err);
process.exit(1);
}
try {
const jsonData = JSON.parse(data);
const validate = require('jsonschema').validate;
const result = validate(jsonData, schema);
if (result.valid) {
console.log('$API_JSON_PATH is valid.');
} else {
console.error('$API_JSON_PATH is invalid.');
result.errors.forEach(error => {
console.error(' ' + error.stack);
});
process.exit(1);
}
} catch (e) {
console.error('Error parsing JSON:', e);
process.exit(1);
}
});
"