carlansley / swagger2

Loading, parsing and validating requests to HTTP services based on Swagger v2.0 documents
MIT License
25 stars 17 forks source link

How about using special JSON schema validator to validate? #13

Open zaaack opened 7 years ago

zaaack commented 7 years ago

I see something like https://github.com/epoberezkin/ajv , they follow the specification, and can handle complex date type like date, which current swagger2 can't.

carlansley commented 7 years ago

Looks interesting, might be worth switching (especially for performance and async deref).

johnschult commented 6 years ago

Yes, that would be nice. Currently defining a property as as below fails validation. Only using type: 'string' fails as well.

As is, there is no way to accurately represent a primitive Data Type of string with format date-time. The only option is to not set type or format.

Schema snippet

{
  ...
  createdAt: {
    type: 'string',
    format: 'date-time',
    example: '2018-04-14T18:41:13.586Z',
    readOnly: true
  },
  updatedAt: {
    type: 'string',
    format: 'date-time',
    example: '2018-04-14T18:41:13.586Z',
    readOnly: true
  }
}

Validation error

{
  code: 'SWAGGER_RESPONSE_VALIDATION_FAILED',
  errors: [
    {
      actual: [
        {
          name: 'My Robot',
          createdAt: '2018-04-25T13:01:08.657Z',
          updatedAt: '2018-04-25T13:01:08.657Z',
          id: '5ae07c1431dbe07868eeace0'
        }
      ],
      expected: {
        schema: {
          type: 'array',
          items: {
            type: 'object',
            required: [
              'name'
            ],
            properties: {
              id: {
                type: 'string',
                description: 'The object ID',
                example: '507f191e810c19729de860ea',
                readOnly: true
              },
              name: {
                type: 'string',
                description: 'The name of the Robot',
                example: 'My Robot'
              },
              createdAt: {
                type: 'string',
                example: '2018-04-14T18:41:13.586Z',
                readOnly: true
              },
              updatedAt: {
                type: 'string',
                example: '2018-04-14T18:41:13.586Z',
                readOnly: true
              }
            }
          }
        }
      },
      error: 'data.0.createdAt is the wrong type\ndata.0.updatedAt is the wrong type',
      where: 'response'
    }
  ]
}