mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Perform full RETURN schema validation in one step, don't try to loop (#46079)
This commit is contained in:
parent
c5a8a911b3
commit
86e8d21667
3 changed files with 44 additions and 38 deletions
|
@ -823,10 +823,15 @@ class ModuleValidator(Validator):
|
||||||
else:
|
else:
|
||||||
error_message = error
|
error_message = error
|
||||||
|
|
||||||
|
if path:
|
||||||
|
combined_path = '%s.%s' % (name, '.'.join(path))
|
||||||
|
else:
|
||||||
|
combined_path = name
|
||||||
|
|
||||||
self.reporter.error(
|
self.reporter.error(
|
||||||
path=self.object_path,
|
path=self.object_path,
|
||||||
code=error_code,
|
code=error_code,
|
||||||
msg='%s.%s: %s' % (name, '.'.join(path), error_message)
|
msg='%s: %s' % (combined_path, error_message)
|
||||||
)
|
)
|
||||||
|
|
||||||
def _validate_docs(self):
|
def _validate_docs(self):
|
||||||
|
@ -980,7 +985,6 @@ class ModuleValidator(Validator):
|
||||||
msg='No EXAMPLES provided'
|
msg='No EXAMPLES provided'
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
examples_exists = True
|
|
||||||
_, errors, traces = parse_yaml(doc_info['EXAMPLES']['value'],
|
_, errors, traces = parse_yaml(doc_info['EXAMPLES']['value'],
|
||||||
doc_info['EXAMPLES']['lineno'],
|
doc_info['EXAMPLES']['lineno'],
|
||||||
self.name, 'EXAMPLES', load_all=True)
|
self.name, 'EXAMPLES', load_all=True)
|
||||||
|
@ -997,7 +1001,6 @@ class ModuleValidator(Validator):
|
||||||
)
|
)
|
||||||
|
|
||||||
if not bool(doc_info['RETURN']['value']):
|
if not bool(doc_info['RETURN']['value']):
|
||||||
returns_exists = True
|
|
||||||
if self._is_new_module():
|
if self._is_new_module():
|
||||||
self.reporter.error(
|
self.reporter.error(
|
||||||
path=self.object_path,
|
path=self.object_path,
|
||||||
|
@ -1014,9 +1017,7 @@ class ModuleValidator(Validator):
|
||||||
data, errors, traces = parse_yaml(doc_info['RETURN']['value'],
|
data, errors, traces = parse_yaml(doc_info['RETURN']['value'],
|
||||||
doc_info['RETURN']['lineno'],
|
doc_info['RETURN']['lineno'],
|
||||||
self.name, 'RETURN')
|
self.name, 'RETURN')
|
||||||
if data:
|
self._validate_docs_schema(data, return_schema, 'RETURN', 319)
|
||||||
for ret_key in data:
|
|
||||||
self._validate_docs_schema(data[ret_key], return_schema(data[ret_key]), 'RETURN.%s' % ret_key, 319)
|
|
||||||
|
|
||||||
for error in errors:
|
for error in errors:
|
||||||
self.reporter.error(
|
self.reporter.error(
|
||||||
|
|
|
@ -16,9 +16,10 @@
|
||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
from voluptuous import PREVENT_EXTRA, All, Any, Length, Required, Schema, Self
|
from voluptuous import ALLOW_EXTRA, PREVENT_EXTRA, All, Any, Length, Required, Schema, Self
|
||||||
from ansible.module_utils.six import string_types
|
from ansible.module_utils.six import string_types
|
||||||
list_string_types = list(string_types)
|
list_string_types = list(string_types)
|
||||||
|
any_string_types = Any(*string_types)
|
||||||
|
|
||||||
|
|
||||||
def sequence_of_sequences(min=None, max=None):
|
def sequence_of_sequences(min=None, max=None):
|
||||||
|
@ -91,34 +92,41 @@ option_schema = Schema(
|
||||||
list_dict_option_schema = [{str_type: option_schema} for str_type in string_types]
|
list_dict_option_schema = [{str_type: option_schema} for str_type in string_types]
|
||||||
|
|
||||||
|
|
||||||
def return_schema(data):
|
def return_contains(v):
|
||||||
|
schema = Schema(
|
||||||
return_schema_dict = {
|
{
|
||||||
Required('description'): Any(list_string_types, *string_types),
|
Required('contains'): Any(dict, list, *string_types)
|
||||||
Required('returned'): Any(*string_types),
|
},
|
||||||
Required('type'): Any('string', 'list', 'boolean', 'dict', 'complex', 'bool', 'float', 'int', 'dictionary', 'str'),
|
extra=ALLOW_EXTRA
|
||||||
'version_added': Any(float, *string_types),
|
|
||||||
'sample': Any(None, list, dict, int, float, *string_types),
|
|
||||||
'example': Any(None, list, dict, int, float, *string_types)
|
|
||||||
}
|
|
||||||
if isinstance(data, dict):
|
|
||||||
if 'type' in data and (data['type'] == 'complex'):
|
|
||||||
# This will just check if the schema has a 'contains' value.
|
|
||||||
# It won't recursively validate the contents of the 'contains' field
|
|
||||||
additional_schema = {
|
|
||||||
Required('contains'): Any(dict, list, *string_types)
|
|
||||||
}
|
|
||||||
return_schema_dict.update(additional_schema)
|
|
||||||
|
|
||||||
return Schema(
|
|
||||||
return_schema_dict,
|
|
||||||
extra=PREVENT_EXTRA
|
|
||||||
)
|
)
|
||||||
|
if v.get('type') == 'complex':
|
||||||
|
return schema(v)
|
||||||
|
return v
|
||||||
|
|
||||||
|
|
||||||
def deprecation_schema():
|
return_schema = Any(
|
||||||
|
All(
|
||||||
|
Schema(
|
||||||
|
{
|
||||||
|
any_string_types: {
|
||||||
|
Required('description'): Any(list_string_types, *string_types),
|
||||||
|
Required('returned'): Any(*string_types),
|
||||||
|
Required('type'): Any('string', 'list', 'boolean', 'dict', 'complex', 'bool', 'float', 'int', 'dictionary', 'str'),
|
||||||
|
'version_added': Any(float, *string_types),
|
||||||
|
'sample': Any(None, list, dict, int, float, *string_types),
|
||||||
|
'example': Any(None, list, dict, int, float, *string_types),
|
||||||
|
'contains': object,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
),
|
||||||
|
Schema({any_string_types: return_contains})
|
||||||
|
),
|
||||||
|
Schema(type(None)),
|
||||||
|
)
|
||||||
|
|
||||||
deprecation_schema_dict = {
|
|
||||||
|
deprecation_schema = Schema(
|
||||||
|
{
|
||||||
# Only list branches that are deprecated or may have docs stubs in
|
# Only list branches that are deprecated or may have docs stubs in
|
||||||
# Deprecation cycle changed at 2.4 (though not retroactively)
|
# Deprecation cycle changed at 2.4 (though not retroactively)
|
||||||
# 2.3 -> removed_in: "2.5" + n for docs stub
|
# 2.3 -> removed_in: "2.5" + n for docs stub
|
||||||
|
@ -127,11 +135,9 @@ def deprecation_schema():
|
||||||
Required('why'): Any(*string_types),
|
Required('why'): Any(*string_types),
|
||||||
Required('alternative'): Any(*string_types),
|
Required('alternative'): Any(*string_types),
|
||||||
'removed': Any(True),
|
'removed': Any(True),
|
||||||
}
|
},
|
||||||
return Schema(
|
extra=PREVENT_EXTRA
|
||||||
deprecation_schema_dict,
|
)
|
||||||
extra=PREVENT_EXTRA
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def doc_schema(module_name):
|
def doc_schema(module_name):
|
||||||
|
@ -155,7 +161,7 @@ def doc_schema(module_name):
|
||||||
|
|
||||||
if deprecated_module:
|
if deprecated_module:
|
||||||
deprecation_required_scheme = {
|
deprecation_required_scheme = {
|
||||||
Required('deprecated'): Any(deprecation_schema()),
|
Required('deprecated'): Any(deprecation_schema),
|
||||||
}
|
}
|
||||||
|
|
||||||
doc_schema_dict.update(deprecation_required_scheme)
|
doc_schema_dict.update(deprecation_required_scheme)
|
||||||
|
|
|
@ -26,7 +26,6 @@ import yaml.reader
|
||||||
|
|
||||||
from ansible.module_utils._text import to_text
|
from ansible.module_utils._text import to_text
|
||||||
from ansible.module_utils.basic import AnsibleModule
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
from ansible.module_utils.parsing.convert_bool import boolean
|
|
||||||
|
|
||||||
|
|
||||||
class AnsibleTextIOWrapper(TextIOWrapper):
|
class AnsibleTextIOWrapper(TextIOWrapper):
|
||||||
|
|
Loading…
Reference in a new issue