user3540722
user3540722

Reputation: 175

YYYYMMDD Date format on yaml definition

I have a requirement where the request pass date in YYYYMMDD format. Based on swagger documentation, date filed defined under string type. However, it follows RFC 3339, section 5.6, documentation (ex.2018-03-20 as format)

below code doesn't work with yaml.

dateOfBirth:
          type: string
          minLength: 8
          maxLength: 8
          format: date
          example: 19000101
          description: Birth date of the  member in YYYYMMDD format.

How to define YAML definition for the date format of YYYYMMDD.

Upvotes: 3

Views: 12635

Answers (1)

bdzzaid
bdzzaid

Reputation: 908

According to documentation for type string you can add regex pattern to define the date format YYYYMMDD :

pattern: '^\d{4}(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])$'

Also let me suggest you to update your example date format with a date like 20190317 to easily understand the expected date format.

definitions:
  Pet:
    type: object
    required:
      - id
      - name
    properties:
      id:
        type: integer
        format: int64
      name:
        type: string
      tag:
        type: string
      dateOfBirth:
        type: string
        minLength: 8
        maxLength: 8
        format: date
        pattern: '^\d{4}(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])$'
        example: 20190816
        description: Birth date of the  member in YYYYMMDD format.

Upvotes: 6

Related Questions