Skip to content

Commit 161a8d8

Browse files
committed
validate: Add suppress_output option to jsonschema validator
Signed-off-by: Songmin Li <[email protected]>
1 parent c4ace98 commit 161a8d8

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

plugins/sub_plugins/validate/jsonschema.py

+23
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@
4545
- name: ANSIBLE_VALIDATE_JSONSCHEMA_CHECK_FORMAT
4646
vars:
4747
- name: ansible_validate_jsonschema_check_format
48+
suppress_output:
49+
description:
50+
- Suppress the fields of error output.
51+
- It's useful when the I(criteria) is too large and the error output is not required.
52+
- And also useful when the I(data) is too large or contains sensible info.
53+
- Can be a boolean or a list of strings.
54+
- If set to true, then the I(found) and I(relative_schema) will be suppressed.
55+
- If set to a list of strings, then the fields mentioned in the list will be suppressed.
56+
type: raw
57+
default: false
58+
env:
59+
- name: ANSIBLE_VALIDATE_JSONSCHEMA_SUPPRESS_OUTPUT
60+
vars:
61+
- name: ansible_validate_jsonschema_suppress_output
4862
notes:
4963
- The value of I(data) option should be either a valid B(JSON) object or a B(JSON) string.
5064
- 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):
221235

222236
draft = self._get_sub_plugin_options("draft")
223237
check_format = self._get_sub_plugin_options("check_format")
238+
suppress_output = self._get_sub_plugin_options("suppress_output")
224239
error_messages = []
225240

226241
for criteria in self._criteria:
@@ -265,6 +280,10 @@ def _validate_jsonschema(self):
265280
if "errors" not in self._result:
266281
self._result["errors"] = []
267282

283+
suppress_fields = []
284+
if suppress_output:
285+
suppress_fields = suppress_output if isinstance(suppress_output, list) else ["found", "relative_schema"]
286+
268287
for validation_error in validation_errors:
269288
if isinstance(validation_error, jsonschema.ValidationError):
270289
error = {
@@ -277,6 +296,10 @@ def _validate_jsonschema(self):
277296
"validator": validation_error.validator,
278297
"found": validation_error.instance,
279298
}
299+
300+
for field in suppress_fields:
301+
error.pop(field, None)
302+
280303
self._result["errors"].append(error)
281304
error_message = "At '{schema_path}' {message}. ".format(
282305
schema_path=error["schema_path"],

tests/unit/plugins/action/test_validate.py

+41
Original file line numberDiff line numberDiff line change
@@ -297,3 +297,44 @@ def test_support_for_disabled_format_with_invalid_data(self):
297297

298298
result = self._plugin.run(task_vars=dict(ansible_validate_jsonschema_check_format=False))
299299
self.assertIn("All checks passed", result["msg"])
300+
301+
def test_suppress_output_is_false(self):
302+
"""The `found` and `relative_schema` will output if suppress_output is false"""
303+
304+
self._plugin._task.args = {
305+
"engine": "ansible.utils.jsonschema",
306+
"data": IN_VALID_DATA,
307+
"criteria": CRITERIA_FORMAT_SUPPORT_CHECK,
308+
}
309+
310+
result = self._plugin.run(task_vars=None)
311+
self.assertIn("found", result)
312+
self.assertIn("relative_schema", result)
313+
314+
def test_suppress_output_is_true(self):
315+
"""The `found` and `relative_schema` will not output if suppress_output is True"""
316+
317+
self._plugin._task.args = {
318+
"engine": "ansible.utils.jsonschema",
319+
"data": IN_VALID_DATA,
320+
"criteria": CRITERIA_FORMAT_SUPPORT_CHECK,
321+
"suppress_output": True,
322+
}
323+
324+
result = self._plugin.run(task_vars=None)
325+
self.assertNotIn("found", result)
326+
self.assertNotIn("relative_schema", result)
327+
328+
def test_suppress_output_is_a_list(self):
329+
"""The fields in suppress_output will be suppressed"""
330+
331+
self._plugin._task.args = {
332+
"engine": "ansible.utils.jsonschema",
333+
"data": IN_VALID_DATA,
334+
"criteria": CRITERIA_FORMAT_SUPPORT_CHECK,
335+
"suppress_output": ["relative_schema"],
336+
}
337+
338+
result = self._plugin.run(task_vars=None)
339+
self.assertIn("found", result)
340+
self.assertNotIn("relative_schema", result)

0 commit comments

Comments
 (0)