Skip to content

Commit

Permalink
Merge pull request #72 from donghquinn/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
donghquinn authored Mar 8, 2024
2 parents 7be839c + e214e2a commit 898bb1b
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 10 deletions.
2 changes: 2 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ const config: JestConfigWithTsJest = {
testEnvironment: 'node',
verbose: true,
moduleNameMapper: {
'^@errors/(.*)$': 'errors/$1',
'^@utils/(.*)$': 'utils/$1',
'^@libraries/(.*)$': 'libraries/$1',
'^@validators/(.*)$': 'validators/$1',
},
transform: {
'^.+\\.ts?$': [
Expand Down
4 changes: 2 additions & 2 deletions src/validators/client/login.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { z } from 'zod';
export const clientLoginValidator = async (request: ClientLoginRequest) => {
try {
const scheme = z.object({
email: z.string(),
password: z.string(),
email: z.string().email("It's Not Email Format"),
password: z.string().min(5, 'Password is Too Short'),
});

const parsed = await scheme.parseAsync(request);
Expand Down
13 changes: 9 additions & 4 deletions src/validators/client/search.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { z } from 'zod';

export const searchEmailRequestValidator = async (request: SearchEmailRequest) => {
try {
const scheme = z.object({ name: z.string() });
const scheme = z.object({ name: z.string().min(1) });

const parse = await scheme.parseAsync(request);

Expand All @@ -31,7 +31,7 @@ export const searchEmailRequestValidator = async (request: SearchEmailRequest) =

export const searchPasswordRequestValidator = async (request: SearchPasswordRequest) => {
try {
const scheme = z.object({ email: z.string(), name: z.string() });
const scheme = z.object({ email: z.string().email('It should be Email Format'), name: z.string().min(1) });

const parse = await scheme.parseAsync(request);

Expand All @@ -52,7 +52,7 @@ export const searchPasswordRequestValidator = async (request: SearchPasswordRequ

export const validatePasswordTempKeyRequestValidator = async (request: ValidatePasswordKeyRequest) => {
try {
const scheme = z.object({ tempKey: z.string() });
const scheme = z.object({ tempKey: z.string().min(5) });

const parse = await scheme.parseAsync(request);

Expand All @@ -73,7 +73,12 @@ export const validatePasswordTempKeyRequestValidator = async (request: ValidateP

export const validateSearchPasswordRequestValidator = async (request: SearchChangePasswordRequest) => {
try {
const scheme = z.object({ email: z.string(), name: z.string(), password: z.string(), newPassword: z.string() });
const scheme = z.object({
email: z.string().email('It should be Email Format'),
name: z.string().min(1),
password: z.string().min(5, 'Old Password is Too Short'),
newPassword: z.string().min(5, 'New Password is Too Short'),
});

const parse = await scheme.parseAsync(request);

Expand Down
6 changes: 3 additions & 3 deletions src/validators/client/signup.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { z } from 'zod';
export const clientSignupValidator = async (request: ClientSignupRequest) => {
try {
const scheme = z.object({
email: z.string(),
password: z.string(),
name: z.string(),
email: z.string().email("It's Not Email Format"),
password: z.string().min(5, 'Password is Too Short'),
name: z.string().min(1),
});

const parsed = await scheme.parseAsync(request);
Expand Down
6 changes: 5 additions & 1 deletion src/validators/client/user.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import { z } from 'zod';

export const changePasswordValidator = async (request: ChangePasswordRequest) => {
try {
const scheme = z.object({ email: z.string(), password: z.string(), newPassword: z.string() });
const scheme = z.object({
email: z.string().email(),
password: z.string().min(5, 'Old Password is Too Short'),
newPassword: z.string().min(5, 'New Password is Too Short'),
});

const parse = await scheme.parseAsync(request);

Expand Down
41 changes: 41 additions & 0 deletions test/validator/login.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { ValidatorError } from '@errors/validator.error';
import { clientLoginValidator } from '@validators/client/login.validator';
import { ClientLoginRequest } from 'types/client.type';

describe('Login Request Validator Test', () => {
const request1: ClientLoginRequest = {
email: '[email protected]',
password: '10957239028523adscasdc',
};

test('Login Validator for first Request', async () => {
const { email, password } = await clientLoginValidator(request1);

expect(email).toEqual('[email protected]');
expect(password).toEqual('10957239028523adscasdc');
});

test('Login Validator for Second Request; Email Format Validation', async () => {
const request2: ClientLoginRequest = {
email: 'example123',
password: '10293745820adsc832',
};
try {
await clientLoginValidator(request2);
} catch (error) {
expect(error).toBeInstanceOf(ValidatorError);
}
});

test('Login Validator for Third Request; Password Length Validation', async () => {
const request3: ClientLoginRequest = {
email: '[email protected]',
password: '132',
};
try {
await clientLoginValidator(request3);
} catch (error) {
expect(error).toBeInstanceOf(ValidatorError);
}
});
});
59 changes: 59 additions & 0 deletions test/validator/signup.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { ValidatorError } from '@errors/validator.error';
import { clientSignupValidator } from '@validators/client/signup.validator';
import { ClientSignupRequest } from 'types/client.type';

describe('Signup Request Validator Test', () => {
const request1: ClientSignupRequest = {
name: 'Daniel',
email: '[email protected]',
password: '10957239028523adscasdc',
};

test('Signup Validator for first Request', async () => {
const { email, password, name } = await clientSignupValidator(request1);

expect(email).toEqual('[email protected]');
expect(password).toEqual('10957239028523adscasdc');
expect(name).toEqual('Daniel');
});

test('Signup Validator for Second Request; Email Format Validation', async () => {
const request2: ClientSignupRequest = {
name: 'Daniel',
email: 'example123',
password: '10293745820adsc832',
};
try {
await clientSignupValidator(request2);
} catch (error) {
expect(error).toBeInstanceOf(ValidatorError);
}
});

test('Signup Validator for Third Request; Password Length Validation', async () => {
const request3: ClientSignupRequest = {
name: 'Daniel',
email: '[email protected]',
password: '132',
};
try {
await clientSignupValidator(request3);
} catch (error) {
expect(error).toBeInstanceOf(ValidatorError);
}
});

test('Signup Validator for Forth Request; Name Length Validation', async () => {
const request3: ClientSignupRequest = {
email: '[email protected]',
password: '132',
name: '',
};

try {
await clientSignupValidator(request3);
} catch (error) {
expect(error).toBeInstanceOf(ValidatorError);
}
});
});

0 comments on commit 898bb1b

Please sign in to comment.