Skip to content

Commit

Permalink
Validation implemented for username
Browse files Browse the repository at this point in the history
  • Loading branch information
djohn06 committed May 18, 2023
1 parent 485ee86 commit 1b4883c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
8 changes: 8 additions & 0 deletions api/controllers/users.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const User = require("../models/user");
const { validateEmail } = require("../helpers/validation");
const { validatePassword } = require("../helpers/validation");
const { validateUsername } = require("../helpers/validation");

const UsersController = {
Create: (req, res) => {
Expand All @@ -10,12 +11,19 @@ const UsersController = {
res.status(400).json({
message: 'Invalid email address!'
})

// if username is ok
} else if (!validateUsername(user.username)) {
res.status(400).json({
message: 'Invalid username!'
})

// if password is ok
} else if (!validatePassword(user.password)) {
res.status(400).json({
message: 'Invalid password!'
})

} else {
user.save((err) => {
if (err) {
Expand Down
3 changes: 3 additions & 0 deletions api/helpers/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ exports.validateEmail = (email) => {
exports.validatePassword = (password) => {
return String(password)
.match(/^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{8,20}$/)
}
exports.validateUsername = (username) => {
return String(username).match(/^.{5,15}$/)
}

0 comments on commit 1b4883c

Please sign in to comment.