Open GilShoshan94 opened 4 years ago
Thanks for your interest in Yamale. I agree, the line number would be helpful. To be complete, we'd probably want to update the error class to hold the line numbers separately. It'll take a little thought. Thanks for the start towards that.
For your code, be careful using .
as the separator since that can be part of the key. We use that as a separator for the output, but internally we use something else. I can see how that may cause confusion when reading the output, and we may need to revisit that later.
Thanks!
My pleasure !
Should I close this "issue" ? I don't really know what people usually do, this is my first "contribution" in an open source project.
No, we'll leave it open as a reminder that people would like to have a line number in the output.
Would very much appreciate this feature :+1:
Would a PR along these lines be welcome? Our yamale fork now has "promoted" error messages, which hold a few separate properties (error message, ancestry list, pointer to data evaluated, etc).
I need to look closer at the above, because having line numbers as another property would be useful for displaying to users.
Anyhow, with a few changes and cleanup, I think it'd be PR-able--if it's not stepping on a WIP, @mildebrandt.
@mechie That would be very welcome....it seems there is interest and we haven't been working towards this feature internally.
Great proposal. We are interested in this feature as well! :-)
Hi @mildebrandt and the other contributors. First thank you for this amazing library. I honestly think this should be the standard way to validate YAML schema. It would be amazing if you can reach "ruamel.yaml" and "pyyaml" so they could include your library and set the way to validate schema in YAML.
Anyway. I started using Yamale and found myself to wish to get the line number in the yaml file where I have errors. I am using ruamel.yaml, (I don't really now pyyaml but I guess it should be possible too). I use ruamel.yaml in round-trip mode and extract the line number of each keys and I copy your format (with the '.' for nested keys). I get a one level dict with the line number.
Here below my code with comments as well as an example with output. I hope you will find it a nice addition, ideally I see that you add the line numbers into
yamale.YamaleError.results.errors
Then in another file I use it like that :
I guess the code can be improved, I tested quiet a lot but I admit I did not try special cases. But it should not crash as I handled errors, maximum you don't get the line number (just a '?'). I tried to be careful to use only "public" method from ruamel.yaml (CommentedMap.lc.data[key][0]). (I am on Python 3.8, ruamel.yaml 0.16.6 and yamale 3.0.1)
Here below an example: schema.yaml:
data.yaml :
And this is the output I am getting: