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

Confusing YAMLValidationError when comments are present in YAML string #164

Open
benelgiac opened this issue Jan 26, 2022 · 2 comments
Open

Comments

@benelgiac
Copy link

Hi, thanks for strictYAML. Looking to replace my Schema-based validation with it.

One of the reason for me to switch is the better error messages that allow my users to fix their YAMLs more quickly.

I've noticed something about the YAMLValidationError strings. When comments are present in the YAML between a key: and its value, if there is a validation error, then the presence of the comment line seems to generate confusing error messages.

This is what I mean: the following yaml is parsed correctly and the comment is ignored and discarded (OK)

yaml_string = """
key:
   # comment
   - 1
"""

generic_config = load(
    yaml_string,
    Map({'key': Seq(Int())}),
)

If I now force a validation error, i.e. providing a string not convertible to a number

yaml_string = """
key:
   # comment
   - value
"""

generic_config = load(
    yaml_string,
    Map({'key': Seq(Int())}),
)

then I get a validation error which seems to refer to the right thing (expecting an integer) but to the wrong line (the one where the comment is present)

strictyaml.exceptions.YAMLValidationError: when expecting an integer
found arbitrary text
  in "<unicode string>", line 2, column 1:
       # comment
    ^ (line: 2)

Other variant: if I postpone the comment line in the YAML string, then the error seems to be double:

yaml_string = """
key:
   - value
   # comment
"""

generic_config = load(
    yaml_string,
    Map({'key': Seq(Int())}),
)

generates the following:

strictyaml.exceptions.YAMLValidationError: when expecting an integer
  in "<unicode string>", line 2, column 1:
    - value
    ^ (line: 2)
found arbitrary text
  in "<unicode string>", line 3, column 1:
       # comment
    ^ (line: 3)

I'd expect comments to just not influence the error messages. Am I right?

Thanks for any time you could dedicate to this.

@crdoconnor
Copy link
Owner

crdoconnor commented Jan 26, 2022 via email

@martin-denizet
Copy link

I see the same behavior using latest (1.7.3)

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

3 participants