Skip to content

Commit

Permalink
feat: add validations
Browse files Browse the repository at this point in the history
  • Loading branch information
saisilinus committed Dec 16, 2021
1 parent cb09ae3 commit 7547099
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 12 deletions.
53 changes: 53 additions & 0 deletions src/Auth/auth.validation.ts
@@ -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(),
}),
};
12 changes: 0 additions & 12 deletions src/Users/user.interfaces.ts
Expand Up @@ -19,14 +19,6 @@ export interface IUserModel extends Model<IUserDoc> {
toJSON(): LeanDocument<this>;
}

export interface IUserStatics extends Model<IUser> {
isEmailTaken(email: string, excludeUserId?: ObjectId): Promise<boolean>;
paginate(filter: Record<string, any>, options: Record<string, any>): Promise<QueryResult>;
toJSON(): LeanDocument<this>;
}

export type IUserAndUserStatics = IUser & IUserStatics;

export interface UpdateUserBody {
name?: string;
email?: string;
Expand All @@ -47,7 +39,3 @@ export interface NewCreatedUser {
password: string;
role: string;
}

export interface ToJSONUser extends IUserDoc {
id: ObjectId;
}
49 changes: 49 additions & 0 deletions src/Users/user.validation.ts
@@ -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),
}),
};
18 changes: 18 additions & 0 deletions src/utils/custom.validation.ts
@@ -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;
};

0 comments on commit 7547099

Please sign in to comment.