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

request-check

v1.5.4

Published

Validate requests required fields and rules on express and other frameworks

Downloads

830

Readme

Request Check

Validate requests required fields and rules on express and other frameworks

You should not always believe the data is exactly what you think. Hopefully, you validate data you receive. This module helps with that. I found that many of the validators out there are either incomplete or not fully customizable. Hence, I built this. It is rather simple and it works.

License: MIT npm version Build Status Coverage Status Downloads

npm

Install

Add request-check with your favorite package manager:

  yarn add request-check

Simple Usage

import requestCheck from 'request-check'
const rc = requestCheck()

// sample payload
const name = undefined
const age = 15
const email = '[email protected]'

// add rules
rc.addRule('age', {
  validator: (age) => age > 18, 
  message: 'You need to be at least 18 years old!'
})

// checks required fields and validation rules
const errors = rc.check(
  { name },
  { email }, 
  { age }
)

if (errors) console.log(errors)

Above code outputs:

[
  { 
    field: 'age', 
    message: 'You need to be at least 18 years old!' 
  },
  { 
    field: 'name', 
    message: 'The field is required!' 
  }
]

It should be noted that the request-check performs two tasks in the above code:

  • First, it checks whether properties name, email and age were provided (required fields).

  • Secondly, if they were provided, it proceeds to validate the age property according to the specified rule (validation rules).

Usage Example with Express

import express, { Request, Response } from 'express'
const app = express()
const router = express.Router()
app.use(router)
router.post('/create', (req: Request, res: Response) => {
  const { email, name } = req.body
  const rc = requestCheck()
  const errors = rc.check({email}, {name})
  if(errors) {
    return res.status(400).json({
      status: 'BAD_REQUEST',
      code: 400,
      message: 'Request is wrong!',
      success: false,
      errors
    })
  }
  // continue code, everything is ok 
})

Check method explained

check will return an array of objects with field and message properties if there are any errors after checking for required fields (1) and validation rules (2) OR, if there are none of these errors, it will return undefined.

(1) First check: required fields

If a variable is not set, the message will be The field is required!.

  const requestBody = { name: 'Felipe' }
  const errors = rc.check(
    { name: requestBody.name }, 
    { surname: requestBody.surname }
  )
  console.log(errors)

Which outputs

[
  { field: 'surname', message: 'The field is required!' }
]

Change default required message

You can change the default required field message by adding a this line of code:

rc.requiredMessage = 'The field :name was not given =('

The symbol :name is optional. It would be replaced with the field name. In the example, the message would be 'The field surname was not given =('.

(2) Second check: validation functions

If a variable passed the require check and there is a rule for that variable, check will run the validation function. If the variable did not pass the validation, the message will be the one specified in the rule.

Example:

  const requestBody = { name: 'Felipe', age: 15 }
  rc.addRule('age', {
    validator: (age) => age > 18, 
    message: 'You need to be at least 18 years old!'
  })
  const errors = rc.check(
    { name: requestBody.name }, 
    { age: requestBody.age }
  )
  console.log(errors)

Which outputs

[
  { field: 'age', message: 'You need to be at least 18 years old!' }
]

No errors (Hooray!)

If all properties passed to check are both set (1) and pass the validation functions (2) of specified rules then check will return undefined.

  const requestBody = { name: 'Felipe', age: 23 }
  rc.addRule('age', {
    validator: (age) => age > 18, 
    message: 'You need to be at least 18 years old!'
  })
  const errors = rc.check(
    { name: requestBody.name }, 
    { age: requestBody.age }
  )
  console.log(errors)

Which outputs

undefined

Validations

This is how you can add a rule:

import requestCheck from 'request-check' 
const rc = requestCheck()
rc.addRule('email', {
  validator: (email) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(String(email)), 
  message: 'The email given is not valid!'
})
const email = 'felipeINVALIDemail.com'
const name = undefined
const invalid = rc.check({email}, {name})
[
  { field: 'name', message: 'This field is required!'},
  { field: 'email', message: 'The email given is not valid!' }
]

Optional validation

What if you want to validate a value only if it is was given, without necessarily making it required?

In this case, you can add a rule with isRequiredField set to false:

const invalid = rc.check(
  { name },
  { age, isRequiredField: false }
)

This will trigger age validation only if age is given.

If age is undefined or null, check won't complain.

Adding multiple rules

To add multiple rules, you may use addRules, which receives an array of rules as the second argument:

rc.addRules('age', [
  { 
    validator: (age: number) => age < 23, 
    message: 'The age must be under 23!' 
  },
  {
    validator: (age: any) => !isNaN(age),
    message: 'The age must be a number!'
  }
])

Alternatively, you can add more rules by passing additional arguments to addRule:

rc.addRule('age', { 
  validator: (age: number) => age > 18, 
  message:'You need to be at least 18 years old!' 
},
  {
  validator: (age: any) => age < 23,
  message: 'The age must be under 23!'
})

Rule Overwrite

You can use both overwriteRule and overwriteRules to overwrite a previously added rule (instead of stacking the rules).

rc.overwriteRule('age', { 
  validator: (age: number) => age > 18, 
  message:'You need to be at least 18 years old!' 
})

The above code will replace previously added rules for age instead of just adding another rule. The same applies to overwriteRules which will overwrite previous rule(s) with the new rule(s).

Advanced

Why arguments are separated as objects?

I made check arguments separated as objects so that it can grab not only the value but also the field name (property key) and use it in the error message. Also, this allows further options in the same object, such as isRequiredField: false (see Optional validation).

The requestCheck instance

Calling requestCheck() method will create a new memory stored rc with its own rules. You can use the same instance for multiple requests, but remember that rc will check all rules added to it previously.

If you want to use the same instance for multiple requests, you can clear the rules array with rc.clearRules().

You can create a default rule class and export it to use in your project, then overwrite it with overwriteRule or overwriteRules if needed.

Usage Recommendation

import express, { Request, Response } from 'express'
import requestCheck from 'request-check'
import responser from 'responser'
const app = express()
const router = express.Router()
app.use(responser)
app.use(router)
router.get('/hello', (req: Request, res: Response) => {
  const { email, name } = req.body
  const rc = requestCheck()
  const errors = rc.check({email}, {name})
  if(errors) {
    res.send_badRequest('Invalid fields!', errors)
  }
})

Responser is a middleware that helps you send responses with a standard format in your Express app. Check it out at: https://www.npmjs.com/package/responser

Testing

Run the test suit with yarn test.

Contributing

If you want to contribute in any of theses ways:

  • Give your ideas or feedback
  • Question something
  • Point out a problem or issue
  • Enhance the code or its documentation
  • Help in any other way

You can (and should) open an issue or even a pull request!

Thanks for your interest in contributing to this repo!

Author

Luiz Felipe Zarco ([email protected])

Contributors

Christopher-Estanis ([email protected])

License

This code is licensed under the MIT License. See the LICENSE.md file for more info.