Skip to content

Commit

Permalink
feat: Add body validation
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielmachin committed Jan 15, 2024
1 parent c14178f commit d32a337
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/controllers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,23 @@ import {
AuthenticatedRequest,
LoginParams,
} from 'types';
import { validateLoginSchema, validateUserSchema } from 'validator/auth';

@Route('v1/auth')
export class AuthControllerV1 extends Controller {
@Post('/register')
public async register(@Body() user: CreateUserParams): Promise<ReturnAuth> {
validateUserSchema(user);

const authReturn = await AuthService.register(user);
this.setStatus(httpStatus.CREATED);
return authReturn;
}

@Post('/login')
public async login(@Body() loginParams: LoginParams): Promise<ReturnAuth> {
validateLoginSchema(loginParams);

const authReturn = await AuthService.login(loginParams);
this.setStatus(httpStatus.OK);
return authReturn;
Expand Down
8 changes: 7 additions & 1 deletion src/middlewares/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ export const errorConverter = (
next(error);
};

export const errorHandler = (err: ApiError, req: Request, res: Response) => {
export const errorHandler = (
err: ApiError,
req: Request,
res: Response,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
next: NextFunction,
) => {
let { httpCode, message } = err;
if (!err.isOperational) {
httpCode =
Expand Down
11 changes: 11 additions & 0 deletions src/utils/validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ZodError } from 'zod';

export const formatZodError = (zodError: ZodError) => {
const formattedError = zodError.errors.map((error) => {
return {
field: error.path,
error: error.message,
};
});
return formattedError;
};
35 changes: 35 additions & 0 deletions src/validator/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { ZodError, z } from 'zod';

import { CreateUserParams, LoginParams } from 'types';
import { ApiError } from 'utils/apiError';
import { errors } from 'config/errors';
import { formatZodError } from 'utils/validator';

const userSchema = z.object({
email: z.string().email({ message: 'Invalid email' }),
name: z.string(),
password: z.string().min(1, { message: "Can't be an empty password" }),
});

const loginSchema = z.object({
email: z.string().email({ message: 'Invalid email' }),
password: z.string().min(1, { message: "Can't be an empty password" }),
});

export const validateUserSchema = (user: CreateUserParams) => {
try {
userSchema.parse(user);
} catch (err) {
const formattedZodError = formatZodError(err as ZodError);
throw new ApiError(errors.VALIDATION_ERROR, true, formattedZodError);
}
};

export const validateLoginSchema = (loginParams: LoginParams) => {
try {
loginSchema.parse(loginParams);
} catch (err) {
const formattedZodError = formatZodError(err as ZodError);
throw new ApiError(errors.VALIDATION_ERROR, true, formattedZodError);
}
};
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"tests/*": ["tests/*"],
"types/*": ["types/*"],
"utils/*": ["utils/*"],
"validator/*": ["validator/*"],
"root/*": ["../*"]
}
}
Expand Down
1 change: 1 addition & 0 deletions tsoa.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"tests/*": ["tests/*"],
"types/*": ["types/*"],
"utils/*": ["utils/*"],
"validator/*": ["validator/*"],
"root/*": ["../*"]
}
}
Expand Down

0 comments on commit d32a337

Please sign in to comment.