Search code examples
spring-bootswaggeropenapiswagger-core

How to use pattern in openapi


I am trying to use pattern to validate the value using regex as below

host:
type: string
description: mail.mydomain.com
example: mail.mydomain.com
format: regex
pattern: '\S'

while generating a class it is generating as 

  @ApiModelProperty(
example = "mail.mydomain.com",
required = true,
value = "mail.mydomain.com"
)
public @NotNull @Pattern(
regexp = "\\S"
) String getHost() {
return this.host;
}

public void setHost(String host) {
this.host = host;
}

it is appending \\ one more escape character

I have added

  <dependency>
    <groupId>javax.validation</groupId>
    artifactId>validation-api</artifactId>
   </dependency>

tried with \ and also quotes nothing worked


Solution

  • using patter with + or * will resolve the issue Ex: pattern: '\S+' or pattern: '\S*'

    \S matches only one character, if we have only one character in the field then it will work with \S.

    when you want a regex to match multiple characters you need to use + sign, \S+.

    Finally able to resolve this issue.