From bf6425d96673cbb25238f4903a5270ac696e8827 Mon Sep 17 00:00:00 2001 From: Songmin Li Date: Sun, 15 Sep 2024 18:59:29 +0800 Subject: [PATCH 1/3] validate: Add suppress_output option to jsonschema validator Signed-off-by: Songmin Li --- plugins/sub_plugins/validate/jsonschema.py | 23 ++++++++++++ tests/unit/plugins/action/test_validate.py | 42 ++++++++++++++++++++++ 2 files changed, 65 insertions(+) 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..4bc91a4a 100644 --- a/tests/unit/plugins/action/test_validate.py +++ b/tests/unit/plugins/action/test_validate.py @@ -297,3 +297,45 @@ 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) From eedf51a6b7b28d88f12ccec1f6ab6852cccbabe9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 15 Sep 2024 12:11:35 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- plugins/sub_plugins/validate/jsonschema.py | 6 +++++- tests/unit/plugins/action/test_validate.py | 10 ++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/plugins/sub_plugins/validate/jsonschema.py b/plugins/sub_plugins/validate/jsonschema.py index 6d55ec72..388cdf2b 100644 --- a/plugins/sub_plugins/validate/jsonschema.py +++ b/plugins/sub_plugins/validate/jsonschema.py @@ -282,7 +282,11 @@ def _validate_jsonschema(self): suppress_fields = [] if suppress_output: - suppress_fields = suppress_output if isinstance(suppress_output, list) else ["found", "relative_schema"] + 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): diff --git a/tests/unit/plugins/action/test_validate.py b/tests/unit/plugins/action/test_validate.py index 4bc91a4a..9928193d 100644 --- a/tests/unit/plugins/action/test_validate.py +++ b/tests/unit/plugins/action/test_validate.py @@ -308,7 +308,7 @@ def test_suppress_output_is_false(self): } result = self._plugin.run(task_vars=None) - error = result.get('errors', [])[0] + error = result.get("errors", [])[0] self.assertIn("found", error) self.assertIn("relative_schema", error) @@ -322,7 +322,7 @@ def test_suppress_output_is_true(self): } result = self._plugin.run(task_vars=dict(ansible_validate_jsonschema_suppress_output=True)) - error = result.get('errors', [])[0] + error = result.get("errors", [])[0] self.assertNotIn("found", error) self.assertNotIn("relative_schema", error) @@ -335,7 +335,9 @@ def test_suppress_output_is_a_list(self): "criteria": CRITERIA_FORMAT_SUPPORT_CHECK, } - result = self._plugin.run(task_vars=dict(ansible_validate_jsonschema_suppress_output=['relative_schema'])) - error = result.get('errors', [])[0] + 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) From ad028981072c49e36987d4e1ffdb1547cb5bf9cc Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 15 Sep 2024 12:21:48 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/unit/plugins/action/test_validate.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/plugins/action/test_validate.py b/tests/unit/plugins/action/test_validate.py index 9928193d..58f5d209 100644 --- a/tests/unit/plugins/action/test_validate.py +++ b/tests/unit/plugins/action/test_validate.py @@ -336,7 +336,7 @@ def test_suppress_output_is_a_list(self): } result = self._plugin.run( - task_vars=dict(ansible_validate_jsonschema_suppress_output=["relative_schema"]) + task_vars=dict(ansible_validate_jsonschema_suppress_output=["relative_schema"]), ) error = result.get("errors", [])[0] self.assertIn("found", error)