From bccc089b0c93097c60f6c091af3932e99dfbd819 Mon Sep 17 00:00:00 2001 From: aXenDeveloper Date: Mon, 12 Aug 2024 09:03:29 +0200 Subject: [PATCH] perf(backend): Add null type for transform input string --- packages/backend/src/utils/text-language.ts | 22 ++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/packages/backend/src/utils/text-language.ts b/packages/backend/src/utils/text-language.ts index 5f4d40940..50a4dddd3 100644 --- a/packages/backend/src/utils/text-language.ts +++ b/packages/backend/src/utils/text-language.ts @@ -29,7 +29,9 @@ export const IsTextLanguageInput = () => { message: 'Each language must have a value', }, validator: { - validate(item: TextLanguageInput | TextLanguageInput[]) { + validate(item: TextLanguageInput | TextLanguageInput[] | null) { + if (!item) return false; + return (Array.isArray(item) ? item : [item]).every( item => item.value?.trim().length > 0, ); @@ -49,7 +51,9 @@ export const MaxLengthLanguageInput = ({ length }: { length: number }) => { message: `Each language must have a value with a maximum length of ${length}`, }, validator: { - validate(item: TextLanguageInput | TextLanguageInput[]) { + validate(item: TextLanguageInput | TextLanguageInput[] | null) { + if (!item) return true; + return (Array.isArray(item) ? item : [item]).every( item => item.value?.trim().length <= length, ); @@ -68,7 +72,9 @@ export const MinLengthLanguageInput = ({ length }: { length: number }) => { message: `Each language must have a value with a minimum length of ${length}`, }, validator: { - validate(item: TextLanguageInput | TextLanguageInput[]) { + validate(item: TextLanguageInput | TextLanguageInput[] | null) { + if (!item) return true; + return (Array.isArray(item) ? item : [item]).every( item => item.value?.trim().length >= length, ); @@ -81,7 +87,7 @@ export const MinLengthLanguageInput = ({ length }: { length: number }) => { export const TransformTextLanguageInput = ({ value, }: { - value: TextLanguageInput | TextLanguageInput[]; + value: TextLanguageInput | TextLanguageInput[] | null; }) => { if (Array.isArray(value)) { let current = value.map(item => ({ @@ -102,13 +108,19 @@ export const TransformTextLanguageInput = ({ return current; } + if (!value) return null; + return { ...value, value: value.value?.trimStart().trimEnd(), }; }; -export const TransformString = ({ value }: { value: string[] | string }) => { +export const TransformString = ({ + value, +}: { + value: string[] | string | null; +}) => { if (Array.isArray(value)) { return value.map(item => item?.trimStart().trimEnd()); }