Giter Site home page Giter Site logo

terrorizer1980 / ratify Goto Github PK

View Code? Open in Web Editor NEW

This project forked from auth0/ratify

0.0 1.0 0.0 142 KB

A Hapi plugin for validating the schema of path, query, request body, and response body params using JSON-schema

License: MIT License

Makefile 0.30% JavaScript 99.70%

ratify's Introduction

ratify

A Hapi plugin for validating the schema of path, query, request body, and response body params using JSON-schema, while providing documenation for your end points via Swagger

Build Status Coverage Status Code Climate NPM version Dependency Status

NPM

Contributing

This module makes use of a Makefile for building/testing purposes. After obtaining a copy of the repo, run the following commands to make sure everything is in working condition before you start your work:

make install
make test

Before committing a change to your fork/branch, run the following commands to make sure nothing is broken:

make test
make test-cov

Don't forget to bump the version in the package.json using the semver spec as a guide for which part to bump. Submit a pull request when your work is complete.

Notes:

  • Please do your best to ensure the code coverage does not drop. If new unit tests are required to maintain the same level of coverage, please include those in your pull request.
  • Please follow the same coding/formatting practices that have been established in the module.

Installation

npm install ratify

Usage

To install this plugin on your Hapi server, do something similar to this:

var Hapi = require('hapi');
var server = new Hapi.Server();

var ratifyOptions = {};

server.register({ register: require('ratify'), options: ratifyOptions }, function(err) {
	if (err) {
		console.log('error', 'Failed loading plugin: ratify');
	}
});

Plugin Options

auth

Used to add authentication to the swagger routes that get created by the plugin. Valid values are described here under the auth property.

Defaults to false

baseUrl

The protocol, hostname, and port where the application is running.

Defaults to 'http://localhost'

startingPath

The path at which all of the swagger routes begin at. This is the endpoint you would pass to an instance of the swagger UI.

Defaults to '/api-docs'

apiVersion

The version of your API.

Defaults to ''

responseContentTypes

A collection of valid response types returned by your services.

Defaults to ['application/json']

swaggerHooks

An object in which the property names represent swagger generated elements and the values must be functions to be invoked to customize how those elements are processed.

Possible values:

  • params: function(params, route, type)
  • operation: function(operation, route, resourceType, path)
  • routeNameGroup: function(route)

disableSwagger

If true, the swagger endpoint will not be exposed.

errorReporters

An object in which the property keys represent elements that can be validated ("headers", "query", "path", "payload", "response") and the values are initialized ZSchemaErrors instances to be used to report those errors.

Parameter Validation

Once your server is set to use ratify, you can specify route-specific validations in each route config like so:

var route = {
	method: 'GET',
	path: '/foo/{bar}',
	config: {
		handler: function(request, reply) {

		},
		plugins: {
			ratify: {
				path: {
					// path parameters schema
				},
				query: {
					// query parameters schema
				},
				headers: {
					// header parameters schema
				},
				payload: {
					// request payload schema
				},
				response: {
					schema: {
						// response payload schema
					},
					sample: 100, // percentage of responses to test against the schema
					failAction: 'log' // action to take when schena validation fails. Valid options are; 'log' and 'error'
				}

			}
		}
	}
};
server.route(route);

All schemas should follow the JSON schema specification.

Notes: In addition to the JSON schema defined types, ratify allows you to specify "file" as a payload type. If this is specified, ratify validates if the file object is a readable stream.

Type Conversion

In the process of validating the properties based on the schema, ratify will attempt to convert path, header, and query params to the type defined in the schema. For example, if you have a query paramter called limit and it's type is number, since all query parameters are parsed as strings by Hapi, ratify will convert the string to a number.

Ratify can also specifically convert query parameters that are intended to be arrays. For example, both of the following query strings will result in a property called types having an array value:

  • ?types=first&types=second&types=third
  • ?types[0]=first&types[2]=third&types[1]=second

Result:

{
	types: ['first', 'second', 'third']
}

Swagger Documentation

Ratify automatically generates routes that produce JSON in the format of the Swagger API Specification. In order to ge tthe most of the documentation, it's best to ensure there are descriptions to all your parameters as allowed by the JSON schema spec.

Version Compatibility

Currently compatible with: Hapi 10.x.x (Node v4)

  • 0.2.x - Hapi 1.x.x
  • 0.3.x - Don't use!
  • 0.4.x - Hapi 4.x.x
  • 0.6.x - Hapi 6.x.x
  • 0.7.x - Hapi 7.x.x
  • 0.8.x - Hapi 8.x.x
  • 0.10.x - Hapi 9.x.x
  • 1.x.x - Hapi 10.x.x (Node v4)
  • 2.x.x - Hapi 11.x.x
  • 3.x.x - Hapi 16.x.x
  • 4.x.x - Hapi 17.x.x

ratify's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.