5

Is there a way to have the API Kit Router validate incoming schema? I have the following in my RAML file but it does not validate the incoming schema.

  - emails: |
      {
        "$schema": "http://json-schema.org/draft-04/schema#",
        "type" : "object",
        "properties" : {
          "email" : {
            "type" : "string"
          },
          "name" : {
            "type" : "string"
          },
          "emailOrigin" : {
            "type" : "string"
          }
        }
      }

resourceTypes: 
  - postbase:
      post:
        responses:
          200:
            body:
              application/json:
          500:
            body:
              application/json:
  - putBase:
      put:
        responses:
          200:
            body:
              application/json:
          500:
            body:
              application/json:

/emails:
  type: postbase
  post:
    description: |
      Recieve emails captured from various parts of the site.
    body: 
     schema: emails   
SteveS
  • 1,008
  • 3
  • 18
  • 32
  • Could you post the JSON which you expect to fail in this case ? – Sudarshan Jun 11 '15 at 00:35
  • I have been off this project since about 2 weeks after I made the post (actually at a different company). What I was looking for was if someone sends an int instead of a string it should fail. In retrospect I suppose that anything should be accepted by a string but perhaps if I had defined an int field and sent a string it would have actually failed. I probably also needed to require a field. At this point it is just an exercise for anyone else who may search for the question it as I am not using RAML in my current project. – SteveS Jun 16 '15 at 13:10

2 Answers2

1

The following references help you further http://forums.raml.org/t/examples-validations-in-raml-parsers/80

Further Example as below: employeeDetailsSchema.json

{
    "type": "object",
    "$schema": "http://json-schema.org/draft-03/schema",
    "id": "http://jsonschema.net",
    "required": true,
    "properties": {
        "employeeID": {
            "type": "string",  -------> Validates the Data type
            "required": true   -------> Validates whether data is present or not 
        },
        "employeeFirstName": {
            "type": "string",
            "required": true
        },
        "employeeLastName": {
            "type": "string",
            "required": true
        },
        "employeeDOB": {
            "type": "string",
            "required": true
        }
    }
}

Schema file used in my RAML

#%RAML 0.8
title: ManageEmployees
version: 1.0

baseUri: http://api.acme.com/

mediaType: application/json


/newEmployee:
  post:
    description: Create new employees

    body:
          schema: !include com/ww/schema/employeeDetailsSchema.json

  put:
    description: Update employees details
    body:
          schema: !include com/ww/schema/employeeDetailsSchema.json

    responses:
          200: 
            body: 
              example: !include com/ww/schema/employeeExample.json
Sudha Ch
  • 127
  • 1
  • 7
  • It has been quite a while since I was looking for this but I believe the "required" may have been what I was looking for. – SteveS Dec 11 '15 at 13:20
0

As far as I can see, any body will be valid for that schema. All the fields are string, not required, not any specific format. Try putting some of the fields as required and see what happens

Cheers!

nohorbee
  • 139
  • 1
  • 5
  • I think you are right, in retrospect I think everything would have validated against that schema. As stated in my comment above, I am off of that project and not using RAML anymore so I won't be able to confirm your solution but it looks correct. – SteveS Jun 16 '15 at 13:12