From 0ff52eeb757a4c1a9484ab70841bd08566d34668 Mon Sep 17 00:00:00 2001 From: Birk Johansson Date: Thu, 16 Jan 2025 14:56:01 +0100 Subject: [PATCH] fix(validation): use dynamic schema validator for common fields (#494) * fix(validation): use dynamic schema validator for common fields * fix(description): just characterlength validator --- .../form/fields/DescriptionField.tsx | 8 ++-- src/components/form/fields/NameField.tsx | 39 ++----------------- src/components/form/fields/ShortNameField.tsx | 39 ++----------------- 3 files changed, 12 insertions(+), 74 deletions(-) diff --git a/src/components/form/fields/DescriptionField.tsx b/src/components/form/fields/DescriptionField.tsx index 40c695f5..a9be5c31 100644 --- a/src/components/form/fields/DescriptionField.tsx +++ b/src/components/form/fields/DescriptionField.tsx @@ -2,7 +2,9 @@ import i18n from '@dhis2/d2-i18n' import { createMaxCharacterLength, TextAreaFieldFF } from '@dhis2/ui' import React from 'react' import { Field as FieldRFF } from 'react-final-form' -import { SchemaSection, useCheckMaxLengthFromSchema } from '../../../lib' +import { SchemaSection } from '../../../lib' + +const validateMaxLength = createMaxCharacterLength(2000) export function DescriptionField({ helpText, @@ -11,8 +13,6 @@ export function DescriptionField({ helpText?: string schemaSection: SchemaSection }) { - const validate = createMaxCharacterLength(2000) - return ( ) diff --git a/src/components/form/fields/NameField.tsx b/src/components/form/fields/NameField.tsx index 0b745d68..9abf33dd 100644 --- a/src/components/form/fields/NameField.tsx +++ b/src/components/form/fields/NameField.tsx @@ -1,40 +1,9 @@ import i18n from '@dhis2/d2-i18n' import { InputFieldFF } from '@dhis2/ui' -import React, { useMemo } from 'react' +import React from 'react' import { Field as FieldRFF, useField } from 'react-final-form' -import { useParams } from 'react-router-dom' -import { - composeAsyncValidators, - required, - useCheckMaxLengthFromSchema, - useIsFieldValueUnique, - SchemaSection, -} from '../../../lib' - -function useValidator({ schemaSection }: { schemaSection: SchemaSection }) { - const params = useParams() - const modelId = params.id as string - const checkIsValueTaken = useIsFieldValueUnique({ - model: schemaSection.namePlural, - field: 'name', - id: modelId, - }) - - const checkMaxLength = useCheckMaxLengthFromSchema( - schemaSection.name, - 'name' - ) - - return useMemo( - () => - composeAsyncValidators([ - checkIsValueTaken, - checkMaxLength, - required, - ]), - [checkIsValueTaken, checkMaxLength] - ) -} +import { SchemaSection } from '../../../lib' +import { useValidator } from '../../../lib/models/useFieldValidators' export function NameField({ schemaSection, @@ -43,7 +12,7 @@ export function NameField({ helpText?: string schemaSection: SchemaSection }) { - const validator = useValidator({ schemaSection }) + const validator = useValidator({ schemaSection, property: 'name' }) const { meta } = useField('name', { subscription: { validating: true }, }) diff --git a/src/components/form/fields/ShortNameField.tsx b/src/components/form/fields/ShortNameField.tsx index e3f22730..6c7405dd 100644 --- a/src/components/form/fields/ShortNameField.tsx +++ b/src/components/form/fields/ShortNameField.tsx @@ -1,40 +1,9 @@ import i18n from '@dhis2/d2-i18n' import { InputFieldFF } from '@dhis2/ui' -import React, { useMemo } from 'react' +import React from 'react' import { Field as FieldRFF, useField } from 'react-final-form' -import { useParams } from 'react-router-dom' -import { - SchemaSection, - composeAsyncValidators, - required, - useCheckMaxLengthFromSchema, - useIsFieldValueUnique, -} from '../../../lib' - -function useValidator({ schemaSection }: { schemaSection: SchemaSection }) { - const params = useParams() - const modelId = params.id as string - const checkIsValueTaken = useIsFieldValueUnique({ - model: schemaSection.namePlural, - field: 'name', - id: modelId, - }) - - const checkMaxLength = useCheckMaxLengthFromSchema( - schemaSection.name, - 'shortName' - ) - - return useMemo( - () => - composeAsyncValidators([ - checkIsValueTaken, - checkMaxLength, - required, - ]), - [checkIsValueTaken, checkMaxLength] - ) -} +import { SchemaSection } from '../../../lib' +import { useValidator } from '../../../lib/models/useFieldValidators' export function ShortNameField({ helpText, @@ -43,7 +12,7 @@ export function ShortNameField({ helpText?: string schemaSection: SchemaSection }) { - const validator = useValidator({ schemaSection }) + const validator = useValidator({ schemaSection, property: 'shortName' }) const { meta } = useField('shortName', { subscription: { validating: true }, })