From 57479b250a578e7bf5c8db0f7dadebde7b217534 Mon Sep 17 00:00:00 2001 From: Zeng1998 Date: Sat, 25 Feb 2023 14:59:29 +0800 Subject: [PATCH] chore: remove validators on the frontend (#1156) * chore: update minlength of username * remove the validator on frontend * update --- api/user.go | 12 +++++ server/user.go | 7 +-- .../components/ChangeMemberPasswordDialog.tsx | 14 ----- web/src/components/ChangePasswordDialog.tsx | 14 ----- web/src/components/UpdateAccountDialog.tsx | 14 ----- web/src/helpers/validator.ts | 53 ------------------- web/src/pages/Auth.tsx | 32 ----------- 7 files changed, 16 insertions(+), 130 deletions(-) delete mode 100644 web/src/helpers/validator.ts diff --git a/api/user.go b/api/user.go index cb1e1ccd..cc2c2ff6 100644 --- a/api/user.go +++ b/api/user.go @@ -67,6 +67,12 @@ func (create UserCreate) Validate() error { if len(create.Username) > 32 { return fmt.Errorf("username is too long, maximum length is 32") } + if len(create.Password) < 3 { + return fmt.Errorf("password is too short, minimum length is 6") + } + if len(create.Password) > 512 { + return fmt.Errorf("password is too long, maximum length is 512") + } if len(create.Nickname) > 64 { return fmt.Errorf("nickname is too long, maximum length is 64") } @@ -107,6 +113,12 @@ func (patch UserPatch) Validate() error { if patch.Username != nil && len(*patch.Username) > 32 { return fmt.Errorf("username is too long, maximum length is 32") } + if len(*patch.Password) < 3 { + return fmt.Errorf("password is too short, minimum length is 6") + } + if len(*patch.Password) > 512 { + return fmt.Errorf("password is too long, maximum length is 512") + } if patch.Nickname != nil && len(*patch.Nickname) > 64 { return fmt.Errorf("nickname is too long, maximum length is 64") } diff --git a/server/user.go b/server/user.go index dc806987..bb48d730 100644 --- a/server/user.go +++ b/server/user.go @@ -178,9 +178,6 @@ func (s *Server) registerUserRoutes(g *echo.Group) { return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch user request").SetInternal(err) } userPatch.ID = userID - if err := userPatch.Validate(); err != nil { - return echo.NewHTTPError(http.StatusBadRequest, "Invalid user patch format").SetInternal(err) - } if userPatch.Password != nil && *userPatch.Password != "" { passwordHash, err := bcrypt.GenerateFromPassword([]byte(*userPatch.Password), bcrypt.DefaultCost) @@ -197,6 +194,10 @@ func (s *Server) registerUserRoutes(g *echo.Group) { userPatch.OpenID = &openID } + if err := userPatch.Validate(); err != nil { + return echo.NewHTTPError(http.StatusBadRequest, "Invalid user patch format").SetInternal(err) + } + user, err := s.Store.PatchUser(ctx, userPatch) if err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to patch user").SetInternal(err) diff --git a/web/src/components/ChangeMemberPasswordDialog.tsx b/web/src/components/ChangeMemberPasswordDialog.tsx index 5101f909..d5befcf4 100644 --- a/web/src/components/ChangeMemberPasswordDialog.tsx +++ b/web/src/components/ChangeMemberPasswordDialog.tsx @@ -1,18 +1,10 @@ import { useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { useUserStore } from "../store/module"; -import { validate, ValidatorConfig } from "../helpers/validator"; import Icon from "./Icon"; import { generateDialog } from "./Dialog"; import toastHelper from "./Toast"; -const validateConfig: ValidatorConfig = { - minLength: 4, - maxLength: 320, - noSpace: true, - noChinese: true, -}; - interface Props extends DialogProps { user: User; } @@ -54,12 +46,6 @@ const ChangeMemberPasswordDialog: React.FC = (props: Props) => { return; } - const passwordValidResult = validate(newPassword, validateConfig); - if (!passwordValidResult.result) { - toastHelper.error(`${t("common.password")} ${t(passwordValidResult.reason as string)}`); - return; - } - try { await userStore.patchUser({ id: propsUser.id, diff --git a/web/src/components/ChangePasswordDialog.tsx b/web/src/components/ChangePasswordDialog.tsx index 6842c621..b2839322 100644 --- a/web/src/components/ChangePasswordDialog.tsx +++ b/web/src/components/ChangePasswordDialog.tsx @@ -1,18 +1,10 @@ import { useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { useUserStore } from "../store/module"; -import { validate, ValidatorConfig } from "../helpers/validator"; import Icon from "./Icon"; import { generateDialog } from "./Dialog"; import toastHelper from "./Toast"; -const validateConfig: ValidatorConfig = { - minLength: 4, - maxLength: 320, - noSpace: true, - noChinese: true, -}; - type Props = DialogProps; const ChangePasswordDialog: React.FC = ({ destroy }: Props) => { @@ -51,12 +43,6 @@ const ChangePasswordDialog: React.FC = ({ destroy }: Props) => { return; } - const passwordValidResult = validate(newPassword, validateConfig); - if (!passwordValidResult.result) { - toastHelper.error(`${t("common.password")} ${t(passwordValidResult.reason as string)}`); - return; - } - try { const user = userStore.getState().user as User; await userStore.patchUser({ diff --git a/web/src/components/UpdateAccountDialog.tsx b/web/src/components/UpdateAccountDialog.tsx index c4bd2ed0..9fc34927 100644 --- a/web/src/components/UpdateAccountDialog.tsx +++ b/web/src/components/UpdateAccountDialog.tsx @@ -2,20 +2,12 @@ import { isEqual } from "lodash-es"; import { useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { useUserStore } from "../store/module"; -import { validate, ValidatorConfig } from "../helpers/validator"; import { convertFileToBase64 } from "../helpers/utils"; import Icon from "./Icon"; import { generateDialog } from "./Dialog"; import toastHelper from "./Toast"; import UserAvatar from "./UserAvatar"; -const validateConfig: ValidatorConfig = { - minLength: 4, - maxLength: 320, - noSpace: true, - noChinese: true, -}; - type Props = DialogProps; interface State { @@ -100,12 +92,6 @@ const UpdateAccountDialog: React.FC = ({ destroy }: Props) => { return; } - const usernameValidResult = validate(state.username, validateConfig); - if (!usernameValidResult.result) { - toastHelper.error(t("common.username") + ": " + t(usernameValidResult.reason as string)); - return; - } - try { const user = userStore.getState().user as User; const userPatch: UserPatch = { diff --git a/web/src/helpers/validator.ts b/web/src/helpers/validator.ts deleted file mode 100644 index 7844b218..00000000 --- a/web/src/helpers/validator.ts +++ /dev/null @@ -1,53 +0,0 @@ -// Validator -// * use for validating form data - -const chineseReg = /[\u3000\u3400-\u4DBF\u4E00-\u9FFF]/; - -export interface ValidatorConfig { - // min length - minLength: number; - // max length - maxLength: number; - // no space - noSpace: boolean; - // no chinese - noChinese: boolean; -} - -export function validate(text: string, config: Partial): { result: boolean; reason?: string } { - if (config.minLength !== undefined) { - if (text.length < config.minLength) { - return { - result: false, - reason: "message.too-short", - }; - } - } - - if (config.maxLength !== undefined) { - if (text.length > config.maxLength) { - return { - result: false, - reason: "message.too-long", - }; - } - } - - if (config.noSpace && text.includes(" ")) { - return { - result: false, - reason: "message.not-allow-space", - }; - } - - if (config.noChinese && chineseReg.test(text)) { - return { - result: false, - reason: "message.not-allow-chinese", - }; - } - - return { - result: true, - }; -} diff --git a/web/src/pages/Auth.tsx b/web/src/pages/Auth.tsx index ae27e4fe..979cb0c9 100644 --- a/web/src/pages/Auth.tsx +++ b/web/src/pages/Auth.tsx @@ -4,7 +4,6 @@ import { useTranslation } from "react-i18next"; import { useGlobalStore, useUserStore } from "../store/module"; import * as api from "../helpers/api"; import { absolutifyLink } from "../helpers/utils"; -import { validate, ValidatorConfig } from "../helpers/validator"; import useLoading from "../hooks/useLoading"; import Icon from "../components/Icon"; import toastHelper from "../components/Toast"; @@ -12,13 +11,6 @@ import AppearanceSelect from "../components/AppearanceSelect"; import LocaleSelect from "../components/LocaleSelect"; import "../less/auth.less"; -const validateConfig: ValidatorConfig = { - minLength: 4, - maxLength: 320, - noSpace: true, - noChinese: true, -}; - const Auth = () => { const { t } = useTranslation(); const globalStore = useGlobalStore(); @@ -64,18 +56,6 @@ const Auth = () => { return; } - const usernameValidResult = validate(username, validateConfig); - if (!usernameValidResult.result) { - toastHelper.error(t("common.username") + ": " + t(usernameValidResult.reason as string)); - return; - } - - const passwordValidResult = validate(password, validateConfig); - if (!passwordValidResult.result) { - toastHelper.error(t("common.password") + ": " + t(passwordValidResult.reason as string)); - return; - } - try { actionBtnLoadingState.setLoading(); await api.signin(username, password); @@ -97,18 +77,6 @@ const Auth = () => { return; } - const usernameValidResult = validate(username, validateConfig); - if (!usernameValidResult.result) { - toastHelper.error(t("common.username") + ": " + t(usernameValidResult.reason as string)); - return; - } - - const passwordValidResult = validate(password, validateConfig); - if (!passwordValidResult.result) { - toastHelper.error(t("common.password") + ": " + t(passwordValidResult.reason as string)); - return; - } - try { actionBtnLoadingState.setLoading(); await api.signup(username, password);