Add support for the validation of formats to the json schema validator (#83)
Add support for the validation of formats to the json schema validator SUMMARY fixes: #81 ISSUE TYPE Bugfix Pull Request COMPONENT NAME ADDITIONAL INFORMATION Reviewed-by: Ganesh Nalawade <None>pull/87/head
parent
6ab60372e1
commit
45eb99fb94
|
@ -0,0 +1,4 @@
|
||||||
|
---
|
||||||
|
bugfixes:
|
||||||
|
- Add support for the validation of formats to the jsonschema validator.
|
||||||
|
- Improve test coverage
|
|
@ -160,13 +160,21 @@ class Validate(ValidateBase):
|
||||||
|
|
||||||
for criteria in self._criteria:
|
for criteria in self._criteria:
|
||||||
if draft == "draft3":
|
if draft == "draft3":
|
||||||
validator = jsonschema.Draft3Validator(criteria)
|
validator = jsonschema.Draft3Validator(
|
||||||
|
criteria, format_checker=jsonschema.draft3_format_checker
|
||||||
|
)
|
||||||
elif draft == "draft4":
|
elif draft == "draft4":
|
||||||
validator = jsonschema.Draft4Validator(criteria)
|
validator = jsonschema.Draft4Validator(
|
||||||
|
criteria, format_checker=jsonschema.draft4_format_checker
|
||||||
|
)
|
||||||
elif draft == "draft6":
|
elif draft == "draft6":
|
||||||
validator = jsonschema.Draft6Validator(criteria)
|
validator = jsonschema.Draft6Validator(
|
||||||
|
criteria, format_checker=jsonschema.draft6_format_checker
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
validator = jsonschema.Draft7Validator(criteria)
|
validator = jsonschema.Draft7Validator(
|
||||||
|
criteria, format_checker=jsonschema.draft7_format_checker
|
||||||
|
)
|
||||||
|
|
||||||
validation_errors = sorted(
|
validation_errors = sorted(
|
||||||
validator.iter_errors(self._data), key=lambda e: e.path
|
validator.iter_errors(self._data), key=lambda e: e.path
|
||||||
|
|
|
@ -0,0 +1,10 @@
|
||||||
|
{
|
||||||
|
"$schema": "https://json-schema.org/schema#",
|
||||||
|
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"name": {"type": "string"},
|
||||||
|
"email": {"format": "email"}
|
||||||
|
},
|
||||||
|
"required": ["email"]
|
||||||
|
}
|
|
@ -0,0 +1,4 @@
|
||||||
|
{
|
||||||
|
"name": "ansible",
|
||||||
|
"email": "ansible@redhat.com"
|
||||||
|
}
|
|
@ -87,3 +87,16 @@
|
||||||
- assert:
|
- assert:
|
||||||
that:
|
that:
|
||||||
- "is_data_valid == true"
|
- "is_data_valid == true"
|
||||||
|
|
||||||
|
- name: read data and criteria from file
|
||||||
|
ansible.builtin.set_fact:
|
||||||
|
data: "{{ lookup('ansible.builtin.file', 'data/test_format_checker.json') }}"
|
||||||
|
criteria_1: "{{ lookup('ansible.builtin.file', 'criteria/format_checker.json') }}"
|
||||||
|
|
||||||
|
- name: validate data using jsonschema engine (valid data read from file)
|
||||||
|
ansible.builtin.set_fact:
|
||||||
|
is_data_valid: "{{ data is ansible.utils.validate(engine='ansible.utils.jsonschema', criteria=[criteria_1], draft='draft7') }}"
|
||||||
|
|
||||||
|
- assert:
|
||||||
|
that:
|
||||||
|
- "is_data_valid == true"
|
||||||
|
|
|
@ -108,6 +108,17 @@ CRITERIA_IN_RATE_CHECK = {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
VALID_DATA = {"name": "ansible", "email": "ansible@redhat.com"}
|
||||||
|
|
||||||
|
IN_VALID_DATA = {"name": "ansible", "email": "redhatcom"}
|
||||||
|
|
||||||
|
CRITERIA_FORMAT_SUPPORT_CHECK = {
|
||||||
|
"$schema": "https://json-schema.org/schema#",
|
||||||
|
"type": "object",
|
||||||
|
"properties": {"name": {"type": "string"}, "email": {"format": "email"}},
|
||||||
|
"required": ["email"],
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class TestValidate(unittest.TestCase):
|
class TestValidate(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
@ -190,6 +201,48 @@ class TestValidate(unittest.TestCase):
|
||||||
result["msg"],
|
result["msg"],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_validate_plugin_config_options_with_draft3(self):
|
||||||
|
"""Check passing invalid validate plugin options"""
|
||||||
|
|
||||||
|
self._plugin._task.args = {
|
||||||
|
"engine": "ansible.utils.jsonschema",
|
||||||
|
"data": DATA,
|
||||||
|
"criteria": CRITERIA_IN_RATE_CHECK,
|
||||||
|
}
|
||||||
|
|
||||||
|
result = self._plugin.run(
|
||||||
|
task_vars={"ansible_validate_jsonschema_draft": "draft3"}
|
||||||
|
)
|
||||||
|
self.assertIn("all checks passed", result["msg"])
|
||||||
|
|
||||||
|
def test_validate_plugin_config_options_with_draft4(self):
|
||||||
|
"""Check passing invalid validate plugin options"""
|
||||||
|
|
||||||
|
self._plugin._task.args = {
|
||||||
|
"engine": "ansible.utils.jsonschema",
|
||||||
|
"data": DATA,
|
||||||
|
"criteria": CRITERIA_IN_RATE_CHECK,
|
||||||
|
}
|
||||||
|
|
||||||
|
result = self._plugin.run(
|
||||||
|
task_vars={"ansible_validate_jsonschema_draft": "draft4"}
|
||||||
|
)
|
||||||
|
self.assertIn("all checks passed", result["msg"])
|
||||||
|
|
||||||
|
def test_validate_plugin_config_options_with_draft6(self):
|
||||||
|
"""Check passing invalid validate plugin options"""
|
||||||
|
|
||||||
|
self._plugin._task.args = {
|
||||||
|
"engine": "ansible.utils.jsonschema",
|
||||||
|
"data": DATA,
|
||||||
|
"criteria": CRITERIA_IN_RATE_CHECK,
|
||||||
|
}
|
||||||
|
|
||||||
|
result = self._plugin.run(
|
||||||
|
task_vars={"ansible_validate_jsonschema_draft": "draft6"}
|
||||||
|
)
|
||||||
|
self.assertIn("all checks passed", result["msg"])
|
||||||
|
|
||||||
def test_invalid_data(self):
|
def test_invalid_data(self):
|
||||||
"""Check passing invalid data as per criteria"""
|
"""Check passing invalid data as per criteria"""
|
||||||
|
|
||||||
|
@ -227,3 +280,27 @@ class TestValidate(unittest.TestCase):
|
||||||
|
|
||||||
result = self._plugin.run(task_vars=None)
|
result = self._plugin.run(task_vars=None)
|
||||||
self.assertIn("all checks passed", result["msg"])
|
self.assertIn("all checks passed", result["msg"])
|
||||||
|
|
||||||
|
def test_support_for_format(self):
|
||||||
|
"""Check passing valid data as per criteria"""
|
||||||
|
|
||||||
|
self._plugin._task.args = {
|
||||||
|
"engine": "ansible.utils.jsonschema",
|
||||||
|
"data": VALID_DATA,
|
||||||
|
"criteria": CRITERIA_FORMAT_SUPPORT_CHECK,
|
||||||
|
}
|
||||||
|
|
||||||
|
result = self._plugin.run(task_vars=None)
|
||||||
|
self.assertIn("all checks passed", result["msg"])
|
||||||
|
|
||||||
|
def test_support_for_format_with_invalid_data(self):
|
||||||
|
"""Check passing valid data as per criteria"""
|
||||||
|
|
||||||
|
self._plugin._task.args = {
|
||||||
|
"engine": "ansible.utils.jsonschema",
|
||||||
|
"data": IN_VALID_DATA,
|
||||||
|
"criteria": CRITERIA_FORMAT_SUPPORT_CHECK,
|
||||||
|
}
|
||||||
|
|
||||||
|
result = self._plugin.run(task_vars=None)
|
||||||
|
self.assertIn("Validation errors were found", result["msg"])
|
||||||
|
|
Loading…
Reference in New Issue