首页 > 解决方案 > openapi - 不允许空格或连字符的正则表达式

问题描述

我正在使用 openapi 3.0.3 自动生成基于 Spring Boot 的 REST API ...

内部src/main/resources/openapi/schema/PurchaseOrder.yaml

openapi: '3.0.3'
info:
  title: 'Purchase Order'  
  version: '1.0'
paths: {}

components:
  schemas:
    PurchaseOrder:
      title: 'Purchase Order'
      type: 'object'
      properties:
        account:
          type: 'string'
          description: Identifier for account making the purchase
          example: 1
          minLength: 1
          pattern: '^\s-$'

所以,我需要它包含字母数字值但没有空格(尝试使用 this pattern: '^\s-$') - 根本不能有空格......

尝试时:

{
    account: 'a b'
}

这是我收到的 JSON 响应:

"validationErrors": [
  {
    "field": "account",
    "message": "must match \"^\\s-$\""
  }
]

当我尝试使用有效的 JSON(包含多个字符且没有空格和连字符)时,也会发生同样的验证错误:

{
   'account': 'ab'
}

这应该是有效的并且没有抛出任何验证错误......


如何在此 yaml 文件中使用正则表达式完全不强制使用空格和/或连字符?

标签: javaregexopenapi

解决方案


推荐阅读