Пример #1
0
    def init_ident_value(self, v, rule, path):
        log.debug(u"Init ident value : %s", path)

        if v is None or isinstance(v, bool):
            raise RuleError(
                msg=u"Value: '{}' 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: '{}' 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,
            )
Пример #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
    def init_default_value(self, v, rule, path):
        """
        """
        log.debug(u"Init default value : %s", path)
        self.default = v

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

        if self.type == "map" or self.type == "seq":
            raise RuleError(
                msg=u"Value: {0} for keyword 'default' is not a scalar type".
                format(v),
                error_key=u"default.not_scalar",
                path=path,
            )

        if not isinstance(v, self.type_class):
            raise RuleError(
                msg=u"Types do not match: '{0}' --> '{1}'".format(
                    v, self.type_class),
                error_key=u"default.type.unmatch",
                path=path,
            )
Пример #4
0
    def init_enum_value(self, v, rule, path):
        log.debug(u"Init enum value : %s", path)

        if not isinstance(v, list):
            raise RuleError(
                msg=u"Enum is not a sequence",
                error_key=u"enum.not_seq",
                path=path,
            )
        self.enum = v

        if is_collection_type(self.type):
            raise RuleError(
                msg=u"Enum is not a scalar",
                error_key=u"enum.not_scalar",
                path=path,
            )

        lookup = set()
        for item in v:
            if not isinstance(item, self.type_class):
                raise RuleError(
                    msg=u"Item: '{}' in enum is not of correct class type: '{}'".format(item, self.type_class),
                    error_key=u"enum.type.unmatch",
                    path=path,
                )

            if item in lookup:
                raise RuleError(
                    msg=u"Duplicate items: '{}' found in enum".format(item),
                    error_key=u"enum.duplicate_items",
                    path=path,
                )

            lookup.add(item)
Пример #5
0
    def init_ident_value(self, v, rule, path):
        log.debug(u"Init ident value : {}".format(path))

        if v is None or isinstance(v, bool):
            raise RuleError(
                msg=u"Value: '{}' 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: '{}' 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,
            )
Пример #6
0
    def init_unique_value(self, v, rule, path):
        """
        """
        log.debug(u"Init unique value : %s", path)

        if not isinstance(v, bool):
            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,
            )
Пример #7
0
    def init_default_value(self, v, rule, path):
        """
        """
        log.debug(u"Init default value : %s", path)
        self.default = v

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

        if self.type == "map" or self.type == "seq":
            raise RuleError(
                msg=u"Value: {0} for keyword 'default' is not a scalar type".format(v),
                error_key=u"default.not_scalar",
                path=path,
            )

        if not isinstance(v, self.type_class):
            raise RuleError(
                msg=u"Types do not match: '{0}' --> '{1}'".format(v, self.type_class),
                error_key=u"default.type.unmatch",
                path=path,
            )
Пример #8
0
    def init_enum_value(self, v, rule, path):
        log.debug(u"Init enum value : {}".format(path))

        if not isinstance(v, list):
            raise RuleError(
                msg=u"Enum is not a sequence",
                error_key=u"enum.not_seq",
                path=path,
            )
        self._enum = v

        if is_collection_type(self._type):
            raise RuleError(
                msg=u"Enum is not a scalar",
                error_key=u"enum.not_scalar",
                path=path,
            )

        lookup = set()
        for item in v:
            if not isinstance(item, self._type_class):
                raise RuleError(
                    msg=u"Item: '{}' in enum is not of correct class type: '{}'".format(item, self._type_class),
                    error_key=u"enum.type.unmatch",
                    path=path,
                )

            if item in lookup:
                raise RuleError(
                    msg=u"Duplicate items: '{}' found in enum".format(item),
                    error_key=u"enum.duplicate_items",
                    path=path,
                )

            lookup.add(item)
Пример #9
0
    def init_default_value(self, v, rule, path):
        log.debug("Init default value : {}".format(path))
        self._default = v

        if is_collection_type(self._type):
            raise RuleError("default.notscalar : {} : {} : {}".format(rule, path, v))

        if self._type == "map" or self._type == "seq":
            raise RuleError("default.notscalar : {} : {} : {}".format(rule, os.path.dirname(path), v))

        if not isinstance(v, self._type_class):
            raise RuleError("default.type.unmatch : {} --> {} : {}".format(v, self._type_class, path))
Пример #10
0
    def init_unique_value(self, v, rule, path):
        log.debug("Init unique value : {}".format(path))

        if not isinstance(v, bool):
            raise RuleError("unique.notbool : {} : {}".format(v, path))

        self._unique = v

        if is_collection_type(self._type):
            raise RuleError("unique.notscalar : {} : {}".format(self._type, path))
        if path == "":
            raise RuleError("unique.onroot")
Пример #11
0
    def init_unique_value(self, v, rule, path):
        log.debug("Init unique value : {}".format(path))

        if not isinstance(v, bool):
            raise RuleError("unique.notbool : {} : {}".format(v, path))

        self._unique = v

        if is_collection_type(self._type):
            raise RuleError("unique.notscalar : {} : {}".format(
                self._type, path))
        if path == "":
            raise RuleError("unique.onroot")
Пример #12
0
    def init_ident_value(self, v, rule, path):
        log.debug("Init ident value : {}".format(path))

        if v is None or isinstance(v, bool):
            raise RuleError("ident.notbool : {} : {}".format(v, path))

        self._ident = bool(v)
        self._required = True

        if is_collection_type(self._type):
            raise RuleError("ident.notscalar : {} : {}".format(self._type, path))
        if path == "":
            raise RuleError("ident.onroot")
        if self._parent is None or not self._parent._type == "map":
            raise RuleError("ident.notmap : {}".format(path))
Пример #13
0
    def init_default_value(self, v, rule, path):
        log.debug("Init default value : {}".format(path))
        self._default = v

        if is_collection_type(self._type):
            raise RuleError("default.notscalar : {} : {} : {}".format(
                rule, path, v))

        if self._type == "map" or self._type == "seq":
            raise RuleError("default.notscalar : {} : {} : {}".format(
                rule, os.path.dirname(path), v))

        if not isinstance(v, self._type_class):
            raise RuleError("default.type.unmatch : {} --> {} : {}".format(
                v, self._type_class, path))
Пример #14
0
    def init_ident_value(self, v, rule, path):
        log.debug("Init ident value : {}".format(path))

        if v is None or isinstance(v, bool):
            raise RuleError("ident.notbool : {} : {}".format(v, path))

        self._ident = bool(v)
        self._required = True

        if is_collection_type(self._type):
            raise RuleError("ident.notscalar : {} : {}".format(
                self._type, path))
        if path == "":
            raise RuleError("ident.onroot")
        if self._parent is None or not self._parent._type == "map":
            raise RuleError("ident.notmap : {}".format(path))
Пример #15
0
    def init_enum_value(self, v, rule, path):
        log.debug("Init enum value : {}".format(path))

        if not isinstance(v, list):
            raise RuleError("enum.notseq")
        self._enum = v

        if is_collection_type(self._type):
            raise RuleError("enum.notscalar")

        lookup = set()
        for item in v:
            if not isinstance(item, self._type_class):
                raise RuleError("enum.type.unmatch : {} --> {} : {}".format(item, self._type_class, path))

            if item in lookup:
                raise RuleError("enum.duplicate : {} : {}".format(item, path))

            lookup.add(item)
Пример #16
0
    def init_enum_value(self, v, rule, path):
        log.debug("Init enum value : {}".format(path))

        if not isinstance(v, list):
            raise RuleError("enum.notseq")
        self._enum = v

        if is_collection_type(self._type):
            raise RuleError("enum.notscalar")

        lookup = set()
        for item in v:
            if not isinstance(item, self._type_class):
                raise RuleError("enum.type.unmatch : {} --> {} : {}".format(
                    item, self._type_class, path))

            if item in lookup:
                raise RuleError("enum.duplicate : {} : {}".format(item, path))

            lookup.add(item)
Пример #17
0
    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")
Пример #18
0
    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")