Fix jsonschma input data validation (#50)

Fix jsonschma input data validation

Reviewed-by: https://github.com/apps/ansible-zuul
pull/55/head
Ganesh Nalawade 2021-03-15 14:30:44 +05:30 committed by GitHub
parent 4c531bb418
commit f5dd4b80e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 51 additions and 35 deletions

View File

@ -0,0 +1,3 @@
---
bugfixes:
- Fix jsonschema input data format checking (https://github.com/ansible-collections/ansible.utils/pull/50).

View File

@ -33,8 +33,7 @@ DOCUMENTATION = """
vars:
- name: ansible_validate_jsonschema_draft
notes:
- The value of I(data) option should be either of type B(dict) or B(strings) which should be
a valid B(dict) when read in python.
- 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
B(string) within the B(list) entry should be a valid B(dict) when read in python.
"""
@ -98,19 +97,14 @@ class Validate(ValidateBase):
:return: None: In case all arguments passed are valid
"""
try:
if isinstance(self._data, dict):
self._data = json.loads(json.dumps(self._data))
elif isinstance(self._data, string_types):
if isinstance(self._data, string_types):
self._data = json.loads(self._data)
else:
msg = "Expected value of 'data' option is either dict or str, received type '{data_type}'".format(
data_type=type(self._data)
)
raise AnsibleError(msg)
self._data = json.loads(json.dumps(self._data))
except (TypeError, JSONDecodeError) as exe:
msg = (
"'data' option value is invalid, value should of type dict or str format of dict."
"'data' option value is invalid, value should a valid JSON."
" Failed to read with error '{err}'".format(
err=to_text(exe, errors="surrogate_then_replace")
)
@ -120,20 +114,15 @@ class Validate(ValidateBase):
try:
criteria = []
for item in to_list(self._criteria):
if isinstance(item, dict):
criteria.append(json.loads(json.dumps(item)))
elif isinstance(self._criteria, string_types):
if isinstance(self._criteria, string_types):
criteria.append(json.loads(item))
else:
msg = "Expected value of 'criteria' option is either list of dict/str or dict or str, received type '{criteria_type}'".format(
criteria_type=type(criteria)
)
raise AnsibleError(msg)
criteria.append(json.loads(json.dumps(item)))
self._criteria = criteria
except (TypeError, JSONDecodeError) as exe:
msg = (
"'criteria' option value is invalid, value should of type dict or str format of dict."
"'criteria' option value is invalid, value should a valid JSON."
" Failed to read with error '{err}'".format(
err=to_text(exe, errors="surrogate_then_replace")
)

View File

@ -0,0 +1,17 @@
{
"definitions": {},
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://example.com/object1615264346.json",
"title": "Root",
"type": "array",
"default": [],
"items":{
"$id": "#root/items",
"title": "Items",
"type": "integer",
"examples": [
1
],
"default": 0
}
}

View File

@ -0,0 +1 @@
[1]

View File

@ -48,7 +48,7 @@
- assert:
that:
- "result['failed'] == true"
- "'\\'data\\' option value is invalid, value should of type dict or str format of dict' in result.msg"
- "'\\'data\\' option value is invalid' in result.msg"
- name: invalid criteria value
ansible.builtin.set_fact:
@ -59,7 +59,7 @@
- assert:
that:
- "result['failed'] == true"
- "'\\'criteria\\' option value is invalid, value should of type dict or str format of dict' in result.msg"
- "'\\'criteria\\' option value is invalid' in result.msg"
- name: read data and criteria from file
ansible.builtin.set_fact:

View File

@ -66,7 +66,7 @@
- assert:
that:
- "result['failed'] == true"
- "'\\'data\\' option value is invalid, value should of type dict or str format of dict' in result.msg"
- "'\\'data\\' option value is invalid' in result.msg"
- name: invalid criteria value
ansible.builtin.set_fact:
@ -77,7 +77,7 @@
- assert:
that:
- "result['failed'] == true"
- "'\\'criteria\\' option value is invalid, value should of type dict or str format of dict' in result.msg"
- "'\\'criteria\\' option value is invalid' in result.msg"
- name: read data and criteria from file
ansible.builtin.set_fact:

View File

@ -79,7 +79,7 @@
- assert:
that:
- "result['failed'] == true"
- "'\\'data\\' option value is invalid, value should of type dict or str format of dict' in result.msg"
- "'\\'data\\' option value is invalid' in result.msg"
- name: invalid criteria value
ansible.utils.validate:
@ -92,7 +92,7 @@
- assert:
that:
- "result['failed'] == true"
- "'\\'criteria\\' option value is invalid, value should of type dict or str format of dict' in result.msg"
- "'\\'criteria\\' option value is invalid' in result.msg"
- name: validate data using jsonschema engine (invalid data read from file)
ansible.utils.validate:
@ -132,3 +132,14 @@
that:
- "'errors' not in result"
- "'all checks passed' in result.msg"
- name: validate list data using jsonschema
ansible.utils.validate:
data: "{{ lookup('ansible.builtin.file', 'data/test_list_data.json') }}"
criteria: "{{ lookup('ansible.builtin.file', 'criteria/check_list_data.json') }}"
engine: ansible.utils.jsonschema
- assert:
that:
- "'errors' not in result"
- "'all checks passed' in result.msg"

View File

@ -46,7 +46,7 @@
- assert:
that:
- "result['failed'] == true"
- "'\\'data\\' option value is invalid, value should of type dict or str format of dict' in result.msg"
- "'\\'data\\' option value is invalid' in result.msg"
- name: invalid criteria value
ansible.builtin.set_fact:
@ -57,7 +57,7 @@
- assert:
that:
- "result['failed'] == true"
- "'\\'criteria\\' option value is invalid, value should of type dict or str format of dict' in result.msg"
- "'\\'criteria\\' option value is invalid' in result.msg"
- name: read data and criteria from file
ansible.builtin.set_fact:

View File

@ -158,10 +158,7 @@ class TestValidate(unittest.TestCase):
with self.assertRaises(AnsibleActionFail) as error:
self._plugin.run(task_vars=None)
self.assertIn(
"'data' option value is invalid, value should of type dict or str format of dict",
str(error.exception),
)
self.assertIn("'data' option value is invalid", str(error.exception))
# invalid criteria option value
self._plugin._task.args = {
@ -173,8 +170,7 @@ class TestValidate(unittest.TestCase):
with self.assertRaises(AnsibleActionFail) as error:
self._plugin.run(task_vars=None)
self.assertIn(
"'criteria' option value is invalid, value should of type dict or str format of dict",
str(error.exception),
"'criteria' option value is invalid", str(error.exception)
)
def test_invalid_validate_plugin_config_options(self):

View File

@ -118,10 +118,9 @@ class TestValidate(unittest.TestCase):
# missing required arguments
with self.assertRaises(AnsibleFilterError) as error:
validate([DATA], {})
validate([DATA])
self.assertIn(
"Expected value of 'data' option is either dict or str, received type",
str(error.exception),
"Missing either 'data' or 'criteria' value", str(error.exception)
)
args = [DATA, [CRITERIA_IN_RATE_CHECK]]