forked from ObieSource/obiesource.github.io
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjsonlinter
More file actions
executable file
·84 lines (72 loc) · 2.56 KB
/
Copy pathjsonlinter
File metadata and controls
executable file
·84 lines (72 loc) · 2.56 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
#!/usr/bin/env python3
# json linter
# William Rehwinkel / obiesource, 2022
import sys
import os
import json
import traceback
from glob import glob
def get_json_files():
return glob("./**/*.json", recursive=True)
MEMBERS_REQUIRED = {
"name": "",
"pronouns": "",
"class": "",
"socials": list(),
"bio": "",
"projects": list()
}
PROJECTS_REQUIRED = {
"name": "",
"description": "",
"website": ""
}
def lint_json(filename):
print("Checking file " + filename)
with open(filename) as file:
try: js = json.load(file)
except Exception as e:
print("Error while parsing " + filename)
traceback.print_tb(e)
return True
failed = False
if filename.startswith("./members/"):
for k, v in MEMBERS_REQUIRED.items():
# note: doesn't check for required keys anymore
t = type(v)
if k in js and type(js[k]) != t:
print("Key " + k + " has incorrect type " + str(type(js[k])) + ", needs " + str(t))
failed=True
# check for any keys in members
for k in js:
if k not in MEMBERS_REQUIRED:
print("Extraneous key " + k + " found in members JSON file.")
failed = True
# check for types in ["socials"]
if "socials" in js:
l = js["socials"]
for item in l:
if type(item) != type(""):
print("A value in socials is not a string.", type(item), type(""))
failed = True
# check for correct types of "projects"
if "projects" in js:
p = js["projects"]
for item in p:
for k in PROJECTS_REQUIRED:
if k in item and type(PROJECTS_REQUIRED[k]) != type(item[k]):
print("Value for key " + k + " has incorrect type. Got", type(item[k]), "got", type(PROJECTS_REQUIRED[k]))
failed = True
for k in item:
if k not in PROJECTS_REQUIRED:
print("Key", k, "is in \"projects\" but is extraneous.")
failed = True
return failed
if __name__ == "__main__":
jsonFiles = get_json_files()
failed = False
for file in jsonFiles:
if lint_json(file) == True:
failed = True
if failed:
sys.exit(1)