如何在Swagger Open API 3.0规范的定义中提供电子邮件格式?

huangapple go评论57阅读模式
英文:

How to give email format in definitions in Swagger Open API 3.0 Specification?

问题

在我的Swagger Open API文档中,我提供了如下的对象定义:

"definitions": {
    "User": {
        "type": "object",
        "properties": {
            "id": {
                "type": "integer",
                "format": "int64"
            },
            "firstName": {
                "type": "string"
            },
            "email": {
                "type": "string"
            },
            "shipDate": {
                "type": "string",
                "format": "date-time"
            },
            "status": {
                "type": "string",
                "description": "Order Status",
                "enum": [
                    "placed",
                    "approved",
                    "delivered"
                ]
            }
        }
    }
}

我无法找到应该如何为电子邮件地址指定格式的信息:

"email": {
    "type": "string",
    "format": "####"
}

我查看了官方文档1,他们提到:

诸如 "email"、"uuid" 等格式,即使未在此规范中定义,也可以使用。没有伴随格式属性的类型应遵循JSON Schema中的类型定义。

我正在努力实现这一目标,有什么提示可以帮助我吗?

英文:

In my swagger Open API document I am giving Object Definition like below:

"definitions": {
  "User": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer",
            "format": "int64"
          },
     
          "firstName": {
            "type": "string"
          },
        
          "email": {
            "type": "string"
          },
          "shipDate": {
            "type": "string",
           "format": "date-time"
          },
         "status": {
          "type": "string",
          "description": "Order Status",
        "enum": [
          "placed",
          "approved",
          "delivered"
        ]
      }
}

I am not able to find format to be metioned for email address like :

 "email": {
        "type": "string",
        "format" : "####"
      }

I went through official Doc, they are saying :

> Formats such as "email", "uuid", and so on, MAY be used even though
> undefined by this specification. Types that are not accompanied by a
> format property follow the type definition in the JSON Schema.

I am struggling to achieve this, Any hint how can I achieve this?

答案1

得分: 4

你可以使用正则表达式 pattern 限制可接受的电子邮件域。例如,如果电子邮件必须以 .io 结尾,你可以使用 \.[Ii][Oo]$ 模式:

"email": {
  "type": "string",
  "format": "email",
  "pattern": "\\.[Ii][Oo]$"
}

请注意,在字符串中的 \ 字符会被转义为 \\

英文:

You can use a regex pattern to limit acceptable email domains. For example, if the email must end with .io you can use the \.[Ii][Oo]$ pattern:

"email": {
  "type": "string",
  "format": "email",
  "pattern": "\\.[Ii][Oo]$"
}

Note that the \ character inside a string is escaped as \\.

huangapple
  • 本文由 发表于 2020年7月31日 19:57:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63191398.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定