Skip to content

Commit

Permalink
add json schema validation
Browse files Browse the repository at this point in the history
  • Loading branch information
haidaraM committed Sep 3, 2024
1 parent d1bfd4c commit f662678
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"pattern": "^PlaybookNode$"
},
"id": {
"format": "^playbook_.+$",
"pattern": "^playbook_.+$",
"type": "string"
},
"name": {
Expand All @@ -43,7 +43,7 @@
"type": "object",
"properties": {
"type": {
"format": "^PlayNode$",
"pattern": "^PlayNode$",
"type": "string"
},
"id": {
Expand Down Expand Up @@ -173,6 +173,7 @@
}
},
"include_role": {
"description": "Flag indicating if this role is an include role or not.",
"type": "boolean"
}
},
Expand Down
3 changes: 2 additions & 1 deletion tests/requirements_tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ pytest==8.3.2
pytest-cov==5.0.0
pyquery==2.0.1
black~=24.8
jq==1.8.0
jq==1.8.0
jsonschema[format]==4.23.0
13 changes: 13 additions & 0 deletions tests/test_json_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import jq
import pytest
from jsonschema import validate
from jsonschema.validators import Draft202012Validator

from ansibleplaybookgrapher import __prog__
from ansibleplaybookgrapher.cli import PlaybookGrapherCLI
Expand Down Expand Up @@ -68,6 +70,17 @@ def _common_tests(
with open(json_path, "r") as f:
output = json.load(f)

with open(os.path.join(FIXTURES_DIR, "json-schemas/v1.json")) as schema_file:
schema = json.load(schema_file)

# If no exception is raised by validate(), the instance is valid.
# I currently don't use format but added it here to not forget to add in case I use in the future.
validate(
instance=output,
schema=schema,
format_checker=Draft202012Validator.FORMAT_CHECKER,
)

playbooks = jq.compile(".playbooks[]").input(output).all()

plays = jq.compile(".playbooks[].plays").input(output).first()
Expand Down

0 comments on commit f662678

Please sign in to comment.