diff --git a/src/config/defaults.ts b/src/config/defaults.ts index 4edd77be5..5727df3a4 100644 --- a/src/config/defaults.ts +++ b/src/config/defaults.ts @@ -5,6 +5,7 @@ export const defaults: Config = { serverURL: '', defaultDepth: 2, maxDepth: 10, + defaultMaxTextLength: 40000, collections: [], globals: [], endpoints: [], diff --git a/src/config/schema.ts b/src/config/schema.ts index 83b8c6f47..8e9c2c338 100644 --- a/src/config/schema.ts +++ b/src/config/schema.ts @@ -99,6 +99,7 @@ export default joi.object({ maxDepth: joi.number() .min(0) .max(100), + defaultMaxTextLength: joi.number(), csrf: joi.array() .items(joi.string().allow('')) .sparse(), diff --git a/src/config/types.ts b/src/config/types.ts index 4d860df41..31e0337a7 100644 --- a/src/config/types.ts +++ b/src/config/types.ts @@ -187,6 +187,7 @@ export type Config = { }, defaultDepth?: number; maxDepth?: number; + defaultMaxTextLength: number; indexSortableFields?: boolean; rateLimit?: { window?: number; diff --git a/src/fields/validations.spec.ts b/src/fields/validations.spec.ts index de09751be..eb867a635 100644 --- a/src/fields/validations.spec.ts +++ b/src/fields/validations.spec.ts @@ -325,7 +325,7 @@ describe('Field Validations', () => { const result = number(val, options); expect(result).toBe(true); }); - it('should validate', () => { + it('should validate 2', () => { const val = 1.5; const result = number(val, options); expect(result).toBe(true); @@ -336,23 +336,23 @@ describe('Field Validations', () => { expect(result).toBe(validNumberMessage); }); it('should handle empty value', () => { - const val = ""; + const val = ''; const result = number(val, { ...options }); expect(result).toBe(true); }); it('should handle required value', () => { - const val = ""; + const val = ''; const result = number(val, { ...options, required: true }); expect(result).toBe(validNumberMessage); }); it('should validate minValue', () => { const val = 2.4; - const result = number(val, { ...options, min: 2.5}); + const result = number(val, { ...options, min: 2.5 }); expect(result).toBe(minValueMessage(val, 2.5)); }); it('should validate maxValue', () => { const val = 1.25; - const result = number(val, { ...options, max: 1}); + const result = number(val, { ...options, max: 1 }); expect(result).toBe(maxValueMessage(val, 1)); }); }); diff --git a/src/fields/validations.ts b/src/fields/validations.ts index 87da3352e..141458f65 100644 --- a/src/fields/validations.ts +++ b/src/fields/validations.ts @@ -48,7 +48,11 @@ export const number: Validate = (value: string, { return true; }; -export const text: Validate = (value: string, { minLength, maxLength, required }) => { +export const text: Validate = (value: string, { minLength, maxLength: fieldMaxLength, required, payload }) => { + let maxLength: number; + + if (typeof payload?.config?.defaultMaxTextLength === 'number') maxLength = payload.config.defaultMaxTextLength; + if (typeof fieldMaxLength === 'number') maxLength = fieldMaxLength; if (value && maxLength && value.length > maxLength) { return `This value must be shorter than the max length of ${maxLength} characters.`; } @@ -66,7 +70,12 @@ export const text: Validate = (value: string, { min return true; }; -export const password: Validate = (value: string, { required, maxLength, minLength }) => { +export const password: Validate = (value: string, { required, maxLength: fieldMaxLength, minLength, payload }) => { + let maxLength: number; + + if (typeof payload?.config?.defaultMaxTextLength === 'number') maxLength = payload.config.defaultMaxTextLength; + if (typeof fieldMaxLength === 'number') maxLength = fieldMaxLength; + if (value && maxLength && value.length > maxLength) { return `This value must be shorter than the max length of ${maxLength} characters.`; } @@ -93,9 +102,14 @@ export const email: Validate = (value: string, { r export const textarea: Validate = (value: string, { required, - maxLength, + maxLength: fieldMaxLength, minLength, + payload, }) => { + let maxLength: number; + + if (typeof payload?.config?.defaultMaxTextLength === 'number') maxLength = payload.config.defaultMaxTextLength; + if (typeof fieldMaxLength === 'number') maxLength = fieldMaxLength; if (value && maxLength && value.length > maxLength) { return `This value must be shorter than the max length of ${maxLength} characters.`; } diff --git a/test/fields/collections/Text/index.ts b/test/fields/collections/Text/index.ts index 58c662d28..00e81689d 100644 --- a/test/fields/collections/Text/index.ts +++ b/test/fields/collections/Text/index.ts @@ -32,6 +32,11 @@ const TextFields: CollectionConfig = { }, 1)); }, }, + { + name: 'Override the 40k text length default (This one has 50k max length)', + type: 'text', + maxLength: 50000, + }, ], };