1
0
Fork 0
mirror of https://github.com/GTFOBins/GTFOBins.github.io.git synced 2024-05-06 23:56:04 +02:00
GTFOBins.github.io/scripts/validate-schema.py
2018-08-31 15:50:54 +02:00

61 lines
1.7 KiB
Python
Executable File

#!/usr/bin/env python3
import jsonschema
import os
import sys
import yaml
def parse_yaml(path):
with open(path) as fs:
text = fs.read()
return yaml.load_all(text)
def build_schema():
function_names = next(parse_yaml('_data/functions.yml')).keys()
return {
"definitions": {
'examples': {
'type': 'array',
'items': {
'type': 'object',
'properties': {
'description': {'type': 'string'},
'code': {'type': 'string'}
},
'required': ['code'],
'additionalProperties': False
},
'minimum': 1
}
},
'type': 'object',
'properties': {
'description': {'type': 'string'},
'functions': {
'type': 'object',
"patternProperties": {
'|'.join(function_names): {'$ref': '#/definitions/examples'}
},
'additionalProperties': False
}
},
'required': ['functions'],
'additionalProperties': False
}
def validate_directory(root):
schema = build_schema()
root, _, files = next(os.walk(root))
for name in files:
if not name.endswith('.md'):
continue
path = os.path.join(root, name)
data = parse_yaml(path)
try:
jsonschema.validate(next(data), schema)
except jsonschema.exceptions.ValidationError as err:
print('{}: {}'.format(name, err))
sys.exit(1)
if __name__ == '__main__':
validate_directory("_gtfobins/")