Skip to content

Commit

Permalink
chore: passing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jmikrut committed Mar 29, 2022
1 parent 2b1a33e commit 60f295b
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 61 deletions.
2 changes: 1 addition & 1 deletion src/admin/components/forms/Form/buildStateFromSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from '../../../../fields/config/types';
import { Fields, Field, Data } from './types';

const buildValidationPromise = async (fieldState: Field, options: ValidateOptions<unknown, unknown>) => {
const buildValidationPromise = async (fieldState: Field, options: ValidateOptions<unknown, unknown, unknown>) => {
const validatedFieldState = fieldState;

let validationResult: boolean | string = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/fields/config/sanitize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/fields/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export type ValidateOptions<T, S, F> = {
operation?: Operation
} & F;

export type Validate<T = any, S = any, F = any> = (value?: T, options?: ValidateOptions<F, S, F>) => string | true | Promise<string | true>;
export type Validate<T = any, S = any, F = any> = (value?: T, options?: ValidateOptions<F, S, Partial<F>>) => string | true | Promise<string | true>;

export type OptionObject = {
label: string
Expand Down
103 changes: 46 additions & 57 deletions src/fields/validations.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ const maxLengthMessage = (length: number) => `This value must be shorter than th
const requiredMessage = 'This field is required.';
let options: ValidateOptions<any, any, any> = {
operation: 'create',
field: {
type: 'text',
name: 'text',
},
data: undefined,
siblingData: undefined,
};
Expand All @@ -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', () => {
Expand All @@ -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);
});
});
Expand All @@ -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);
});

Expand All @@ -73,37 +69,38 @@ 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);
expect(result).toBe(true);
});
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', () => {
Expand All @@ -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';
Expand Down Expand Up @@ -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);
});
Expand All @@ -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);
});
Expand All @@ -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);
});
Expand Down

0 comments on commit 60f295b

Please sign in to comment.