Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: return valid as true if no validation function provided #6016

Open
wants to merge 2 commits into
base: beta
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 3 additions & 14 deletions packages/richtext-lexical/src/field/Field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { FieldLabel } from '@payloadcms/ui/forms/FieldLabel'
import { useFieldProps } from '@payloadcms/ui/forms/FieldPropsProvider'
import { useField } from '@payloadcms/ui/forms/useField'
import { withCondition } from '@payloadcms/ui/forms/withCondition'
import React, { useCallback } from 'react'
import React from 'react'
import { ErrorBoundary } from 'react-error-boundary'

import type { SanitizedClientEditorConfig } from './lexical/config/types.js'
Expand Down Expand Up @@ -47,25 +47,14 @@ const _RichText: React.FC<
width,
} = props

const memoizedValidate = useCallback(
(value, validationOptions) => {
if (typeof validate === 'function') {
return validate(value, { ...validationOptions, props, required })
}
},
// Important: do not add props to the dependencies array.
// This would cause an infinite loop and endless re-rendering.
// Removing props from the dependencies array fixed this issue: https://github.com/payloadcms/payload/issues/3709
[validate, required],
)
const { path: pathFromContext } = useFieldProps()

const fieldType = useField<SerializedEditorState>({
path: pathFromContext || pathFromProps || name,
validate: memoizedValidate,
validate,
})

const { errorMessage, initialValue, path, schemaPath, setValue, showError, value } = fieldType
const { initialValue, path, setValue, showError, value } = fieldType

const classes = [
baseClass,
Expand Down
22 changes: 3 additions & 19 deletions packages/richtext-slate/src/field/RichText.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client'

import type { FormFieldBase } from '@payloadcms/ui/fields/shared'
import type { ClientValidate, FieldBase } from 'payload/types'
import type { FieldBase } from 'payload/types'
import type { BaseEditor, BaseOperation } from 'slate'
import type { HistoryEditor } from 'slate-history'
import type { ReactEditor } from 'slate-react'
Expand All @@ -25,7 +25,6 @@ import type { ElementNode, RichTextPlugin, TextNode } from '../types.js'
import type { EnabledFeatures } from './types.js'

import { defaultRichTextValue } from '../data/defaultValue.js'
import { richTextValidate } from '../data/validation.js'
import { listTypes } from './elements/listTypes.js'
import { hotkeys } from './hotkeys.js'
import './index.scss'
Expand Down Expand Up @@ -77,7 +76,7 @@ const RichTextField: React.FC<
readOnly,
required,
style,
validate = richTextValidate,
validate,
width,
} = props

Expand All @@ -88,26 +87,11 @@ const RichTextField: React.FC<
const drawerDepth = useEditDepth()
const drawerIsOpen = drawerDepth > 1

const memoizedValidate: ClientValidate = useCallback(
(value, validationOptions) => {
if (typeof validate === 'function') {
return validate(value, {
...validationOptions,
req: {
t: i18n.t,
},
required,
})
}
},
[validate, required, i18n],
)

const { path: pathFromContext } = useFieldProps()

const { initialValue, path, schemaPath, setValue, showError, value } = useField({
path: pathFromContext || pathFromProps || name,
validate: memoizedValidate,
validate,
})

const editor = useMemo(() => {
Expand Down
15 changes: 1 addition & 14 deletions packages/ui/src/fields/Array/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,6 @@ export const _ArrayField: React.FC<ArrayFieldProps> = (props) => {

const labels = getLabels(props)

const memoizedValidate = useCallback(
(value, options) => {
// alternative locales can be null
if (!editingDefaultLocale && value === null) {
return true
}
if (typeof validate === 'function') {
return validate(value, { ...options, maxRows, minRows, required })
}
},
[maxRows, minRows, required, validate, editingDefaultLocale],
)

const { path: pathFromContext } = useFieldProps()

const {
Expand All @@ -125,7 +112,7 @@ export const _ArrayField: React.FC<ArrayFieldProps> = (props) => {
} = useField<number>({
hasRows: true,
path: pathFromContext || pathFromProps || name,
validate: memoizedValidate,
validate,
})

const addRow = useCallback(
Expand Down
15 changes: 1 addition & 14 deletions packages/ui/src/fields/Blocks/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,6 @@ const _BlocksField: React.FC<BlocksFieldProps> = (props) => {
return true
})()

const memoizedValidate = useCallback(
(value, options) => {
// alternative locales can be null
if (!editingDefaultLocale && value === null) {
return true
}
if (typeof validate === 'function') {
return validate(value, { ...options, maxRows, minRows, required })
}
},
[maxRows, minRows, required, validate, editingDefaultLocale],
)

const { path: pathFromContext } = useFieldProps()

const {
Expand All @@ -127,7 +114,7 @@ const _BlocksField: React.FC<BlocksFieldProps> = (props) => {
} = useField<number>({
hasRows: true,
path: pathFromContext || pathFromProps || name,
validate: memoizedValidate,
validate,
})

const addRow = useCallback(
Expand Down
11 changes: 1 addition & 10 deletions packages/ui/src/fields/Checkbox/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,13 @@ const CheckboxField: React.FC<CheckboxFieldProps> = (props) => {

const { uuid } = useForm()

const memoizedValidate: ClientValidate = useCallback(
(value, options) => {
if (typeof validate === 'function') {
return validate(value, { ...options, required })
}
},
[validate, required],
)

const { path: pathFromContext, readOnly: readOnlyFromContext } = useFieldProps()
const readOnly = readOnlyFromProps || readOnlyFromContext

const { path, setValue, showError, value } = useField({
disableFormData,
path: pathFromContext || pathFromProps || name,
validate: memoizedValidate,
validate,
})

const onToggle = useCallback(() => {
Expand Down
13 changes: 2 additions & 11 deletions packages/ui/src/fields/Code/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { FieldDescription } from '@payloadcms/ui/forms/FieldDescription'
import { FieldError } from '@payloadcms/ui/forms/FieldError'
import { FieldLabel } from '@payloadcms/ui/forms/FieldLabel'
import { useFieldProps } from '@payloadcms/ui/forms/FieldPropsProvider'
import React, { useCallback } from 'react'
import React from 'react'

import type { FormFieldBase } from '../shared/index.js'

Expand Down Expand Up @@ -55,21 +55,12 @@ const CodeField: React.FC<CodeFieldProps> = (props) => {
width,
} = props

const memoizedValidate = useCallback(
(value, options) => {
if (typeof validate === 'function') {
return validate(value, { ...options, required })
}
},
[validate, required],
)

const { path: pathFromContext, readOnly: readOnlyFromContext } = useFieldProps()
const readOnly = readOnlyFromProps || readOnlyFromContext

const { path, setValue, showError, value } = useField({
path: pathFromContext || pathFromProps || name,
validate: memoizedValidate,
validate,
})

return (
Expand Down
14 changes: 2 additions & 12 deletions packages/ui/src/fields/DateTime/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/* eslint-disable react/destructuring-assignment */
'use client'
import type { ClientValidate } from 'payload/types'

import { getTranslation } from '@payloadcms/translations'
import React, { useCallback } from 'react'
import React from 'react'

import { DatePickerField } from '../../elements/DatePicker/index.js'
import { FieldLabel } from '../../forms/FieldLabel/index.js'
Expand Down Expand Up @@ -58,20 +57,11 @@ const DateTimeField: React.FC<DateFieldProps> = (props) => {

const { i18n } = useTranslation()

const memoizedValidate: ClientValidate = useCallback(
(value, options) => {
if (typeof validate === 'function') {
return validate(value, { ...options, required })
}
},
[validate, required],
)

const { path: pathFromContext, readOnly: readOnlyFromContext } = useFieldProps()

const { path, setValue, showError, value } = useField<Date>({
path: pathFromContext || pathFromProps || name,
validate: memoizedValidate,
validate,
})

const readOnly = readOnlyFromProps || readOnlyFromContext
Expand Down
14 changes: 1 addition & 13 deletions packages/ui/src/fields/JSON/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use client'
import type { ClientValidate } from 'payload/types'

import React, { useCallback, useEffect, useState } from 'react'

Expand Down Expand Up @@ -53,23 +52,14 @@ const JSONFieldComponent: React.FC<JSONFieldProps> = (props) => {
} = props

const [stringValue, setStringValue] = useState<string>()
const [jsonError, setJsonError] = useState<string>()
const [hasLoadedValue, setHasLoadedValue] = useState(false)

const memoizedValidate: ClientValidate = useCallback(
(value, options) => {
if (typeof validate === 'function')
return validate(value, { ...options, jsonError, required })
},
[validate, required, jsonError],
)

const { path: pathFromContext, readOnly: readOnlyFromContext } = useFieldProps()
const readOnly = readOnlyFromProps || readOnlyFromContext

const { initialValue, path, setValue, showError, value } = useField<string>({
path: pathFromContext || pathFromProps || name,
validate: memoizedValidate,
validate,
})

const handleMount = useCallback(
Expand Down Expand Up @@ -98,10 +88,8 @@ const JSONFieldComponent: React.FC<JSONFieldProps> = (props) => {

try {
setValue(val ? JSON.parse(val) : null)
setJsonError(undefined)
} catch (e) {
setValue(val ? val : null)
setJsonError(e)
}
},
[readOnly, setValue, setStringValue],
Expand Down
11 changes: 1 addition & 10 deletions packages/ui/src/fields/Number/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,12 @@ const NumberFieldComponent: React.FC<NumberFieldProps> = (props) => {

const { i18n, t } = useTranslation()

const memoizedValidate = useCallback(
(value, options) => {
if (typeof validate === 'function') {
return validate(value, { ...options, max, min, required })
}
},
[validate, min, max, required],
)

const { path: pathFromContext, readOnly: readOnlyFromContext } = useFieldProps()
const readOnly = readOnlyFromProps || readOnlyFromContext

const { path, setValue, showError, value } = useField<number | number[]>({
path: pathFromContext || pathFromProps || name,
validate: memoizedValidate,
validate,
})

const handleChange = useCallback(
Expand Down
15 changes: 3 additions & 12 deletions packages/ui/src/fields/Password/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use client'
import type { ClientValidate, Description, Validate } from 'payload/types'
import type { Description, Validate } from 'payload/types'

import { FieldError } from '@payloadcms/ui/forms/FieldError'
import { FieldLabel } from '@payloadcms/ui/forms/FieldLabel'
import React, { useCallback } from 'react'
import React from 'react'

import type { FormFieldBase } from '../shared/index.js'

Expand Down Expand Up @@ -44,18 +44,9 @@ const PasswordField: React.FC<PasswordFieldProps> = (props) => {
width,
} = props

const memoizedValidate: ClientValidate = useCallback(
(value, options) => {
if (typeof validate === 'function') {
return validate(value, { ...options, required })
}
},
[validate, required],
)

const { formProcessing, path, setValue, showError, value } = useField({
path: pathFromProps || name,
validate: memoizedValidate,
validate,
})

return (
Expand Down
14 changes: 2 additions & 12 deletions packages/ui/src/fields/Point/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable react/destructuring-assignment */
'use client'
import type { ClientValidate, FieldBase } from 'payload/types'
import type { FieldBase } from 'payload/types'

import { getTranslation } from '@payloadcms/translations'
import React, { useCallback } from 'react'
Expand Down Expand Up @@ -44,7 +44,6 @@ const PointField: React.FC<PointFieldProps> = (props) => {
path: pathFromProps,
placeholder,
readOnly: readOnlyFromProps,
required,
step,
style,
validate,
Expand All @@ -53,15 +52,6 @@ const PointField: React.FC<PointFieldProps> = (props) => {

const { i18n, t } = useTranslation()

const memoizedValidate: ClientValidate = useCallback(
(value, options) => {
if (typeof validate === 'function') {
return validate(value, { ...options, required })
}
},
[validate, required],
)

const { path: pathFromContext, readOnly: readOnlyFromContext } = useFieldProps()
const readOnly = readOnlyFromProps || readOnlyFromContext

Expand All @@ -72,7 +62,7 @@ const PointField: React.FC<PointFieldProps> = (props) => {
value = [null, null],
} = useField<[number, number]>({
path: pathFromContext || pathFromProps || name,
validate: memoizedValidate,
validate,
})

const handleChange = useCallback(
Expand Down