Skip to content

Commit 7ed6634

Browse files
authored
fix: types for the 'validate' property across fields so internal validation functions can be reused (#7394)
Fixes the types for validate functions so that internal validation functions can be re-used Currently this has a type error ```ts validate: (value, args) => { return text(value, args) }, ```
1 parent 09a0ee3 commit 7ed6634

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

packages/payload/src/fields/config/types.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,12 @@ export type NumberField = {
266266
/** Set a value for the number field to increment / decrement using browser controls. */
267267
step?: number
268268
} & Admin
269-
/** Maximum value accepted. Used in the default `validation` function. */
269+
/** Maximum value accepted. Used in the default `validate` function. */
270270
max?: number
271-
/** Minimum value accepted. Used in the default `validation` function. */
271+
/** Minimum value accepted. Used in the default `validate` function. */
272272
min?: number
273273
type: 'number'
274+
validate?: Validate<number | number[], unknown, unknown, NumberField>
274275
} & (
275276
| {
276277
/** Makes this field an ordered array of numbers instead of just a single number. */
@@ -306,6 +307,7 @@ export type TextField = {
306307
maxLength?: number
307308
minLength?: number
308309
type: 'text'
310+
validate?: Validate<string | string[], unknown, unknown, TextField>
309311
} & (
310312
| {
311313
/** Makes this field an ordered array of strings instead of just a single string. */
@@ -338,6 +340,7 @@ export type EmailField = {
338340
placeholder?: Record<string, string> | string
339341
} & Admin
340342
type: 'email'
343+
validate?: Validate<string, unknown, unknown, EmailField>
341344
} & FieldBase
342345

343346
export type TextareaField = {
@@ -355,6 +358,7 @@ export type TextareaField = {
355358
maxLength?: number
356359
minLength?: number
357360
type: 'textarea'
361+
validate?: Validate<string, unknown, unknown, TextareaField>
358362
} & FieldBase
359363

360364
export type CheckboxField = {
@@ -367,6 +371,7 @@ export type CheckboxField = {
367371
}
368372
} & Admin
369373
type: 'checkbox'
374+
validate?: Validate<unknown, unknown, unknown, CheckboxField>
370375
} & FieldBase
371376

372377
export type DateField = {
@@ -381,6 +386,7 @@ export type DateField = {
381386
placeholder?: Record<string, string> | string
382387
} & Admin
383388
type: 'date'
389+
validate?: Validate<unknown, unknown, unknown, DateField>
384390
} & FieldBase
385391

386392
export type GroupField = {
@@ -396,15 +402,16 @@ export type GroupField = {
396402
*/
397403
interfaceName?: string
398404
type: 'group'
399-
} & Omit<FieldBase, 'required' | 'validation'>
405+
validate?: Validate<unknown, unknown, unknown, GroupField>
406+
} & Omit<FieldBase, 'required'>
400407

401408
export type RowAdmin = Omit<Admin, 'description'>
402409

403410
export type RowField = {
404411
admin?: RowAdmin
405412
fields: Field[]
406413
type: 'row'
407-
} & Omit<FieldBase, 'admin' | 'label' | 'name'>
414+
} & Omit<FieldBase, 'admin' | 'label' | 'name' | 'validate'>
408415

409416
export type CollapsibleField = {
410417
fields: Field[]
@@ -426,7 +433,7 @@ export type CollapsibleField = {
426433
label: Required<FieldBase['label']>
427434
}
428435
) &
429-
Omit<FieldBase, 'label' | 'name'>
436+
Omit<FieldBase, 'label' | 'name' | 'validate'>
430437

431438
export type TabsAdmin = Omit<Admin, 'description'>
432439

@@ -435,7 +442,7 @@ type TabBase = {
435442
fields: Field[]
436443
interfaceName?: string
437444
saveToJWT?: boolean | string
438-
} & Omit<FieldBase, 'required' | 'validation'>
445+
} & Omit<FieldBase, 'required' | 'validate'>
439446

440447
export type NamedTab = {
441448
/** Customize generated GraphQL and Typescript schema names.
@@ -521,6 +528,7 @@ export type UploadField = {
521528
maxDepth?: number
522529
relationTo: CollectionSlug
523530
type: 'upload'
531+
validate?: Validate<unknown, unknown, unknown, UploadField>
524532
} & FieldBase
525533

526534
type CodeAdmin = {
@@ -537,6 +545,7 @@ export type CodeField = {
537545
maxLength?: number
538546
minLength?: number
539547
type: 'code'
548+
validate?: Validate<string, unknown, unknown, CodeField>
540549
} & Omit<FieldBase, 'admin'>
541550

542551
type JSONAdmin = {
@@ -555,6 +564,7 @@ export type JSONField = {
555564
uri: string
556565
}
557566
type: 'json'
567+
validate?: Validate<Record<string, unknown>, unknown, unknown, JSONField>
558568
} & Omit<FieldBase, 'admin'>
559569

560570
export type SelectField = {
@@ -577,6 +587,7 @@ export type SelectField = {
577587
hasMany?: boolean
578588
options: Option[]
579589
type: 'select'
590+
validate?: Validate<string, unknown, unknown, SelectField>
580591
} & FieldBase
581592

582593
type SharedRelationshipProperties = {
@@ -589,6 +600,7 @@ type SharedRelationshipProperties = {
589600
*/
590601
maxDepth?: number
591602
type: 'relationship'
603+
validate?: Validate<unknown, unknown, unknown, SharedRelationshipProperties>
592604
} & (
593605
| {
594606
hasMany: true
@@ -627,12 +639,14 @@ type RelationshipAdmin = {
627639
}
628640
isSortable?: boolean
629641
} & Admin
642+
630643
export type PolymorphicRelationshipField = {
631644
admin?: {
632645
sortOptions?: { [collectionSlug: CollectionSlug]: string }
633646
} & RelationshipAdmin
634647
relationTo: CollectionSlug[]
635648
} & SharedRelationshipProperties
649+
636650
export type SingleRelationshipField = {
637651
admin?: {
638652
sortOptions?: string
@@ -707,6 +721,7 @@ export type ArrayField = {
707721
maxRows?: number
708722
minRows?: number
709723
type: 'array'
724+
validate?: Validate<unknown[], unknown, unknown, ArrayField>
710725
} & FieldBase
711726

712727
export type RadioField = {
@@ -727,6 +742,7 @@ export type RadioField = {
727742
enumName?: DBIdentifierName
728743
options: Option[]
729744
type: 'radio'
745+
validate?: Validate<string, unknown, unknown, RadioField>
730746
} & FieldBase
731747

732748
export type Block = {
@@ -781,10 +797,12 @@ export type BlockField = {
781797
maxRows?: number
782798
minRows?: number
783799
type: 'blocks'
800+
validate?: Validate<string, unknown, unknown, BlockField>
784801
} & FieldBase
785802

786803
export type PointField = {
787804
type: 'point'
805+
validate?: Validate<unknown, unknown, unknown, PointField>
788806
} & FieldBase
789807

790808
export type Field =

packages/payload/src/fields/hooks/beforeChange/promise.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ export const promise = async ({
121121
}
122122

123123
// Validate
124-
if (!skipValidationFromHere && field.validate) {
124+
if (!skipValidationFromHere && 'validate' in field && field.validate) {
125125
const valueToValidate = siblingData[field.name]
126126
let jsonError: object
127127

0 commit comments

Comments
 (0)