Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
138 additions
and
27 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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,30 @@ | ||
/* | ||
* JsBerry example: validation middleware | ||
*/ | ||
|
||
const { routes } = require('../config.json'); | ||
const validation = require('../validation-schemas'); | ||
|
||
module.exports = (ACTIONS) => async(req, res, next) => { | ||
try { | ||
// Find route with searched validation | ||
const currentRoute = Object.values(routes).find((r) => | ||
req.url.indexOf(r.validation.path) !== -1 && // compare url's | ||
(req.method || '').toLowerCase() === r.method); // compare methods | ||
|
||
// Combine (query | body | params) parameters by config values from request | ||
const parameters = (currentRoute.validation.parameters || []) | ||
.map((type) => req[type]).reduce((prev, next) => ({ ...prev, ...next })); | ||
|
||
// Get validation schema by name from config | ||
const result = validation[currentRoute.validation.schema](parameters); | ||
|
||
if (result.error && result.error.message) { | ||
throw new Error(result.error.message); | ||
} | ||
|
||
next(); | ||
} catch (error) { | ||
res.send({ error: 400, message: error.message }); | ||
} | ||
}; |
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,12 @@ | ||
const Joi = require('joi'); | ||
|
||
const schema = Joi.object().keys({ | ||
login: Joi.string().alphanum().min(4).max(30).required(), | ||
password: Joi.string().regex(/^[a-zA-Z0-9_\-]{6,30}$/g).required() | ||
.error(() => `'password' must be at least 6 characters long and must | ||
contain at least one lower case letter, one upper case letter, one number | ||
and one symbol character [_-]`), | ||
email: Joi.string().email().min(6).max(30).required(), | ||
}); | ||
|
||
module.exports = (payload) => Joi.validate(payload, schema); |
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,7 @@ | ||
const Joi = require('joi'); | ||
|
||
const schema = Joi.object().keys({ | ||
id: Joi.string().regex(/^[a-zA-Z0-9_]{6,30}$/).required(), | ||
}); | ||
|
||
module.exports = (payload) => Joi.validate(payload, schema); |
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,7 @@ | ||
const auth = require('./auth'); | ||
const getUser = require('./getUser'); | ||
|
||
module.exports = { | ||
auth, | ||
getUser, | ||
}; |
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