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)
},
```
This commit is contained in:
Paul
2024-07-29 12:36:28 -04:00
committed by GitHub
parent 09a0ee3ab9
commit 7ed6634bc5
2 changed files with 25 additions and 7 deletions

View File

@@ -266,11 +266,12 @@ export type NumberField = {
/** Set a value for the number field to increment / decrement using browser controls. */ /** Set a value for the number field to increment / decrement using browser controls. */
step?: number step?: number
} & Admin } & Admin
/** Maximum value accepted. Used in the default `validation` function. */ /** Maximum value accepted. Used in the default `validate` function. */
max?: number max?: number
/** Minimum value accepted. Used in the default `validation` function. */ /** Minimum value accepted. Used in the default `validate` function. */
min?: number min?: number
type: 'number' type: 'number'
validate?: Validate<number | number[], unknown, unknown, NumberField>
} & ( } & (
| { | {
/** Makes this field an ordered array of numbers instead of just a single number. */ /** Makes this field an ordered array of numbers instead of just a single number. */
@@ -306,6 +307,7 @@ export type TextField = {
maxLength?: number maxLength?: number
minLength?: number minLength?: number
type: 'text' type: 'text'
validate?: Validate<string | string[], unknown, unknown, TextField>
} & ( } & (
| { | {
/** Makes this field an ordered array of strings instead of just a single string. */ /** Makes this field an ordered array of strings instead of just a single string. */
@@ -338,6 +340,7 @@ export type EmailField = {
placeholder?: Record<string, string> | string placeholder?: Record<string, string> | string
} & Admin } & Admin
type: 'email' type: 'email'
validate?: Validate<string, unknown, unknown, EmailField>
} & FieldBase } & FieldBase
export type TextareaField = { export type TextareaField = {
@@ -355,6 +358,7 @@ export type TextareaField = {
maxLength?: number maxLength?: number
minLength?: number minLength?: number
type: 'textarea' type: 'textarea'
validate?: Validate<string, unknown, unknown, TextareaField>
} & FieldBase } & FieldBase
export type CheckboxField = { export type CheckboxField = {
@@ -367,6 +371,7 @@ export type CheckboxField = {
} }
} & Admin } & Admin
type: 'checkbox' type: 'checkbox'
validate?: Validate<unknown, unknown, unknown, CheckboxField>
} & FieldBase } & FieldBase
export type DateField = { export type DateField = {
@@ -381,6 +386,7 @@ export type DateField = {
placeholder?: Record<string, string> | string placeholder?: Record<string, string> | string
} & Admin } & Admin
type: 'date' type: 'date'
validate?: Validate<unknown, unknown, unknown, DateField>
} & FieldBase } & FieldBase
export type GroupField = { export type GroupField = {
@@ -396,7 +402,8 @@ export type GroupField = {
*/ */
interfaceName?: string interfaceName?: string
type: 'group' type: 'group'
} & Omit<FieldBase, 'required' | 'validation'> validate?: Validate<unknown, unknown, unknown, GroupField>
} & Omit<FieldBase, 'required'>
export type RowAdmin = Omit<Admin, 'description'> export type RowAdmin = Omit<Admin, 'description'>
@@ -404,7 +411,7 @@ export type RowField = {
admin?: RowAdmin admin?: RowAdmin
fields: Field[] fields: Field[]
type: 'row' type: 'row'
} & Omit<FieldBase, 'admin' | 'label' | 'name'> } & Omit<FieldBase, 'admin' | 'label' | 'name' | 'validate'>
export type CollapsibleField = { export type CollapsibleField = {
fields: Field[] fields: Field[]
@@ -426,7 +433,7 @@ export type CollapsibleField = {
label: Required<FieldBase['label']> label: Required<FieldBase['label']>
} }
) & ) &
Omit<FieldBase, 'label' | 'name'> Omit<FieldBase, 'label' | 'name' | 'validate'>
export type TabsAdmin = Omit<Admin, 'description'> export type TabsAdmin = Omit<Admin, 'description'>
@@ -435,7 +442,7 @@ type TabBase = {
fields: Field[] fields: Field[]
interfaceName?: string interfaceName?: string
saveToJWT?: boolean | string saveToJWT?: boolean | string
} & Omit<FieldBase, 'required' | 'validation'> } & Omit<FieldBase, 'required' | 'validate'>
export type NamedTab = { export type NamedTab = {
/** Customize generated GraphQL and Typescript schema names. /** Customize generated GraphQL and Typescript schema names.
@@ -521,6 +528,7 @@ export type UploadField = {
maxDepth?: number maxDepth?: number
relationTo: CollectionSlug relationTo: CollectionSlug
type: 'upload' type: 'upload'
validate?: Validate<unknown, unknown, unknown, UploadField>
} & FieldBase } & FieldBase
type CodeAdmin = { type CodeAdmin = {
@@ -537,6 +545,7 @@ export type CodeField = {
maxLength?: number maxLength?: number
minLength?: number minLength?: number
type: 'code' type: 'code'
validate?: Validate<string, unknown, unknown, CodeField>
} & Omit<FieldBase, 'admin'> } & Omit<FieldBase, 'admin'>
type JSONAdmin = { type JSONAdmin = {
@@ -555,6 +564,7 @@ export type JSONField = {
uri: string uri: string
} }
type: 'json' type: 'json'
validate?: Validate<Record<string, unknown>, unknown, unknown, JSONField>
} & Omit<FieldBase, 'admin'> } & Omit<FieldBase, 'admin'>
export type SelectField = { export type SelectField = {
@@ -577,6 +587,7 @@ export type SelectField = {
hasMany?: boolean hasMany?: boolean
options: Option[] options: Option[]
type: 'select' type: 'select'
validate?: Validate<string, unknown, unknown, SelectField>
} & FieldBase } & FieldBase
type SharedRelationshipProperties = { type SharedRelationshipProperties = {
@@ -589,6 +600,7 @@ type SharedRelationshipProperties = {
*/ */
maxDepth?: number maxDepth?: number
type: 'relationship' type: 'relationship'
validate?: Validate<unknown, unknown, unknown, SharedRelationshipProperties>
} & ( } & (
| { | {
hasMany: true hasMany: true
@@ -627,12 +639,14 @@ type RelationshipAdmin = {
} }
isSortable?: boolean isSortable?: boolean
} & Admin } & Admin
export type PolymorphicRelationshipField = { export type PolymorphicRelationshipField = {
admin?: { admin?: {
sortOptions?: { [collectionSlug: CollectionSlug]: string } sortOptions?: { [collectionSlug: CollectionSlug]: string }
} & RelationshipAdmin } & RelationshipAdmin
relationTo: CollectionSlug[] relationTo: CollectionSlug[]
} & SharedRelationshipProperties } & SharedRelationshipProperties
export type SingleRelationshipField = { export type SingleRelationshipField = {
admin?: { admin?: {
sortOptions?: string sortOptions?: string
@@ -707,6 +721,7 @@ export type ArrayField = {
maxRows?: number maxRows?: number
minRows?: number minRows?: number
type: 'array' type: 'array'
validate?: Validate<unknown[], unknown, unknown, ArrayField>
} & FieldBase } & FieldBase
export type RadioField = { export type RadioField = {
@@ -727,6 +742,7 @@ export type RadioField = {
enumName?: DBIdentifierName enumName?: DBIdentifierName
options: Option[] options: Option[]
type: 'radio' type: 'radio'
validate?: Validate<string, unknown, unknown, RadioField>
} & FieldBase } & FieldBase
export type Block = { export type Block = {
@@ -781,10 +797,12 @@ export type BlockField = {
maxRows?: number maxRows?: number
minRows?: number minRows?: number
type: 'blocks' type: 'blocks'
validate?: Validate<string, unknown, unknown, BlockField>
} & FieldBase } & FieldBase
export type PointField = { export type PointField = {
type: 'point' type: 'point'
validate?: Validate<unknown, unknown, unknown, PointField>
} & FieldBase } & FieldBase
export type Field = export type Field =

View File

@@ -121,7 +121,7 @@ export const promise = async ({
} }
// Validate // Validate
if (!skipValidationFromHere && field.validate) { if (!skipValidationFromHere && 'validate' in field && field.validate) {
const valueToValidate = siblingData[field.name] const valueToValidate = siblingData[field.name]
let jsonError: object let jsonError: object