Skip to content

Commit

Permalink
Update validator to only run validation on a callback function (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vylpes committed Oct 13, 2023
1 parent 208147c commit 3a6375b
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ ssl/
*.tfstate
*.tfstate.backup

.docker/
.env
ormconfig.json
yarn-error.log
Expand Down
7 changes: 6 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ services:
ports:
- "3306:3306"
volumes:
- $DB_DATA_LOCATION:/var/lib/mysql
- $DB_DATA_LOCATION:/var/lib/mysql
mailhog:
image: mailhog/mailhog
ports:
- 1025:1025 # smtp server
- 8025:8025 # web ui
1 change: 1 addition & 0 deletions src/contracts/IValidationRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export default interface IValidationRule {
to?: string,
length?: number,
errorMessage?: string,
whenCallback?: Function,
}
17 changes: 17 additions & 0 deletions src/helpers/Validation/Body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ export default class Body {
for (let i = 0; i < this.rules.length; i++) {
const rule = this.rules[i];

if (rule.whenCallback) {
const shouldRun = rule.whenCallback(req);

if (!shouldRun) {
next();
return;
}
}

switch (rule.rule) {
case ValidationRule.NotEmpty:
if (!req.body[rule.field] || req.body[rule.field].length == 0) {
Expand Down Expand Up @@ -314,6 +323,14 @@ export default class Body {
return this;
}

public When(whenCallback: Function): Body {
if (this.rules.length == 0) return this;

this.rules[this.rules.length - 1].whenCallback = whenCallback;

return this;
}

public ChangeField(field: string): Body {
this.field = field;

Expand Down
17 changes: 17 additions & 0 deletions src/helpers/Validation/Params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ export default class Params {
for (let i = 0; i < this.rules.length; i++) {
const rule = this.rules[i];

if (rule.whenCallback) {
const shouldRun = rule.whenCallback(req);

if (!shouldRun) {
next();
return;
}
}

switch (rule.rule) {
case ValidationRule.NotEmpty:
if (!req.params[rule.field] || req.params[rule.field].length == 0) {
Expand Down Expand Up @@ -245,6 +254,14 @@ export default class Params {
return this;
}

public When(whenCallback: Function): Params {
if (this.rules.length == 0) return this;

this.rules[this.rules.length - 1].whenCallback = whenCallback;

return this;
}

public ChangeField(field: string): Params {
this.field = field;

Expand Down
17 changes: 17 additions & 0 deletions src/helpers/Validation/Query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ export default class Query {
for (let i = 0; i < this.rules.length; i++) {
const rule = this.rules[i];

if (rule.whenCallback) {
const shouldRun = rule.whenCallback(req);

if (!shouldRun) {
next();
return;
}
}

switch (rule.rule) {
case ValidationRule.NotEmpty:
if (!req.query[rule.field] || req.query[rule.field].length == 0) {
Expand Down Expand Up @@ -245,6 +254,14 @@ export default class Query {
return this;
}

public When(whenCallback: Function): Query {
if (this.rules.length == 0) return this;

this.rules[this.rules.length - 1].whenCallback = whenCallback;

return this;
}

public ChangeField(field: string): Query {
this.field = field;

Expand Down
3 changes: 2 additions & 1 deletion src/routes/storage/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export default class New extends Page {
.ChangeField("skuPrefix")
.NotEmpty()
.ChangeField("parentId")
.NotEmpty();
.NotEmpty()
.When((req: Request) => req.body.type != 'building');

super.router.post('/new', UserMiddleware.Authorise, bodyValidation.Validate.bind(bodyValidation), async (req: Request, res: Response) => {
const type = req.body.type;
Expand Down

0 comments on commit 3a6375b

Please sign in to comment.