diff --git a/src/admin/components/forms/Form/buildStateFromSchema.ts b/src/admin/components/forms/Form/buildStateFromSchema.ts index 6be49cb2550..b23911b308e 100644 --- a/src/admin/components/forms/Form/buildStateFromSchema.ts +++ b/src/admin/components/forms/Form/buildStateFromSchema.ts @@ -9,7 +9,7 @@ import { } from '../../../../fields/config/types'; import { Fields, Field, Data } from './types'; -const buildValidationPromise = async (fieldState: Field, options: ValidateOptions) => { +const buildValidationPromise = async (fieldState: Field, options: ValidateOptions) => { const validatedFieldState = fieldState; let validationResult: boolean | string = true; diff --git a/src/admin/components/views/collections/Edit/Auth/APIKey.tsx b/src/admin/components/views/collections/Edit/Auth/APIKey.tsx index 85436dab10a..71c99d782c5 100644 --- a/src/admin/components/views/collections/Edit/Auth/APIKey.tsx +++ b/src/admin/components/views/collections/Edit/Auth/APIKey.tsx @@ -10,7 +10,7 @@ import GenerateConfirmation from '../../../../elements/GenerateConfirmation'; const path = 'apiKey'; const baseClass = 'api-key'; -const validate = (val) => text(val, { field: { minLength: 24, maxLength: 48 }, data: {}, siblingData: {} }); +const validate = (val) => text(val, { minLength: 24, maxLength: 48, data: {}, siblingData: {} }); const APIKey: React.FC = () => { const [initialAPIKey, setInitialAPIKey] = useState(null); diff --git a/src/fields/config/sanitize.ts b/src/fields/config/sanitize.ts index 9ceae2fe52a..5a4dbf23550 100644 --- a/src/fields/config/sanitize.ts +++ b/src/fields/config/sanitize.ts @@ -43,7 +43,7 @@ const sanitizeFields = (fields, validRelationships: string[]) => { if (typeof field.validate === 'undefined') { const defaultValidate = validations[field.type]; if (defaultValidate) { - field.validate = (val) => (defaultValidate(val, { field })); + field.validate = defaultValidate; } else { field.validate = () => true; } diff --git a/src/fields/config/types.ts b/src/fields/config/types.ts index deb8116e435..b25a1aa2359 100644 --- a/src/fields/config/types.ts +++ b/src/fields/config/types.ts @@ -59,7 +59,7 @@ export type ValidateOptions = { operation?: Operation } & F; -export type Validate = (value?: T, options?: ValidateOptions) => string | true | Promise; +export type Validate = (value?: T, options?: ValidateOptions>) => string | true | Promise; export type OptionObject = { label: string diff --git a/src/fields/validations.spec.ts b/src/fields/validations.spec.ts index be9a9fde613..f8cde31d18c 100644 --- a/src/fields/validations.spec.ts +++ b/src/fields/validations.spec.ts @@ -6,10 +6,6 @@ const maxLengthMessage = (length: number) => `This value must be shorter than th const requiredMessage = 'This field is required.'; let options: ValidateOptions = { operation: 'create', - field: { - type: 'text', - name: 'text', - }, data: undefined, siblingData: undefined, }; @@ -23,7 +19,7 @@ describe('Field Validations', () => { }); it('should show required message', () => { const val = undefined; - const result = text(val, { ...options, field: { ...options.field, required: true } }); + const result = text(val, { ...options, required: true }); expect(result).toBe(requiredMessage); }); it('should handle undefined', () => { @@ -33,22 +29,22 @@ describe('Field Validations', () => { }); it('should validate maxLength', () => { const val = 'toolong'; - const result = text(val, { ...options, field: { ...options.field, maxLength: 5 } }); + const result = text(val, { ...options, maxLength: 5 }); expect(result).toBe(maxLengthMessage(5)); }); it('should validate minLength', () => { const val = 'short'; - const result = text(val, { ...options, field: { ...options.field, minLength: 10 } }); + const result = text(val, { ...options, minLength: 10 }); expect(result).toBe(minLengthMessage(10)); }); it('should validate maxLength with no value', () => { const val = undefined; - const result = text(val, { ...options, field: { ...options.field, maxLength: 5 } }); + const result = text(val, { ...options, maxLength: 5 }); expect(result).toBe(true); }); it('should validate minLength with no value', () => { const val = undefined; - const result = text(val, { ...options, field: { ...options.field, minLength: 10 } }); + const result = text(val, { ...options, minLength: 10 }); expect(result).toBe(true); }); }); @@ -62,7 +58,7 @@ describe('Field Validations', () => { }); it('should show required message', () => { const val = undefined; - const result = textarea(val, { ...options, field: { ...options.field, required: true } }); + const result = textarea(val, { ...options, required: true }); expect(result).toBe(requiredMessage); }); @@ -73,29 +69,30 @@ describe('Field Validations', () => { }); it('should validate maxLength', () => { const val = 'toolong'; - const result = textarea(val, { ...options, field: { ...options.field, maxLength: 5 } }); + const result = textarea(val, { ...options, maxLength: 5 }); expect(result).toBe(maxLengthMessage(5)); }); it('should validate minLength', () => { const val = 'short'; - const result = textarea(val, { ...options, field: { ...options.field, minLength: 10 } }); + const result = textarea(val, { ...options, minLength: 10 }); expect(result).toBe(minLengthMessage(10)); }); it('should validate maxLength with no value', () => { const val = undefined; - const result = textarea(val, { ...options, field: { ...options.field, maxLength: 5 } }); + const result = textarea(val, { ...options, maxLength: 5 }); expect(result).toBe(true); }); it('should validate minLength with no value', () => { const val = undefined; - const result = textarea(val, { ...options, field: { ...options.field, minLength: 10 } }); + const result = textarea(val, { ...options, minLength: 10 }); expect(result).toBe(true); }); }); describe('password', () => { - options.field = { type: 'password', name: 'test' }; + options.type = 'password'; + options.name = 'test'; it('should validate', () => { const val = 'test'; const result = password(val, options); @@ -103,7 +100,7 @@ describe('Field Validations', () => { }); it('should show required message', () => { const val = undefined; - const result = password(val, { ...options, field: { ...options.field, required: true } }); + const result = password(val, { ...options, required: true }); expect(result).toBe(requiredMessage); }); it('should handle undefined', () => { @@ -113,60 +110,52 @@ describe('Field Validations', () => { }); it('should validate maxLength', () => { const val = 'toolong'; - const result = password(val, { ...options, field: { ...options.field, maxLength: 5 } }); + const result = password(val, { ...options, maxLength: 5 }); expect(result).toBe(maxLengthMessage(5)); }); it('should validate minLength', () => { const val = 'short'; - const result = password(val, { ...options, field: { ...options.field, minLength: 10 } }); + const result = password(val, { ...options, minLength: 10 }); expect(result).toBe(minLengthMessage(10)); }); it('should validate maxLength with no value', () => { const val = undefined; - const result = password(val, { ...options, field: { ...options.field, maxLength: 5 } }); + const result = password(val, { ...options, maxLength: 5 }); expect(result).toBe(true); }); it('should validate minLength with no value', () => { const val = undefined; - const result = password(val, { ...options, field: { ...options.field, minLength: 10 } }); + const result = password(val, { ...options, minLength: 10 }); expect(result).toBe(true); }); }); describe('select', () => { - options.field = { - type: 'select', - options: ['one', 'two', 'three'], - }; + options.type = 'select'; + options.options = ['one', 'two', 'three']; const optionsRequired = { ...options, - field: { - ...options.field, - required: true, - options: [{ - value: 'one', - label: 'One', - }, { - value: 'two', - label: 'two', - }, { - value: 'three', - label: 'three', - }], - }, + required: true, + options: [{ + value: 'one', + label: 'One', + }, { + value: 'two', + label: 'two', + }, { + value: 'three', + label: 'three', + }], }; const optionsWithEmptyString = { ...options, - field: { - ...options.field, - options: [{ - value: '', - label: 'None', - }, { - value: 'option', - label: 'Option', - }], - }, + options: [{ + value: '', + label: 'None', + }, { + value: 'option', + label: 'Option', + }], }; it('should allow valid input', () => { const val = 'one'; @@ -204,35 +193,35 @@ describe('Field Validations', () => { }); it('should prevent undefined input with required and hasMany', () => { let val; - options.field.hasMany = true; + options.hasMany = true; const result = select(val, optionsRequired); expect(result).not.toStrictEqual(true); }); it('should prevent empty array input with required and hasMany', () => { - optionsRequired.field.hasMany = true; + optionsRequired.hasMany = true; const result = select([], optionsRequired); expect(result).not.toStrictEqual(true); }); it('should prevent empty string array input with required and hasMany', () => { - options.field.hasMany = true; + options.hasMany = true; const result = select([''], optionsRequired); expect(result).not.toStrictEqual(true); }); it('should prevent null input with required and hasMany', () => { const val = null; - options.field.hasMany = true; + options.hasMany = true; const result = select(val, optionsRequired); expect(result).not.toStrictEqual(true); }); it('should allow valid input with option objects', () => { const val = 'one'; - options.field.hasMany = false; + options.hasMany = false; const result = select(val, optionsRequired); expect(result).toStrictEqual(true); }); it('should prevent invalid input with option objects', () => { const val = 'bad'; - options.field.hasMany = false; + options.hasMany = false; const result = select(val, optionsRequired); expect(result).not.toStrictEqual(true); }); @@ -243,7 +232,7 @@ describe('Field Validations', () => { }); it('should allow empty string input with option object and required', () => { const val = ''; - optionsWithEmptyString.field.required = true; + optionsWithEmptyString.required = true; const result = select(val, optionsWithEmptyString); expect(result).toStrictEqual(true); }); @@ -259,13 +248,13 @@ describe('Field Validations', () => { }); it('should allow valid input with hasMany option objects', () => { const val = ['one', 'three']; - optionsRequired.field.hasMany = true; + optionsRequired.hasMany = true; const result = select(val, optionsRequired); expect(result).toStrictEqual(true); }); it('should prevent invalid input with hasMany option objects', () => { const val = ['three', 'bad']; - optionsRequired.field.hasMany = true; + optionsRequired.hasMany = true; const result = select(val, optionsRequired); expect(result).not.toStrictEqual(true); });