def get_spec_version(spec): if not isinstance(spec, dict): msg = _('The provided spec is not a map.') raise exception.SpecValidationFailed(message=msg) if 'type' not in spec: msg = _("The 'type' key is missing from the provided spec map.") raise exception.SpecValidationFailed(message=msg) if 'version' not in spec: msg = _("The 'version' key is missing from the provided spec map.") raise exception.SpecValidationFailed(message=msg) return (spec['type'], spec['version'])
def validate(self): '''Validate the schema.''' for (k, s) in self._schema.items(): try: # validate through resolve self.resolve_value(k) except (TypeError, ValueError) as err: msg = _('Spec validation error (%(key)s): %(err)s') % dict( key=k, err=six.text_type(err)) raise exception.SpecValidationFailed(message=msg) for key in self._data: if key not in self._schema: msg = _('Unrecognizable spec item "%s"') % key raise exception.SpecValidationFailed(message=msg)
def resolve(self, value): try: return int(value) except (TypeError, ValueError): msg = _('The value "%s" cannot be converted into an ' 'integer.') % value raise exception.SpecValidationFailed(message=msg)
def validate(self, value, context=None): if not isinstance(value, six.string_types): msg = _('The value "%s" cannot be converted into a ' 'string.') % value raise exception.SpecValidationFailed(message=msg) self.resolve(value) self.validate_constraints(value, self, context)
def validate_constraints(self, value, context=None, skipped=None): if not skipped: skipped = [] try: for constraint in self.constraints: if type(constraint) not in skipped: constraint.validate(value, context) except ValueError as ex: raise exception.SpecValidationFailed(message=six.text_type(ex))
def resolve(self, value): if str(value).lower() not in ('true', 'false'): msg = _('The value "%s" is not a valid Boolean') % value raise exception.SpecValidationFailed(message=msg) return strutils.bool_from_string(value, strict=True)