def coerce(self, instance, num): if num is not None: if is_array(num) and num.shape == (): num = num.item() # convert scalar array to Python object if not is_number(num): raise ValidationError("Must be a number; got '%s'" % num, attr=self.name, obj=instance) low_comp = 0 if self.low_open else -1 if self.low is not None and compare(num, self.low) <= low_comp: raise ValidationError( "Value must be greater than %s%s (got %s)" % ("" if self.low_open else "or equal to ", self.low, num), attr=self.name, obj=instance, ) high_comp = 0 if self.high_open else 1 if self.high is not None and compare(num, self.high) >= high_comp: raise ValidationError( "Value must be less than %s%s (got %s)" % ("" if self.high_open else "or equal to ", self.high, num), attr=self.name, obj=instance, ) return super().coerce(instance, num)
def coerce(self, instance, num): if num is not None: if is_array(num) and num.shape == (): num = num.item() # convert scalar array to Python object if not is_number(num): raise ValidationError( f"Must be a number; got '{num}'", attr=self.name, obj=instance ) low_comp = 0 if self.low_open else -1 if self.low is not None and compare(num, self.low) <= low_comp: eq_phrase = "" if self.low_open else " or equal to" raise ValidationError( f"Value must be greater than{eq_phrase} {self.low} (got {num})", attr=self.name, obj=instance, ) high_comp = 0 if self.high_open else 1 if self.high is not None and compare(num, self.high) >= high_comp: eq_phrase = "" if self.high_open else " or equal to" raise ValidationError( f"Value must be less than{eq_phrase} {self.high} (got {num})", attr=self.name, obj=instance, ) return super().coerce(instance, num)
def validate(self, instance, num): if num is not None: if not is_number(num): raise ValueError("Must be a number; got '%s'" % num) low_comp = 0 if self.low_open else -1 if self.low is not None and compare(num, self.low) <= low_comp: raise ValueError("Value must be greater than %s%s (got %s)" % ( "" if self.low_open else "or equal to ", self.low, num)) high_comp = 0 if self.high_open else 1 if self.high is not None and compare(num, self.high) >= high_comp: raise ValueError("Value must be less than %s%s (got %s)" % ( "" if self.high_open else "or equal to ", self.high, num)) super(NumberParam, self).validate(instance, num)
def coerce(self, instance, num): if num is not None: if is_array(num) and num.shape == (): num = num.item() # convert scalar array to Python object if not is_number(num): raise ValidationError("Must be a number; got '%s'" % num, attr=self.name, obj=instance) low_comp = 0 if self.low_open else -1 if self.low is not None and compare(num, self.low) <= low_comp: raise ValidationError( "Value must be greater than %s%s (got %s)" % ( "" if self.low_open else "or equal to ", self.low, num), attr=self.name, obj=instance) high_comp = 0 if self.high_open else 1 if self.high is not None and compare(num, self.high) >= high_comp: raise ValidationError( "Value must be less than %s%s (got %s)" % ( "" if self.high_open else "or equal to ", self.high, num), attr=self.name, obj=instance) return super().coerce(instance, num)