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

@th3hero/request-validator

v1.0.3

Published

A package using which we can validate request sent to the server with multiple scenarios & validations in a form of object, with proper error messages & all.

Downloads

7

Readme

@th3hero/request-validator

The @th3hero/request-validator is a Node.js module for validating incoming HTTP requests, ensuring that the provided data meets specified rules and requirements.

Author

Installation

You can install the @th3hero/request-validator package via npm. Open your terminal and run the following command:

  npm install @th3hero/request-validator

Usage

Importing the Validator

To use the validator in your Node.js application, import it as follows:

  const { validateInput } = require('@th3hero/request-validator');

Defining Validation Rules

Define validation rules as an object where the keys represent the fields to validate, and the values represent the validation rules. Here's an example:

const rules = {
    username: 'required|min:3|max:15',
    email: 'required|email|unique:users,email',
    password: 'required|min:8',
    profile_picture: 'file|mimetype:image/jpeg,image/png'
};

Performing Validation

You can perform validation on incoming requests using the validateInput function. Here's how you can use it:

app.post('/submit', async (req, res) => {
    const result = await validateInput(req, rules);
    if (result.failed) {
        return res.status(400).json({ errors: result.errors });
    }

    // Proceed with processing the request...
});

Documentation

Documentation

Rule Syntax

The validation rules are defined using a simple syntax:

  • required: Field must be present and not empty.
username: 'required'
  • min:length: Field must have a minimum length of length.
username: 'min:3'
  • max:length: Field must have a maximum length of length.
username: 'max:3'
  • email: Field must be a valid email address.
email: 'email'
  • file: Field must be a file upload.
profile_picture: 'file'
  • file|important: Field must be a file upload & file is required.
profile_picture: 'file|important'
  • mimetype:types: File mimetype must be one of the specified types.
profile_picture: 'mimetype:image/jpeg,image/png'

-unique:table,column: Field must be unique in the specified database table and column.

email: 'unique:users,email'

-exists:table,column: Field value must exist in the specified database table and column.

user_id: 'exists:users,id'

-not-empty: Field cannot be empty.

description: 'not-empty'

-nullable: Field can be null or undefined.

middle_name: 'nullable'

-digits:length: Field must be a string with exactly length characters.

postal_code: 'digits:6' //this will accept 6 characters only.

-in:values: Field must be one of the specified values.

gender: 'in:male,female,other'

-date:format: Field must be a valid date with the specified format.

birth_date: 'date:YYYY-MM-DD'

-string: Field must be a string.

note: 'string'

-integer: Field must be a integer.

age: 'integer'

-boolean: Field must be a boolean.

active: 'boolean'

Example

Here's a basic example of how you can use the validator in an Express.js application:

const express = require('express');
const { validateInput } = require('@th3hero/request-validator');

const app = express();

const rules = {
    username: 'required|min:3|max:15',
    email: 'required|email|unique:users,email',
    password: 'required|min:8',
    gender: 'required|string|in:male,female,others',
    married: 'required_if:gender,male|in:0,1',
    profile_picture: 'file|important|mimetype:image/jpeg,image/png'
};

app.post('/submit', async (req, res) => {
    const result = await validateInput(req, rules);
    if (result.failed) {
        return res.status(400).json({ errors: result.errors });
    }

    // Proceed with processing the request...
});

app.listen(3000, () => {
    console.log('Server is running on port 3000');
});

Contributing

Contributions are welcome! If you find any issues or have suggestions for improvements, please feel free to submit issues, pull requests, or suggestions.

License

This project is licensed under the MIT License.