mulesoft-labs / osprey-method-handler

Middleware for validating requests and responses based on a RAML method object
Other
16 stars 16 forks source link

Allow other types than only objects as root type in RAML 1.0 #25

Closed cmd-johnson closed 7 years ago

cmd-johnson commented 7 years ago

Depends on mulesoft-labs/node-raml-validate#6

Allows for elements other than objets at the root of JSON requests.


Currently, osprey-method-handler only accepts resources that use object types in their body. However, all RAML types should be valid for request and response bodies, including array, string, integer, ... (Granted, using strings or integers as root elements in request or response-bodies isn't a nice thing to do, but arrays should be perfectly valid and if you must, even a single number is valid JSON.)

Example: Using the following specification in Osprey yields an error in osprey-method-handler.

#%RAML 1.0
title: Test API

/test:
  post:
    body:
      application/json:
        type: array
        items: string
SyntaxError: Unable to compile JSON schema for post /test: Unexpected token a in JSON at position 0
    at JSON.parse (<anonymous>)
    at jsonBodyValidationHandler (/[...]/osprey-method-handler/osprey-method-handler.js:429:21)
    at jsonBodyHandler (/[...]/osprey-method-handler/osprey-method-handler.js:350:21)
    at /[...]/osprey-method-handler/osprey-method-handler.js:295:29
    at Array.forEach (native)
    at /[...]/osprey-method-handler/osprey-method-handler.js:291:14
    at Array.forEach (native)
    at bodyHandler (/[...]/osprey-method-handler/osprey-method-handler.js:279:9)
    at ospreyMethodHandler (/[...]/osprey-method-handler/osprey-method-handler.js:105:21)
    at /[...]/osprey/lib/server.js:39:12

(This PR also includes #24)

coveralls commented 7 years ago

Coverage Status

Changes Unknown when pulling 6f2794779416448ed02bb63c94cc5ce2db506176 on cmd-johnson:master into on mulesoft-labs:master.

jstoiko commented 7 years ago

merged this PR into the raml1.0-fixes branch.

added support for root-level inline types and type:object with empty properties.