Validating datetime param in swaggerhub API

34 Views Asked by At

I'm encountering an issue with date-time format validation in my API responses, and I'm seeking guidance on resolving it.

Problem:
I have an API that returns date-time values in the "YYYY-MM-DDTHH:MM:SS" format, which adheres to the "date-time" format standard. However, when I send requests to the API, I consistently receive the following error response:

{
    "type": "about:blank",
    "title": "Bad Request",
    "detail": "'2021-01-30T08:30:00' is not a 'date-time'\n\nFailed validating 'format' in schema:\n    {'format': 'date-time', 'type': 'string'}\n\nOn instance:\n    '2021-01-30T08:30:00'",
    "status": 400
}

and my yml file:

openapi: 3.0.0
info:
  version: "1.0.0"
  title: Health and Fitness Tracking Service
  description: API for logging health and fitness activities

paths:
  /getNutrition:
    get:
      summary: Get nutrition.
      operationId: appstorage.get_nutrition
      description: Enables users to receive their daily food intake between time periods.
      parameters:
        - in: query
          name: start_timestamp
          required: true
          schema:
            type: string
            format: date-time
        - in: query
          name: end_timestamp
          required: true
          schema:
            type: string
            format: date-time
      responses:
        '200':
          description: Successfully returned nutrition data.
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/NutritionDetails'
        '400':
          description: Failed to return nutrition data.
          content:
            application/json:
              schema:
                type: object
                properties:
                  message:
                    type: string

  /getActivity:
    get:
      summary: Get activities.
      operationId: appstorage.get_activity
      description: Enables users to receive their daily activity intake between time periods.
      parameters:
        - in: query
          name: start_timestamp
          required: true
          schema:
            type: string
            format: date-time
        - in: query
          name: end_timestamp
          required: true
          schema:
            type: string
            format: date-time
      responses:
        '200':
          description: Successfully returned activity data.
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/ActivityDetails'
        '400':
          description: Failed to return activity data.
          content:
            application/json:
              schema:
                type: object
                properties:
                  message:
                    type: string

                  

Checked the datetime strings to ensure they conform to the "date-time" format standard. They do. Examined the validator settings to ensure it recognizes the "date-time" format correctly. Ensured there are no extraneous characters or spaces around the datetime strings.

1

There are 1 best solutions below

2
Helen On

The date-time format is defined by RFC 3339 and requires a time zone offset at the end - either ±HH:MM or Z / z (zero offset).

Examples of valid values:

2021-01-30T08:30:00Z
2021-01-30T08:30:00+05:30

OpenAPI does not have a format defined for YYYY-MM-DDTHH:MM:SS values without a time zone offset. Such values are considered as just strings.