Exemple #1
0
    def _validate_scalar(self, value, rule, path, done=None):
        """
        """
        log.debug(u"Validate scalar")
        log.debug(u" Scalar : Value : %s", value)
        log.debug(u" Scalar : Rule :  %s", rule)
        log.debug(u" Scalar : RuleType : %s", rule.type)
        log.debug(u" Scalar : Path %s", path)

        # Handle 'func' argument on this scalar
        self._handle_func(value, rule, path, done)

        if rule.assertion is not None:
            self._validate_assert(rule, value, path)

        if value is None:
            return True

        if rule.enum is not None and value not in rule.enum:
            self.errors.append(
                SchemaError.SchemaErrorEntry(
                    msg=
                    u"Enum '{value}' does not exist. Path: '{path}' Enum: {enum_values}",
                    path=path,
                    value=nativestr(value) if tt['str'](value) else value,
                    enum_values=rule.enum,
                ))

        # Set default value
        if rule.default and value is None:
            value = rule.default

        if not self._validate_scalar_type(value, rule.type, path):
            return

        if value is None:
            return

        if rule.pattern is not None:
            #
            # Try to trim away the surrounding slashes around ruby style /<regex>/ if they are defined.
            # This is a quirk from ruby that they define regex patterns with surrounding slashes.
            # Docs on how ruby regex works can be found here: https://ruby-doc.org/core-2.4.0/Regexp.html
            # The original ruby implementation uses this code to validate patterns
            #   unless value.to_s =~ rule.regexp
            # Becuase python do not work with surrounding slashes we have to trim them away in order to make the regex work
            #
            if rule.pattern.startswith('/') and rule.pattern.endswith(
                    '/') and self.fix_ruby_style_regex:
                rule.pattern = rule.pattern[1:-1]
                log.debug(
                    "Trimming slashes around ruby style regex. New pattern value: '{0}'"
                    .format(rule.pattern))

            try:
                log.debug("Matching pattern '{0}' to regex '{1}".format(
                    rule.pattern, value))
                res = re.match(rule.pattern, value, re.UNICODE)
            except TypeError:
                res = None

            if res is None:  # Not matching
                self.errors.append(
                    SchemaError.SchemaErrorEntry(
                        msg=
                        u"Value '{value}' does not match pattern '{pattern}'. Path: '{path}'",
                        path=path,
                        value=nativestr(str(value)),
                        pattern=rule._pattern))
            else:
                log.debug("Pattern matched...")

        if rule.range is not None:
            if not is_scalar(value):
                raise CoreError(u"value is not a valid scalar")

            r = rule.range

            try:
                v = len(value)
                value = v
            except Exception:
                pass

            self._validate_range(
                r.get("max"),
                r.get("min"),
                r.get("max-ex"),
                r.get("min-ex"),
                value,
                path,
                "scalar",
            )

        if rule.length is not None:
            self._validate_length(
                rule.length,
                value,
                path,
                'scalar',
            )

        # Validate timestamp
        if rule.type == "timestamp":
            self._validate_scalar_timestamp(value, path)

        if rule.type == "date":
            if not is_scalar(value):
                raise CoreError(u'value is not a valid scalar')
            date_format = rule.format
            self._validate_scalar_date(value, date_format, path)
Exemple #2
0
    def _validate_scalar(self, value, rule, path, done=None):
        log.debug(u"Validate scalar")
        log.debug(u" # %s", value)
        log.debug(u" # %s", rule)
        log.debug(u" # %s", rule.type)
        log.debug(u" # %s", path)

        # Handle 'func' argument on this scalar
        self._handle_func(value, rule, path, done)

        if rule.enum is not None:
            if value not in rule.enum:
                self.errors.append(SchemaError.SchemaErrorEntry(
                    msg=u"Enum '{value}' does not exist. Path: '{path}'",
                    path=path,
                    value=nativestr(value) if tt['str'](value) else value,
                ))

        # Set default value
        if rule.default and value is None:
            value = rule.default

        self._validate_scalar_type(value, rule.type, path)

        if value is None:
            return

        if rule.pattern is not None:
            res = re.match(rule.pattern, value, re.UNICODE)
            if res is None:  # Not matching
                self.errors.append(SchemaError.SchemaErrorEntry(
                    msg=u"Value '{value}' does not match pattern '{pattern}'. Path: '{path}'",
                    path=path,
                    value=nativestr(str(value)),
                    pattern=rule._pattern))

        if rule.range is not None:
            if not is_scalar(value):
                raise CoreError(u"value is not a valid scalar")

            r = rule.range

            try:
                v = len(value)
                value = v
            except Exception:
                pass

            self._validate_range(
                r.get("max", None),
                r.get("min", None),
                r.get("max-ex", None),
                r.get("min-ex", None),
                value,
                path,
                "scalar",
            )

        # Validate timestamp
        if rule.type == "timestamp":
            self._validate_scalar_timestamp(value, path)
Exemple #3
0
    def _validate_scalar(self, value, rule, path, done=None):
        log.debug(u"Validate scalar")
        log.debug(u" # %s", value)
        log.debug(u" # %s", rule)
        log.debug(u" # %s", rule.type)
        log.debug(u" # %s", path)

        # Handle 'func' argument on this scalar
        self._handle_func(value, rule, path, done)

        if rule.enum is not None:
            if value not in rule.enum:
                self.errors.append(
                    SchemaError.SchemaErrorEntry(
                        msg=u"Enum '{value}' does not exist. Path: '{path}'",
                        path=path,
                        value=nativestr(value) if tt['str'](value) else value,
                    ))

        # Set default value
        if rule.default and value is None:
            value = rule.default

        self._validate_scalar_type(value, rule.type, path)

        if value is None:
            return

        if rule.pattern is not None:
            res = re.match(rule.pattern, str(value))
            if res is None:  # Not matching
                self.errors.append(
                    SchemaError.SchemaErrorEntry(
                        msg=
                        u"Value '{value}' does not match pattern '{pattern}'. Path: '{path}'",
                        path=path,
                        value=nativestr(str(value)),
                        pattern=rule._pattern))

        if rule.range is not None:
            if not is_scalar(value):
                raise CoreError(u"value is not a valid scalar")

            r = rule.range

            try:
                v = len(value)
                value = v
            except Exception:
                pass

            self._validate_range(
                r.get("max", None),
                r.get("min", None),
                r.get("max-ex", None),
                r.get("min-ex", None),
                value,
                path,
                "scalar",
            )

        # Validate timestamp
        if rule.type == "timestamp":
            self._validate_scalar_timestamp(value, path)
Exemple #4
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")
Exemple #5
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")
Exemple #6
0
    def _validate_scalar(self, value, rule, path, errors, done=None):
        log.debug(u"Validate scalar")
        log.debug(u" # {}".format(value))
        log.debug(u" # {}".format(rule))
        log.debug(u" # {}".format(rule._type))
        log.debug(u" # {}".format(path))

        # Handle 'func' argument on this scalar
        self._handle_func(value, rule, path, errors, done)

        if rule._enum is not None:
            if value not in rule._enum:
                errors.append(SchemaError.SchemaErrorEntry(
                    msg=u"Enum '{value}' does not exist. Path: '{path}'",
                    path=path,
                    value=nativestr(value) if tt['str'](value) else value,
                ))

        # Set default value
        if rule._default and value is None:
            value = rule._default

        self._validate_scalar_type(value, rule._type, errors, path)

        if value is None:
            return

        if rule._pattern is not None:
            res = re.match(rule._pattern, str(value))
            if res is None:  # Not matching
                errors.append(SchemaError.SchemaErrorEntry(
                    msg=u"Value '{value}' does not match pattern '{pattern}'. Path: '{path}'",
                    path=path,
                    value=nativestr(value),
                    pattern=rule._pattern))

        if rule._range is not None:
            if not is_scalar(value):
                raise CoreError(u"value is not a valid scalar")

            r = rule._range

            try:
                v = len(value)
                value = v
            except Exception:
                pass

            self._validate_range(
                r.get("max", None),
                r.get("min", None),
                r.get("max-ex", None),
                r.get("min-ex", None),
                errors,
                value,
                path,
                "scalar",
            )

        # Validate timestamp
        if rule._type == "timestamp":
            v = value.strip()

            # parse("") will give a valid date but it should not be
            # considered a valid timestamp
            if v == "":
                errors.append(SchemaError.SchemaErrorEntry(
                    msg=u"Timestamp value is empty. Path: '{path}'",
                    path=path,
                    value=nativestr(value),
                    timestamp=nativestr(value)))
            else:
                try:
                    parse(value)
                    # If it can be parsed then it is valid
                except Exception:
                    errors.append(SchemaError.SchemaErrorEntry(
                        msg=u"Timestamp: '{timestamp}'' is invalid. Path: '{path}'",
                        path=path,
                        value=nativestr(value),
                        timestamp=nativestr(value)))
Exemple #7
0
    def _validate_scalar(self, value, rule, path, errors, done=None):
        log.debug("Validate scalar")
        log.debug(" # {}".format(value))
        log.debug(" # {}".format(rule))
        log.debug(" # {}".format(rule._type))
        log.debug(" # {}".format(path))

        # Handle 'func' argument on this scalar
        self._handle_func(value, rule, path, errors, done)

        if rule._enum is not None:
            if value not in rule._enum:
                errors.append(
                    SchemaError.SchemaErrorEntry(
                        msg="Enum '{value}' does not exist. Path: '{path}'",
                        path=path,
                        value=value))

        # Set default value
        if rule._default and value is None:
            value = rule._default

        self._validate_scalar_type(value, rule._type, errors, path)

        if value is None:
            return

        if rule._pattern is not None:
            res = re.match(rule._pattern, str(value))
            if res is None:  # Not matching
                errors.append(
                    SchemaError.SchemaErrorEntry(
                        msg=
                        "Value '{value}' does not match pattern '{pattern}'. Path: '{path}'",
                        path=path,
                        value=value,
                        pattern=rule._pattern))

        if rule._range is not None:
            if not is_scalar(value):
                raise CoreError("value is not a valid scalar")

            r = rule._range

            try:
                v = len(value)
                value = v
            except Exception:
                pass

            self._validate_range(
                r.get("max", None),
                r.get("min", None),
                r.get("max-ex", None),
                r.get("min-ex", None),
                errors,
                value,
                path,
                "scalar",
            )

        # Validate timestamp
        if rule._type == "timestamp":
            v = value.strip()

            # parse("") will give a valid date but it should not be
            # considered a valid timestamp
            if v == "":
                errors.append("timestamp.empty : {} : {}".format(value, path))
            else:
                try:
                    parse(value)
                    # If it can be parsed then it is valid
                except Exception:
                    errors.append("timestamp.invalid : {} : {}".format(
                        value, path))
Exemple #8
0
    def _validate_scalar(self, value, rule, path, errors, done=None):
        log.debug("Validate scalar")
        log.debug(" # {}".format(value))
        log.debug(" # {}".format(rule))
        log.debug(" # {}".format(rule._type))
        log.debug(" # {}".format(path))

        # Handle 'func' argument on this scalar
        self._handle_func(value, rule, path, errors, done)

        if rule._enum is not None:
            if value not in rule._enum:
                errors.append("enum.notexists : {} : {}".format(value, path))

        # Set default value
        if rule._default and value is None:
            value = rule._default

        self._validate_scalar_type(value, rule._type, errors, path)

        if value is None:
            return

        if rule._pattern is not None:
            res = re.match(rule._pattern, str(value))
            if res is None:  # Not matching
                errors.append("pattern.unmatch : {} --> {} : {}".format(rule._pattern, value, path))

        if rule._range is not None:
            if not is_scalar(value):
                raise CoreError("value is not a valid scalar")

            r = rule._range

            try:
                v = len(value)
                value = v
            except Exception:
                pass

            self._validate_range(
                r.get("max", None),
                r.get("min", None),
                r.get("max-ex", None),
                r.get("min-ex", None),
                errors,
                value,
                path,
                "scalar",
            )

        # Validate timestamp
        if rule._type == "timestamp":
            v = value.strip()

            # parse("") will give a valid date but it should not be
            # considered a valid timestamp
            if v == "":
                errors.append("timestamp.empty : {} : {}".format(value, path))
            else:
                try:
                    parse(value)
                    # If it can be parsed then it is valid
                except Exception:
                    errors.append("timestamp.invalid : {} : {}".format(value, path))