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

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

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

问题

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

  1. "definitions": {
  2. "User": {
  3. "type": "object",
  4. "properties": {
  5. "id": {
  6. "type": "integer",
  7. "format": "int64"
  8. },
  9. "firstName": {
  10. "type": "string"
  11. },
  12. "email": {
  13. "type": "string"
  14. },
  15. "shipDate": {
  16. "type": "string",
  17. "format": "date-time"
  18. },
  19. "status": {
  20. "type": "string",
  21. "description": "Order Status",
  22. "enum": [
  23. "placed",
  24. "approved",
  25. "delivered"
  26. ]
  27. }
  28. }
  29. }
  30. }

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

  1. "email": {
  2. "type": "string",
  3. "format": "####"
  4. }

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

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

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

英文:

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

  1. "definitions": {
  2. "User": {
  3. "type": "object",
  4. "properties": {
  5. "id": {
  6. "type": "integer",
  7. "format": "int64"
  8. },
  9. "firstName": {
  10. "type": "string"
  11. },
  12. "email": {
  13. "type": "string"
  14. },
  15. "shipDate": {
  16. "type": "string",
  17. "format": "date-time"
  18. },
  19. "status": {
  20. "type": "string",
  21. "description": "Order Status",
  22. "enum": [
  23. "placed",
  24. "approved",
  25. "delivered"
  26. ]
  27. }
  28. }

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

  1. "email": {
  2. "type": "string",
  3. "format" : "####"
  4. }

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]$ 模式:

  1. "email": {
  2. "type": "string",
  3. "format": "email",
  4. "pattern": "\\.[Ii][Oo]$"
  5. }

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

英文:

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:

  1. "email": {
  2. "type": "string",
  3. "format": "email",
  4. "pattern": "\\.[Ii][Oo]$"
  5. }

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:

确定