strictyaml icon indicating copy to clipboard operation
strictyaml copied to clipboard

Confusing YAMLValidationError when comments are present in YAML string

Open benelgiac opened this issue 3 years ago • 2 comments

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.

benelgiac avatar Jan 26 '22 13:01 benelgiac

Thanks Giacomo,

Ill take a look tonight.

On Wed, 26 Jan 2022, 13:23 Giacomo Benelli, @.***> wrote:

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 "", 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 "", line 2, column 1: - value ^ (line: 2) found arbitrary text in "", 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.

— Reply to this email directly, view it on GitHub https://github.com/crdoconnor/strictyaml/issues/164, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABOJKNMOLJTZGZB5WLBUFPLUX7YTNANCNFSM5M3AYUKA . You are receiving this because you are subscribed to this thread.Message ID: @.***>

crdoconnor avatar Jan 26 '22 14:01 crdoconnor