Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace z.email method with regular expression #216

Merged
merged 1 commit into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions template/apps/api/src/resources/account/account.constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const emailRegex = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/;
export const passwordRegex = /^(?=.*[a-zA-Z])(?=.*\d)[a-zA-Z\d\W]{6,}$/g;
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import { emailService } from 'services';
import { validateMiddleware } from 'middlewares';
import { AppKoaContext, Next, AppRouter, Template } from 'types';
import { userService, User } from 'resources/user';
import { accountConstants } from 'resources/account';

const schema = z.object({
email: z.string().min(1, 'Please enter email').email('Email format is incorrect.'),
email: z.string().regex(accountConstants.emailRegex, 'Email format is incorrect.'),
});

interface ValidatedData extends z.infer<typeof schema> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import { emailService } from 'services';
import { validateMiddleware } from 'middlewares';
import { AppKoaContext, Next, AppRouter, Template } from 'types';
import { userService, User } from 'resources/user';
import { accountConstants } from 'resources/account';

const schema = z.object({
email: z.string().min(1, 'Please enter email').email('Email format is incorrect.'),
email: z.string().regex(accountConstants.emailRegex, 'Email format is incorrect.'),
});

interface ValidatedData extends z.infer<typeof schema> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import { securityUtil } from 'utils';
import { validateMiddleware } from 'middlewares';
import { AppKoaContext, Next, AppRouter } from 'types';
import { userService, User } from 'resources/user';
import { accountConstants } from 'resources/account';

const schema = z.object({
token: z.string().min(1, 'Token is required'),
password: z.string().regex(
/^(?=.*[a-zA-Z])(?=.*\d)[a-zA-Z\d\W]{6,}$/g,
'The password must contain 6 or more characters with at least one letter (a-z) and one number (0-9).',
),
password: z.string().regex(accountConstants.passwordRegex, 'The password must contain 6 or more characters with at least one letter (a-z) and one number (0-9).'),
});

interface ValidatedData extends z.infer<typeof schema> {
Expand Down
5 changes: 3 additions & 2 deletions template/apps/api/src/resources/account/actions/sign-in.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { z } from 'zod';

import { User, userService } from 'resources/user';
import { accountConstants } from 'resources/account';

import { rateLimitMiddleware, validateMiddleware } from 'middlewares';
import { securityUtil } from 'utils';
Expand All @@ -9,8 +10,8 @@ import { authService } from 'services';
import { AppKoaContext, AppRouter, Next } from 'types';

const schema = z.object({
email: z.string().min(1, 'Please enter email').email('Email format is incorrect.'),
password: z.string().min(1, 'Please enter password'),
email: z.string().regex(accountConstants.emailRegex, 'Email format is incorrect.'),
password: z.string().regex(accountConstants.passwordRegex, 'The password must contain 6 or more characters with at least one letter (a-z) and one number (0-9).'),
});

interface ValidatedData extends z.infer<typeof schema> {
Expand Down
8 changes: 3 additions & 5 deletions template/apps/api/src/resources/account/actions/sign-up.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ import { analyticsService, emailService } from 'services';
import { validateMiddleware } from 'middlewares';
import { AppKoaContext, Next, AppRouter, Template } from 'types';
import { userService, User } from 'resources/user';
import { accountConstants } from 'resources/account';

const schema = z.object({
firstName: z.string().min(1, 'Please enter First name').max(100),
lastName: z.string().min(1, 'Please enter Last name').max(100),
email: z.string().min(1, 'Please enter email').email('Email format is incorrect.'),
password: z.string().regex(
/^(?=.*[a-zA-Z])(?=.*\d)[a-zA-Z\d\W]{6,}$/g,
'The password must contain 6 or more characters with at least one letter (a-z) and one number (0-9).',
),
email: z.string().regex(accountConstants.emailRegex, 'Email format is incorrect.'),
password: z.string().regex(accountConstants.passwordRegex, 'The password must contain 6 or more characters with at least one letter (a-z) and one number (0-9).'),
});

interface ValidatedData extends z.infer<typeof schema> {
Expand Down
6 changes: 2 additions & 4 deletions template/apps/api/src/resources/account/actions/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ import { AppKoaContext, Next, AppRouter } from 'types';
import { securityUtil } from 'utils';
import { validateMiddleware } from 'middlewares';
import { userService } from 'resources/user';
import { accountConstants } from 'resources/account';

const schema = z.object({
firstName: z.string().min(1, 'Please enter First name').max(100),
lastName: z.string().min(1, 'Please enter Last name').max(100),
password: z.string().regex(
/^$|^(?=.*[a-z])(?=.*\d)[A-Za-z\d\W]{6,}$/g,
'The password must contain 6 or more characters with at least one letter (a-z) and one number (0-9).',
),
password: z.string().regex(accountConstants.passwordRegex, 'The password must contain 6 or more characters with at least one letter (a-z) and one number (0-9).'),
});

interface ValidatedData extends z.infer<typeof schema> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import config from 'config';
import { validateMiddleware } from 'middlewares';
import { AppKoaContext, AppRouter } from 'types';
import { User, userService } from 'resources/user';
import { accountConstants } from 'resources/account';

const schema = z.object({
email: z.string().min(1, 'Please enter email').email('Email format is incorrect.'),
email: z.string().regex(accountConstants.emailRegex, 'Email format is incorrect.'),
token: z.string().min(1, 'Token is required'),
});

Expand Down
2 changes: 2 additions & 0 deletions template/apps/api/src/resources/account/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import accountRoutes from './account.routes';
import * as accountConstants from './account.constants';

export {
accountRoutes,
accountConstants,
};
3 changes: 2 additions & 1 deletion template/apps/api/src/resources/user/actions/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { z } from 'zod';
import { AppKoaContext, Next, AppRouter } from 'types';
import { validateMiddleware } from 'middlewares';
import { userService } from 'resources/user';
import { accountConstants } from 'resources/account';

const schema = z.object({
firstName: z.string().min(1, 'Please enter First name').max(100),
lastName: z.string().min(1, 'Please enter Last name').max(100),
email: z.string().min(1, 'Please enter email').email('Email format is incorrect.'),
email: z.string().regex(accountConstants.emailRegex, 'Email format is incorrect.'),
});

type ValidatedData = z.infer<typeof schema>;
Expand Down
2 changes: 1 addition & 1 deletion template/apps/api/src/resources/user/user.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const schema = z.object({
firstName: z.string(),
lastName: z.string(),
fullName: z.string(),
email: z.string().email(),
email: z.string(),
passwordHash: z.string().nullable().optional(),
signupToken: z.string().nullable().optional(),
resetPasswordToken: z.string().nullable().optional(),
Expand Down
4 changes: 2 additions & 2 deletions template/apps/web/src/pages/forgot-password/index.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import { RoutePath } from 'routes';
import { handleError } from 'utils';
import { Link } from 'components';

import { accountApi } from 'resources/account';
import { accountApi, accountConstants } from 'resources/account';

const schema = z.object({
email: z.string().min(1, 'Please enter email').email('Email format is incorrect.'),
email: z.string().regex(accountConstants.emailRegex, 'Email format is incorrect.'),
});

type ForgotPasswordParams = {
Expand Down
4 changes: 2 additions & 2 deletions template/apps/web/src/pages/sign-in/index.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import { RoutePath } from 'routes';
import { handleError } from 'utils';
import { Link } from 'components';

import { accountApi } from 'resources/account';
import { accountApi, accountConstants } from 'resources/account';

const schema = z.object({
email: z.string().min(1, 'Please enter email').email('Email format is incorrect.'),
email: z.string().regex(accountConstants.emailRegex, 'Email format is incorrect.'),
password: z.string().min(1, 'Please enter password'),
});

Expand Down
9 changes: 3 additions & 6 deletions template/apps/web/src/pages/sign-up/index.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,13 @@ import { RoutePath } from 'routes';
import { handleError } from 'utils';
import { Link } from 'components';

import { accountApi } from 'resources/account';
import { accountApi, accountConstants } from 'resources/account';

const schema = z.object({
firstName: z.string().min(1, 'Please enter First name').max(100),
lastName: z.string().min(1, 'Please enter Last name').max(100),
email: z.string().min(1, 'Please enter email').email('Email format is incorrect.'),
password: z.string().regex(
/^(?=.*[a-zA-Z])(?=.*\d)[a-zA-Z\d\W]{6,}$/g,
'The password must contain 6 or more characters with at least one letter (a-z) and one number (0-9).',
),
email: z.string().regex(accountConstants.emailRegex, 'Email format is incorrect.'),
password: z.string().regex(accountConstants.passwordRegex, 'The password must contain 6 or more characters with at least one letter (a-z) and one number (0-9).'),
});

type SignUpParams = z.infer<typeof schema>;
Expand Down
2 changes: 2 additions & 0 deletions template/apps/web/src/resources/account/account.constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const emailRegex = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/;
export const passwordRegex = /^(?=.*[a-zA-Z])(?=.*\d)[a-zA-Z\d\W]{6,}$/g;
2 changes: 2 additions & 0 deletions template/apps/web/src/resources/account/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as accountApi from './account.api';
import * as accountConstants from './account.constants';

export {
accountApi,
accountConstants,
};
68 changes: 3 additions & 65 deletions template/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.