mule4raml

Mukle 4 : RAML : how to define the schema of a POST BODY request in a RAML file?


A POST REST request having 3 body params as follows:

{
   "name" : "ABC",
   "age": 34,
   "uniqueID": "12345sdfgh"
}

My requirement is to define constraints (type, maxlength, min length, regex, etc.) for each field name, age and unique id.

How can I define that?


Solution

  • There are some different ways to define it. The 'pure' RAML way it is to define a data type fragment for the data object using RAML definitions for types. Those should cover all your needs.

    Example:

    dataType.raml

    #%RAML 1.0 DataType
    type: object                   
    displayName: Booking
    properties:
      BookingDetail:
        type: object
        required: true
        displayName: "BookingDetail"
        description: "BookingDetail"
        properties:  
            Name:
              type: string
              required: true
              displayName: "Name"
              description: "Name"
              example: "John"
            NumberOfDays:
              type: integer
              required: true
              minimum: 1
              maximum: 10
    

    API:

    #%RAML 1.0
    title: so-type
    
    /bookings:
      post:
        body:
          application/json:
            type: !include dataType.raml
    

    You can also use JSON schemas if you prefer:

    /orders:
      post:
        body:
          application/json:
            type: !include schemas/OrdersSchema.json