Skip to content

Commit 85cf456

Browse files
committed
JOI fix validation error
1 parent 9c225c4 commit 85cf456

File tree

5 files changed

+300
-1
lines changed

5 files changed

+300
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

node_modules/.package-lock.json

Lines changed: 93 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 182 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@
99
"author": "",
1010
"license": "ISC",
1111
"dependencies": {
12+
"@hapi/joi": "^17.1.1",
13+
"@hapi/joi-date": "^2.0.1",
1214
"dotenv": "^16.0.3",
1315
"express": "^4.18.2",
16+
"joi": "^17.7.0",
1417
"mongoose": "^6.8.2"
1518
},
1619
"devDependencies": {

routes/auth.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
11
const router = require('express').Router();
22
const User = require('../model/user');
3+
const Joi = require ('joi')
34

5+
const schema = Joi.object({
6+
name: Joi.string()
7+
.alphanum()
8+
.min(3)
9+
.max(30)
10+
.required(),
11+
password: Joi.string()
12+
.pattern(new RegExp('^[a-zA-Z0-9]{3,30}$')),
13+
repeat_password: Joi.ref('password'),
14+
email: Joi.string()
15+
.email({ minDomainSegments: 2, tlds: { allow: ['com', 'net'] } })
16+
17+
18+
});
419
router.post('/register', async (req, res) => {
5-
const user = new User({
20+
// Validate the data before we make a user
21+
const validation = schema.validate(req.body)
22+
res.send(validation)
23+
24+
/*const user = new User({
625
name: req.body.name,
726
email: req.body.email,
827
password: req.body.password,
@@ -14,6 +33,7 @@ router.post('/register', async (req, res) => {
1433
}catch(err){
1534
res .status(400).send(err);
1635
}
36+
*/
1737
});
1838

1939
module.exports = router;

0 commit comments

Comments
 (0)