mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
chore: remove validators on the frontend (#1156)
* chore: update minlength of username * remove the validator on frontend * update
This commit is contained in:
12
api/user.go
12
api/user.go
@ -67,6 +67,12 @@ func (create UserCreate) Validate() error {
|
|||||||
if len(create.Username) > 32 {
|
if len(create.Username) > 32 {
|
||||||
return fmt.Errorf("username is too long, maximum length is 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 {
|
if len(create.Nickname) > 64 {
|
||||||
return fmt.Errorf("nickname is too long, maximum length is 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 {
|
if patch.Username != nil && len(*patch.Username) > 32 {
|
||||||
return fmt.Errorf("username is too long, maximum length is 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 {
|
if patch.Nickname != nil && len(*patch.Nickname) > 64 {
|
||||||
return fmt.Errorf("nickname is too long, maximum length is 64")
|
return fmt.Errorf("nickname is too long, maximum length is 64")
|
||||||
}
|
}
|
||||||
|
@ -178,9 +178,6 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
|
|||||||
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch user request").SetInternal(err)
|
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch user request").SetInternal(err)
|
||||||
}
|
}
|
||||||
userPatch.ID = userID
|
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 != "" {
|
if userPatch.Password != nil && *userPatch.Password != "" {
|
||||||
passwordHash, err := bcrypt.GenerateFromPassword([]byte(*userPatch.Password), bcrypt.DefaultCost)
|
passwordHash, err := bcrypt.GenerateFromPassword([]byte(*userPatch.Password), bcrypt.DefaultCost)
|
||||||
@ -197,6 +194,10 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
|
|||||||
userPatch.OpenID = &openID
|
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)
|
user, err := s.Store.PatchUser(ctx, userPatch)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to patch user").SetInternal(err)
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to patch user").SetInternal(err)
|
||||||
|
@ -1,18 +1,10 @@
|
|||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { useUserStore } from "../store/module";
|
import { useUserStore } from "../store/module";
|
||||||
import { validate, ValidatorConfig } from "../helpers/validator";
|
|
||||||
import Icon from "./Icon";
|
import Icon from "./Icon";
|
||||||
import { generateDialog } from "./Dialog";
|
import { generateDialog } from "./Dialog";
|
||||||
import toastHelper from "./Toast";
|
import toastHelper from "./Toast";
|
||||||
|
|
||||||
const validateConfig: ValidatorConfig = {
|
|
||||||
minLength: 4,
|
|
||||||
maxLength: 320,
|
|
||||||
noSpace: true,
|
|
||||||
noChinese: true,
|
|
||||||
};
|
|
||||||
|
|
||||||
interface Props extends DialogProps {
|
interface Props extends DialogProps {
|
||||||
user: User;
|
user: User;
|
||||||
}
|
}
|
||||||
@ -54,12 +46,6 @@ const ChangeMemberPasswordDialog: React.FC<Props> = (props: Props) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const passwordValidResult = validate(newPassword, validateConfig);
|
|
||||||
if (!passwordValidResult.result) {
|
|
||||||
toastHelper.error(`${t("common.password")} ${t(passwordValidResult.reason as string)}`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await userStore.patchUser({
|
await userStore.patchUser({
|
||||||
id: propsUser.id,
|
id: propsUser.id,
|
||||||
|
@ -1,18 +1,10 @@
|
|||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { useUserStore } from "../store/module";
|
import { useUserStore } from "../store/module";
|
||||||
import { validate, ValidatorConfig } from "../helpers/validator";
|
|
||||||
import Icon from "./Icon";
|
import Icon from "./Icon";
|
||||||
import { generateDialog } from "./Dialog";
|
import { generateDialog } from "./Dialog";
|
||||||
import toastHelper from "./Toast";
|
import toastHelper from "./Toast";
|
||||||
|
|
||||||
const validateConfig: ValidatorConfig = {
|
|
||||||
minLength: 4,
|
|
||||||
maxLength: 320,
|
|
||||||
noSpace: true,
|
|
||||||
noChinese: true,
|
|
||||||
};
|
|
||||||
|
|
||||||
type Props = DialogProps;
|
type Props = DialogProps;
|
||||||
|
|
||||||
const ChangePasswordDialog: React.FC<Props> = ({ destroy }: Props) => {
|
const ChangePasswordDialog: React.FC<Props> = ({ destroy }: Props) => {
|
||||||
@ -51,12 +43,6 @@ const ChangePasswordDialog: React.FC<Props> = ({ destroy }: Props) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const passwordValidResult = validate(newPassword, validateConfig);
|
|
||||||
if (!passwordValidResult.result) {
|
|
||||||
toastHelper.error(`${t("common.password")} ${t(passwordValidResult.reason as string)}`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const user = userStore.getState().user as User;
|
const user = userStore.getState().user as User;
|
||||||
await userStore.patchUser({
|
await userStore.patchUser({
|
||||||
|
@ -2,20 +2,12 @@ import { isEqual } from "lodash-es";
|
|||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { useUserStore } from "../store/module";
|
import { useUserStore } from "../store/module";
|
||||||
import { validate, ValidatorConfig } from "../helpers/validator";
|
|
||||||
import { convertFileToBase64 } from "../helpers/utils";
|
import { convertFileToBase64 } from "../helpers/utils";
|
||||||
import Icon from "./Icon";
|
import Icon from "./Icon";
|
||||||
import { generateDialog } from "./Dialog";
|
import { generateDialog } from "./Dialog";
|
||||||
import toastHelper from "./Toast";
|
import toastHelper from "./Toast";
|
||||||
import UserAvatar from "./UserAvatar";
|
import UserAvatar from "./UserAvatar";
|
||||||
|
|
||||||
const validateConfig: ValidatorConfig = {
|
|
||||||
minLength: 4,
|
|
||||||
maxLength: 320,
|
|
||||||
noSpace: true,
|
|
||||||
noChinese: true,
|
|
||||||
};
|
|
||||||
|
|
||||||
type Props = DialogProps;
|
type Props = DialogProps;
|
||||||
|
|
||||||
interface State {
|
interface State {
|
||||||
@ -100,12 +92,6 @@ const UpdateAccountDialog: React.FC<Props> = ({ destroy }: Props) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const usernameValidResult = validate(state.username, validateConfig);
|
|
||||||
if (!usernameValidResult.result) {
|
|
||||||
toastHelper.error(t("common.username") + ": " + t(usernameValidResult.reason as string));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const user = userStore.getState().user as User;
|
const user = userStore.getState().user as User;
|
||||||
const userPatch: UserPatch = {
|
const userPatch: UserPatch = {
|
||||||
|
@ -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,
|
|
||||||
};
|
|
||||||
}
|
|
@ -4,7 +4,6 @@ import { useTranslation } from "react-i18next";
|
|||||||
import { useGlobalStore, useUserStore } from "../store/module";
|
import { useGlobalStore, useUserStore } from "../store/module";
|
||||||
import * as api from "../helpers/api";
|
import * as api from "../helpers/api";
|
||||||
import { absolutifyLink } from "../helpers/utils";
|
import { absolutifyLink } from "../helpers/utils";
|
||||||
import { validate, ValidatorConfig } from "../helpers/validator";
|
|
||||||
import useLoading from "../hooks/useLoading";
|
import useLoading from "../hooks/useLoading";
|
||||||
import Icon from "../components/Icon";
|
import Icon from "../components/Icon";
|
||||||
import toastHelper from "../components/Toast";
|
import toastHelper from "../components/Toast";
|
||||||
@ -12,13 +11,6 @@ import AppearanceSelect from "../components/AppearanceSelect";
|
|||||||
import LocaleSelect from "../components/LocaleSelect";
|
import LocaleSelect from "../components/LocaleSelect";
|
||||||
import "../less/auth.less";
|
import "../less/auth.less";
|
||||||
|
|
||||||
const validateConfig: ValidatorConfig = {
|
|
||||||
minLength: 4,
|
|
||||||
maxLength: 320,
|
|
||||||
noSpace: true,
|
|
||||||
noChinese: true,
|
|
||||||
};
|
|
||||||
|
|
||||||
const Auth = () => {
|
const Auth = () => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const globalStore = useGlobalStore();
|
const globalStore = useGlobalStore();
|
||||||
@ -64,18 +56,6 @@ const Auth = () => {
|
|||||||
return;
|
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 {
|
try {
|
||||||
actionBtnLoadingState.setLoading();
|
actionBtnLoadingState.setLoading();
|
||||||
await api.signin(username, password);
|
await api.signin(username, password);
|
||||||
@ -97,18 +77,6 @@ const Auth = () => {
|
|||||||
return;
|
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 {
|
try {
|
||||||
actionBtnLoadingState.setLoading();
|
actionBtnLoadingState.setLoading();
|
||||||
await api.signup(username, password);
|
await api.signup(username, password);
|
||||||
|
Reference in New Issue
Block a user