Node-Restify Validation

1.3.0 · abandoned · verified Sun Apr 19

node-restify-validation is a Node.js library designed to integrate request validation directly into REST services built using the `node-restify` framework. It allows developers to define validation schemas for path parameters (resources), query parameters, request body content, and HTTP headers directly within the `restify` route definition object. The current stable version is 1.3.0, released after 1.1.0, but the project explicitly states "Maintener wanted" in its README, indicating an abandoned or maintenance-only status with no active development. Its primary differentiator is its close integration with `restify`'s route definition, aiming to keep validation logic co-located with the route itself. It supports basic validation rules like `isRequired`, `isIn`, `isEmail`, `isNatural`, `isDictionary`, and `isObject` with nested properties and array validations.

Common errors

Warnings

Install

Imports

Quickstart

Initializes a Restify server with the validation plugin and defines a GET route with path, query, and header parameter validation rules, demonstrating how to attach validation schemas directly to routes.

const restify = require('restify');
const restifyValidation = require('node-restify-validation');

const server = restify.createServer();
server.use(restify.queryParser());
server.use(restify.bodyParser()); // Required for 'content' validation
server.use(restifyValidation.validationPlugin({
    errorsAsArray: false,
    forbidUndefinedVariables: false,
    errorHandler: restify.errors.InvalidArgumentError
}));

server.get({
    url: '/test/:name',
    validation: {
        resources: { // Path parameters
            name: { isRequired: true, isIn: ['foo', 'bar'] }
        },
        queries: { // Query parameters
            status: { isRequired: true, isIn: ['foo', 'bar'] },
            email: { isRequired: false, isEmail: true },
            age: { isRequired: true, isNatural: true }
        },
        headers: { // HTTP Headers
            requestid: { isRequired: true }
        }
    }
}, function (req, res, next) {
    res.send(200, { message: `Hello ${req.params.name}, your status is ${req.query.status}.` });
    return next();
});

server.listen(8001, function () {
    console.log('%s listening at %s', server.name, server.url);
});

view raw JSON →