-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_yamlschema.py
87 lines (73 loc) · 2.75 KB
/
check_yamlschema.py
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
import argparse
import json
import logging
import os
import re
import jsonschema
import requests
import yaml
def load_yaml_documents(file_path):
with open(file_path) as file:
content = file.read()
documents = content.split("---\n")
yaml_documents = []
for doc in documents:
if doc.strip() != "":
comment = None
schema_url = None
for line in doc.split("\n"):
if line.startswith("# yaml-language-server"):
comment = line
match = re.match(
r"# yaml-language-server: \$schema=(.*)$",
comment,
re.MULTILINE,
)
if match:
schema_url = match.group(1)
break
doc_dict = {
"content": yaml.safe_load(doc),
"comment": comment,
"schema_url": schema_url,
}
yaml_documents.append(doc_dict)
return yaml_documents
def download_schema(schema_url):
response = requests.get(schema_url)
response.raise_for_status()
return response.json()
def validate_document(file, document, schema_url):
if schema_url.startswith("http://") or schema_url.startswith("https://"):
schema = download_schema(schema_url)
else:
current_dir = os.path.dirname(os.path.realpath(file))
schema_path = os.path.join(current_dir, schema_url)
with open(schema_path) as file:
schema = json.load(file)
jsonschema.validate(instance=document, schema=schema)
def main():
logging.basicConfig(format="%(message)s")
parser = argparse.ArgumentParser(
description="Validate YAML document(s) against JSON schema."
)
parser.add_argument("files", nargs="+", help="Path to YAML files to validate")
parser.add_argument("-v", "--verbose", action="store_true")
args = parser.parse_args()
if args.verbose:
logging.getLogger().setLevel(logging.INFO)
for file in args.files:
yaml_documents = load_yaml_documents(file)
logging.info("File %s: %s documents", file, len(yaml_documents))
for index, doc in enumerate(yaml_documents):
if doc["schema_url"] is not None:
validate_document(file, doc["content"], doc["schema_url"])
logging.info(
"- Document %s validated according to %s", index, doc["schema_url"]
)
else:
logging.info(
"- Document %s has no JSON schema defined in comments", index
)
if __name__ == "__main__":
main()