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

validator-param

v0.4.1

Published

A library, which can create validators to validate request parameters easily.

Downloads

30

Readme

validator-param

A library, which can create validators to validate request parameters easily.

NPM

NPM version Build status Test coverage

Install

npm install validator-param --save

How to use

The basic usage

const {expect} = require('chai');
const {Validator} = require('validator-param');

const schema = {
    requiredParam : {
        required: [true, new Error('The requiredParam can not be empty')]//when check failed return a Error object
    }
};
const validator = new Validator(schema);
const error = validator.doValidate({});
expect(error).to.be.an('error');

const schema2 = {
    jsonParam : {
        required:true,//when check failed return a inner error string
        type:[JSON,{code:1,msg:'The jsonParam must be a JSON object'}]//when check failed return a custom object
    }
};
const validator2 = new Validator(schema2);
const error2 = validator2.doValidate({jsonParam:'xx'});
expect(error2).to.be.an('object').and.have.property('code').and.equal(1);

code 2.1.1 The basic usage

Use as express middleware

This is a directory tree of our web project:

│─app.js
│
├─bin
│      www
│
├─public
│  ├─images
│  ├─javascripts
│  └─stylesheets
│          style.css
│
├─routes
│      index.js
│
├─validators
│      data_list_schema.js
│
└─views
        error.ejs
        index.ejs

directory tree

And then it is the code of app.js , where we introduce the middleware supplied by request-param:

const express = require('express');
const path = require('path');
const logger = require('morgan');
const bodyParser = require('body-parser');
const {filter:requestValidator} = require('validator-param');
const index = require('./routes/index');

const app = express();

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));
app.use(requestValidator({
  basePath:path.join(__dirname,'./validators'),
  urlPrefix:'/i/',
  filenameReplaces:{'_':'/'},
  filenameSuffix:'_schema.js'
}));

code 2.2.1 The code of app.js

As showed above, the files in directory of validators with a suffix of _schema.js will be parsed. And we have the file data_list_schema.js in it, so the url of /i/data/list will be validate by the definition of the file data_list_schema.js. For the parameter urlPrefix is /i/, the paramter filenameReplaces is {'_':'/'}, the filename will be remove the filenameSuffix and replace filenameReplaces's key with its value, and unshfited with urlPrefix.

module.exports = Object.freeze({
    begin_time : {
        required:[true,{code:1,msg:'The begin_time is empty'}],
        type:[Date,{code:2,msg:'the begin_time should be a Date'}]
    }
});

code 2.2.2 The code of data_list_schema.js

As showed in code 2.2.2, when request /i/data/list without parameter begin_time or in wrong type(not a Date), the middleware will return a error object.

API

See the api document.

Attention

Not use querysting in POST method, it will case the req.query's data type transform fail.

License

MIT