
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
req-trapper
Advanced tools
req-trapper is an Express middleware for request validation inspired by Laravel's request validation. It allows you to define validation rules for your API requests and ensures that incoming data in the request payload meets your specified criteria.
req-trapper into your Express application as middleware for route-specific request validation.Install req-trapper using npm:
npm install req-trapper
import ReqTrapper from 'req-trapper';
import express from 'express';
const app = express();
const reqTrapper = new ReqTrapper();
// Use req-trapper as middleware
app.post('/example',
reqTrapper.validate([{ name: 'phone', validation: 'required|number' }]), // Validation rules
(req, res) => {
// Your route logic here
});
// Use req-trapper in another route
app.post('/example2',
reqTrapper.validate([{ name: 'email', validation: 'required|email' }]), // Validation rules
(req, res) => {
// Your route logic here
});
Here are the built-in validation rules you can use:
required: Ensures that the specified field exists in the request data.
requiredemail: Ensures that the specified field contains a valid email address.
emailmin:x: Ensures that the specified field has at least x characters or is greater than or equal to x.
min:5max:x: Ensures that the specified field has no more than x characters or is less than or equal to x.
max:10in:values: Ensures that the specified field is one of the given values (comma-separated).
in:admin,user,guestnumber: Ensures that the specified field is a number.
numbergreater_than:x: Ensures that the specified field is greater than x.
greater_than:18nullable: Allows a field to be null.
nullableurl: Ensures that the specified field is a valid URL.
urlboolean: Ensures that the specified field is a boolean.
booleanalpha: Ensures that the specified field contains only alphabetic characters.
alphaalpha_num: Ensures that the specified field contains only alphabetic and numeric characters.
alpha_numarray: Ensures that the specified field is an array.
arrayjson: Ensures that the specified field is valid JSON.
jsondate: Ensures that the specified field is a valid date.
dateafter:date: Ensures that the specified date field is after the given date.
after:2024-01-01before:date: Ensures that the specified date field is before the given date.
before:2024-01-01unique: Ensures that the specified field is unique in the database (database integration required).
unique:users,emaildigits:x: Ensures that the specified field contains exactly x digits.
digits:10digits_between:min,max: Ensures that the specified field contains digits between the minimum and maximum values.
digits_between:5,10exists: Ensures that the specified field exists in the database (database integration required).
exists:users,emailimage: Ensures that the specified field is an image file (based on MIME type).
imagefile: Ensures that the specified field is a file.
filemimes:types: Ensures that the file is of the specified MIME type(s).
mimes:jpeg,pngrequired_if:other_field: Requires the field if the other field is present.
required_if:role,adminrequired_unless:other_field: Requires the field unless the other field is present.
required_unless:role,guestrequired_with:other_field: Requires the field if the other field is present.
required_with:passwordrequired_with_all:fields: Requires the field if all the other fields are present.
required_with_all:password,confirm_passwordrequired_without:other_field: Requires the field if the other field is not present.
required_without:emailrequired_without_all:fields: Requires the field if none of the other fields are present.
required_without_all:email,phoneYou can define your own custom validation rules. For example:
const customValidations = [
{
validation: 'isEven',
action: (value) => value % 2 === 0
}
];
const reqTrapper = new ReqTrapper({ customValidations });
You can then use your custom validation in the rules:
app.post('/example',
reqTrapper.validate([{ name: 'number', validation: 'required|isEven' }]),
(req, res) => {
// Your route logic here
}
);
You can override the default error messages by providing custom messages:
const reqTrapper = new ReqTrapper();
reqTrapper.setCustomMessages({
'email.required': 'Email is mandatory!',
'phone.number': 'Phone number must be a valid number.'
});
These custom messages will be used in place of the default ones.
Contributions are welcome! Feel free to open issues or submit pull requests on Github.
FAQs
A middleware to help you easily validate your request body in express js.
We found that req-trapper demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 0 open source maintainers 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.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.