Swagger Java日期格式验证引发异常

时间:2018-11-30 07:45:25

标签: java spring swagger

请帮助我解决与swagger验证有关的问题,因为同一正则表达式在java中工作,但在swagger中失败。

有效的Java代码:

@Test
public void test1() {
    String regex = "^((19|20)\\d\\d)(0?[1-9]|1[012])(0?[1-9]|[12][0-9]|3[01])$";
    Assert.assertTrue("Date: matched.", Pattern.matches(regex, "19881231")); -- true
}

Swagger Contract.yml

properties:
  lineofValue:
    type: string
    pattern: ^AB|CD$
    description:  mandatory
  date:
    type: string
    pattern: ^((19|20)\\d\\d)(0?[1-9]|1[012])(0?[1-9]|[12][0-9]|3[01])$
    description:  mandatory

样品要求:

{
"lineofValue":"AB"
"date":"19881231"
}

例外:

{
   "code": "400",
   "status": 400,
   "message": "Validation Failed: ECMA 262 regex \"^((19|20)\\\\d\\\\d)(0?[1-9]|1[012])(0?[1-9]|[12][0-9]|3[01])$\" does not match input string \"19881231\""
}

1 个答案:

答案 0 :(得分:3)

通过Swagger Documentation,看来您 不需要以与Java中相同的方式转义/字符:

properties:
  lineofValue:
    type: string
    pattern: ^AB|CD$
    description:  mandatory
  date:
    type: string
    pattern: ^((19|20)\d\d)(0?[1-9]|1[012])(0?[1-9]|[12][0-9]|3[01])$
    description:  mandatory