Skip to content

Commit

Permalink
OV-2: + implement validation
Browse files Browse the repository at this point in the history
  • Loading branch information
JKaypa committed Aug 19, 2024
1 parent 1beb23a commit 9139197
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 6 deletions.
7 changes: 5 additions & 2 deletions backend/src/bundles/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import {
type UserSignInRequestDto,
type UserSignUpRequestDto,
} from '~/bundles/users/users.js';
import { userSignUpValidationSchema } from '~/bundles/users/users.js';
import {
userSignInValidationSchema,
userSignUpValidationSchema,
} from '~/bundles/users/users.js';
import {
type ApiHandlerOptions,
type ApiHandlerResponse,
Expand All @@ -27,7 +30,7 @@ class AuthController extends BaseController {
path: AuthApiPath.SIGN_IN,
method: 'POST',
validation: {
body: userSignUpValidationSchema,
body: userSignInValidationSchema,
},
handler: (options) =>
this.signIn(
Expand Down
5 changes: 4 additions & 1 deletion backend/src/bundles/users/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ export {
type UserSignUpResponseDto,
} from './types/types.js';
export { UserModel } from './user.model.js';
export { userSignUpValidationSchema } from './validation-schemas/validation-schemas.js';
export {
userSignInValidationSchema,
userSignUpValidationSchema,
} from './validation-schemas/validation-schemas.js';
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { userSignUpValidationSchema } from 'shared';
export { userSignInValidationSchema, userSignUpValidationSchema } from 'shared';
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const UserValidationMessage = {
EMAIL_REQUIRE: 'Email is required',
EMAIL_WRONG: 'Email is wrong',
EMAIL_INVALID: 'Please enter a valid email',
FIELD_REQUIRE: 'Please fill out this field',
PASSWORD_LENGTH: 'Password must have from 6 to 12 characters',
INVALID_DATA: 'Incorrect email or password. Please try again.',
WRONG_CREDENTIALS: 'Email or password are incorrect',
} as const;

Expand Down
5 changes: 4 additions & 1 deletion shared/src/bundles/users/enums/user-validation-rule.enum.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const UserValidationRule = {
EMAIL_MINIMUM_LENGTH: 1,
EMAIL_MINIMUM_LENGTH: 6,
EMAIL_MAXIMUM_LENGTH: 320,
PASSWORD_MINIMUM_LENGTH: 6,
PASSWORD_MAXIMUM_LENGTH: 12,
} as const;

export { UserValidationRule };
5 changes: 4 additions & 1 deletion shared/src/bundles/users/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ export {
type UserSignUpRequestDto,
type UserSignUpResponseDto,
} from './types/types.js';
export { userSignUp as userSignUpValidationSchema } from './validation-schemas/validation-schemas.js';
export {
userSignIn as userSignInValidationSchema,
userSignUp as userSignUpValidationSchema,
} from './validation-schemas/validation-schemas.js';
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { z } from 'zod';

import { UserValidationMessage, UserValidationRule } from '../enums/enums.js';

type UserSignInRequestValidationDto = {
email: z.ZodString;
password: z.ZodString;
};

const userSignIn = z
.object<UserSignInRequestValidationDto>({
email: z
.string({ required_error: UserValidationMessage.FIELD_REQUIRE })
.trim()
.min(UserValidationRule.EMAIL_MINIMUM_LENGTH, {
message: UserValidationMessage.EMAIL_INVALID,
})
.max(UserValidationRule.EMAIL_MAXIMUM_LENGTH, {
message: UserValidationMessage.EMAIL_INVALID,
})
.email({
message: UserValidationMessage.EMAIL_INVALID,
}),
password: z
.string({ required_error: UserValidationMessage.FIELD_REQUIRE })
.trim()
.min(UserValidationRule.PASSWORD_MINIMUM_LENGTH, {
message: UserValidationMessage.PASSWORD_LENGTH,
})
.max(UserValidationRule.PASSWORD_MAXIMUM_LENGTH, {
message: UserValidationMessage.PASSWORD_LENGTH,
}),
})
.required();

export { userSignIn };
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { userSignIn } from './user-sig-in.validation-schema.js';
export { userSignUp } from './user-sign-up.validation-schema.js';
1 change: 1 addition & 0 deletions shared/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export {
type UserSignUpRequestDto,
type UserSignUpResponseDto,
UsersApiPath,
userSignInValidationSchema,
userSignUpValidationSchema,
UserValidationMessage,
} from './bundles/users/users.js';
Expand Down

0 comments on commit 9139197

Please sign in to comment.