express-openapi-validator icon indicating copy to clipboard operation
express-openapi-validator copied to clipboard

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

Open zeeshanali786 opened this issue 3 years ago • 2 comments

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 })

//https://github.com/expressjs/generator/issues/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 avatar Oct 19 '21 18:10 zeeshanali786

@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/

alexthehurst avatar Nov 09 '21 18:11 alexthehurst

This could be related to #699.

robertjustjones avatar Feb 14 '22 21:02 robertjustjones