Add a simple YAML schema validation test

This commit is contained in:
Andrea Cardaci 2018-08-31 15:33:14 +02:00
parent 6bfc58daab
commit 7a3422666f
4 changed files with 63 additions and 2 deletions

View File

@ -1,5 +1,5 @@
---
install:
- pip install --user yamllint
- pip install --user jsonschema yamllint
script:
- make lint

View File

@ -11,3 +11,4 @@ bundle:
lint:
yamllint . _gtfobins/*.md
scripts/validate-schema.py

View File

@ -1,7 +1,7 @@
---
title: GTFOBins
exclude: ['/Gemfile', '/Makefile', '/README.md', '/CONTRIBUTING.md']
exclude: ['/scripts', '/Gemfile', '/Makefile', '/README.md', '/CONTRIBUTING.md']
permalink: pretty

60
scripts/validate-schema.py Executable file
View File

@ -0,0 +1,60 @@
#!/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/")