Skip to content

express-openapi-validator is giving error on correct date-time #668

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
zeeshanali786 opened this issue Oct 19, 2021 · 2 comments
Open

Comments

@zeeshanali786
Copy link

zeeshanali786 commented Oct 19, 2021

Describe the bug
In OpenAPI specs, I have a field with date-time format like
startDateTime:
type: string
format:date-time

express-openapi-validator is giving error on even correct date-time dates("startDateTime": "2022-02-15T00:00:00Z") . Below is the error, I am receiving
{
"message": "request.body.events[0].startDateTime should match format "date-time", request.body.events[0].endDateTime should match format "date-time", request.body.events[0].captureDate should match format "date-time"",
"errors": [
{
"path": ".body.events[0].startDateTime",
"message": "should match format "date-time"",
"errorCode": "format.openapi.validation"
}
]
}
To Reproduce
Steps to reproduce the behavior.
When I am hitting the api from Postman with below payload, I am getting invalid date-time error. "should match format "date-time"

{
"events": [

  {
   "eventId": "222222",

    "eventName": "AI Summit",
    "description": "Automated ground-breaking solutions that are transforming business productivity",
    "startDateTime": "2022-02-15T00:00:00Z"
}

],
"metadata": {
"sessionId": "33e95d8e-b72a-46ce-aa2b-b242ef0cad88",
"pageSize": 2,
"currentPage": 1,
"numberOfPages": 1,
"totalElements": 1
}
}

Actual behavior
A clear and concise description of what happens.

Expected behavior
A clear and concise description of what you expected to happen
Should not give validation error on correct date-time format

Examples and context
OPenAPI SPecs
openapi: 3.0.1
info:
title: OpenAPI definition
version: v0
servers:

  • url: 'http://someurl.com'
    description: Generated server url
    paths:
    /special-events/v1:
    put:
    tags:
    - special-event-inbound-controller
    summary: >-
    All special events which are either newly added or has some changes
    since last week
    description: 'In success case, Http Code 200 will be returned'
    operationId: putSpecialEventsInbound

    requestBody:
    description: Events list to process
    required: true
    content:
    application/json:
    schema:
    $ref: '#/components/schemas/SpecialEventRequest'
    responses:
    '200':
    description: Success
    content: {}
    '400':
    description: Bad request
    content:
    '/':
    schema:
    $ref: '#/components/schemas/ApiError'
    '401':
    description: Authentication Failure
    '500':
    description: Internal server error
    content:
    '/':
    schema:
    $ref: '#/components/schemas/ApiError'
    components:
    schemas:
    Metadata:
    required:
    - currentPage
    - numberOfPages
    - pageSize
    - sessionId
    - totalElements
    type: object
    properties:
    sessionId:
    type: string
    description: A unique ID to correlate multiple pages of data being delivered
    example: 33e95d8e-b72a-46ce-aa2b-b242ef0cad99
    pageSize:
    type: integer
    description: The number of items being delivered per page of data
    format: int32
    example: 2500
    currentPage:
    type: integer
    description: 'The current page, of numberOfPages, being sent'
    format: int32
    example: 5
    numberOfPages:
    type: integer
    description: The total number of pages to be sent for the session
    format: int32
    example: 10
    totalElements:
    type: integer
    description: The total number of elements to be sent for the session
    format: int32
    example: 24500
    SpecialEvent:
    required:
    - eventId
    - eventName
    - startDateTime
    type: object
    properties:
    eventId:
    type: string
    description: A unique identifier for the event
    example: 519863
    eventName:
    type: string
    description: The name of the event
    example: AI Summit
    description:
    type: string
    description: The description of the event
    example: >-
    Automated ground-breaking solutions that are transforming business
    productivity
    startDateTime:
    type: string
    description: Start date of the event
    format: date-time
    SpecialEventRequest:
    type: object
    properties:
    events:
    type: array
    items:
    $ref: '#/components/schemas/SpecialEvent'
    metadata:
    $ref: '#/components/schemas/Metadata'

    ApiError:
    required:
    - error
    - message
    - status
    - timestamp
    type: object
    properties:
    error:
    type: string
    description: HTTP status message
    exception:
    type: string
    description: Fully qualified exception type
    message:
    type: string
    description: Exception / error message
    path:
    type: string
    status:
    type: integer
    description: HTTP status code for the error
    format: int32
    timestamp:
    type: string
    description: Date and time the error occurred
    format: date-time

App.ts and handler code
import express from 'express'
import serverLess from 'serverless-http'
import setupXRay from "./core/util/setupXRay"
import log from "./core/util/log.service"

const path = require('path')
const specialEventsController = require('./controller/special-events-controller')
const specialEventsReprocessController = require('./controller/special-events-reprocess-controller')
const app = express()

const bodyParser = require('body-parser')
const OpenApiValidator = require('express-openapi-validator')
app.use(bodyParser.urlencoded({extended: true}))
app.use(bodyParser.json({limit: '200mb'}))

const apiSpec = path.join(__dirname, 'public/special-events-inbound-api.yaml')

app.use('/spec', express.static(apiSpec))
app.use(
OpenApiValidator.middleware({
apiSpec,
validateResponses: true
}),
)
setupXRay()

app.put('/special-events/v1', async function (req, res) {
await specialEventsController.ingestSpecialEvents(req, res)
return undefined
})

app.put('/replay-processor/v1', async function (req, res) {
await specialEventsReprocessController.reprocessSpecialEvents(req, res)
return undefined
})

//expressjs/generator#78
// eslint-disable-next-line @typescript-eslint/no-unused-vars
app.use((err, req, res, next) => {
log.error(API call failed, Request: ${req}, err.message)
res.status(err.status || 500).json({
message: err.message,
errors: err.errors,
})
})

app.listen(3000, () => {
console.log("Server is running on port", 3000)
})

module.exports.handler = serverLess(app)

@zeeshanali786 zeeshanali786 changed the title express-openapi-validator is not giving error on correct date-time express-openapi-validator is giving error on correct date-time Oct 19, 2021
@alexthehurst
Copy link

@zeeshanali786 It's hard to diagnose this because of the lost formatting in your code samples, especially the YAML. Are you able to edit so that the all the code samples are in formatted blocks? There's a Markdown syntax guide here: https://guides.github.com/features/mastering-markdown/

@robertjustjones
Copy link
Contributor

This could be related to #699.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants