A simple but powerful middleware to validate request object in express
import { validateRequestBody, Validators } from 'enkyro';
import express from 'express';
const app = express();
app.get("/", validateRequestBody([
{
key: "username",
validator: Validators.rangeChar(5, 10),
response: {
message: "Username must be between 5-10 characters."
}
},
{
key: "password",
validator: [Validators.minChar(8), Validators.regex(/regex/)]
}
]), (req, res) => {
res.send("Successfully validated!");
})
type ValidationableResponse = CustomResponseOption | CustomResponseFunction;
type ValidatorFunction<T> = (test: T) => boolean;
type ValidationableValidator<T> = ValidatorFunction<T> | ValidatorFunction<T>[];
interface Validationable<T> {
key: string;
validator: ValidationableValidator<T>;
response?: ValidationableResponse;
}
👤 Rich
Give a ⭐️ if this project helped you!
This README was generated with ❤️ by readme-md-generator