swagger-tools icon indicating copy to clipboard operation
swagger-tools copied to clipboard

Custom validation for Request data

Open vishal1 opened this issue 7 years ago • 3 comments

Hi,

My nodejs app is using swagger-tools middleware with express which works fine with most of my service's contracts defined in the swagger spec.

I have a requirement where a request's body object's (json payload) some part is dynamic. So, I can't validate it using the spec. I am thinking of using express validation middleware for any custom validation. Is that a sensible approach or can I use swagger middleware for custom validation as well?

Thankyou

vishal1 avatar Feb 28 '18 12:02 vishal1

Can you elaborate? Swagger should allow for some level of dynamic even if it's just having an open schema for the dynamic part(s).

whitlockjc avatar Feb 28 '18 17:02 whitlockjc

Hello, thankyou for the response. Based on the below example I would like vehicleType inside vehicle definition to be of type either car or bike:

paths:
  /vehicle:
    post:
      parameters:
      - $ref: '#/parameters/vehicle'
      responses:
        200:
          description: Success!
parameters:
  vehicle:
    name: vehicle
    in: body
    required: true
    schema: 
      $ref: "#/definitions/vehicle"

definitions:
  vehicle:
    type: object
    properties:
      id:
        type: string
      name:
        type: string
      vehicleType:
        type: object
        anyOf:
        - $ref: "#/definitions/car"
        - $ref: "#/definitions/bike"
        
  car:
    type: object
    properties:
      model:
        type: string
      type:
        type: string
        enum:
          - Saloon
          - Estate

  bike:
    type: object
    properties:
      model:
        type: string
      type:
        type: string
        enum:
          - Scooter
          - Sport

vishal1 avatar Feb 28 '18 19:02 vishal1

Sorry for the indentation problem above. Not sure how to paste it indented.

vishal1 avatar Feb 28 '18 19:02 vishal1