feat: adds default max length for text-based fields
* feat: Added to types.ts the default Max Field Length * feat: Added the defaultMaxFieldLength to the schema.ts * feat: applying defaultMaxFieldLength to 3 validators * feat: renamed defaultMaxFieldLength to defaultMaxTextLength , adding defaultMax and min nums * feat: validating numbers with new defaultminnum and defaultmaxnum * feat: FIXED BUG, do not return an error message on the defaultmaxnum and minnum override checks * Added test fields * Eslint compliance * feat : eslint compliacnce * Added tests, though a reasonable payload config needs to be imported to them * Removed my failed jest tests, relying on the yarn dev test instead * Increased default num max and min range to JS safe integer * Jmi suggestions * feat: removing the superfluous number max and min default * Added test for max text field Co-authored-by: Tom Do <tom@iifuture.com> Co-authored-by: TomDoFuture <108644869+TomDoFuture@users.noreply.github.com>
This commit is contained in:
@@ -5,6 +5,7 @@ export const defaults: Config = {
|
|||||||
serverURL: '',
|
serverURL: '',
|
||||||
defaultDepth: 2,
|
defaultDepth: 2,
|
||||||
maxDepth: 10,
|
maxDepth: 10,
|
||||||
|
defaultMaxTextLength: 40000,
|
||||||
collections: [],
|
collections: [],
|
||||||
globals: [],
|
globals: [],
|
||||||
endpoints: [],
|
endpoints: [],
|
||||||
|
|||||||
@@ -99,6 +99,7 @@ export default joi.object({
|
|||||||
maxDepth: joi.number()
|
maxDepth: joi.number()
|
||||||
.min(0)
|
.min(0)
|
||||||
.max(100),
|
.max(100),
|
||||||
|
defaultMaxTextLength: joi.number(),
|
||||||
csrf: joi.array()
|
csrf: joi.array()
|
||||||
.items(joi.string().allow(''))
|
.items(joi.string().allow(''))
|
||||||
.sparse(),
|
.sparse(),
|
||||||
|
|||||||
@@ -187,6 +187,7 @@ export type Config = {
|
|||||||
},
|
},
|
||||||
defaultDepth?: number;
|
defaultDepth?: number;
|
||||||
maxDepth?: number;
|
maxDepth?: number;
|
||||||
|
defaultMaxTextLength: number;
|
||||||
indexSortableFields?: boolean;
|
indexSortableFields?: boolean;
|
||||||
rateLimit?: {
|
rateLimit?: {
|
||||||
window?: number;
|
window?: number;
|
||||||
|
|||||||
@@ -325,7 +325,7 @@ describe('Field Validations', () => {
|
|||||||
const result = number(val, options);
|
const result = number(val, options);
|
||||||
expect(result).toBe(true);
|
expect(result).toBe(true);
|
||||||
});
|
});
|
||||||
it('should validate', () => {
|
it('should validate 2', () => {
|
||||||
const val = 1.5;
|
const val = 1.5;
|
||||||
const result = number(val, options);
|
const result = number(val, options);
|
||||||
expect(result).toBe(true);
|
expect(result).toBe(true);
|
||||||
@@ -336,23 +336,23 @@ describe('Field Validations', () => {
|
|||||||
expect(result).toBe(validNumberMessage);
|
expect(result).toBe(validNumberMessage);
|
||||||
});
|
});
|
||||||
it('should handle empty value', () => {
|
it('should handle empty value', () => {
|
||||||
const val = "";
|
const val = '';
|
||||||
const result = number(val, { ...options });
|
const result = number(val, { ...options });
|
||||||
expect(result).toBe(true);
|
expect(result).toBe(true);
|
||||||
});
|
});
|
||||||
it('should handle required value', () => {
|
it('should handle required value', () => {
|
||||||
const val = "";
|
const val = '';
|
||||||
const result = number(val, { ...options, required: true });
|
const result = number(val, { ...options, required: true });
|
||||||
expect(result).toBe(validNumberMessage);
|
expect(result).toBe(validNumberMessage);
|
||||||
});
|
});
|
||||||
it('should validate minValue', () => {
|
it('should validate minValue', () => {
|
||||||
const val = 2.4;
|
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));
|
expect(result).toBe(minValueMessage(val, 2.5));
|
||||||
});
|
});
|
||||||
it('should validate maxValue', () => {
|
it('should validate maxValue', () => {
|
||||||
const val = 1.25;
|
const val = 1.25;
|
||||||
const result = number(val, { ...options, max: 1});
|
const result = number(val, { ...options, max: 1 });
|
||||||
expect(result).toBe(maxValueMessage(val, 1));
|
expect(result).toBe(maxValueMessage(val, 1));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -48,7 +48,11 @@ export const number: Validate<unknown, unknown, NumberField> = (value: string, {
|
|||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const text: Validate<unknown, unknown, TextField> = (value: string, { minLength, maxLength, required }) => {
|
export const text: Validate<unknown, unknown, TextField> = (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) {
|
if (value && maxLength && value.length > maxLength) {
|
||||||
return `This value must be shorter than the max length of ${maxLength} characters.`;
|
return `This value must be shorter than the max length of ${maxLength} characters.`;
|
||||||
}
|
}
|
||||||
@@ -66,7 +70,12 @@ export const text: Validate<unknown, unknown, TextField> = (value: string, { min
|
|||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const password: Validate<unknown, unknown, TextField> = (value: string, { required, maxLength, minLength }) => {
|
export const password: Validate<unknown, unknown, TextField> = (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) {
|
if (value && maxLength && value.length > maxLength) {
|
||||||
return `This value must be shorter than the max length of ${maxLength} characters.`;
|
return `This value must be shorter than the max length of ${maxLength} characters.`;
|
||||||
}
|
}
|
||||||
@@ -93,9 +102,14 @@ export const email: Validate<unknown, unknown, EmailField> = (value: string, { r
|
|||||||
|
|
||||||
export const textarea: Validate<unknown, unknown, TextareaField> = (value: string, {
|
export const textarea: Validate<unknown, unknown, TextareaField> = (value: string, {
|
||||||
required,
|
required,
|
||||||
maxLength,
|
maxLength: fieldMaxLength,
|
||||||
minLength,
|
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) {
|
if (value && maxLength && value.length > maxLength) {
|
||||||
return `This value must be shorter than the max length of ${maxLength} characters.`;
|
return `This value must be shorter than the max length of ${maxLength} characters.`;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,6 +32,11 @@ const TextFields: CollectionConfig = {
|
|||||||
}, 1));
|
}, 1));
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: 'Override the 40k text length default (This one has 50k max length)',
|
||||||
|
type: 'text',
|
||||||
|
maxLength: 50000,
|
||||||
|
},
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user