-
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.
refactor(validators): separate validation from route logic
[Finishes #166075761]
- Loading branch information
Showing
7 changed files
with
469 additions
and
285 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,11 @@ | ||
import { validationResult } from 'express-validator/check'; | ||
|
||
const validate_error_or_next = (req, res, next) => { | ||
const errors = validationResult(req); | ||
if (!errors.isEmpty()) { | ||
return res.status(422).json({ errors: errors.array() }); | ||
} | ||
return next(); | ||
}; | ||
|
||
export default validate_error_or_next; |
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,38 @@ | ||
import { body } from 'express-validator/check'; | ||
import { sanitizeBody } from 'express-validator/filter'; | ||
import { test_logger } from '../utils/loggers'; | ||
|
||
import validate_error_or_next from './validate_error_or_next'; | ||
|
||
const LoansValidators = { | ||
validateAmount: [ | ||
body('amount') | ||
.isFloat({ min: 50000 }) | ||
.withMessage('Amount cannot be less than 50,000') | ||
.isFloat({ max: 1000000 }) | ||
.withMessage('Amount cannot be greater than 1,000,000'), | ||
sanitizeBody('amount').toFloat(), | ||
validate_error_or_next | ||
], | ||
|
||
validateRepayAmount: [ | ||
body('amount') | ||
.isFloat() | ||
.withMessage('Repayment amount must be a number'), | ||
sanitizeBody('amount').toFloat(), | ||
validate_error_or_next | ||
], | ||
|
||
validateTenor: [ | ||
body('tenor') | ||
.isInt({ min: 1 }) | ||
.withMessage('Tenor cannot be less than 1') | ||
.isInt({ max: 12 }) | ||
.withMessage('Tenor cannot be greater than 12'), | ||
sanitizeBody('tenor').toInt(), | ||
validate_error_or_next | ||
|
||
], | ||
}; | ||
|
||
export default LoansValidators; |
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
Oops, something went wrong.