
Product
Redesigned Repositories Page: A Faster Way to Prioritize Security Risk
Our redesigned Repositories page adds alert severity, filtering, and tabs for faster triage and clearer insights across all your projects.
Joitor is a middleware that helps validate body, headers, cookies, params and query in express application using joi validation.
Joitor is a middleware that helps validate body
, headers
, cookies
, params
and query
in express application using Joi validation.
npm install joitor
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;
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(),
},
};
npm install
npm test
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
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();
});
{
status: 400,
statusText: 'Bad Request',
errors: {
body: {
email: '"email" is required',
password: '"password" is required'
}
}
}
FAQs
Joitor is a middleware that helps validate body, headers, cookies, params and query in express application using joi validation.
The npm package joitor receives a total of 12 weekly downloads. As such, joitor popularity was classified as not popular.
We found that joitor demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Our redesigned Repositories page adds alert severity, filtering, and tabs for faster triage and clearer insights across all your projects.
Security News
Slopsquatting is a new supply chain threat where AI-assisted code generators recommend hallucinated packages that attackers register and weaponize.
Security News
Multiple deserialization flaws in PyTorch Lightning could allow remote code execution when loading untrusted model files, affecting versions up to 2.4.0.