Skip to content

armgit / memos

Public Imported from GitHub
0

Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.

chore: remove validators on the frontend (#1156)

Zeng199857479b250a57
diff --git a/api/user.go b/api/user.go
index cb1e1ccd0..cc2c2ff67 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 dc806987d..bb48d730e 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 5101f9095..d5befcf4e 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: 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 6842c621a..b2839322c 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<Props> = ({ destroy }: Props) => {
@@ -51,12 +43,6 @@ const ChangePasswordDialog: React.FC<Props> = ({ 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 c4bd2ed08..9fc349279 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<Props> = ({ 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 7844b218e..000000000
--- 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<ValidatorConfig>): { 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 ae27e4fe7..979cb0c90 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);