RAML structure for Arrays

1.2k views Asked by At

My JSON input is as below

{
  "test" : ["t1259", "t2895"]
}

I want to generate RAML data type for the above as below but it is failing with the validation, please let me know whether the below RAML data type is the correct representation for the above JSON.

#%RAML 1.0 DataType
    
type: object
properties:
  test:
    type: array
    items:
      properties:
        type: string
3

There are 3 answers

0
Biltu - Technology On

#%RAML 1.0

title: ValidatorApps

types:

InputType: !include example/json_schema.json

/validate:

description: "This call will validate the input payload"

post:

body:

  application/json:

    type: InputType

    example: !include example/input_file.json

responses:

  200:

    body:

      application/json:

        example:

          {"Success" : "Successfully validated"}

    

Please find the above raml.

Please find the

  • json_schema.json

file code

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "test": {
      "type": "array",
      "items": [
        {
          "type": "string"
        },
        {
          "type": "string"
        }
      ]
    }
  },
  "required": [
    "test"
  ]
}

Please find the

  • input json file

    { "test" : ["t1259", "t2895"] }

1
aled On

I believe that you want an array of strings:

type: string[]
0
Harshank Bansal On

What you have defined is an Array of Objects, where each object has a field "type" of type "string". The problem is in the following part.

items:
  properties: # This is the problem. You have defined "properties" in each item. RAML thinks that it is an object with property "type"
  type: string

So your RAML will accept this array: [{"type": "stringvalue"}] You have to remove the properties keyword under items. Or you can use the shorthand version that Aled mentioned in his answer (which is more readable too, but I am mentioning this as sometimes you may need to define complex objects in which case you will have to use the below one).

#%RAML 1.0 DataType

type: object
properties:
  test:
    type: array
    items:
      type: string # This means that type of each item is string