Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validate: Add suppress_output option to jsonschema validator #374

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 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,14 @@ 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 +300,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
44 changes: 44 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,47 @@ 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)
error = result.get("errors", [])[0]
self.assertIn("found", error)
self.assertIn("relative_schema", error)

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,
}

result = self._plugin.run(task_vars=dict(ansible_validate_jsonschema_suppress_output=True))
error = result.get("errors", [])[0]
self.assertNotIn("found", error)
self.assertNotIn("relative_schema", error)

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,
}

result = self._plugin.run(
task_vars=dict(ansible_validate_jsonschema_suppress_output=["relative_schema"]),
)
error = result.get("errors", [])[0]
self.assertIn("found", error)
self.assertNotIn("relative_schema", error)
Loading