代码之家  ›  专栏  ›  技术社区  ›  sunleo

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

  •  1
  • sunleo  · 技术社区  · 6 年前

    工作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
    }
    

    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 回复  |  直到 6 年前
        1
  •  3
  •   npinti    6 年前

    通过 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