Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Wrong line number on YAMLValidationError output #180

Open
jrom99 opened this issue Apr 13, 2022 · 1 comment
Open

[Bug] Wrong line number on YAMLValidationError output #180

jrom99 opened this issue Apr 13, 2022 · 1 comment

Comments

@jrom99
Copy link

jrom99 commented Apr 13, 2022

Okay, I found a pretty weird case where YAMLValidationError output shows the wrong line numbers.

I don't know why, but the combination of Slug, values with brackets and missing keys change the behaviour of line counter.

I couldn't reliably test it to discover what actually caused it, sorry.

Each line with brackets adds one to the line counter, but only in certain conditions (replacing "nós descobrimos" with "aaaaa" corrects this, as does changing what is inside some brackets). The number is correct if I don't use Slug as a key validator.

Edit: It seems to be due to long lines, actually.

image

Example yaml file that causes this.

# pode ser o nome de um arquivo em resources/saude ou o texto diretamente
Titulo: Sample text {key1} {key2} foi aaaaa para {underline_} lot{s} value{s} nós descobrimos
Resumo: Sample text {key1} {key2} foi aaaaa para {underline_} lot{s} value{s} nós descobrimos

Formatação:
   Resumo:
      MargemTopo: 24mm
      Alinhamento Horizontal Numeros: desenho

Minimum example I could create that causes this behaviour

import re
import unicodedata

from strictyaml import (EmptyDict, EmptyNone, Enum, FixedSeq, Int, Map,
                        Optional, Regex, ScalarValidator, Str,
                        YAMLValidationError, load)
from strictyaml.yamllocation import YAMLChunk


class Slug(ScalarValidator):
    def __init__(self, is_uppercase: bool = False, space_char: str = "-") -> None:
        super().__init__()
        self.is_uppercase = is_uppercase
        self.space_char = space_char

    def slugify(self, string: str):
        """
        Slugify a unicode string.

        Example:
            >>> slugify(u"Héllø Wörld")
            u"hello-world"
        """
        simplified = re.sub(
            r'[^\w\s-]',
            '',
            unicodedata.normalize('NFKD', string).encode('ascii', 'ignore').decode('ascii')).strip()
        no_spaces = re.sub(r'[-\s]+', self.space_char, simplified)
        return no_spaces.upper() if self.is_uppercase else no_spaces.lower()

    def validate_scalar(self, chunk: YAMLChunk):
        return self.slugify(chunk.contents)


file_schema = Map({
    "titulo": Str(),
    "resumo": Str(),
    "detalhe": Str()
}, Slug())


with open("test.yaml") as handle:
    file_data = handle.read()

load(file_data, file_schema)
@perseoGI
Copy link

perseoGI commented Aug 6, 2024

Same error here!
I'm receiving a line error greater than lines exist in the file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants