diff --git a/src/lib/models/useCheckMaxLengthFromSchema.ts b/src/lib/models/useCheckMaxLengthFromSchema.ts index 3d9a50ca..777e4b6c 100644 --- a/src/lib/models/useCheckMaxLengthFromSchema.ts +++ b/src/lib/models/useCheckMaxLengthFromSchema.ts @@ -1,14 +1,14 @@ import { createMaxCharacterLength } from '@dhis2/ui' import { useMemo } from 'react' import { SchemaName } from '../../types' -import { useSchemas } from '../schemas' +import { useSchema } from '../schemas' export function useCheckMaxLengthFromSchema( model: SchemaName, property: string ) { - const schemas = useSchemas() - const maxLength = schemas[model].properties[property].length as number + const schema = useSchema(model) + const maxLength = schema.properties[property].length as number const checkMaxLength = useMemo( () => createMaxCharacterLength(maxLength), [maxLength] diff --git a/src/lib/models/useIsFieldValueUnique.ts b/src/lib/models/useIsFieldValueUnique.ts index 3709c48e..d0b263ad 100644 --- a/src/lib/models/useIsFieldValueUnique.ts +++ b/src/lib/models/useIsFieldValueUnique.ts @@ -38,7 +38,7 @@ export function useIsFieldValueUnique({ const memoized = useMemo( () => - memoize(async (value: string) => { + memoize(async (value?: string) => { if (!value) { return undefined } @@ -59,7 +59,10 @@ export function useIsFieldValueUnique({ // Doing it this way to prevent extra arguments to be passed. // The "allValues" argument changes for every changed value and therefore // circumvents memoization - const validate = useCallback((value: string) => memoized(value), [memoized]) + const validate = useCallback( + (value?: string) => memoized(value), + [memoized] + ) return useDebouncedCallback(validate, 200, { leading: true }) } diff --git a/src/pages/dataElements/fields/NameField.tsx b/src/pages/dataElements/fields/NameField.tsx index f17152f3..b6c38724 100644 --- a/src/pages/dataElements/fields/NameField.tsx +++ b/src/pages/dataElements/fields/NameField.tsx @@ -9,12 +9,9 @@ import { useCheckMaxLengthFromSchema, useIsFieldValueUnique, } from '../../../lib' -import type { Validator } from '../../../lib' -import type { SchemaName } from '../../../types' +import { SchemaName } from '../../../types' import type { FormValues } from '../form' -type NameValidator = Validator - function useValidator() { const params = useParams() const dataElementId = params.id as string @@ -25,14 +22,14 @@ function useValidator() { }) const checkMaxLength = useCheckMaxLengthFromSchema( - 'dataElement' as SchemaName, + SchemaName.dataElement, 'name' ) return useMemo( () => composeAsyncValidators([ - checkIsValueTaken as NameValidator, + checkIsValueTaken, checkMaxLength, required, ]), diff --git a/src/pages/dataElements/fields/ShortNameField.tsx b/src/pages/dataElements/fields/ShortNameField.tsx index ee237b87..7e1845d5 100644 --- a/src/pages/dataElements/fields/ShortNameField.tsx +++ b/src/pages/dataElements/fields/ShortNameField.tsx @@ -9,12 +9,9 @@ import { useCheckMaxLengthFromSchema, useIsFieldValueUnique, } from '../../../lib' -import type { Validator } from '../../../lib' import type { SchemaName } from '../../../types' import type { FormValues } from '../form' -type ShortNameValidator = Validator - function useValidator() { const params = useParams() const dataElementId = params.id as string @@ -32,7 +29,7 @@ function useValidator() { return useMemo( () => composeAsyncValidators([ - checkIsValueTaken as ShortNameValidator, + checkIsValueTaken, checkMaxLength, required, ]),