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

Suggestions on post validation line & column information? #340

Closed
ascertrobw opened this issue Oct 31, 2020 · 7 comments · Fixed by #352
Closed

Suggestions on post validation line & column information? #340

ascertrobw opened this issue Oct 31, 2020 · 7 comments · Fixed by #352

Comments

@ascertrobw
Copy link
Contributor

One aspect we are really battling with.

How to log line/column information to a user for a YAML document that passes schema validation, but then fails semantic checks on the content of the JSON node tree?

Maybe we're missing something obvious here - but so far we have a multi step process:

  1. Loading the YAML file/stream into JSON and then passing the loaded JSON tree to the factory for validation
  2. Application code for processing the validated Node tree

After step 1 we have effectively lost the original source file information, so if we detect any semantic errors we can't easily report them back to the user with line/col information against the original file.

I doubt this is feasible, but I'm wondering if there couldn't be an option to include meta information in the processed JSON Node tree such as the original source positions of various nodes, which would then allow such error reporting.

@ascertrobw
Copy link
Contributor Author

ascertrobw commented Nov 13, 2020

Closing this issue as we found a way to handle this.

If it's of any interest, it wasn't that hard, involving a few fairly simple steps.

  • Create a custom subclass of JsonNodeFactory that had access to the YAMLParser, where "token" and "current" file location information is available
  • Create custom subclass of the various JsonNodes (TextNode, NumericNode, ObjectNode etc etc) which can be instantiated with this location information. Each of these node subclasses implements a common "LocationProvider" interface to allow retrieval of the original YAML file location details
  • When asked to create a new JsonNode, the custom JsonNodeFactory returns one of the new subclassed node types, with the YAMLParser file location information embedded within it
  • When processing the JsonNode tree later, any nodes which are instanceof "LocationProvider" can be interrogated if original YAML file location information is needed e.g. for error or other reporting

@stevehu
Copy link
Contributor

stevehu commented Nov 15, 2020

@ascertrobw Your workaround makes perfect sense. I am wondering if you want to contribute to the library so that other users can enjoy it. Thanks.

@ascertrobw
Copy link
Contributor Author

@ascertrobw Your workaround makes perfect sense. I am wondering if you want to contribute to the library so that other users can enjoy it. Thanks.

Not sure how generally applicable it will be - but happy to share the code I used. Is there an "examples" or "library" section somewhere for that, or should I just ZIP and post here?

@ascertrobw
Copy link
Contributor Author

ascertrobw commented Nov 16, 2020

On a similar topic - I'm not seeing any way to get line/col information out of a ValidationMessage when a document fails validation against a schema. Which makes sense I guess, since the schema validate is also working from the converted JSON which has lost it's trace back to the original YAML source numbers. I'm unsure whether our approach would be workable for internal use, but if so then it might help bridge that gap also.

@stevehu
Copy link
Contributor

stevehu commented Nov 16, 2020

@ascertrobw I just sent an invite to join the team. You can create a feature branch and add an example folder to show us how it works. If we can find a way to generalize it, then we merge into the library. Otherwise, it will live in the example folder for users to adopt.

@ascertrobw
Copy link
Contributor Author

ascertrobw commented Nov 16, 2020

@ascertrobw I just sent an invite to join the team. You can create a feature branch and add an example folder to show us how it works. If we can find a way to generalize it, then we merge into the library. Otherwise, it will live in the example folder for users to adopt.

Cool, thanks, will do. I'm just chasing down the "line numbers in ValidationMessage" aspect, see if I can create an example for that also. Should hopefully be able to carve out some time to create standalone examples in the next week or two once I'm done and have got my current work off my desk.

ascertrobw added a commit that referenced this issue Nov 24, 2020
Example describing how to retain YAML source file location information in processed and validated JSON tree

Fixes #340
@ascertrobw
Copy link
Contributor Author

@ascertrobw I just sent an invite to join the team. You can create a feature branch and add an example folder to show us how it works. If we can find a way to generalize it, then we merge into the library. Otherwise, it will live in the example folder for users to adopt.

A tad short of time this week to create a full worked exampled - so I've created a .MD instead which describes the scenarios and solutions

stevehu pushed a commit that referenced this issue Nov 24, 2020
* YAML source location handling (#340)

Example describing how to retain YAML source file location information in processed and validated JSON tree

Fixes #340

* Fixed typos

* Woops - more typos!
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

Successfully merging a pull request may close this issue.

2 participants