-
Notifications
You must be signed in to change notification settings - Fork 84
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
1 parent
cb09ae3
commit 7547099
Showing
4 changed files
with
120 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import Joi from 'joi'; | ||
import { password } from '../utils/custom.validation'; | ||
import { NewRegisteredUser } from '../Users/user.interfaces'; | ||
|
||
const registerBody: Record<keyof NewRegisteredUser, any> = { | ||
email: Joi.string().required().email(), | ||
password: Joi.string().required().custom(password), | ||
name: Joi.string().required(), | ||
}; | ||
|
||
export const register = { | ||
body: Joi.object().keys(registerBody), | ||
}; | ||
|
||
export const login = { | ||
body: Joi.object().keys({ | ||
email: Joi.string().required(), | ||
password: Joi.string().required(), | ||
}), | ||
}; | ||
|
||
export const logout = { | ||
body: Joi.object().keys({ | ||
refreshToken: Joi.string().required(), | ||
}), | ||
}; | ||
|
||
export const refreshTokens = { | ||
body: Joi.object().keys({ | ||
refreshToken: Joi.string().required(), | ||
}), | ||
}; | ||
|
||
export const forgotPassword = { | ||
body: Joi.object().keys({ | ||
email: Joi.string().email().required(), | ||
}), | ||
}; | ||
|
||
export const resetPassword = { | ||
query: Joi.object().keys({ | ||
token: Joi.string().required(), | ||
}), | ||
body: Joi.object().keys({ | ||
password: Joi.string().required().custom(password), | ||
}), | ||
}; | ||
|
||
export const verifyEmail = { | ||
query: Joi.object().keys({ | ||
token: Joi.string().required(), | ||
}), | ||
}; |
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,49 @@ | ||
import Joi from 'joi'; | ||
import { password, objectId } from '../utils/custom.validation'; | ||
import { NewCreatedUser } from './user.interfaces'; | ||
|
||
const createUserBody: Record<keyof NewCreatedUser, any> = { | ||
email: Joi.string().required().email(), | ||
password: Joi.string().required().custom(password), | ||
name: Joi.string().required(), | ||
role: Joi.string().required().valid('user', 'admin'), | ||
}; | ||
|
||
export const createUser = { | ||
body: Joi.object().keys(createUserBody), | ||
}; | ||
|
||
export const getUsers = { | ||
query: Joi.object().keys({ | ||
name: Joi.string(), | ||
role: Joi.string(), | ||
sortBy: Joi.string(), | ||
limit: Joi.number().integer(), | ||
page: Joi.number().integer(), | ||
}), | ||
}; | ||
|
||
export const getUser = { | ||
params: Joi.object().keys({ | ||
userId: Joi.string().custom(objectId), | ||
}), | ||
}; | ||
|
||
export const updateUser = { | ||
params: Joi.object().keys({ | ||
userId: Joi.required().custom(objectId), | ||
}), | ||
body: Joi.object() | ||
.keys({ | ||
email: Joi.string().email(), | ||
password: Joi.string().custom(password), | ||
name: Joi.string(), | ||
}) | ||
.min(1), | ||
}; | ||
|
||
export const deleteUser = { | ||
params: Joi.object().keys({ | ||
userId: Joi.string().custom(objectId), | ||
}), | ||
}; |
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,18 @@ | ||
import { CustomHelpers } from 'joi'; | ||
|
||
export const objectId = (value: string, helpers: CustomHelpers) => { | ||
if (!value.match(/^[0-9a-fA-F]{24}$/)) { | ||
return helpers.message({ custom: '"{{#label}}" must be a valid mongo id' }); | ||
} | ||
return value; | ||
}; | ||
|
||
export const password = (value: string, helpers: CustomHelpers) => { | ||
if (value.length < 8) { | ||
return helpers.message({ custom: 'password must be at least 8 characters' }); | ||
} | ||
if (!value.match(/\d/) || !value.match(/[a-zA-Z]/)) { | ||
return helpers.message({ custom: 'password must contain at least 1 letter and 1 number' }); | ||
} | ||
return value; | ||
}; |