Skip to content

Commit

Permalink
refactor(compose async validators): rename "Validator" to "FormFieldV…
Browse files Browse the repository at this point in the history
…alidator"
  • Loading branch information
Mohammer5 committed Jan 8, 2024
1 parent 703c282 commit 5b905e0
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/lib/models/useCheckMaxLengthFromSchema.ts
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
7 changes: 5 additions & 2 deletions src/lib/models/useIsFieldValueUnique.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function useIsFieldValueUnique({

const memoized = useMemo(
() =>
memoize(async (value: string) => {
memoize(async (value?: string) => {
if (!value) {
return undefined
}
Expand All @@ -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 })
}
9 changes: 3 additions & 6 deletions src/pages/dataElements/fields/NameField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, FormValues>

function useValidator() {
const params = useParams()
const dataElementId = params.id as string
Expand All @@ -25,14 +22,14 @@ function useValidator() {
})

const checkMaxLength = useCheckMaxLengthFromSchema(
'dataElement' as SchemaName,
SchemaName.dataElement,
'name'
)

return useMemo(
() =>
composeAsyncValidators<string, FormValues>([
checkIsValueTaken as NameValidator,
checkIsValueTaken,
checkMaxLength,
required,
]),
Expand Down
5 changes: 1 addition & 4 deletions src/pages/dataElements/fields/ShortNameField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, FormValues>

function useValidator() {
const params = useParams()
const dataElementId = params.id as string
Expand All @@ -32,7 +29,7 @@ function useValidator() {
return useMemo(
() =>
composeAsyncValidators<string, FormValues>([
checkIsValueTaken as ShortNameValidator,
checkIsValueTaken,
checkMaxLength,
required,
]),
Expand Down

0 comments on commit 5b905e0

Please sign in to comment.