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

Assistance / Feature Request : Retrieving JSON Path of Validation Errors #120

Open
tommynanny opened this issue Feb 4, 2025 · 1 comment
Labels
enhancement New feature or request

Comments

@tommynanny
Copy link

I am using "github.com/pb33f/libopenapi-validator" to validate HTTP requests (converted from fasthttp). The validation works as expected, and the error messages provide meaningful reasons.

However, I need to extract the JSON path of the problematic item in the request payload. The Location field in the validation failure only provides the schema path from the OpenAPI (Swagger) YAML, while both DeepLocation and AbsoluteLocation are empty strings.

Upon debugging in VS Code, I found that within OriginalError, there is a Causes field. By recursively unwrapping the errors inside Causes, I eventually reach a v6.ValidatorError instance, whose InstanceLocation field correctly holds the JSON path as a []string array.

My Questions:

  • Is this the expected behavior, where InstanceLocation is deeply nested inside OriginalError -> Causes?
  • Is there a helper method in libopenapi-validator that allows me to directly retrieve the JSON path of the payload error without manually unwrapping the causes?

Feature Suggestion:

I think returning the JSON path of the problematic payload more directly could be very helpful for users to precisely locate the problematic item, especially when dealing with large and complex payloads. Would it be possible to expose this information in a more accessible way?

Any guidance or recommendations would be greatly appreciated.

Image

Image

@daveshanley
Copy link
Member

daveshanley commented Feb 5, 2025

Hi,

When you step into v6 you're stepping down into this: https://github.com/santhosh-tekuri/jsonschema

I can't control how it works down there, only use what is available, in the best way I can.

I have done by best to hide the API from consumers, because, it's not great to be honest. Your suggestions are good ones and I agree, extracting the JSON path from the string slice would be valuable.

There are no mechanisms in place to do this for you today, it would need to be added as new methods on the validator. So to give you a (not great) answer, there isn't anything that will help you today in the lib - but you have basically defined the new feature and how it should work.

@daveshanley daveshanley added the enhancement New feature or request label Feb 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants