All Projects → mulesoft-labs → osprey-method-handler

mulesoft-labs / osprey-method-handler

Licence: other
Middleware for validating requests and responses based on a RAML method object

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to osprey-method-handler

raml2obj
RAML to object.
Stars: ✭ 23 (+64.29%)
Mutual labels:  raml, raml-tooling, raml-utilities
api-console-cli
A CLI tools for the API console.
Stars: ✭ 14 (+0%)
Mutual labels:  raml, raml-tooling, raml-utilities
commercetools-api-reference
commercetools API reference documentation
Stars: ✭ 41 (+192.86%)
Mutual labels:  raml, raml-utilities
raml-java-client-generator
Raml Java Client Generator
Stars: ✭ 32 (+128.57%)
Mutual labels:  raml, raml-tooling
raml-dotnet-parser-2
No description or website provided.
Stars: ✭ 17 (+21.43%)
Mutual labels:  raml, raml-tooling
raml-javascript-generator
Generate a JavaScript API client from RAML
Stars: ✭ 30 (+114.29%)
Mutual labels:  raml, raml-tooling
raml-sublime-plugin
Syntax highlighter for the RESTful API Modeling Language
Stars: ✭ 49 (+250%)
Mutual labels:  raml, raml-tooling
node-raml-validate
Strict validation of RAML parameters in JavaScript
Stars: ✭ 18 (+28.57%)
Mutual labels:  raml, raml-tooling
raml-typesystem
(deprecated) Typescript implementation of RAML type system
Stars: ✭ 15 (+7.14%)
Mutual labels:  raml, raml-tooling
oas-raml-converter
(DEPRECATED) Converts between OAS and RAML API specifications
Stars: ✭ 75 (+435.71%)
Mutual labels:  raml, raml-tooling
Hikaku
A library that tests if the implementation of a REST-API meets its specification.
Stars: ✭ 154 (+1000%)
Mutual labels:  raml
Raml Examples
This repository contains valid RAML 1.0 examples. These examples are not only part of the spec, but also represent RAML features in different scenarios.
Stars: ✭ 154 (+1000%)
Mutual labels:  raml
smockin
Dynamic API, S3 & Mail mocking for web, mobile & microservice development.
Stars: ✭ 74 (+428.57%)
Mutual labels:  raml
generaptr
Generaptr is a node package that helps when starting up a project by generating boilerplate code for Express api.
Stars: ✭ 16 (+14.29%)
Mutual labels:  raml
Raml Js Parser 2
(deprecated)
Stars: ✭ 140 (+900%)
Mutual labels:  raml
symfony-skeleton
Skeleton rest-api based on symfony
Stars: ✭ 15 (+7.14%)
Mutual labels:  raml
Raml Client Generator
Template-driven generator of clients for APIs described by a RAML spec
Stars: ✭ 119 (+750%)
Mutual labels:  raml
Osprey Mock Service
Generate an API mock service from a RAML definition using Osprey
Stars: ✭ 106 (+657.14%)
Mutual labels:  raml
Ramlfications
Python parser for RAML
Stars: ✭ 234 (+1571.43%)
Mutual labels:  raml
Raml ruby
Raml Ruby
Stars: ✭ 95 (+578.57%)
Mutual labels:  raml

Osprey Method Handler

NPM version NPM Downloads Build status Test coverage Greenkeeper badge

Middleware for validating requests and responses based on a RAML method object.

Installation

npm install osprey-method-handler --save

Features

  • Supports RAML 0.8 and RAML 1.0
  • Header validation (ignores undocumented headers)
  • Query validation (ignores undocumented parameters)
  • Request body validation
    • JSON schemas
    • XML schemas
    • URL-encoded formParameters (ignores undocumented parameters)
    • Multipart form data formParameters (ignores undocumented parameters)
    • Discards unknown bodies
  • Accept content type negotiation (based on defined success response bodies)
  • Automatically parsed request bodies
    • JSON (req.body)
    • URL-encoded (req.body)
    • XML (req.xml)
    • Form Data (req.form using Busboy, but you need to pipe the request into it - req.pipe(req.form))

Please note: Due to the build time of libxmljs, it does not come bundled. If you need XML validation, please install libxmljs as a dependency of your own project.

Usage

const express = require('express')
const handler = require('osprey-method-handler')
const utils = require('./utils')

const app = express()

// webapi-parser.Operation
const methodObj = utils.getMethodObj()
const options = {}

app.post(
  '/users',
  handler(methodObj, '/users', 'POST', options),
  function (req, res) {
    res.send('success')
  }
)

Accepts webapi-parser Operation object as first argument, path string as second argument, method name as third and options object as final argument.

Options

  • ajv Custom Ajv instance to be used to validate query strings, request headers and request bodied (url-encoded, form-data, json)
  • discardUnknownBodies Discard undefined request streams (default: true)
  • discardUnknownQueryParameters Discard undefined query parameters (default: true)
  • discardUnknownHeaders Discard undefined header parameters (always includes known headers) (default: true)
  • parseBodiesOnWildcard Toggle parsing bodies on wildcard body support (default: false)
  • reviver The reviver passed to JSON.parse for JSON endpoints
  • limit The maximum bytes for XML, JSON and URL-encoded endpoints (default: '100kb')
  • parameterLimit The maximum number of URL-encoded parameters (default: 1000)
  • busboyLimits The multipart limits defined by Busboy

Adding JSON schemas

If you are using external JSON schemas with $ref, you can add them to the module before you compile the middleware. Use handler.addJsonSchema(schema, key) to compile automatically when used.

handler.addJsonSchema() accepts a third (optional) options argument. Supported options are:

  • ajv Custom Ajv instance. E.g. handler.addJsonSchema(schema, key, {ajv: myAjvInstance}). The provided ajv instance can later be passed as an option to the handler to perform validation.

Validation Errors

The library intercepts incoming requests and does validation. It will respond with 400, 406 or 415 error instances from http-errors. Validation errors are attached to 400 instances and noted using ramlValidation = true and requestErrors = [] (an array of errors that were found, compatible with request-error-handler).

See the code for a complete list of errors formats.

Please note: XML validation does not have a way to get the keyword, dataPath, data or schema. Instead, it has a meta object that contains information from libxmljs (domain, code, level, column, line).

To render the error messages for your application, look into error handling for Express, Connect, Router or any other middleware error handler. If you want a pre-built error handler, try using request-error-handler, which provides a pre-defined error formatter.

License

MIT license

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].