Skip to content

Commit

Permalink
validate: Add suppress_output option to jsonschema validator
Browse files Browse the repository at this point in the history
Signed-off-by: Songmin Li <[email protected]>
  • Loading branch information
lisongmin committed Sep 15, 2024
1 parent c4ace98 commit b4a0a01
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
23 changes: 23 additions & 0 deletions plugins/sub_plugins/validate/jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@
- name: ANSIBLE_VALIDATE_JSONSCHEMA_CHECK_FORMAT
vars:
- name: ansible_validate_jsonschema_check_format
suppress_output:
description:
- Suppress the fields of error output.
- It's useful when the I(criteria) is too large and the error output is not required.
- And also useful when the I(data) is too large or contains sensible info.
- Can be a boolean or a list of strings.
- If set to true, then the I(found) and I(relative_schema) will be suppressed.
- If set to a list of strings, then the fields mentioned in the list will be suppressed.
type: raw
default: false
env:
- name: ANSIBLE_VALIDATE_JSONSCHEMA_SUPPRESS_OUTPUT
vars:
- name: ansible_validate_jsonschema_suppress_output
notes:
- The value of I(data) option should be either a valid B(JSON) object or a B(JSON) string.
- The value of I(criteria) should be B(list) of B(dict) or B(list) of B(strings) and each
Expand Down Expand Up @@ -221,6 +235,7 @@ def _validate_jsonschema(self):

draft = self._get_sub_plugin_options("draft")
check_format = self._get_sub_plugin_options("check_format")
suppress_output = self._get_sub_plugin_options("suppress_output")
error_messages = []

for criteria in self._criteria:
Expand Down Expand Up @@ -265,6 +280,10 @@ def _validate_jsonschema(self):
if "errors" not in self._result:
self._result["errors"] = []

suppress_fields = []
if suppress_output:
suppress_fields = suppress_output if isinstance(suppress_output, list) else ["found", "relative_schema"]

for validation_error in validation_errors:
if isinstance(validation_error, jsonschema.ValidationError):
error = {
Expand All @@ -277,6 +296,10 @@ def _validate_jsonschema(self):
"validator": validation_error.validator,
"found": validation_error.instance,
}

for field in suppress_fields:
error.pop(field, None)

self._result["errors"].append(error)
error_message = "At '{schema_path}' {message}. ".format(
schema_path=error["schema_path"],
Expand Down
41 changes: 41 additions & 0 deletions tests/unit/plugins/action/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,3 +297,44 @@ def test_support_for_disabled_format_with_invalid_data(self):

result = self._plugin.run(task_vars=dict(ansible_validate_jsonschema_check_format=False))
self.assertIn("All checks passed", result["msg"])

def test_suppress_output_is_false(self):
"""The `found` and `relative_schema` will output if suppress_output is false"""

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("found", result)
self.assertIn("relative_schema", result)

def test_suppress_output_is_true(self):
"""The `found` and `relative_schema` will not output if suppress_output is True"""

self._plugin._task.args = {
"engine": "ansible.utils.jsonschema",
"data": IN_VALID_DATA,
"criteria": CRITERIA_FORMAT_SUPPORT_CHECK,
"suppress_output": True,
}

result = self._plugin.run(task_vars=dict(ansible_validate_jsonschema_suppress_output=True))
self.assertNotIn("found", result)
self.assertNotIn("relative_schema", result)

def test_suppress_output_is_a_list(self):
"""The fields in suppress_output will be suppressed"""

self._plugin._task.args = {
"engine": "ansible.utils.jsonschema",
"data": IN_VALID_DATA,
"criteria": CRITERIA_FORMAT_SUPPORT_CHECK,
"suppress_output": ["relative_schema"],
}

result = self._plugin.run(task_vars=dict(ansible_validate_jsonschema_suppress_output=['relative_schema']))
self.assertIn("found", result)
self.assertNotIn("relative_schema", result)

0 comments on commit b4a0a01

Please sign in to comment.