@stephenboyd/post-validator

1.0.3 • Public • Published

A lightweight express middleware to make basic post request body validations

Install

npm install @stephenboyd/post-validator

Explanation

This package allows you do add simple validations to Express endpoints. To do so, just import the package, create your array of required fields, and start the validator.


Usage

const express = require('express')
const bodyParser = require('body-parser')
const app = express();

app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())

// Import post-validator
const apiValidator = require('post-validator')
const validator = apiValidator.apiValidator

// Create the validation you want
const baseValidator = [
    {field: 'itemDescription', type: 'string'}, 
    {field: 'itemNumber', type: 'number'}
]

// Add the validator as a middleware
app.post('/', validator(baseValidator), (req, res) => {
    return res.send('hello')
})

// After all endpoints have been set up, initialize the validators
app.use(apiValidator.startValidators)

app.listen(3000, () => {
    console.log('app started')
})

Package Sidebar

Install

npm i @stephenboyd/post-validator

Weekly Downloads

1

Version

1.0.3

License

MIT

Unpacked Size

2.77 kB

Total Files

4

Last publish

Collaborators

  • stephenboyd