def init_range_value(self, v, rule, path): log.debug(u"Init range value : %s", path) supported_types = ["str", "int", "float", "number", "map", "seq"] if not isinstance(v, dict): raise RuleError( msg=u"Range value is not a dict type: '{}'".format(v), error_key=u"range.not_map", path=path, ) if self.type not in supported_types: raise RuleError( msg=u"Range value type: '{}' is not a supported type".format(self.type), error_key=u"range.not_supported_type", path=path, ) # dict that should contain min, max, min-ex, max-ex keys self.range = v # This should validate that only min, max, min-ex, max-ex exists in the dict for k, v in self.range.items(): if k not in ["max", "min", "max-ex", "min-ex"]: raise RuleError( msg=u"Unknown key: '{}' found in range keyword".format(k), error_key=u"range.unknown_key", path=path, ) if "max" in self.range and "max-ex" in self.range: raise RuleError( msg=u"'max' and 'max-ex' can't be used in the same range rule", error_key=u"range.max_duplicate_keywords", path=path, ) if "min" in self.range and "min-ex" in self.range: raise RuleError( msg=u"'min' and 'min-ex' can't be used in the same range rule", error_key=u"range.min_duplicate_keywords", path=path, ) max = self.range.get("max", None) min = self.range.get("min", None) max_ex = self.range.get("max-ex", None) min_ex = self.range.get("min-ex", None) if max is not None and not is_number(max) or is_bool(max): raise RuleError( msg=u"Value: '{}' for 'max' keyword is not a number".format(v), error_key=u"range.max.not_number", path=path, ) if min is not None and not is_number(min) or is_bool(min): raise RuleError( msg=u"Value: '{}' for 'min' keyword is not a number".format(v), error_key=u"range.min.not_number", path=path, ) if max_ex is not None and not is_number(max_ex) or is_bool(max_ex): raise RuleError( msg=u"Value: '{}' for 'max-ex' keyword is not a number".format(v), error_key=u"range.max_ex.not_number", path=path, ) if min_ex is not None and not is_number(min_ex) or is_bool(min_ex): raise RuleError( msg=u"Value: '{}' for 'min-ex' keyword is not a number".format(v), error_key=u"range.min_ex.not_number", path=path, ) # only numbers allow negative ranges # string, map and seq require non negative ranges if self.type not in ["int", "float", "number"]: if min is not None and min < 0: raise RuleError( msg=u"Value for 'min' can't be negative in case of type {}.".format(self.type), error_key=u"range.min_negative", path=path, ) elif min_ex is not None and min_ex < 0: raise RuleError( msg=u"Value for 'min-ex' can't be negative in case of type {}.".format(self.type), error_key=u"range.min-ex_negative", path=path, ) if max is not None and max < 0: raise RuleError( msg=u"Value for 'max' can't be negative in case of type {}.".format(self.type), error_key=u"range.max_negative", path=path, ) elif max_ex is not None and max_ex < 0: raise RuleError( msg=u"Value for 'max-ex' can't be negative in case of type {}.".format(self.type), error_key=u"range.max-ex_negative", path=path, ) if max is not None: if min is not None and max < min: raise RuleError( msg=u"Value for 'max' can't be less then value for 'min'. {} < {}".format(max, min), error_key=u"range.max_lt_min", path=path, ) elif min_ex is not None and max <= min_ex: raise RuleError( msg=u"Value for 'max' can't be less then value for 'min-ex'. {} <= {}".format(max, min_ex), error_key=u"range.max_le_min-ex", path=path, ) elif max_ex is not None: if min is not None and max_ex < min: raise RuleError( msg=u"Value for 'max-ex' can't be less then value for 'min'. {} < {}".format(max_ex, min), error_key=u"range.max-ex_le_min", path=path, ) elif min_ex is not None and max_ex <= min_ex: raise RuleError( msg=u"Value for 'max-ex' can't be less then value for 'min-ex'. {} <= {}".format(max_ex, min_ex), error_key=u"range.max-ex_le_min-ex", path=path, )
def test_types(self): """ Test that all type helper methods works correctly """ assert types.type_class("str") == str assert types.is_builtin_type("str") assert types.is_collection_type("map") assert types.is_collection_type("seq") assert not types.is_collection_type("str") assert types.is_scalar_type("str") assert not types.is_scalar_type("seq") assert not types.is_scalar_type("map") assert types.is_collection([]) assert types.is_collection({}) assert not types.is_collection("foo") assert types.is_scalar("") assert types.is_scalar(True) assert not types.is_scalar([]) assert types.is_correct_type("", str) assert types.is_correct_type({}, dict) assert types.is_string("foo") assert not types.is_string([]) assert types.is_int(1) assert not types.is_int("foo") assert types.is_bool(True) assert not types.is_bool(1) assert not types.is_bool("true") assert types.is_float(1.0) assert not types.is_float("foo") assert types.is_number(1) assert types.is_number(1.0) assert not types.is_number("foo") assert types.is_text("foo") assert types.is_text(1) assert types.is_text(1.0) assert not types.is_text([]) assert not types.is_text(True) assert types.is_any("foo") assert types.is_any(True) assert types.is_any(1) assert types.is_any(1.0) assert types.is_any({}) assert types.is_any([]) assert types.is_enum("foo") assert not types.is_enum(1) assert types.is_none(None) assert not types.is_none("foo")
def init_range_value(self, v, rule, path): log.debug(u"Init range value : {}".format(path)) supported_types = ["str", "int", "float", "number", "map", "seq"] if not isinstance(v, dict): raise RuleError( msg=u"Range value is not a dict type: '{}'".format(v), error_key=u"range.not_map", path=path, ) if self._type not in supported_types: raise RuleError( msg=u"Range value type: '{}' is not a supported type".format(self._type), error_key=u"range.not_supported_type", path=path, ) # dict that should contain min, max, min-ex, max-ex keys self._range = v # This should validate that only min, max, min-ex, max-ex exists in the dict for k, v in self._range.items(): if k not in ["max", "min", "max-ex", "min-ex"]: raise RuleError( msg=u"Unknown key: '{}' found in range keyword".format(k), error_key=u"range.unknown_key", path=path, ) if "max" in self._range and "max-ex" in self._range: raise RuleError( msg=u"'max' and 'max-ex' can't be used in the same range rule", error_key=u"range.max_duplicate_keywords", path=path, ) if "min" in self._range and "min-ex" in self._range: raise RuleError( msg=u"'min' and 'min-ex' can't be used in the same range rule", error_key=u"range.min_duplicate_keywords", path=path, ) max = self._range.get("max", None) min = self._range.get("min", None) max_ex = self._range.get("max-ex", None) min_ex = self._range.get("min-ex", None) if max is not None and not is_number(max) or is_bool(max): raise RuleError( msg=u"Value: '{}' for 'max' keyword is not a number".format(v), error_key=u"range.max.not_number", path=path, ) if min is not None and not is_number(min) or is_bool(min): raise RuleError( msg=u"Value: '{}' for 'min' keyword is not a number".format(v), error_key=u"range.min.not_number", path=path, ) if max_ex is not None and not is_number(max_ex) or is_bool(max_ex): raise RuleError( msg=u"Value: '{}' for 'max-ex' keyword is not a number".format(v), error_key=u"range.max_ex.not_number", path=path, ) if min_ex is not None and not is_number(min_ex) or is_bool(min_ex): raise RuleError( msg=u"Value: '{}' for 'min-ex' keyword is not a number".format(v), error_key=u"range.min_ex.not_number", path=path, ) # only numbers allow negative ranges # string, map and seq require non negative ranges if self._type not in ["int", "float", "number"]: if min is not None and min < 0: raise RuleError( msg=u"Value for 'min' can't be negative in case of type {}.".format(self._type), error_key=u"range.min_negative", path=path, ) elif min_ex is not None and min_ex < 0: raise RuleError( msg=u"Value for 'min-ex' can't be negative in case of type {}.".format(self._type), error_key=u"range.min-ex_negative", path=path, ) if max is not None and max < 0: raise RuleError( msg=u"Value for 'max' can't be negative in case of type {}.".format(self._type), error_key=u"range.max_negative", path=path, ) elif max_ex is not None and max_ex < 0: raise RuleError( msg=u"Value for 'max-ex' can't be negative in case of type {}.".format(self._type), error_key=u"range.max-ex_negative", path=path, ) if max is not None: if min is not None and max < min: raise RuleError( msg=u"Value for 'max' can't be less then value for 'min'. {} < {}".format(max, min), error_key=u"range.max_lt_min", path=path, ) elif min_ex is not None and max <= min_ex: raise RuleError( msg=u"Value for 'max' can't be less then value for 'min-ex'. {} <= {}".format(max, min_ex), error_key=u"range.max_le_min-ex", path=path, ) elif max_ex is not None: if min is not None and max_ex < min: raise RuleError( msg=u"Value for 'max-ex' can't be less then value for 'min'. {} < {}".format(max_ex, min), error_key=u"range.max-ex_le_min", path=path, ) elif min_ex is not None and max_ex <= min_ex: raise RuleError( msg=u"Value for 'max-ex' can't be less then value for 'min-ex'. {} <= {}".format(max_ex, min_ex), error_key=u"range.max-ex_le_min-ex", path=path, )
def test_types(self): """ Test that all type helper methods works correctly """ assert types.type_class("str") == str assert types.is_builtin_type("str") assert types.is_collection_type("map") assert types.is_collection_type("seq") assert not types.is_collection_type("str") assert types.is_scalar_type("str") assert not types.is_scalar_type("seq") assert not types.is_scalar_type("map") assert types.is_collection([]) assert types.is_collection({}) assert not types.is_collection("foo") assert types.is_scalar("") assert types.is_scalar(True) assert not types.is_scalar([]) assert types.is_correct_type("", str) assert types.is_correct_type({}, dict) assert types.is_string("foo") assert not types.is_string([]) assert types.is_int(1) assert not types.is_int("foo") assert types.is_bool(True) assert not types.is_bool(1) assert not types.is_bool("true") assert types.is_float(1.0) assert not types.is_float("foo") assert types.is_number(1) assert types.is_number(1.0) assert not types.is_number("foo") assert types.is_text("foo") assert types.is_text(1) assert types.is_text(1.0) assert not types.is_text([]) assert not types.is_text(True) assert types.is_any("foo") assert types.is_any(True) assert types.is_any(1) assert types.is_any(1.0) assert types.is_any({}) assert types.is_any([]) assert types.is_enum("foo") assert not types.is_enum(1) assert types.is_none(None) assert not types.is_none("foo") assert types.is_url("https://github.com")