Perform full RETURN schema validation in one step, don't try to loop (#46079)

This commit is contained in:
Matt Martz 2018-09-24 16:38:04 -05:00 committed by GitHub
parent c5a8a911b3
commit 86e8d21667
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 38 deletions

View file

@ -16,9 +16,10 @@
# You should have received a copy of the GNU General Public License
# 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
list_string_types = list(string_types)
any_string_types = Any(*string_types)
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]
def return_schema(data):
return_schema_dict = {
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)
}
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
def return_contains(v):
schema = Schema(
{
Required('contains'): Any(dict, list, *string_types)
},
extra=ALLOW_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
# Deprecation cycle changed at 2.4 (though not retroactively)
# 2.3 -> removed_in: "2.5" + n for docs stub
@ -127,11 +135,9 @@ def deprecation_schema():
Required('why'): Any(*string_types),
Required('alternative'): Any(*string_types),
'removed': Any(True),
}
return Schema(
deprecation_schema_dict,
extra=PREVENT_EXTRA
)
},
extra=PREVENT_EXTRA
)
def doc_schema(module_name):
@ -155,7 +161,7 @@ def doc_schema(module_name):
if deprecated_module:
deprecation_required_scheme = {
Required('deprecated'): Any(deprecation_schema()),
Required('deprecated'): Any(deprecation_schema),
}
doc_schema_dict.update(deprecation_required_scheme)