1
0
mirror of https://github.com/ansible/awx.git synced 2024-11-02 09:51:09 +03:00

check for existence and type of all spec items

This commit is contained in:
AlanCoding 2018-04-04 13:54:14 -04:00
parent c2446beb6e
commit 02b7424b29
No known key found for this signature in database
GPG Key ID: FD2C3C012A72926B

View File

@ -3128,16 +3128,22 @@ class JobTemplateSurveySpec(GenericAPIView):
return Response() return Response()
def _validate_spec_data(self, new_spec, old_spec): def _validate_spec_data(self, new_spec, old_spec):
if "name" not in new_spec: schema_errors = {}
return Response(dict(error=_("'name' missing from survey spec.")), status=status.HTTP_400_BAD_REQUEST) for field, expect_type, type_label in [
if "description" not in new_spec: ('name', six.string_types, 'string'),
return Response(dict(error=_("'description' missing from survey spec.")), status=status.HTTP_400_BAD_REQUEST) ('description', six.string_types, 'string'),
if "spec" not in new_spec: ('spec', list, 'list of items')]:
return Response(dict(error=_("'spec' missing from survey spec.")), status=status.HTTP_400_BAD_REQUEST) if field not in new_spec:
if not isinstance(new_spec["spec"], list): schema_errors['error'] = _("Field '{}' is missing from survey spec.").format(field)
return Response(dict(error=_("'spec' must be a list of items.")), status=status.HTTP_400_BAD_REQUEST) elif not isinstance(new_spec[field], expect_type):
if len(new_spec["spec"]) < 1: schema_errors['error'] = _("Expected {} for field '{}', received {} type.").format(
return Response(dict(error=_("'spec' doesn't contain any items.")), status=status.HTTP_400_BAD_REQUEST) type_label, field, type(new_spec[field]).__name__)
if isinstance(new_spec.get('spec', None), list) and len(new_spec["spec"]) < 1:
schema_errors['error'] = _("'spec' doesn't contain any items.")
if schema_errors:
return Response(schema_errors, status=status.HTTP_400_BAD_REQUEST)
variable_set = set() variable_set = set()
old_spec_dict = JobTemplate.pivot_spec(old_spec) old_spec_dict = JobTemplate.pivot_spec(old_spec)