swagger2 icon indicating copy to clipboard operation
swagger2 copied to clipboard

How about using special JSON schema validator to validate?

Open zaaack opened this issue 8 years ago • 2 comments

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.

zaaack avatar Feb 11 '17 04:02 zaaack

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

carlansley avatar Feb 11 '17 09:02 carlansley

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'
    }
  ]
}

johnschult avatar Apr 26 '18 13:04 johnschult