From 7a3422666f99c6af25aed54e67aa5169daf78836 Mon Sep 17 00:00:00 2001 From: Andrea Cardaci Date: Fri, 31 Aug 2018 15:33:14 +0200 Subject: [PATCH] Add a simple YAML schema validation test --- .travis.yml | 2 +- Makefile | 1 + _config.yml | 2 +- scripts/validate-schema.py | 60 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+), 2 deletions(-) create mode 100755 scripts/validate-schema.py diff --git a/.travis.yml b/.travis.yml index 43f3914..7248b87 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,5 @@ --- install: - - pip install --user yamllint + - pip install --user jsonschema yamllint script: - make lint diff --git a/Makefile b/Makefile index 0193ca1..bfdc002 100644 --- a/Makefile +++ b/Makefile @@ -11,3 +11,4 @@ bundle: lint: yamllint . _gtfobins/*.md + scripts/validate-schema.py diff --git a/_config.yml b/_config.yml index e5f5010..a84b358 100644 --- a/_config.yml +++ b/_config.yml @@ -1,7 +1,7 @@ --- title: GTFOBins -exclude: ['/Gemfile', '/Makefile', '/README.md', '/CONTRIBUTING.md'] +exclude: ['/scripts', '/Gemfile', '/Makefile', '/README.md', '/CONTRIBUTING.md'] permalink: pretty diff --git a/scripts/validate-schema.py b/scripts/validate-schema.py new file mode 100755 index 0000000..64fd588 --- /dev/null +++ b/scripts/validate-schema.py @@ -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/")