def _validate_input_value(input_name, input_constraints, input_value, type_name, value_getter): if input_constraints and utils.get_function(input_value): raise exceptions.DSLParsingException( exceptions.ERROR_INPUT_WITH_FUNCS_AND_CONSTRAINTS, 'Input value {0}, of input {1}, cannot contain an intrinsic ' 'function and also have ' 'constraints.'.format(input_value, input_name)) if type_name in TYPES_BASED_ON_DB_ENTITIES: if not value_getter: raise exceptions.ConstraintException( 'Invalid call to validate_input_value: value_getter not set') if type_name in TYPES_WHICH_REQUIRE_DEPLOYMENT_ID_CONSTRAINT \ and not value_getter.has_deployment_id() \ and 'deployment_id' not in {c.name for c in input_constraints}: raise exceptions.ConstraintException( "Input '{0}' of type '{1}' lacks 'deployment_id' constraint.". format(input_name, type_name)) if type_name not in TYPES_WHICH_REQUIRE_DEPLOYMENT_ID_CONSTRAINT \ or ('deployment_id' not in {c.name for c in input_constraints} and value_getter.has_deployment_id()): matching_values = value_getter.get(type_name, input_value) if not any(v == input_value for v in matching_values or []): raise exceptions.ConstraintException( "Value '{0}' of '{1}' is not a valid value for data type " "'{2}'.".format(input_value, input_name, type_name)) data_based_constraints = [] for c in input_constraints: if c.data_based(type_name): data_based_constraints.append(c) continue if not c.predicate(input_value): raise exceptions.ConstraintException( "Value {0} of input {1} violates constraint " "{2}.".format(input_value, input_name, c)) if ((type_name in TYPES_WHICH_REQUIRE_DEPLOYMENT_ID_CONSTRAINT or data_based_constraints) and not predicate_many( input_value, type_name, value_getter, data_based_constraints)): raise exceptions.ConstraintException( "Value '{0}' of input '{1}' does not match any relevant entity " "or violates at least one of the constraints: {2}.".format( input_value, input_name, ", ".join(str(c) for c in data_based_constraints)))
def validate_input_value(input_name, input_constraints, input_value): if input_constraints and functions.is_function(input_value): raise exceptions.DSLParsingException( exceptions.ERROR_INPUT_WITH_FUNCS_AND_CONSTRAINTS, 'Input value {0}, of input {1}, cannot contain an intrinsic ' 'function and also have ' 'constraints.'.format(input_value, input_name)) for c in input_constraints: if not c.predicate(input_value): raise exceptions.ConstraintException( "Value {0} of input {1} violates constraint " "{2}.".format(input_value, input_name, c))
def _try_predicate_func(predicate_function, err_msg): """Tries to return the value of the predicate func `p_func`, if it catches a TypeError it raises a ConstraintException instead. :param predicate_function: the actual predicate function of a constraint, the one that checks if a given value complies with the constraint. E.g. value == 2 for an Equal(2) constraint. :param err_msg: an error message to initialize the ConstraintException with, in case TypeError is raised during the `p_func` runtime. :return: whatever `p_func` returns. """ try: return predicate_function() except TypeError: raise exceptions.ConstraintException(err_msg)
def predicate(self, value): if not isinstance(value, basestring): raise exceptions.ConstraintException( "Value must be of type string, got type " "'{}'".format(type(value).__name__)) return bool(re.match(self.args, value))
def type_check(self, data_type): if data_type not in self.SUPPORTED_DATA_TYPES: raise exceptions.ConstraintException( "'{0}' constraint is not defined for " "'{1}' data types".format(self.name, data_type))