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

my-validator

v0.1.0

Published

My validator module for me.

Downloads

3

Readme

my-validator v0.1.0

my-validator is my simple validator for me.

It provides functions to validate string. It also works with express.

Installation

npm install my-validator

Usage

Just use validator functions:

var validator=require('my-validator');

var funcs=validator.funcs;

// returns null if valid
console.log(funcs.isInteger("123"));	// null

// returns error object if not valid
console.log(funcs.isInteger("foo"));
/* returns {
       name: "ValidationError",
       code: "ERR_FORMAT_INTEGER",
       value: "foo"
   } */


Use with express:

```js
var express=require('express'),
    validator=require('my-validator');

var app=express();

app.use(validator.forExpress);	//register validator middleware

app.get("/",function(req,res){
    //validate query/body/params
    req.validateQuery("foo").length(100);	//limit length to 100

    // throw if validation error: handle it in error-handling middleware
    req.throwValidationErrors();

    // or handle errors manually
    if(req._validationErrors.length>0){
        res.json({
            validationErrors: req._validationErrors
        });
        return;
    }
    res.send("ok");
});

validators

All of these functions return null if valid or error object if not valid.

matches(value:string, pattern:RegExp)

Checks if value matches pattern.

Possible error code is error.code.pattern.unmatch.

isnotEmpty(value:string)

Checks if value !== "".

Possible error code is error.code.empty.

length(value:string[, min:number], max:number)

Checks if the length of value is within the range specified.

Possible error code is error.code.length.min if value.length < min and error.code.length.max if max < value.length.

minLength(value:string, min:number)

Just checks lower bound of the length.

Possible error code is error.code.length.min.

lines(value: string, max:number)

Checks if the number of lines that value contains does not exceed max.

Possible error code is error.code.lines.max.

isASCIIPrintable(value:string)

Checks if all the characters of value is ASCII printable character (0x20 <= code <= 0x7E).

Possible error code is error.code.character.asciiPrintable.

isHiragana(value:string[, allowSpace: boolean])

Checks if all the characters of value is ひらがな for me. If allowSpace is true, it also allows U+3000 IDEOGRAPHIC SPACE. Defaults to false.

Possible error code is error.code.character.hiragana.

isKatakana(value:string[, allowSpace: boolean])

Checks if all the characters of value is カタカナ for me.

Possible error code is error.code.character.katakana.

isNumber(value:string[, allowNegatives: boolean])

Checks if value is a valid expression of number. If allowNegatives is true, it allows negative numbers (defaults to false).

Possible error code is error.code.format.number.

isInteger(value:string[, allowNegatives: boolean])

Checks if value is a valid expression of integer If allowNegatives is true, it allows negative numbers (defaults to false).

Possible error code is error.code.format.integer.

isEmail(value:string)

Checks if value is a valid expression of email address.

Possible error code is error.code.format.email.

Error objects (error = validator.error)

error.ValidationError

The Error object returned by validator functions. It has following properties:

  • name:string the name of error.
  • code:string the error code that represents the cause of error.
  • value:string the value that is validated.

APIs for express

validator.forExpress

Middleware function for express application.

req.validateQuery(name:string)

Returns a validator object for req.query[name]. The validator object is equipped with any validator function, of which first argument (value) is omitted, and chainable.

req.validateBody(name:string)

Returns a validator object for req.body[name].

req.validateParams(name:string)

Returns a validator object for req.params[name].

req._validationErrors

Array of all validation errors detected by validator objects.

req.throwValidationErrors()

Throws if there is any validation error. Otherwise do nothing.

Other APIs

validator.addCustomValidator(name:string[, code:string], func:Function)

Adds new validator represented by func. func should return true if valid or false if invalid.

The custom validator can be used in the same way as build-in validators.

License

MIT