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

paralidate

v1.1.6

Published

Koa params validator

Downloads

144

Readme

Paralidate - Parameters Validator for Koa Router

Paralidate makes a Koa middelware, which validates the request's data include ctx.params and ctx.request.body If the request is valid then Koa continues run the next() middleware. Else, Paralidate stops the request and throw an 400 Http Error.

Paralidate uses Parameter for validating function.

Required

Paralidate will check ctx.params and ctx.request.body, so it assumes you already used koa-router and koa-bodyparser.

Usage

Install

Install

npm install paralidate --save

# or use yarn

yarn add paralidate

Import

import paralidate from 'paralidate';

or

var paralidate = require('paralidate');

Paralidate options:

const validator = paralidate(rule, opts);

Paralidate is a validator factory. It accepts 2 arguments:

  • rule: An object that contains all rules following The Parameter Rules
  • opts: Middleware configurations
    • opts.box: ["params" | "box" | callback function ] the place to get data. Default is "params" box also can be a function, that receives ctx as first argument, so you can choose any piece data as you want.
    • opts.outputType: ["simple" | "json" | "complex"] Default is 'simple'
    • opts.errorCode: HTTP Error Code. Default is 409

Validation rules:

There are two ways to declare a rule:

{
  param_name1: 'int', //compact way
  param_name2: 'date',
  param_name3: [1,2,3],
  param_name4: {type: 'enum', values: [1, 2]} // specific way
}

List below presents the equivalence between two ways:

  • 'int' => {type: 'int', required: true}
  • 'integer' => {type: 'integer', required: true}
  • 'number' => {type: 'number', required: true}
  • 'date' => {type: 'date', required: true}
  • 'dateTime' => {type: 'dateTime', required: true}
  • 'id' => {type: 'id', required: true}
  • 'boolean' => {type: 'boolean', required: true}
  • 'bool' => {type: 'bool', required: true}
  • 'string' => {type: 'string', required: true, allowEmpty: false}
  • 'email' => {type: 'email', required: true, allowEmpty: false, format: EMAIL_RE}
  • 'password' => {type: 'password', required: true, allowEmpty: false, format: PASSWORD_RE, min: 6}
  • 'object' => {type: 'object', required: true}
  • 'array' => {type: 'array', required: true}
  • [1, 2] => {type: 'enum', values: [1, 2]}
  • /\d+/ => {type: 'string', required: true, allowEmpty: false, format: /\d+/}

go here for more details.

Example API

This is a very simple Koa Api with koa-router and koa-bodyparser, which we will change it soon!

import Koa from 'koa';
import bodyParser from 'koa-bodyparser';
import Router 'koa-router';

const app = new Koa();
const router = new Router();


router.get('/', function (ctx, next) {
  // ctx.router available
  ctx.body = 'Hello World!';
});

app
  .use(whiteListOrigin)
  .use(bodyParser({
    enableTypes: ['json'],
    extendTypes: ['application/json'],
    onerror: function (err, ctx) {
      ctx.throw('Body parse error', 422);
    }
  }))
  .use(routers)

app.listen(3000, 'localhost');
console.log(`API Server started at http://localhost:3000`);

Validate for all route

To apply a rule for all route, place it after the router middleware:


const validator = paralidate({
  key: {
    type: 'string',
    max: 32
  }
},{
  box: 'params',
  outputType: 'json'
});

app
  .use(bodyParser({
    enableTypes: ['json'],
    extendTypes: ['application/json'],
    onerror: function (err, ctx) {
      ctx.throw('Body parse error', 422);
    }
  }))
  .use(routers)
  .use(validator)

.user(validator) ensures all routers must be included the param key, which is a string of 32 characters.

Validate for specific route

To apply the validator for specific router, place it before the router middelware

const validator = paralidate({
  id: {
    type: 'int',
    min: 1,
    max: 10
  }
}, {
  box: (ctx) => ctx.params,
  outputType: 'json'
});

router.get('/user/:id', validator,function (ctx, next) {
  // ctx.router available
  ctx.body = 'Hello World!';
});

The validator checks ID params in the request, which is integer and between 1 and 10. You also see the opts.box is used as a callback function.