Skip to content

Commit

Permalink
Merge pull request #243 from aXenDeveloper/fix/topic_name_limit
Browse files Browse the repository at this point in the history
fix(forum): Add max length for name topic to 100 characters
  • Loading branch information
aXenDeveloper authored Mar 1, 2024
2 parents dd998e8 + 9a4e693 commit 47e9c38
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 3 deletions.
2 changes: 1 addition & 1 deletion backend/src/apps/admin/forum/database/schema/topics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 2 additions & 0 deletions backend/src/apps/forum/topics/create/dto/create.args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Transform } from "class-transformer";

import {
IsTextLanguageInput,
MaxLengthLanguageInput,
TextLanguageInput,
TransformTextLanguageInput
} from "@/types/database/text-language.type";
Expand All @@ -15,6 +16,7 @@ export class CreateForumTopicsArgs {
@ArrayMinSize(1)
@IsTextLanguageInput()
@Transform(TransformTextLanguageInput)
@MaxLengthLanguageInput({ length: 100 })
@Field(() => [TextLanguageInput])
title: TextLanguageInput[];

Expand Down
38 changes: 38 additions & 0 deletions backend/src/types/database/text-language.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,44 @@ export const IsTextLanguageInput = () => {
};
};

export const MaxLengthLanguageInput = ({ length }: { length: number }) => {
return (object: Record<string, any>, 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<string, any>, 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
}: {
Expand Down
10 changes: 8 additions & 2 deletions frontend/hooks/forums/forum/topics/create/use-create-topic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
});

Expand All @@ -31,7 +36,8 @@ export const useCreateTopic = ({ forumId }: Props) => {
defaultValues: {
title: [],
content: []
}
},
mode: "onTouched"
});

const onSubmit = async (values: z.infer<typeof formSchema>) => {
Expand Down
2 changes: 2 additions & 0 deletions frontend/langs/en/core.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions frontend/langs/pl/core.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 47e9c38

Please sign in to comment.