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
[Java][Spring] Add support for custom validation messages in Beanvalidation annotations #5643
Comments
+1 |
+2 |
+1 |
+1 very important, our clients are getting a ton of regex instead of a readable error message |
Hello @TomSoflow321 @cartick @shexbeer @gmgk93 @unphon i can implement this using vendor extensions.
I would update templates to read the |
While I do think that the use of vendor extensions might work for this, your suggestion might be problematic when multiple constraints are required (e.g. minLength + maxLength). It might also be hard to relate this to 'required' constraints, since these are separated from the field declaration. One option that I see is to still use vendor extensions, but have one per constraint:
What are your thoughts on this? |
i like that, i can do it that way. thanks |
+1 |
You might want to support localisation and might want to consider message keys as well. |
hello guys, i'm really really sorry for delay. i just merged a PR making possible the custom message. swagger: '2.0'
info:
version: '1.0.0'
title: 'simple'
description: 'it is simple'
paths:
/foo:
post:
parameters:
- in: body
name: body
schema:
$ref: '#/definitions/BodyParam'
responses:
200:
description: OK
schema:
$ref: '#/definitions/OKResponse'
400:
description: Error
schema:
$ref: '#/definitions/ErrorResponse'
definitions:
BodyParam:
type: object
properties:
foo:
type: string
pattern: "^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$"
x-pattern-message: "extension pattern message."
message: "The provided String is not Base64 encoded"
OKResponse:
type: object
properties:
bar:
type: string
ErrorResponse:
type: object
properties:
error:
type: integer
message:
type: string
required:
- error
- message The custom message for patter will have this output on BodyParam model: @Validated
@javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "2019-01-25T11:05:38.641-05:00")
public class BodyParam {
@JsonProperty("foo")
private String foo = null;
public BodyParam foo(String foo) {
this.foo = foo;
return this;
}
/**
* Get foo
* @return foo
**/
@ApiModelProperty(value = "")
@Pattern(regexp="^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$", message="extension pattern message.")
public String getFoo() {
return foo;
}
public void setFoo(String foo) {
this.foo = foo;
}
... |
is openapi 3.0.0 has message option to give custom message for validation failure like swagger 2.0 |
@finnuser Even I want that feature. |
Is there a way to have custom message in open spec ? is there any jira on this issue |
Hi, |
+1 want this feature in 3.0.0 |
The PR #4600 added support for beanvalidation annotations, which work fine, but generated annotations also support a
message
property to specify a custom error message if the validation for that annotation fails.This can look like this:
Are there plans to add support for such a message? It might be possible to add it to the declaration file like:
I guess this feature must be added in the
beanValidationCore.mustache
template (inswagger-codegen/src/main/java/resources/
), but I'm not proficient enough with that template language to do that myself.(I've tested the release version of 2.2.0 and the current master version, but both don't support these message properties)
The text was updated successfully, but these errors were encountered: