Skip to content

Commit

Permalink
feat: implement create promotion validation chain
Browse files Browse the repository at this point in the history
  • Loading branch information
riyoneri committed Nov 19, 2024
1 parent afc5ae4 commit 5b7a349
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
4 changes: 0 additions & 4 deletions src/validations/admin-products.validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ export const createProductChain = () => [
.trim()
.notEmpty({ ignore_whitespace: true }),
body("price", "Price is required")
.isString()
.trim()
.notEmpty({ ignore_whitespace: true })
.isNumeric({ no_symbols: true })
.withMessage("Price must not have any symbols")
Expand Down Expand Up @@ -52,8 +50,6 @@ export const updateProductChain = () => [
.trim()
.notEmpty({ ignore_whitespace: true }),
body("price", "Price is required")
.isString()
.trim()
.notEmpty({ ignore_whitespace: true })
.isNumeric({ no_symbols: true })
.withMessage("Price must not have any symbols")
Expand Down
32 changes: 32 additions & 0 deletions src/validations/admin-promotions.validation.ts
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"),
];
1 change: 1 addition & 0 deletions src/validations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from "./auth.validation";
export * from "./admin-categories.validation";
export { default as getAllSanitizer } from "./get-all.validation";
export * from "./admin-products.validation";
export * from "./admin-promotions.validation";

0 comments on commit 5b7a349

Please sign in to comment.