在我 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" : "####"
}
我通过了官方Doc ,他们说:
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.
我正在努力实现这一目标,有什么提示可以实现吗?
最佳答案
您可以使用正则表达式 pattern
来限制可接受的电子邮件域。例如,如果电子邮件必须以 .io
结尾,您可以使用 \.[Ii][Oo]$
模式:
"email": {
"type": "string",
"format": "email",
"pattern": "\\.[Ii][Oo]$"
}
请注意,字符串中的 \
字符被转义为 \\
。
关于java - 如何在 Swagger Open API 3.0 规范中定义电子邮件格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63191398/