npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ratify

v3.0.0

Published

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

Downloads

77

Readme

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.pack.register({ plugin: 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)

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, no validation against JSON schema is performed, but swagger documentation will still be provided.

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