apigee-127 / swagger-tools

A Node.js and browser module that provides tooling around Swagger.
MIT License
701 stars 373 forks source link

Custom validation for Request data #563

Open vishal1 opened 6 years ago

vishal1 commented 6 years ago

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

whitlockjc commented 6 years ago

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

vishal1 commented 6 years ago

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 commented 6 years ago

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