-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement create promotion validation chain
- Loading branch information
Showing
3 changed files
with
33 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import dayjs from "dayjs"; | ||
import { body } from "express-validator"; | ||
|
||
export const createPromotion = () => [ | ||
body("name", "Name is required") | ||
.isString() | ||
.trim() | ||
.notEmpty({ ignore_whitespace: true }), | ||
body("price", "Price is required") | ||
.notEmpty({ ignore_whitespace: true }) | ||
.isNumeric({ no_symbols: true }) | ||
.withMessage("Price must not have any symbols") | ||
.bail() | ||
.custom((value) => { | ||
if (value > 500) throw "Price must be less than 500"; | ||
return true; | ||
}), | ||
body("startDate", "Start date is required") | ||
.notEmpty({ ignore_whitespace: true }) | ||
.isISO8601({ strict: true, strictSeparator: true }) | ||
.withMessage("Start date must be a valid ISO 8601 date") | ||
.bail() | ||
.custom((value) => dayjs().isBefore(dayjs(value))) | ||
.withMessage("Start date must be in future"), | ||
body("endDate", "End date is required") | ||
.notEmpty({ ignore_whitespace: true }) | ||
.isISO8601({ strict: true, strictSeparator: true }) | ||
.withMessage("End date must be a valid ISO 8601 date") | ||
.bail() | ||
.custom((value, { req }) => dayjs(value).isAfter(dayjs(req.body.startDate))) | ||
.withMessage("End date must after start date"), | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters