Bibek Kr. Bazaz
Bibek Kr. Bazaz

Reputation: 545

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?

Upvotes: 0

Views: 2770

Answers (2)

agentv
agentv

Reputation: 779

One more thing, I think. To require input to comply with a regex, you might do this:

properties:  
Name:
  type: string
  required: true
  displayName: "Name"
  description: "Name"
  pattern: ^[-A-Za-z ]+$
  example: "John"

That pattern is overly restrictive, but does match many Western traditional names. Your own regex is presumably more carefully constructed.

Upvotes: 1

aled
aled

Reputation: 25812

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

Upvotes: 3

Related Questions