Git Product home page Git Product logo

middleware-params-validator's Introduction

middleware-params-validator

A simple middleware to validate the request object (body, params, querystring, headers, customData, ...) againts a json structure

Requirements

node >= 6.9

usage

Simply add the validator middleware to your routes with the desired json structure

{
  /* body: just an object inside the request */
  body: {
    /* parameter of the body */
    email: [    /* array of validations to execute    */
      'email',  /* just checking if its a valid email */
    ],          /*------------------------------------*/
  },
  /* params: another object inside the request */
  params: 'id', /* you can also pass a string instead of an object, this means that it will only check if the parameter exists */
}

extra data on validations

{
  /* custom: just an object inside the request */
  params: [
    /* parameter of the custom object */
    'id',
    /* another parameter of the custom object */
    'date',
    /* and yet another parameter of the custom object */
    {
      name: [
        {
          /* this is an example, on how to pass extra data to a validator */
          length: 'min_5',
        },
      ],
    },
  ]
}

custom data on request

If you have a controller/middleware/... that adds data to the request object, you can also check it

{
  /* custom: just an object inside the request */
  custom: [
    /* parameter of the custom object */
    {
      email: [
        'email',
      ],
    },
    /* another parameter of the custom object */
    'date',
    /* and yet another parameter of the custom object */
    {
      name: [
        {
          /* this is an example, on how to pass extra data to a validator */
          length: 'min_5',
        },
      ],
    },
  ]
}

available validators

  • email
  • length (supports extra data: min_N, max_N, exact_N)
  • number
  • words (supports extra data that is the minimum number of words: N )

example

// controller
import friendController from 'controllers/friend'

// middleware
import validator from 'middleware-params-validator'

const router = Router()

router.post('/:id/addFriend',
  validator({
    body: {
      email: [
        'email',
      ],
    },
    params: 'id',
  }),
  someController.addFriend
)

router.delete('/:id/removeFriend/:email',
  validator({
    params: [
      'id',
      {
        'email': [
          'email',
        ],
      },
    ],
  }),
  friendController.removeFriend
)

router.delete('/:id',
  validator({
    params: 'id',
  }),
  friendController.deleteHoliday
)

router.post('/',
  validator({
    body: {
      email: [
        'email',
      ],
    },
  }),
  friendController.addFriend
)

middleware-params-validator's People

Contributors

jwebcoder avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.