@zarcobox/validator
Checks if request body is valid
Last updated a month ago by zarco .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @zarcobox/validator 
SYNC missed versions from official npm registry.

Validator

Check whether data is what it is meant to be

You should not always believe the data is exactly what you think it is. Hopefully, you will validate data you received, for example when it comes to one of your controllers. This module helps with that. I found that many of the validators out there are either incomplete or not fully customizable. Therefore, I built my own. It is rather simple and it works.

License: MIT npm version Build Status Coverage Status Downloads

npm

Install

Add validator with your favorite package manager:

yarn add @zarcobox/validator

Usage


import validator from '@zarcobox/validator'

import { Request, Response } from 'express'

class UserController {

  async create(request: Request, response: Response) {

  const { email, name } = request.body

  const invalid = validator.check({email}, {name})

  if(invalid) response.status(400).json({ invalid })

      // ...

  }
}

Basic Check

This line checks whether those two variables are set.

  const invalid = validator.check({email}, {name})

Check will return an Array of objects with field and message or it will return undefined.

In the above example, if none of the variables are given, invalid will contain:

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

If both fields are given, check will return undefined and invalid would avaliate to false.

Validations

In addition to check if the variable is set, check will also look for a rule definition for that variable.

This is how you can add a rule:

validator.addRule('email', {
  validator: (email: any) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(String(email)), 
  message: 'The email given is not valid!'
})

Suppose the variable values:

const email = 'felipezarco@hotmail.com'
const name = undefined

Now, when you call:

  const invalid = validator.check({email}, {name})

The output stored in invalid will contain:

[
  { field: 'name', message: 'The field name is required!' },
  { field: 'email', message: 'The email given is not valid!' }
]

Configuration

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

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

The symbol :name will be replaced with the field name. (Its use is optional)

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 (felipezarco@hotmail.com)

License

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

Current Tags

  • 0.2.6                                ...           latest (a month ago)

7 Versions

  • 0.2.6                                ...           a month ago
  • 0.2.4                                ...           a month ago
  • 0.2.3                                ...           a month ago
  • 0.2.2                                ...           a month ago
  • 0.2.1                                ...           a month ago
  • 0.2.0                                ...           a month ago
  • 0.1.0                                ...           a month ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (6)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |