From 9a4e693aa566ef7b1761fc3628fa7b752ddddba3 Mon Sep 17 00:00:00 2001 From: aXenDeveloper Date: Fri, 1 Mar 2024 19:38:13 +0100 Subject: [PATCH] fix(forum): Add max length for name topic to 100 characters --- .../admin/forum/database/schema/topics.ts | 2 +- .../forum/topics/create/dto/create.args.ts | 2 + .../src/types/database/text-language.type.ts | 38 +++++++++++++++++++ .../forum/topics/create/use-create-topic.ts | 10 ++++- frontend/langs/en/core.json | 2 + frontend/langs/pl/core.json | 2 + 6 files changed, 53 insertions(+), 3 deletions(-) diff --git a/backend/src/apps/admin/forum/database/schema/topics.ts b/backend/src/apps/admin/forum/database/schema/topics.ts index f60ea78d8..118d24bce 100644 --- a/backend/src/apps/admin/forum/database/schema/topics.ts +++ b/backend/src/apps/admin/forum/database/schema/topics.ts @@ -61,7 +61,7 @@ export const forum_topics_titles = pgTable( .references(() => core_languages.code, { onDelete: "cascade" }), - value: varchar("value").notNull() + value: varchar("value", { length: 100 }).notNull() }, table => ({ topic_id_idx: index("forum_topics_titles_topic_id_idx").on(table.topic_id), diff --git a/backend/src/apps/forum/topics/create/dto/create.args.ts b/backend/src/apps/forum/topics/create/dto/create.args.ts index ad73c1853..0a82b1e47 100644 --- a/backend/src/apps/forum/topics/create/dto/create.args.ts +++ b/backend/src/apps/forum/topics/create/dto/create.args.ts @@ -4,6 +4,7 @@ import { Transform } from "class-transformer"; import { IsTextLanguageInput, + MaxLengthLanguageInput, TextLanguageInput, TransformTextLanguageInput } from "@/types/database/text-language.type"; @@ -15,6 +16,7 @@ export class CreateForumTopicsArgs { @ArrayMinSize(1) @IsTextLanguageInput() @Transform(TransformTextLanguageInput) + @MaxLengthLanguageInput({ length: 100 }) @Field(() => [TextLanguageInput]) title: TextLanguageInput[]; diff --git a/backend/src/types/database/text-language.type.ts b/backend/src/types/database/text-language.type.ts index 17e831f6b..eac6eeeb4 100644 --- a/backend/src/types/database/text-language.type.ts +++ b/backend/src/types/database/text-language.type.ts @@ -38,6 +38,44 @@ export const IsTextLanguageInput = () => { }; }; +export const MaxLengthLanguageInput = ({ length }: { length: number }) => { + return (object: Record, propertyName: string) => { + registerDecorator({ + target: object.constructor, + propertyName, + options: { + message: `Each language must have a value with a maximum length of ${length}` + }, + validator: { + validate(item?: TextLanguageInput | TextLanguageInput[]) { + return (Array.isArray(item) ? item : [item]).every( + item => item.value?.trim().length <= length + ); + } + } + }); + }; +}; + +export const MinLengthLanguageInput = ({ length }: { length: number }) => { + return (object: Record, propertyName: string) => { + registerDecorator({ + target: object.constructor, + propertyName, + options: { + message: `Each language must have a value with a minimum length of ${length}` + }, + validator: { + validate(item?: TextLanguageInput | TextLanguageInput[]) { + return (Array.isArray(item) ? item : [item]).every( + item => item.value?.trim().length >= length + ); + } + } + }); + }; +}; + export const TransformTextLanguageInput = ({ value }: { diff --git a/frontend/hooks/forums/forum/topics/create/use-create-topic.ts b/frontend/hooks/forums/forum/topics/create/use-create-topic.ts index c76d065c0..b638b69c0 100644 --- a/frontend/hooks/forums/forum/topics/create/use-create-topic.ts +++ b/frontend/hooks/forums/forum/topics/create/use-create-topic.ts @@ -22,7 +22,12 @@ export const useCreateTopic = ({ forumId }: Props) => { const { convertNameToLink } = useTextLang(); const formSchema = z.object({ - title: zodInput.languageInputRequired, + title: zodInput.languageInputRequired.refine( + value => value.every(item => item.value.length <= 100), + { + message: t("errors.max_length", { length: 100 }) + } + ), content: zodInput.languageInputRequired }); @@ -31,7 +36,8 @@ export const useCreateTopic = ({ forumId }: Props) => { defaultValues: { title: [], content: [] - } + }, + mode: "onTouched" }); const onSubmit = async (values: z.infer) => { diff --git a/frontend/langs/en/core.json b/frontend/langs/en/core.json index d9e3efc7d..6a92713b0 100644 --- a/frontend/langs/en/core.json +++ b/frontend/langs/en/core.json @@ -268,6 +268,8 @@ "no_connection_api": "No connection to the API", "access_denied": "Access denied", "internal_server_error": "Internal server error", + "min_length": "This field must be at least {length} characters.", + "max_length": "This field must be less than {length} characters.", "403": "Access denied", "404": "Page not found", "500": "Internal server error", diff --git a/frontend/langs/pl/core.json b/frontend/langs/pl/core.json index c8699c03e..520a5c2a9 100644 --- a/frontend/langs/pl/core.json +++ b/frontend/langs/pl/core.json @@ -268,6 +268,8 @@ "no_connection_api": "Brak połączenia z API", "access_denied": "Brak dostępu", "internal_server_error": "Wewnętrzny błąd serwera", + "min_length": "Minimalna długość to {length} znaków.", + "max_length": "Maksymalna długość to {length} znaków.", "403": "Brak dostępu", "404": "Nie znaleziono strony", "500": "Wewnętrzny błąd serwera",