コード例 #1
0
ファイル: rule.py プロジェクト: isabella232/pykwalify
    def init_range_value(self, v, rule, path):
        log.debug("Init range value : {}".format(path))

        if not isinstance(v, dict):
            raise RuleError("range.notmap : {} : {}".format(v, path))
        if self._type not in ["str", "int", "map", "seq"]:
            raise RuleError("range.not-supported-type : {} : {}".format(
                self._type, path))

        self._range = v  # dict that should contain min, max, min-ex, max-ex keys

        # 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("range.undefined key : {} : {}".format(
                    k, path))

        if "max" in self._range and "max-ex" in self._range:
            raise RuleError("range.twomax : {}".format(path))
        if "min" in self._range and "min-ex" in self._range:
            raise RuleError("range.twomin : {}".format(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_int(max) or is_bool(max):
            raise RuleError("range.max.notint : {} : {}".format(max, path))

        if min is not None and not is_int(min) or is_bool(min):
            raise RuleError("range.min.notint : {} : {}".format(min, path))

        if max_ex is not None and not is_int(max_ex) or is_bool(max_ex):
            raise RuleError("range.max_ex.notint : {} : {}".format(
                max_ex, path))

        if min_ex is not None and not is_int(min_ex) or is_bool(min_ex):
            raise RuleError("range.min_ex.notint : {} : {}".format(
                min_ex, path))

        if max is not None:
            if min is not None and max < min:
                raise RuleError("range.maxltmin : {} < {} : {}".format(
                    max, min, path))
            elif min_ex is not None and max <= min_ex:
                raise RuleError("range.maxleminex : {} <= {} : {}".format(
                    max, min_ex, path))
        elif max_ex is not None:
            if min is not None and max_ex < min:
                raise RuleError("range.maxexlemiin : {} < {} : {}".format(
                    max_ex, min, path))
            elif min_ex is not None and max_ex <= min_ex:
                raise RuleError("range.maxexleminex : {} <= {} : {}".format(
                    max_ex, min_ex, path))
コード例 #2
0
    def init_unique_value(self, v, rule, path):
        """
        """
        log.debug(u"Init unique value : %s", path)

        if not is_bool(v):
            raise RuleError(
                msg=u"Value: '{0}' for 'unique' keyword is not boolean".format(
                    v),
                error_key=u"unique.not_bool",
                path=path,
            )

        self.unique = v

        if is_collection_type(self.type):
            raise RuleError(
                msg=
                u"Type of the value: '{0}' for 'unique' keyword is not a scalar type"
                .format(self.type),
                error_key=u"unique.not_scalar",
                path=path,
            )
        if path == "":
            raise RuleError(
                msg=u"Keyword 'unique' can't be on root level of schema",
                error_key=u"unique.not_on_root_level",
                path=path,
            )
コード例 #3
0
ファイル: rule.py プロジェクト: Grokzen/pykwalify
    def init_unique_value(self, v, rule, path):
        """
        """
        log.debug(u"Init unique value : %s", path)

        if not is_bool(v):
            raise RuleError(
                msg=u"Value: '{0}' for 'unique' keyword is not boolean".format(v),
                error_key=u"unique.not_bool",
                path=path,
            )

        self.unique = v

        if is_collection_type(self.type):
            raise RuleError(
                msg=u"Type of the value: '{0}' for 'unique' keyword is not a scalar type".format(self.type),
                error_key=u"unique.not_scalar",
                path=path,
            )
        if path == "":
            raise RuleError(
                msg=u"Keyword 'unique' can't be on root level of schema",
                error_key=u"unique.not_on_root_level",
                path=path,
            )
コード例 #4
0
ファイル: rule.py プロジェクト: Grokzen/pykwalify
    def init_ident_value(self, v, rule, path):
        """
        """
        log.debug(u"Init ident value : %s", path)

        if v is None or not is_bool(v):
            raise RuleError(
                msg=u"Value: '{0}' of 'ident' is not a boolean value".format(v),
                error_key=u"ident.not_bool",
                path=path,
            )

        self.ident = bool(v)
        self.required = True

        if is_collection_type(self.type):
            raise RuleError(
                msg=u"Value: '{0}' of 'ident' is not a scalar value".format(v),
                error_key=u"ident.not_scalar",
                path=path,
            )

        if path == "":
            raise RuleError(
                msg=u"Keyword 'ident' can't be on root level of schema",
                error_key=u"ident.not_on_root_level",
                path=path,
            )

        if self.parent is None or not self.parent.type == "map":
            raise RuleError(
                msg=u"Keword 'ident' can't be inside 'map'",
                error_key=u"ident.not_in_map",
                path=path,
            )
コード例 #5
0
ファイル: rule.py プロジェクト: intelie/pykwalify
    def init_range_value(self, v, rule, path):
        log.debug("Init range value : {}".format(path))

        if not isinstance(v, dict):
            raise RuleError("range.notmap : {} : {}".format(v, path))
        if self._type not in ["str", "int", "map", "seq"]:
            raise RuleError("range.not-supported-type : {} : {}".format(self._type, path))

        self._range = v  # dict that should contain min, max, min-ex, max-ex keys

        # 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("range.undefined key : {} : {}".format(k, path))

        if "max" in self._range and "max-ex" in self._range:
            raise RuleError("range.twomax : {}".format(path))
        if "min" in self._range and "min-ex" in self._range:
            raise RuleError("range.twomin : {}".format(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_int(max) or is_bool(max):
            raise RuleError("range.max.notint : {} : {}".format(max, path))

        if min is not None and not is_int(min) or is_bool(min):
            raise RuleError("range.min.notint : {} : {}".format(min, path))

        if max_ex is not None and not is_int(max_ex) or is_bool(max_ex):
            raise RuleError("range.max_ex.notint : {} : {}".format(max_ex, path))

        if min_ex is not None and not is_int(min_ex) or is_bool(min_ex):
            raise RuleError("range.min_ex.notint : {} : {}".format(min_ex, path))

        if max is not None:
            if min is not None and max < min:
                raise RuleError("range.maxltmin : {} < {} : {}".format(max, min, path))
            elif min_ex is not None and max <= min_ex:
                raise RuleError("range.maxleminex : {} <= {} : {}".format(max, min_ex, path))
        elif max_ex is not None:
            if min is not None and max_ex < min:
                raise RuleError("range.maxexlemiin : {} < {} : {}".format(max_ex, min, path))
            elif min_ex is not None and max_ex <= min_ex:
                raise RuleError("range.maxexleminex : {} <= {} : {}".format(max_ex, min_ex, path))
コード例 #6
0
ファイル: rule.py プロジェクト: Grokzen/pykwalify
    def init_required_value(self, v, rule, path):
        """
        """
        log.debug(u"Init required value : %s", path)

        if not is_bool(v):
            raise RuleError(
                msg=u"Value: '{0}' for required keyword must be a boolean".format(v),
                error_key=u"required.not_bool",
                path=path,
            )
        self.required = v
コード例 #7
0
    def init_required_value(self, v, rule, path):
        """
        """
        log.debug(u"Init required value : %s", path)

        if not is_bool(v):
            raise RuleError(
                msg=u"Value: '{0}' for required keyword must be a boolean".
                format(v),
                error_key=u"required.not_bool",
                path=path,
            )
        self.required = v
コード例 #8
0
    def init_ident_value(self, v, rule, path):
        """
        """
        log.debug(u"Init ident value : %s", path)

        if v is None or not is_bool(v):
            raise RuleError(
                msg=u"Value: '{0}' of 'ident' is not a boolean value".format(
                    v),
                error_key=u"ident.not_bool",
                path=path,
            )

        self.ident = bool(v)
        self.required = True

        if is_collection_type(self.type):
            raise RuleError(
                msg=u"Value: '{0}' of 'ident' is not a scalar value".format(v),
                error_key=u"ident.not_scalar",
                path=path,
            )

        if path == "":
            raise RuleError(
                msg=u"Keyword 'ident' can't be on root level of schema",
                error_key=u"ident.not_on_root_level",
                path=path,
            )

        if self.parent is None or not self.parent.type == "map":
            raise RuleError(
                msg=u"Keword 'ident' can't be inside 'map'",
                error_key=u"ident.not_in_map",
                path=path,
            )
コード例 #9
0
    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,
                )
コード例 #10
0
ファイル: rule.py プロジェクト: cpaillet/pykwalify
    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,
                )
コード例 #11
0
ファイル: test_types.py プロジェクト: hideaki-t/pykwalify
    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")
コード例 #12
0
ファイル: test_types.py プロジェクト: ikowin/pykwalify
    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")