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

@kodinggen/express-validator

v1.0.3

Published

validation package for express application

Downloads

3

Readme

Express Validator

a simple validator package for validating your form requests.

INSTALLATION

npm install @kodinggen/express-validator --save

USAGE

A Basic Setup

const express = require('express');
const { validation } = require('express-validator');

express.use(validation());

Validating request

When initial setup was done, the validator instance also available on req object. You can build your validation rule using req.validator object. for instance :

app.post('/validation', function(req, res) {

    const { username, email, age, description } = req.body;

    // Build the validation
    const validator = req.validator.build({ username, email, age, description }, {
        username: 'required|string|alpha_numeric',
        email: 'required|string|email',
        age: 'required|integer',
        description: 'optional|string'
    });

    // Validate fields with rules.
    validator.validate().then(function(result) {
         // Check if validation has error for each of rules.
        if (result.status === 'error') {

            // return status 425 and return error all error messages for each rules and fields.
            res.status(425).json(result.data);
        }

        res.end('validation success');
    });
});

We build the validation using req.validator.build() this function will return the Validator instance. first parameter will be your field that need to be validated and second parameter validation rules seperated with |, each given field will check all given rules so the error messages for each field will return array.

validateSync function was method since version 1.0.2, use validate() instead

The validator.validateSync will validate all given fields and fill the error message if validation was fail for given rules.

If you working with session library like express-session is highly recommended using the validate() method, it will return promise to make sure all validation errors is saved to session object. **

Example validation with session

const { validation }  = require('./index');
const app = express();

app.set('view engine', 'ejs');

app.use(session({
    secret: 'flying cat',
    resave: false,
    saveUninitialized: true,
    cookie: { maxAge: 60 * 60 * 60 * 24, httpOnly: false }
}));

app.get('/', function(req, res) {
    return res.render('home');
});

app.post('/validation-session', function(req, res) {
    const { username, email, age, description } = req.body;

    // Build the validator
    const validator = req.validator.build({ username, email, age, description }, {
        username: 'required|string|alpha_numeric',
        email: 'required|string|email',
        age: 'required|integer',
        description: 'optional|string'
    });

    // Validate with rules
    validator.validate().then(function(result) {
        if (result.status === 'error') {
            // If there are validations error redirect to `home` route
            return res.redirect('/home');
        }

        return res.end('validation success');
    }).catch(function(error) {
        return res.end(error);
    });

});

app.listen(3000);

Since version 1.0.2, you will assign the validationErrors object by your self because this package no longer using express-session library for flexibility.

This can be done with:

validator.validate().then(function(result) {
    if (result.status === 'error') {
        // If there are validations error redirect to `home` route
        res.locals.validationErrors = result.data;
        return res.redirect('/home');
    }

    return res.end('validation success');
}).catch(function(error) {
    return res.end(error);
});

Now you can access validationErrors in the view file :

<% if(validationErrors) { %>
    <% if(validationErrors.username) { %>
        <span class="badge badge-danger"><%= validationErrors.username[0] %></span>
    <% } %>
<% } %>

The promise result object will contain two properties status and data, status will contains 'error' or 'success' and data will contains validation error messages if validation fail or return all fields with all value already been trim.

validator.validate().then(function(result) {
    if (result.status === 'error') {
        // If there are validations error redirect to `home` route
        res.status(200).json(result.data) // This will return validation error messages
    }

    if (result.status === 'success) {
        res.status(200).json(result.data) // This will return all fields and its value.
    }

    return res.end('validation success');
}).catch(function(error) {
    return res.end(error);
});

Custom Error Messages

This feature available for version 1.0.2++

Now you can define a custom error message before validation process. For example :

const validator = req.validator.build(req.body, {
    name: 'required|string|min:4',
    email: 'required|string|email',
    content: 'optional'
});

validator.setErrorMessages({
    email: function(fieldName) {
        return `this ${fieldName} field should be a valid email`
    },
    min: function(fieldName, args) {
        return `this ${fieldName} field should have minimal length of ${args[0]}`
    }
});

Available Validation Rules

| Rule Name | Parameters | Descriptions | |-----------|------------|-------------| | requried | none | Field under this rule must be filled in | | max | max_value| Field must be have maximum value at given parameter, example usage with parameter max:10 -> if string passed it will match length of given string | min | min_value| Field must be have minimum value at given parameter, example usage with parameter min:10 -> if string passed it will match length of given string | string | none | Field under this rule must be type of string | | numeric | none | Field under this rule must be numeric value | | alpha | none | Field under this rule must be alphabetic character | | alpha_numeric | none | Field under this rule must be alphabetic and numeric value | | email | none | Field under this rule must be valid email | | integer | none | Field under this rule must be an integer value | | optional | none | This rule allow some field to be empty | | between | min,max | Field under this rule must have value between given parameters, example usage with parameter between:5,20 | | url | none | Field under this rule must be valid url | | enum | a,b,c,...,n | Field under enum rule must match between enum set enum:example1,example2,example3 |

API Reference

| Method Name | Parameters | Return Value | Description | |-------------|------------|--------------|-------------- | | build()| none | validator instance | initiate the validation. | | hasError()| none | boolean | check if validation has any error from each field.| | flashError()| none | void | store and receive validation messages from the session.| | getAllErrors()| none | objects | return all errors for each field.| | getError()| field_name | object | return validation errors for specified field.| | validate()| none | Promise | promise based validation process, it will store validation error messages on session storage | | setErrorMessages()| object | void | set the custom validation error messages |

Testing

npm run test

Http test

npm run test-http