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.
The
date-timeformat is defined by RFC 3339 and requires a time zone offset at the end - either±HH:MMorZ/z(zero offset).Examples of valid values:
OpenAPI does not have a format defined for
YYYY-MM-DDTHH:MM:SSvalues without a time zone offset. Such values are considered as just strings.