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

Support for resolving deserialized objects to their line/ char positions #494

Closed
djluck opened this issue May 19, 2020 · 5 comments
Closed

Comments

@djluck
Copy link

djluck commented May 19, 2020

I'm currently attempting to validate the deserialized output of a YAML file using YamlDotNet. There's a few types of validation I'd like to be able to do:

  1. Verifying property values on deserialized objects are valid
  2. Verifying deserialized collections are valid
  3. Verifying deserialized dictionary keys and members are valid

For each type of validation, I'd like to be able to give the exact file location of an invalid value. I've seen the validation sample in the wiki and while this works (and the idea could be extended to collections and dictionaries), it doesn't support fetching the position of invalid values- we can just throw exceptions for the final position of the deserialized item.

Looking at the codebase, I think it might be possible to extend the various INodeDeserializer implementations to emit positional information of property names & values/ dictionary keys & values and collection members. Before I attempt a PR, I'd like to hear thoughts on if this is a good idea or not or if there's an easier way to get what I want.

@aaubry
Copy link
Owner

aaubry commented May 20, 2020

You can extend the various INodeDeserializer by registering your own implementation of that interface and add whatever behaviour you need, as shown in that sample. There's no need to change the existing code to achieve what you want.
I don't think that this should be incorporated in the main YamlDotNet library, as this is a very niche requirement and it is trivially added by the user is needed.

@MaorDavidzon
Copy link

@djluck Did you managed to solve your problem using INodeDeserializer ?

@smaillet
Copy link

@aaubry - I'll have to respectfully disagree with you on the point this doesn't belong in the core library. Location information is a staple part of any parser technology. Providing a way to get access to the original source line and character position is key to properly reporting errors, not just in parsing but in later sematic analysis of the resulting data. There's no such thing as a schema to use for validation with YAML. Thus, code parsing YAML content needs to have a way to get at that information to report when things are syntactically correct, yet semantically incorrect. Forcing everyone into building their own deserializers for all such cases is brutal and pointlessly repetitions effort on library users that is much more easily resolved once, for all in the library itself.

@djluck
Copy link
Author

djluck commented Sep 12, 2022

I ended up writing a project to help achieve the request- https://github.com/djluck/YamlDotNet.Locations. it's an early release so any feedback would be appreciated.

@EdwardCooke
Copy link
Collaborator

EdwardCooke commented Jul 11, 2024

One option is using these attributes on methods in your classes for validation. Throwing an exception would fail the serialization/deserialization which would include the start and ending position of the current element in the yaml file which seems like it will give you want. Closing this issue.

https://github.com/aaubry/YamlDotNet/tree/master/YamlDotNet/Serialization/Callbacks

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

5 participants