Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 990
Pramod S. Nikam Avatar asked Nov 01 '25 19:11

Pramod S. Nikam


1 Answers

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 \\.

like image 54
Helen Avatar answered Nov 03 '25 10:11

Helen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!