2.0.1 • Published 5 years ago

validation-schema v2.0.1

Weekly downloads
1
License
ISC
Repository
-
Last release
5 years ago

Validation Schema

Build Status Maintenance License: GPL v3 npm version

Documentation

About

validation-schema is a Mongoose inspired express middleware with the purpose of validating and sanitizing JSON requests. It supports custom validate/sanitize with asynchronous capabilities.

Note: validation-schema is still early in development and has many planned features, along with optimization improvements.

Install

npm install validation-schema

Example

A quick example of how validation-schema could be used. View the documentation.

const validationSchema = require('validation-schema')

app.post('/', validationSchema({
  username: {
    type: 'string', // Data type check
    required: [true, 'A username is required'],
    minlength: [5, 'Username must be at least 5 characters'],
    maxlength: [25, 'Username must be no more than 25 characters']
  },
  password: { // can use single values for a default error message
    type: 'string',
    required: true,
    minlength: 5 
  }
}), (req, res, next) => {
  // Detailed report of all errors
  // Shows error location, error type e.g 'maxlength' and error messages.
  console.log(req.valid.errors) 

  // Nicely formatted array of error messages
  console.log(req.valid.errorMessages) 

  // Validated and sanitized values
  console.log(req.valid.values)
})