Node-Restify Validation
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
-
TypeError: server.use is not a function
cause The `restify` server instance has not been properly created before attempting to attach middleware.fixEnsure `const server = restify.createServer();` is called before any `server.use()` statements. -
Error: InvalidArgumentError: Resource 'name' is required but was not found.
cause A request parameter (path, query, body, or header) marked as `isRequired: true` in the validation schema was missing from the incoming request.fixVerify that the client request includes all required fields as defined in the route's `validation` object. Check the exact field name and location (resources, queries, content, headers). -
ReferenceError: restify is not defined
cause The `restify` module was not imported or required at the top of the file.fixAdd `const restify = require('restify');` to your file before using the `restify` variable. -
TypeError: Cannot read properties of undefined (reading 'validationPlugin')
cause The `node-restify-validation` module was not correctly imported or `require`d, leading to `restifyValidation` being `undefined`.fixEnsure `const restifyValidation = require('node-restify-validation');` is correctly placed and executed.
Warnings
- breaking The project is explicitly marked as 'Maintener wanted' in its README. This indicates that the library is unmaintained, will not receive bug fixes, security patches, or new features. Using it in production carries significant risk.
- gotcha This library has a strict dependency on `restify >= 2.6.0`. Using older versions of `restify` will lead to runtime errors due to changes in Restify's route object API that `node-restify-validation` relies upon.
- gotcha The library is primarily designed for CommonJS (`require()`) environments. While Node.js offers some interoperability, using ES Module `import` syntax directly might cause issues without proper configuration or transpilation.
- gotcha By default, validation failures trigger a `restify.errors.InvalidArgumentError`. Applications must implement appropriate error handling middleware in Restify to gracefully catch and respond to these validation errors, rather than letting them crash the server or return generic 500s.
Install
-
npm install node-restify-validation -
yarn add node-restify-validation -
pnpm add node-restify-validation
Imports
- validationPlugin
import { validationPlugin } from 'node-restify-validation';const restifyValidation = require('node-restify-validation'); // then use restifyValidation.validationPlugin - validation route property
server.get({ url: '/test', validation: { ... } }, handler)
Quickstart
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);
});