From 0ff52eeb757a4c1a9484ab70841bd08566d34668 Mon Sep 17 00:00:00 2001 From: Birk Johansson Date: Thu, 16 Jan 2025 14:56:01 +0100 Subject: [PATCH 1/2] 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 40c695f59..a9be5c31d 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 0b745d686..9abf33dd0 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 e3f227302..6c7405dd9 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 }, }) From 7527246bf4d193bb957ffefca877a8773352d326 Mon Sep 17 00:00:00 2001 From: "@dhis2-bot" Date: Thu, 16 Jan 2025 14:11:29 +0000 Subject: [PATCH 2/2] chore(release): cut 0.23.1 [skip release] ## [0.23.1](https://github.com/dhis2/maintenance-app-beta/compare/v0.23.0...v0.23.1) (2025-01-16) ### Bug Fixes * **validation:** use dynamic schema validator for common fields ([#494](https://github.com/dhis2/maintenance-app-beta/issues/494)) ([0ff52ee](https://github.com/dhis2/maintenance-app-beta/commit/0ff52eeb757a4c1a9484ab70841bd08566d34668)) --- CHANGELOG.md | 7 +++++++ package.json | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e4d92b3f6..4c4e09f78 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## [0.23.1](https://github.com/dhis2/maintenance-app-beta/compare/v0.23.0...v0.23.1) (2025-01-16) + + +### Bug Fixes + +* **validation:** use dynamic schema validator for common fields ([#494](https://github.com/dhis2/maintenance-app-beta/issues/494)) ([0ff52ee](https://github.com/dhis2/maintenance-app-beta/commit/0ff52eeb757a4c1a9484ab70841bd08566d34668)) + # [0.23.0](https://github.com/dhis2/maintenance-app-beta/compare/v0.22.0...v0.23.0) (2025-01-15) diff --git a/package.json b/package.json index e4c6ee742..5e58c197b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "maintenance-app", - "version": "0.23.0", + "version": "0.23.1", "description": "", "license": "BSD-3-Clause", "private": true,