Skip to content

Commit

Permalink
refactor helpers and form validation
Browse files Browse the repository at this point in the history
  • Loading branch information
nasdan committed Mar 28, 2021
1 parent 9d84e9b commit 19c186d
Show file tree
Hide file tree
Showing 4 changed files with 1,131 additions and 27 deletions.
20 changes: 14 additions & 6 deletions src/form-validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import {
} from './validation-engine';
import {
hasFieldIdArrayValidator,
getBaseFieldIdFromArrayField,
formatFieldForArrayField,
get,
} from './helpers';

export class FormValidation {
Expand Down Expand Up @@ -55,12 +56,19 @@ export class FormValidation {
value: any,
values?: any
): Promise<ValidationResult | { [fieldId: string]: ValidationResult }> => {
const id = hasFieldIdArrayValidator(fieldId, this.fieldSchema)
? getBaseFieldIdFromArrayField(fieldId)
: fieldId;
const field = hasFieldIdArrayValidator(fieldId, this.fieldSchema)
? formatFieldForArrayField(fieldId, value)
: { id: fieldId, value };

return validateField(id, value, values, this.fieldSchema).then(
mapInternalValidationResultToValidationResult
return validateField(field.id, field.value, values, this.fieldSchema).then(
(internalValidationResult) => {
const validationResult = mapInternalValidationResultToValidationResult(
internalValidationResult
);
return fieldId !== field.id
? get(validationResult, fieldId)
: validationResult;
}
);
};

Expand Down
98 changes: 80 additions & 18 deletions src/helpers/helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
reduceAsync,
isFieldIdInSchema,
hasFieldIdArrayValidator,
getBaseFieldIdFromArrayField,
formatFieldForArrayField,
} from './helpers';

describe('safeArrayLength', () => {
Expand Down Expand Up @@ -1088,59 +1088,121 @@ describe('hasFieldIdArrayValidator', () => {
});
});

describe('getBaseFieldIdFromArrayField', () => {
it('should return empty string when it feeds fieldId equals undefined', () => {
describe('formatFieldForArrayField', () => {
it('should return empty string when it feeds fieldId and value equals undefined', () => {
// Arrange
const fieldId: string = undefined;
const value: string = undefined;

// Act
const result = getBaseFieldIdFromArrayField(fieldId);
const result = formatFieldForArrayField(fieldId, value);

// Assert
expect(result).toEqual('');
const expectedResult = {
id: '',
value: undefined,
};
expect(result).toEqual(expectedResult);
});

it('should return empty string when it feeds fieldId equals null', () => {
it('should return empty string when it feeds fieldId and value equals null', () => {
// Arrange
const fieldId: string = null;
const value: string = null;

// Act
const result = getBaseFieldIdFromArrayField(fieldId);
const result = formatFieldForArrayField(fieldId, value);

// Assert
expect(result).toEqual('');
const expectedResult = {
id: '',
value: undefined,
};
expect(result).toEqual(expectedResult);
});

it('should return empty string when it feeds fieldId equals empty string', () => {
it('should return empty string when it feeds fieldId equals empty string and value equals undefined', () => {
// Arrange
const fieldId: string = '';
const value: string = undefined;

// Act
const result = getBaseFieldIdFromArrayField(fieldId);
const result = formatFieldForArrayField(fieldId, value);

// Assert
expect(result).toEqual('');
const expectedResult = {
id: '',
value: undefined,
};
expect(result).toEqual(expectedResult);
});

it('should return empty string when it feeds fieldId without array syntax', () => {
it('should return empty string when it feeds fieldId without array syntax and value equals undefined', () => {
// Arrange
const fieldId: string = 'test-field.name';
const value: string = undefined;

// Act
const result = getBaseFieldIdFromArrayField(fieldId);
const result = formatFieldForArrayField(fieldId, value);

// Assert
expect(result).toEqual('');
const expectedResult = {
id: 'test-field',
value: {
name: undefined,
},
};
expect(result).toEqual(expectedResult);
});

it('should return baseFieldId when it feeds fieldId with array syntax', () => {
it('should return baseFieldId when it feeds fieldId with array syntax and value equals undefined', () => {
// Arrange
const fieldId: string = 'test-fields[234].name';
const fieldId: string = 'test-fields[2].name';
const value: string = undefined;

// Act
const result = getBaseFieldIdFromArrayField(fieldId);
const result = formatFieldForArrayField(fieldId, value);

// Assert
expect(result).toEqual('test-fields');
const expectedResult = {
id: 'test-fields',
value: [
undefined,
undefined,
{
name: undefined,
},
],
};
expect(result).toEqual(expectedResult);
});

it('should return baseFieldId when it feeds fieldId with array syntax and value with value', () => {
// Arrange
const fieldId: string = 'test-fields[2].nested.field[1].name';
const value: string = 'test';

// Act
const result = formatFieldForArrayField(fieldId, value);

// Assert
const expectedResult = {
id: 'test-fields',
value: [
undefined,
undefined,
{
nested: {
field: [
undefined,
{
name: 'test',
},
],
},
},
],
};
expect(result).toEqual(expectedResult);
});
});
12 changes: 9 additions & 3 deletions src/helpers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
InternalFieldValidationSchema,
InternalRecordValidationSchema,
} from '../model';
import set from './set';

// TODO: Better naming for this?
export const safeArrayLength = <T>(collection: T[]) =>
Expand Down Expand Up @@ -46,7 +47,12 @@ export const hasFieldIdArrayValidator = (
schema: InternalFieldValidationSchema
): boolean => /\[.*\]/.test(fieldId) && !isFieldIdInSchema(fieldId, schema);

export const getBaseFieldIdFromArrayField = (fieldId: string) => {
const [, baseFieldId] = fieldId?.match(/(.*)\[.*\]/) || [];
return baseFieldId || '';
export const formatFieldForArrayField = (fieldId: string, value) => {
const formattedValue = set({}, fieldId, value);
const keys = Boolean(fieldId) ? Object.keys(formattedValue) : [''];
const id = Array.isArray(keys) && keys.length > 0 ? keys[0] : '';
return {
id,
value: formattedValue[id],
};
};
Loading

0 comments on commit 19c186d

Please sign in to comment.