How to use the pykwalify.compat.nativestr function in pykwalify

To help you get started, we’ve selected a few pykwalify examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Grokzen / pykwalify / pykwalify / core.py View on Github external
log.debug(
            u"Validate range : %s : %s : %s : %s : %s : %s",
            max_,
            min_,
            max_ex,
            min_ex,
            value,
            path,
        )

        if max_ is not None and max_ < value:
                self.errors.append(SchemaError.SchemaErrorEntry(
                    msg=u"Type '{prefix}' has size of '{value}', greater than max limit '{max_}'. Path: '{path}'",
                    path=path,
                    value=nativestr(value) if tt['str'](value) else value,
                    prefix=prefix,
                    max_=max_))

        if min_ is not None and min_ > value:
                self.errors.append(SchemaError.SchemaErrorEntry(
                    msg=u"Type '{prefix}' has size of '{value}', less than min limit '{min_}'. Path: '{path}'",
                    path=path,
                    value=nativestr(value) if tt['str'](value) else value,
                    prefix=prefix,
                    min_=min_))

        if max_ex is not None and max_ex <= value:
                self.errors.append(SchemaError.SchemaErrorEntry(
                    msg=u"Type '{prefix}' has size of '{value}', greater than or equals to max limit(exclusive) '{max_ex}'. Path: '{path}'",
                    path=path,
                    value=nativestr(value) if tt['str'](value) else value,
github Grokzen / pykwalify / pykwalify / core.py View on Github external
prefix=prefix,
                    min_=min_))

        if max_ex is not None and max_ex <= value:
                self.errors.append(SchemaError.SchemaErrorEntry(
                    msg=u"Type '{prefix}' has size of '{value}', greater than or equals to max limit(exclusive) '{max_ex}'. Path: '{path}'",
                    path=path,
                    value=nativestr(value) if tt['str'](value) else value,
                    prefix=prefix,
                    max_ex=max_ex))

        if min_ex is not None and min_ex >= value:
                self.errors.append(SchemaError.SchemaErrorEntry(
                    msg=u"Type '{prefix}' has size of '{value}', less than or equals to min limit(exclusive) '{min_ex}'. Path: '{path}'",
                    path=path,
                    value=nativestr(value) if tt['str'](value) else value,
                    prefix=prefix,
                    min_ex=min_ex))
github Grokzen / pykwalify / pykwalify / core.py View on Github external
#
            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
github Grokzen / pykwalify / pykwalify / core.py View on Github external
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 // if they are defined.
github Grokzen / pykwalify / pykwalify / core.py View on Github external
if isinstance(timestamp_value, (int, float)):
            _check_int_timestamp_boundaries(timestamp_value)
        elif isinstance(timestamp_value, datetime.datetime):
            # Datetime objects currently have nothing to validate.
            # In the future, more options will be added to datetime validation
            pass
        elif isinstance(timestamp_value, basestring):
            v = timestamp_value.strip()

            # parse("") will give a valid date but it should not be
            # considered a valid timestamp
            if v == "":
                self.errors.append(SchemaError.SchemaErrorEntry(
                    msg=u"Timestamp value is empty. Path: '{path}'",
                    path=path,
                    value=nativestr(timestamp_value),
                    timestamp=nativestr(timestamp_value)))
            else:
                # A string can contain a valid unit timestamp integer. Check if it is valid and validate it
                try:
                    int_v = int(v)
                    _check_int_timestamp_boundaries(int_v)
                except ValueError:
                    # Just continue to parse it as a timestamp
                    try:
                        parse(timestamp_value)
                        # If it can be parsed then it is valid
                    except Exception:
                        self.errors.append(SchemaError.SchemaErrorEntry(
                            msg=u"Timestamp: '{timestamp}'' is invalid. Path: '{path}'",
                            path=path,
                            value=nativestr(timestamp_value),
github Grokzen / pykwalify / pykwalify / core.py View on Github external
else:
                # A string can contain a valid unit timestamp integer. Check if it is valid and validate it
                try:
                    int_v = int(v)
                    _check_int_timestamp_boundaries(int_v)
                except ValueError:
                    # Just continue to parse it as a timestamp
                    try:
                        parse(timestamp_value)
                        # If it can be parsed then it is valid
                    except Exception:
                        self.errors.append(SchemaError.SchemaErrorEntry(
                            msg=u"Timestamp: '{timestamp}'' is invalid. Path: '{path}'",
                            path=path,
                            value=nativestr(timestamp_value),
                            timestamp=nativestr(timestamp_value)))
        else:
            self.errors.append(SchemaError.SchemaErrorEntry(
                msg=u"Not a valid timestamp",
                path=path,
                value=timestamp_value,
                timestamp=timestamp_value,
            ))
github Grokzen / pykwalify / pykwalify / core.py View on Github external
timestamp=nativestr(timestamp_value)))
            else:
                # A string can contain a valid unit timestamp integer. Check if it is valid and validate it
                try:
                    int_v = int(v)
                    _check_int_timestamp_boundaries(int_v)
                except ValueError:
                    # Just continue to parse it as a timestamp
                    try:
                        parse(timestamp_value)
                        # If it can be parsed then it is valid
                    except Exception:
                        self.errors.append(SchemaError.SchemaErrorEntry(
                            msg=u"Timestamp: '{timestamp}'' is invalid. Path: '{path}'",
                            path=path,
                            value=nativestr(timestamp_value),
                            timestamp=nativestr(timestamp_value)))
        else:
            self.errors.append(SchemaError.SchemaErrorEntry(
                msg=u"Not a valid timestamp",
                path=path,
                value=timestamp_value,
                timestamp=timestamp_value,
            ))