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

iform

v0.0.2

Published

Form data validation middleware

Downloads

2

Readme

node-iform

node-iform is a connect middleware help you validate and convert form data.

NOTE You need to view node-validator for more information, if you want to use this node-iform.

NOTE If you find a bug, or want some feature, send a pull request.

Example

    var iform = require('iform');

    var userForm = iform({
            username: {
                required : true,
                len : [4, 15]
            },
            password: {
                required : true,
                len : [6, 20]
            },

            email : {
                type : 'email'
            },

            birth : {
                type : Date,
                isAfter: new Date('01/01/1900'),
                isBefore : null // means now
            },

            avatar : {
                defaultValue : function(req) {
                    return '/avatar/' + req.body.username + '.png';
                }
            },

            age : 'int',

            blog : 'url'
    });

    app.post('/signup', userForm(), function(req, res, next) {
            if(req.iform.errors) {
                return res.json(req.iform.errors);
            }
            db.users.insert(req.iform.data, function(err, data) {
                res.json({success : true, message: 'Sign up successfully'});
            });
    });

    app.post('/profile', userForm('birth', 'age', 'blog'), function(req, res, next){
            if(req.iform.errors) {
                return res.json(req.iform.errors);
            }
            db.users.update({username : req.session.user.username}, req.iform.data, function(err, data) {
                res.json({success : true, message: 'Update profile successfully'});
            });
    });

define rules

At first you need define some rules for validation

As you see in the example, define a form like this : var form = iform(rules);

rules is like {fieldName : fieldRules, ...}

fieldRules is like {ruleName : ruleParameter, ...}

        // field name | rule name | rule parameters
           username  :{ len       : [4, 15] }

The rule names can find at node-validator project page.

All the methods of Validator and Filter of node-validator can be use as a rule name. The rule parameters is the arguments for that method.

The len is defined by node-validator like this

    Validator.prototype.len = function(min, max) { ... }

It takes two parameters. so we use a array as the parameters.

The type is a special rule ,e.g.

    email : {
        type : 'email'
    }

it is equals to

    email : {
        'isEmail' : []
    }

you can also use int, date etc, cause the Validator defined isInt and isDate

all the method of Valiator starts with is and take no arguments can be use as a type.

if you only have a type rule you can use fieldName : type define it.

You can also use Date Number instead of 'date', 'number'

use the middleware

userForm you just defined is a function which returns a middleware, use like this

    app.post('/signup', userForm(), function(req, res, next) {
            if(req.iform.errors) {
                return res.json(req.iform.errors);
            }
            db.users.insert(req.iform.data, function(err, data) {
                res.json({success : true, message: 'Sign up successfully'});
            });
    });

the middleware will check the req.body by your rules, all the validation errors go to req.iform.errors, and the filtered and converted data go to req.iform.data.

Since the data has been cleaned, you can use it immediately.

If there is another page also use the smae rules but only part of fields, you can reuse it like this.

    app.post('/profile', userForm('birth', 'age', 'blog'), function(req, res, next){
            if(req.iform.errors) {
                return res.json(req.iform.errors);
            }
            db.users.update({username : req.session.user.username}, req.iform.data, function(err, data) {
                res.json({success : true, message: 'Update profile successfully'});
            });
    });