
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
A Hapi plugin for validating the schema of path, query, request body, and response body params using JSON-schema
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
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:
npm install ratify
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');
}
});
authUsed 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
baseUrlThe protocol, hostname, and port where the application is running.
Defaults to 'http://localhost'
startingPathThe 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'
apiVersionThe version of your API.
Defaults to ''
responseContentTypesA collection of valid response types returned by your services.
Defaults to ['application/json']
swaggerHooksAn 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)errorReportersAn 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.
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.
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]=secondResult:
{
types: ['first', 'second', 'third']
}
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.
FAQs
A Hapi plugin for validating the schema of path, query, request body, and response body params using JSON-schema
We found that ratify demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.