express-protector

<!-- [![Build Status](https://travis-ci.org/jgtbz/express-protector.svg?branch=master)](https://travis-ci.org/jgtbz/express-protector) --> [![License](https://badgen.net/github/license/jgtbz/express-protector)](./LICENSE) [![Library minified size](https:/

Usage no npm install needed!

<script type="module">
  import expressProtector from 'https://cdn.skypack.dev/express-protector';
</script>

README

Express Protector

License Library minified size Library minified + gzipped size

Installation

This library is published in the NPM registry and can be installed using any compatible package manager.

npm install express-protector --save

# For Yarn, use the command below.
yarn add express-protector

Installation from CDN

This module has an UMD bundle available through JSDelivr and Unpkg CDNs.

<!-- For UNPKG use the code below. -->
<script src="https://unpkg.com/express-protector"></script>

<!-- For JSDelivr use the code below. -->
<script src="https://cdn.jsdelivr.net/npm/express-protector"></script>

<script>
  // UMD module is exposed through the "ExpressProtector" global variable.
  console.log(ExpressProtector);
</script>

Usage

import ExpressProtector from 'express-protector'

const req = {
  query: { name: 'João', status: 'active' },
  params: { id: 10, role: 2 },
  body: {
    name: 'João',
    phone: '99 9 9999-9999',
    email: 'email@example.com',
    status: 'inactive'
  }
}

Routes
  .get(
    '/example',
    ExpressProtector.acceptQuery(['name']),
    (req) => {
      console.log(req.query)
      // {
      //   name: 'João'
      // }
    }
  )
  .post(
    '/example',
    ExpressProtector.acceptParams('id'),
    (req) => {
      console.log(req.params)
      // {
      //   id: 10
      // }
    }
  )
  .post(
    '/example',
    ExpressProtector.ignoreBody('status'),
    (req) => {
      console.log(req.body)
      // {
      //   name: 'João',
      //   phone: '99 9 9999-9999',
      //   email: 'email@example.com'
      // }
    }
  )

Options

ExpressProtector.accept('query', 'name,email')
ExpressProtector.accept('query', ['name', 'email'])

ExpressProtector.acceptQuery('name,email')
ExpressProtector.acceptQuery(['name', 'email'])

ExpressProtector.acceptParams('name,email')
ExpressProtector.acceptParams(['name', 'email'])

ExpressProtector.acceptBody('name,email')
ExpressProtector.acceptBody(['name', 'email'])

ExpressProtector.ignore('query', 'name,email')
ExpressProtector.ignore('query', ['name', 'email'])

ExpressProtector.ignoreQuery('name,email')
ExpressProtector.ignoreQuery(['name', 'email'])

ExpressProtector.ignoreParams('name,email')
ExpressProtector.ignoreParams(['name', 'email'])

ExpressProtector.ignoreBody('name,email')
ExpressProtector.ignoreBody(['name', 'email'])