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:
TomDo1234
2022-10-25 09:57:45 +11:00
committed by GitHub
parent 17dbbc7775
commit 6a1b25ab30
6 changed files with 30 additions and 8 deletions

View File

@@ -5,6 +5,7 @@ export const defaults: Config = {
serverURL: '',
defaultDepth: 2,
maxDepth: 10,
defaultMaxTextLength: 40000,
collections: [],
globals: [],
endpoints: [],

View File

@@ -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(),

View File

@@ -187,6 +187,7 @@ export type Config = {
},
defaultDepth?: number;
maxDepth?: number;
defaultMaxTextLength: number;
indexSortableFields?: boolean;
rateLimit?: {
window?: number;

View File

@@ -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));
});
});

View File

@@ -48,7 +48,11 @@ export const number: Validate<unknown, unknown, NumberField> = (value: string, {
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) {
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;
};
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) {
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, {
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.`;
}

View File

@@ -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,
},
],
};