def _validate_default(self, context): if self.default is not None: default_value = self.default if self.type == self.LIST and not isinstance(self.default, list): try: default_value = self.default.split(',') except (KeyError, AttributeError) as err: raise constr.InvalidSchemaError( _('Default must be a ' 'comma-delimited list ' 'string: %s') % err) try: self.validate_constraints(default_value, context) except (ValueError, TypeError, exception.StackValidationFailed) as exc: raise constr.InvalidSchemaError( _('Invalid default ' '%(default)s (%(exc)s)') % dict(default=self.default, exc=exc))
def constraints(): constraints = schema_dict.get(CONSTRAINTS) if constraints is None: return if not isinstance(constraints, list): raise constr.InvalidSchemaError( _("Invalid parameter constraints for parameter %s, " "expected a list") % param_name) valid_keys = (DESCRIPTION, LENGTH, RANGE, ALLOWED_VALUES, ALLOWED_PATTERN, CUSTOM_CONSTRAINT) for constraint in constraints: cls._check_dict(constraint, valid_keys, 'parameter constraints') desc = constraint.get(DESCRIPTION) if RANGE in constraint: cdef = constraint.get(RANGE) cls._check_dict(cdef, (MIN, MAX), 'range constraint') yield constr.Range(parameters.Schema.get_num(MIN, cdef), parameters.Schema.get_num(MAX, cdef), desc) elif LENGTH in constraint: cdef = constraint.get(LENGTH) cls._check_dict(cdef, (MIN, MAX), 'length constraint') yield constr.Length(parameters.Schema.get_num(MIN, cdef), parameters.Schema.get_num(MAX, cdef), desc) elif ALLOWED_VALUES in constraint: cdef = constraint.get(ALLOWED_VALUES) yield constr.AllowedValues(cdef, desc) elif ALLOWED_PATTERN in constraint: cdef = constraint.get(ALLOWED_PATTERN) yield constr.AllowedPattern(cdef, desc) elif CUSTOM_CONSTRAINT in constraint: cdef = constraint.get(CUSTOM_CONSTRAINT) yield constr.CustomConstraint(cdef, desc) else: raise constr.InvalidSchemaError( _("No constraint expressed"))
def _validate_dict(cls, schema_dict): cls._check_dict(schema_dict, cls.PARAMETER_KEYS, "parameter") if cls.TYPE not in schema_dict: raise constr.InvalidSchemaError(_("Missing parameter type"))