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

layer-one-validator

v0.4.0

Published

Expressjs middleware to validate data from client side requests

Downloads

23

Readme

layer-one-validator

Some expressjs middleware callback functions to validate data from requests, checks the amount of properties, their types and the business logic.

Installation and Usage

Server-side usage

Install the library with:

npm install layer-one-validator

The validator should be called using the bind() method. The first and only argument could be an object or an array.

NOTE:: If the value being tested is an array, the type:function must be examined for each element within that array. In the case of the biz:function, testing can be performed on the entire array or on each individual element.

Syntax

layerOneValidator.body.bind(object)

layerOneValidator.body.bind(objectsArray)

Object

{ prop:string [, type:function][, biz:function] }

{ prop:string [, type:function][, biz:function] }[]

  • prop <string> - The name of the property named on the request.

  • type <function> - Optional, a function to test the type of the property. Should return a boolean.

  • biz <function> - Optional, a function to test the business part of the property. Should return a boolean.

Body, Query or Params

The layer-one-validator is an object with 3 properties, 'body','params' and 'query', representing each express request.

Responses

When fails, the response will be an json object, with some properties:

  • { success: false } - Will throw an error on the server with additional information.

  • { /* ... */, message } - Indicates the stage at which the failures occurred.

  • { /* ... */, fail } - Displays the name of the failing property.

  • { /* ... */, layer } - Displays 'body','params' or 'query'.

  • { /* ... */, source } - Displays the source as layer-one-validator.

If successful, will go to the next middleware.

HTTP response status codes

If the bound object or objectsArray is incorrect, or if the type property function checks the entire array instead of individual items:

  • 500 Internal Server Error

When there are issues with prop, type or inconsistencies in property quantities:

  • 400 Bad Request

When fails on biz:

  • 422 Unprocessable Content

Route only

// route
const layerOneValidator = require('layer-one-validator');

router.post('/user',

    layerOneValidator.body.bind([
        { prop: 'weight', type: v => Number.isInteger(v), biz: v => v > 0 },
        { prop: 'username', biz: v => /^[a-z]{4,8}$/.test(v) }
    ]),

    (request, response, next) => {
        /* ... */
    }
);

module.exports = router;

Route & Helpers & Controller

// route
const userController = require(/* ... */);

router.post('/user',
    userController.user.validation.layerOne,
    userController.user.execute
);
// helpers - fns.js
module.exports = {
    isNumber: value => typeof value === 'number',
    isString: value => typeof value === 'string'
};

// helpers - biz.js
module.exports = {
    isWeight: value => Number.isInteger(value) && value > 0,
    isUsername: value => /^[a-z]{4,8}$/.test(value)
};
// controller
const layerOneValidator = require('layer-one-validator');
const fns = require('./path_to_helpers/fns');
const biz = require('./path_to_helpers/biz');

module.exports = {

    // * POST
    user: {

        validation: {
            layerOne: layerOneValidator.body.bind([
                { prop: 'weight', type: fns.isNumber, biz: biz.isWeight },
                { prop: 'username', type: fns.isString, biz: biz.isUsername }
            ])
        },

        execute(request, response, next) {
            /* ... */
        }
    }
};

Tests

Tests are using mocha, to run the tests use:

$ npm test

Conducts tests without displaying layer-one-validator helper errors in the console.

$ npm run test-no-print-error

License

Licensed under the MIT