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

javax.validation... error message from annotation to validator error message #373

Closed
VovkaSOL opened this issue Aug 9, 2023 · 10 comments
Closed

Comments

@VovkaSOL
Copy link

VovkaSOL commented Aug 9, 2023

Hi developers, annotation like @NotNull(message="xxxxxx") have error message text xxx. Json schema support's message for concrete validation(
"message": {
"maxItems" : "MaxItem must be 3 only",
"type" : "Invalid type"
}
)
Please add options in JavaxValidationOption enum for support error message copy in schema

@CarstenWickner
Copy link
Member

CarstenWickner commented Aug 9, 2023

Hi @VovkaSOL,

Can you please point me to the part of the JSON Schema Specification where this is described?
I couldn't find it myself, unfortunately.

@VovkaSOL
Copy link
Author

@CarstenWickner i can't find it in specification, but i found it in stackoverflow :-) https://stackoverflow.com/questions/40423376/json-schema-validator-custom-message
some PR in github json-schema-org/json-schema-spec#643

victools/jsonschema-generator support this error messages format too :-)
...
"properties": {
"beneficiary": {
"type": "object",
"message": {
"type": "type error text",
"required": "required error text"
},
...

@VovkaSOL
Copy link
Author

@CarstenWickner
Copy link
Member

I see no mentioning of "message" in that JSON Schema specification.

Where exactly do you see it?

@VovkaSOL
Copy link
Author

Very strange situation with custom messages for json schema validations errors, specification says "

12.3.4. Error or Annotation
The error or annotation that is produced by the validation.

For errors, the specific wording for the message is not defined by this specification. Implementations will need to provide this.

For annotations, each keyword that produces an annotation specifies its format. By default, it is the keyword's value.

The JSON key for failed validations is "error"; for successful validations it is "annotation".
"

but this message blocks https://stackoverflow.com/questions/40423376/json-schema-validator-custom-message work good in victools/jsonschema-generator.

@CarstenWickner
Copy link
Member

With this not being a standard feature in the specification, I'm hesitant to add it as standard feature in the generator here. Additionally, those validation annotations may specify a message key, which also would need to be resolved first.

Through the existing configuration options, you're able to add this yourself though.
Let me know, if you need assistance with that configuration and I'll be happy assist in creating an example for it.

@fernandosesma
Copy link

Hi @CarstenWickner,

I've encountered the issue discussed here, can you still create an example of how this can be accomplished with the existing configuration options. Thanks!

@CarstenWickner
Copy link
Member

Hi @fernandosesma,

I've created a basic example and will be adding it to the repository.
Refer to PR #390 for a possible configuration and the corresponding schema output.

In order to cover all possible validation annotations, I'm afraid you'll have to do considerable custom configurations.
But the example should give you an idea how it can be achieved in general.
I hope that helps.

@fernandosesma
Copy link

Hi @fernandosesma,

I've created a basic example and will be adding it to the repository. Refer to PR #390 for a possible configuration and the corresponding schema output.

In order to cover all possible validation annotations, I'm afraid you'll have to do considerable custom configurations. But the example should give you an idea how it can be achieved in general. I hope that helps.

Great! This worked like a charm. Thanks so much!

@CarstenWickner CarstenWickner closed this as not planned Won't fix, can't repro, duplicate, stale Oct 31, 2023
@kunalshukla11
Copy link

kunalshukla11 commented Nov 30, 2023

Hi @fernandosesma,

I've created a basic example and will be adding it to the repository. Refer to PR #390 for a possible configuration and the corresponding schema output.

In order to cover all possible validation annotations, I'm afraid you'll have to do considerable custom configurations. But the example should give you an idea how it can be achieved in general. I hope that helps.

@CarstenWickner
We are generating schemas based on Jarkarta Annotations and Groups , and we use Jakarta's "message" property to produces error messages while validation.
While we can do the internal backend validation with with JakartaValidators , we get the desired messages , because it's Jakarta's implementation to validate and produce error messages.

Problem arose when we created the schema with Jakarta Annotations and messages(with Jakarta Module) , no messages were populated in the Json.
Your PR/Example seems to be very good option to include messages(hopefully standard) which JsonValidators can consume and produce meaning ful custom messages.
Initially I was trying a workaround with Additional properties ,but it didn't help with standard validators to consume the error messages.

Thanks for your PR/Example , hopefully we would use this for our use case and will update if it worked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants