英文:
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 \\
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论