Skip to content

Commit 7a34226

Browse files
committed
Add a simple YAML schema validation test
1 parent 6bfc58d commit 7a34226

File tree

4 files changed

+63
-2
lines changed

4 files changed

+63
-2
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
22
install:
3-
- pip install --user yamllint
3+
- pip install --user jsonschema yamllint
44
script:
55
- make lint

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ bundle:
1111

1212
lint:
1313
yamllint . _gtfobins/*.md
14+
scripts/validate-schema.py

_config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: GTFOBins
33

4-
exclude: ['/Gemfile', '/Makefile', '/README.md', '/CONTRIBUTING.md']
4+
exclude: ['/scripts', '/Gemfile', '/Makefile', '/README.md', '/CONTRIBUTING.md']
55

66
permalink: pretty
77

scripts/validate-schema.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env python3
2+
import jsonschema
3+
import os
4+
import sys
5+
import yaml
6+
7+
def parse_yaml(path):
8+
with open(path) as fs:
9+
text = fs.read()
10+
return yaml.load_all(text)
11+
12+
def build_schema():
13+
function_names = next(parse_yaml('_data/functions.yml')).keys()
14+
return {
15+
"definitions": {
16+
'examples': {
17+
'type': 'array',
18+
'items': {
19+
'type': 'object',
20+
'properties': {
21+
'description': {'type': 'string'},
22+
'code': {'type': 'string'}
23+
},
24+
'required': ['code'],
25+
'additionalProperties': False
26+
},
27+
'minimum': 1
28+
}
29+
},
30+
'type': 'object',
31+
'properties': {
32+
'description': {'type': 'string'},
33+
'functions': {
34+
'type': 'object',
35+
"patternProperties": {
36+
'|'.join(function_names): {'$ref': '#/definitions/examples'}
37+
},
38+
'additionalProperties': False
39+
}
40+
},
41+
'required': ['functions'],
42+
'additionalProperties': False
43+
}
44+
45+
def validate_directory(root):
46+
schema = build_schema()
47+
root, _, files = next(os.walk(root))
48+
for name in files:
49+
if not name.endswith('.md'):
50+
continue
51+
path = os.path.join(root, name)
52+
data = parse_yaml(path)
53+
try:
54+
jsonschema.validate(next(data), schema)
55+
except jsonschema.exceptions.ValidationError as err:
56+
print('{}: {}'.format(name, err))
57+
sys.exit(1)
58+
59+
if __name__ == '__main__':
60+
validate_directory("_gtfobins/")

0 commit comments

Comments
 (0)