diff --git a/plugins/sub_plugins/validate/jsonschema.py b/plugins/sub_plugins/validate/jsonschema.py index 1fda0af6..6d55ec72 100644 --- a/plugins/sub_plugins/validate/jsonschema.py +++ b/plugins/sub_plugins/validate/jsonschema.py @@ -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 @@ -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: @@ -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 = { @@ -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"], diff --git a/tests/unit/plugins/action/test_validate.py b/tests/unit/plugins/action/test_validate.py index dc1f861e..2305c7fb 100644 --- a/tests/unit/plugins/action/test_validate.py +++ b/tests/unit/plugins/action/test_validate.py @@ -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)