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

restify-validation-engine

v1.0.3

Published

Customisable validator middleware

Downloads

14

Readme

restify-validation-engine build badge


NPM

Validation middleware for restify powered by validator.

Get it

This module is available on npm under the name of restify-validation-engine.

npm install restify-validation-engine

Load it

Then you can load it as a simple middleware.

  • You can add your own validators in the customValidators key of the options.
  • You can specify if you want a list of error or just one with multipleErrors.
  • You can change the way the error looks with the formatter.
var restify = require('restify'),
    restifyValidator = require('restify-validation-engine');


var server = restify.createServer({
    name: 'myServer'
});

server.use(restifyValidator({
    customValidators: {
        myValidator: function (value) {
            // Here put your custom validation and return true for valid or false
        }
    },
    multipleErrors: false // True if you want a list as result
    formatter: function (errors) {
        // errors can be an array or an object depending on `multipleErrors`
        if (!Array.isArray(errors)) {
            return errors;
        }
        return {
            status: 'error',
            errors: errors
        };
    }
}));

Use it

Now you can use the module in the definitions of your routes e.g.

server.get({
    url: '/my-url',
    validate: { // Entry point of the module
        params: { // Which scope
            myParam: { // The field to validate
                required: true // A validator
            }
        }
    }
});

You can also add a formatter to the validate object for a route specific format. This will override the default formatter

server.get({
    url: '/my-url',
    validate: { // Entry point of the module
        params: { // Which scope
            ...
        },
        formatter: function (errors) {
            return {
                name: 'my custom format',
                errors: errors
            }
        }
    }
});

In the validate key of the route you can define your fields in the params or body key. Each field can have a set of validators. The validators available are the required validator, every validator from the (validator)[https://github.com/chriso/validator.js] module and your custom ones. A validator can be set to:

  • true/false
  • a custom error message
  • an Error object
  • an object with:
    • msg a custom error message
    • params when the validator need some
server.post({
    url: '/platypuses',
    validate: { // Entry point of the module
        params: { // Which scope
            filter: {
                isIn: {
                    // A custom error message
                    msg: 'The filter param must be one of these values: small, normal, big',
                    // A list of params to give to isIn
                    params: [['small', 'normal', 'big']]
                }
            }
        },
        body: {
            email: { // The field to validate
                required: true, // A validator
                isEmail: "The mail is required" // This string will be used as a custom error message
            },
            password: {
                required: new restify.BadRequestError('Password is required')
            }
        }
    }
});

In this example, the filter param doesn't have a required validator but have a isIn validator. In this case, if the field isn't given, the module will not check the other validators.

isIn is defined as :

isIn(str, values) - check if the string is in a array of allowed values.

The str param is provided by restify-validation-engine, but it will need the values param. You can give this param by putting it in the params option.

Another example could be:

isLength(str, min [, max]) - check if the string's length falls in a range. Note: this function takes into account surrogate pairs.

In this case, if you want to validate your field being between 4 and 24 characters long you will set the params options to [4, 24].

Love it

Now that your validation engine is in place, you will get a response like this when a field fails the validation:

{
        "message": "The filter param must be one of these values: small, normal, big",
        "field": "filter",
        "scope": "params",
        "given": ""
}

Now in your middleware, you can be sure that your fields are valid.

Make it better

Please give me some feedback about it. And if you have some idea, make a pull request.