libopenapi-validator icon indicating copy to clipboard operation
libopenapi-validator copied to clipboard

Assistance / Feature Request : Retrieving JSON Path of Validation Errors

Open tommynanny opened this issue 1 year ago • 1 comments

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

tommynanny avatar Feb 04 '25 18:02 tommynanny

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 avatar Feb 05 '25 17:02 daveshanley

Just adding a +1 for this feature request

its-hammer-time avatar May 20 '25 17:05 its-hammer-time

Yeah, this is a good idea. I'm gonna build it.

daveshanley avatar May 20 '25 21:05 daveshanley

ExtractJSONPathFromValidationError(e *jsonschema.ValidationError) string

and

ExtractJSONPathsFromValidationErrors(errors []*jsonschema.ValidationError) []string

Both now available in the helpers module.

Dives down to the bottom of the stack to pull out the JSON path.

daveshanley avatar May 20 '25 21:05 daveshanley

I used AI to build this feature btw.

daveshanley avatar May 20 '25 21:05 daveshanley