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

koa-validator

v0.6.4

Published

a koa port of express-validator

Downloads

51

Readme

koa-validator

Build Status NPM version Dependency Status

NPM

a koa port of express-validator

Install

npm install koa-validator

Usage

var koa = require('koa')
    , validator = require('koa-validator')
    , bodyParser = require('koa-bodyparser')
    , app = koa()
    ;

app
    .use(bodyParser())
    .use(validator({
        onValidationError: function(errMsg){
            console.log('Validation error:', errMsg);
        }
    }))
    .use(functon *(next){
        this.checkParams('testparam', 'Invalid number').isInt();
        yield next;
    })
    .listen(3000)
    ;

Options

  • onValidationError - function(errMsg), default to null. The errMsg is the errMsg you defined when you check one variable

    You can define the function like this

      function(errMsg){
          throw new Error('Validation error: ' + errMsg);
      }
  • validationErrorFormatter - function(paramName, errMsg, value), the default function is below

      function(paramName, errMsg, value){
          return {
              param: paramName
              , msg: errMsg
              , value: value
          };
      }

Note

If you will use checkBody or assertBody or sanitizeBody, you should use one bodyparse middleware before validator.

Test

npm test

API

  • checkParams
  • checkQuery
  • checkBody
  • checkHeader
  • sanitizeParams
  • sanitizeQuery
  • sanitizeBody
  • assertParams => checkParams
  • assertQuery => checkQuery
  • assertBody => checkBody
  • assertHeader => checkHeader
  • haveValidationError, return true if have any validationError
  • validationErrors, if have any validationError, return an array of error object you returned in validationErrorFormatter

Check

You can use all check methods in validator.js checks.

In addition, you can use below methods extended to validator.js.

  • notEmpty
  • empty
  • eq, use ==
  • eqeq, use ===
  • neq, use !=
  • neqeq, use !==
  • gt
  • lt
  • ge
  • le
  • notContains
  • isTime
  • len, an alias to validator.js' isLength
  • in, an alias to validator.js' isIn
  • byteLength, a function support set charset.
  • isUrl, an alias to validator.js' isURL
  • isIp, an alias to validator.js' isIP
  • isLowerCase, an alias to validator.js' isLowercase
  • isUpperCase, an alias to validator.js' isUppercase

Sanitize

You can use all sanitize methods in validator.js.

In addition, you can use below methods extended to validator.js.

  • default, set default value
  • toLowerCase
  • toUpperCase
  • encodeURI
  • encodeURIComponent
  • decodeURI
  • decodeURIComponent
  • replace
  • toLow, an alias to toLowerCase
  • toUp, an alias to toUpperCase

Extend

If you want to extend validator.js, you can use extendCheck and extendSanitize like below examples.

When you extend validator.js, your check function name best match is*, and your sanitize function name best match to*. This is not force, but recommended. If you extend one function that validator.js have already had, the function will be ignored, and an error will be thrown.

var koa = require('koa')
    , bodyParser = require('koa-bodyparser')
    , validator = require('koa-validator')
    ;

validator.extendCheck('isFinite', function(str){
    return isFinite(str);
});
validator.extendCheck({
    isFinite: function(str){
        return isFinite(str);
    }
    , isFinite2: function(str){
        return isFinite(str);
    }
});

validator.extendSanitize('toZero', function(str){
    return 0;
});
validator.extendSanitize({
    toOne: function(str){
        return 1;
    }
    , toTwo: function(str){
        return 2;
    }
});

koa()
.use(bodyParser())
.use(validator())
.use(function *(next){
    this.checkParams('test', 'Invalid test value').isFinite();
    yield next;
})
.listen(3000)
;

Thanks

Thanks to koa-validate, some extended methods and test suites are based on koa-validate.

LICENSE

MIT