The express-validator was used to validate the input data to the API
All validations are inside the folder src\validations
and can be created via the validationMiddleware
support function.
The validationMiddleware
support function is located in src\middlewares\validation-middleware.ts
,
accepts as input an array of ValidationChain and return a ValidationMiddleware
type that can used in a route.
If the input is invalid throw a BadRequestException with input errors. (See exceptions documentation)
You can use the src\interfaces\crud-validation.ts
interfaces to create validations in a controlled way.
import validationMiddleware, {ValidationMiddleware} from "../middlewares/validation-middleware";
import {ICreateValidation} from "../interfaces/crud-validation";
...
// With CRUD Interface
const MyValidation: ICreateValidation = {
create: validationMiddleware([
// Any ValidationChain
])
};
export default MyValidation;
...
// Custom
const myValidation: ValidationMiddleware = validationMiddleware([
// Any ValidationChain
]);
export default myValidation;
const router = express.Router();
// With CRUD Interface
router.use('/your-route', MyValidation.create, anyController);
// Custom
router.use('/your-route', myValidation, anyController);