GithubHelp home page GithubHelp logo

fedoryakubovich / joitor Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 431 KB

Middleware that helps validate body, headers, cookies, params and query in express application using Joi validation.

License: MIT License

JavaScript 100.00%
node nodejs joi joi-validation express express-js validation

joitor's Introduction

Joitor · GitHub license npm version Build Status NPM downloadscodecov

Joitor is a middleware that helps validate body, headers, cookies, params and query in express application using Joi validation.

Table of Contents

Installation

npm install joitor

Features

  • body
  • cookies
  • headers
  • query
  • params

Usage

const express = require('express');
const bodyParser = require('body-parser');
const http = require('http');
const cookieParser = require('cookie-parser');
const validate = require('joitor');
const Joi = require('@hapi/joi');

const app = express();

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

const signupValidation = {
  body: {
    email: Joi.string().email().required(),

    password: Joi.string().min(6).max(256).required(),
  },
};

app.post('/signup', validate(signupValidation), (req, res) => res.status(200).send());

const server = http.createServer(app);
server.listen(3000, () => console.log(`Server is running on http://localhost:3000`));

module.exports = app;

Options

By default, Joi don't allow object to contain unknown keys which, they are ignored. If an object can contain unknown keys, pass the following keys: allowUnknown: true. See an example below:

const signupValidation = {
  body: {
    allowUnknown: true,

    email: Joi.string().email().required(),

    password: Joi.string().min(6).max(256).required(),
  },
};

Tests

npm install
npm test

Errors

Custom error status

By default, Joitor returns 400 error status and Bad Request text. If you want to change them you can pass the second argument to the validate function, For example:

// some code

app.post(
  '/signup',
  validate(signupValidation, { status: 409, statusText: 'Conflict' }),
  (req, res) => res.status(200).send(),
);

// some code

Custom handler

Joitor provides his own type of error. With it you can manually handle the error. For example:

// some code

const validate = require('joitor');

// some code

app.use(function (err, req, res, next) {
  if (err instanceof validate.JoitorError) {
    // handler for the error
    return res.status(err.status || 400).json(err);
  }

  res.status(500).send();
});

Examples of errors

{
  status: 400,
  statusText: 'Bad Request',
  errors: {
    body: {
      email: '"email" is required',
      password: '"password" is required'
    }
  }
}

joitor's People

Contributors

fedoryakubovich avatar

Stargazers

 avatar  avatar

Watchers

 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.