diff --git a/.gitignore b/.gitignore index 4a714499..c4066544 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ ssl/ *.tfstate *.tfstate.backup +.docker/ .env ormconfig.json yarn-error.log diff --git a/docker-compose.yml b/docker-compose.yml index b183fc4f..7bb8eb12 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -14,4 +14,9 @@ services: ports: - "3306:3306" volumes: - - $DB_DATA_LOCATION:/var/lib/mysql \ No newline at end of file + - $DB_DATA_LOCATION:/var/lib/mysql + mailhog: + image: mailhog/mailhog + ports: + - 1025:1025 # smtp server + - 8025:8025 # web ui \ No newline at end of file diff --git a/src/contracts/IValidationRule.ts b/src/contracts/IValidationRule.ts index f04522dd..010f2dee 100644 --- a/src/contracts/IValidationRule.ts +++ b/src/contracts/IValidationRule.ts @@ -6,4 +6,5 @@ export default interface IValidationRule { to?: string, length?: number, errorMessage?: string, + whenCallback?: Function, } \ No newline at end of file diff --git a/src/helpers/Validation/Body.ts b/src/helpers/Validation/Body.ts index b74c0970..9f476058 100644 --- a/src/helpers/Validation/Body.ts +++ b/src/helpers/Validation/Body.ts @@ -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) { @@ -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; diff --git a/src/helpers/Validation/Params.ts b/src/helpers/Validation/Params.ts index 61a4a1ee..af056893 100644 --- a/src/helpers/Validation/Params.ts +++ b/src/helpers/Validation/Params.ts @@ -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) { @@ -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; diff --git a/src/helpers/Validation/Query.ts b/src/helpers/Validation/Query.ts index 2ec1acb7..f8609f37 100644 --- a/src/helpers/Validation/Query.ts +++ b/src/helpers/Validation/Query.ts @@ -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) { @@ -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; diff --git a/src/routes/storage/new.ts b/src/routes/storage/new.ts index 5f643212..11ae37e9 100644 --- a/src/routes/storage/new.ts +++ b/src/routes/storage/new.ts @@ -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;