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

Any way to get more detailed errors #390

Closed
rtfpessoa opened this issue Jul 29, 2021 · 10 comments · Fixed by #394
Closed

Any way to get more detailed errors #390

rtfpessoa opened this issue Jul 29, 2021 · 10 comments · Fixed by #394

Comments

@rtfpessoa
Copy link
Contributor

Hi,

When using the oapi.OapiRequestValidatorWithOptions is there a way to get a more detailed error?

When I have a parameter which is an enum and the value passed is not in the enum would be great to know the field failing the validation.

Instead I get a huge message about the whole schema:

{"error": "code=400, message=Request body has an error: doesn't match the schema: Doesn't match schema \"oneOf\", internal=Request body has an error: doesn't match the schema: Doesn't match schema \"oneOf\"\nSchema:\n  {\n    \"description\": \"Details of an event.\",\n    \"oneOf\": [\n      {\n        \"$ref\": \"#/components/schemas/Deployment\"\n      },\n      {\n        \"$ref\": \"#/components/schemas/Change\"\n      },\n      {\n        \"$ref\": \"#/components/schemas/Incident\"\n      }\n    ],\n    \"type\": \"object\"\n  }\n\nValue:\n  {\n    \"$type\": \"incidenta\",\n    \"incident_id\": \"1627554151\",\n    \"source\": \"api\",\n    \"time_created\": \"2021-07-29T09:10:00Z\",\n    \"time_resolved\": \"2021-07-29T09:10:00Z\"\n  }\n"}

In this case only the field $type is wrong not the whole object.
Also would be even better if we could let people know what are the valid options.

Any idea if any of this is possible or if this could even be implemented? I would be up to help if some pointers are provided.

@fenollp
Copy link
Collaborator

fenollp commented Jul 29, 2021

#230 should change the schema validation code and bring more detailed errors.

I'm not sure what you mean with OapiRequestValidatorWithOptions: git-grep'ing returns nothing.

Also would be even better if we could let people know what are the valid options.

We can always write more documentation :)

@rtfpessoa
Copy link
Contributor Author

Now I see the problem. I am in the wrong project. 🤦‍♂️
I need to ask this in github.com/deepmap/oapi-codegen

I am sorry for the confusion

@rtfpessoa
Copy link
Contributor Author

rtfpessoa commented Jul 29, 2021

After more digging seems like https://github.com/deepmap/oapi-codegen is using this project after all for the validation of the requests.

The code to do the validation is around here https://github.com/getkin/kin-openapi/blob/master/openapi3/schema.go

Do you think it would be possible to add more details as I was looking for above? Would it be worth looking deeper?

@rtfpessoa rtfpessoa reopened this Jul 29, 2021
@fenollp
Copy link
Collaborator

fenollp commented Jul 31, 2021

I mean, now I'm curious.

@kevin-postman
Copy link

Me too.. as I had a previous issue with trying to get line specific details of an error when it's returned. Not sure that is possible.. but the more details, especially if it can be parsed by the consuming caller in a way that could say.. be helpful to allow more specific information provided back to the client.. that would be fantastic. Line number, position in line, and so forth would all be helpful.

@rtfpessoa
Copy link
Contributor Author

My idea would be to try to add the name of the field with the problem and ideally the schema affected.
But I do not know the code so I would have to dig a bit deeper. That was why I was asking @fenollp if you had any insight into why this was not done. If it was just hard, or impossible with the current implementation.

@rtfpessoa
Copy link
Contributor Author

rtfpessoa commented Aug 4, 2021

I went ahead and navigated and the code and the problem is not the messages. They are actually pretty nice.

It seems to happen in my specific use case of the oneOf where the validator would get possibly N errors and in the current scenario it discards them and just says it does not fulfil the oneOf rule.

I now have a better understanding about this and I think this decision is ok. But I believe that it could really help the person getting the validator output to know more about the failure.

@fenollp what do you think about returning either the last error or returning all the errors?

EDIT: We could also choose the error based on the discriminator if it exists and matches one.

@rtfpessoa
Copy link
Contributor Author

@fenollp I did an experiment here https://github.com/getkin/kin-openapi/pull/394/files

What do you think?

@rtfpessoa
Copy link
Contributor Author

rtfpessoa commented Aug 4, 2021

@fenollp after that PR seems like I still have some vague messages cause by the failfast parameters.

Can you explain or provide some context why it is used internally?

EDIT:

To provide some extra context, I have a schema with a oneOf and in one of the schemas inside it if I have a field missing, a bad enum, etc I always get only the generic message input does not match the schema.
I really would like to get the more detailed message here, but besides removing the failfast logic I have no ideas since I do not understand why it is being used.

@fenollp
Copy link
Collaborator

fenollp commented Aug 4, 2021

failfast when returns "a very cheap to build error" instead of something more involved. I'm unsure this still holds any truth today and will probably disappear with the new schema lib I mentioned on the PR.
Feel free to skip the failfast logic in the context of the oneOf :)

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.

3 participants