Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove lodash set when it builts fieldErrors with nested properties #93

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@
],
"dependencies": {
"es6-promise": "4.1.0",
"lodash.get": "^4.4.2",
"lodash.set": "^4.3.2"
"lodash.get": "^4.4.2"
}
}
28 changes: 12 additions & 16 deletions lib/src/spec/validationEngineValidateForm.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,14 +537,12 @@ describe('ValidationEngine Validate Form', () => {
expect(formValidationResult.succeeded).to.be.true;

expect(formValidationResult.fieldErrors).to.deep.equal({
fullname: {
firstName: {
key: 'fullname.firstName',
succeeded: true,
type: 'REQUIRED',
errorMessage: '',
} as FieldValidationResult
},
'fullname.firstName': {
key: 'fullname.firstName',
succeeded: true,
type: 'REQUIRED',
errorMessage: '',
} as FieldValidationResult,
});

done();
Expand Down Expand Up @@ -580,14 +578,12 @@ describe('ValidationEngine Validate Form', () => {
expect(formValidationResult.succeeded).to.be.false;

expect(formValidationResult.fieldErrors).to.deep.equal({
fullname: {
firstName: {
key: 'fullname.firstName',
succeeded: false,
type: 'REQUIRED',
errorMessage: 'Mandatory field',
} as FieldValidationResult
},
'fullname.firstName': {
key: 'fullname.firstName',
succeeded: false,
type: 'REQUIRED',
errorMessage: 'Mandatory field',
} as FieldValidationResult,
});

done();
Expand Down
33 changes: 33 additions & 0 deletions lib/src/spec/validationsResultBuilder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,5 +425,38 @@ describe('ValidationsResultBuilder ', () => {
expect(result.formGlobalErrors).to.have.length(1);
expect(result.formGlobalErrors[0].key).to.be.equal('_GLOBAL_FORM_');
});

it('Spec #26 => should returns new FormValidationResult equals ' +
'{ fieldErrors: { "nested.key1": { key: "nested.key1" }, test2: { key: "test2" } } } ' +
'when passing fieldValidationResults with two item first equals { key: "nested.key1" }' +
'and second equals { key: "test2" }', () => {
//Arrange
const fieldValidationResult1 = new FieldValidationResult();
fieldValidationResult1.key = 'nested.key1';

const fieldValidationResult2 = new FieldValidationResult();
fieldValidationResult2.key = 'test2';

const fieldValidationResults = [fieldValidationResult1, fieldValidationResult2];

//Act
const result = validationsResultBuilder.buildFormValidationsResult(fieldValidationResults);

//Assert
expect(result.fieldErrors).to.deep.equal({
'nested.key1': {
key: 'nested.key1',
succeeded: false,
errorMessage: '',
type: '',
} as FieldValidationResult,
test2: {
key: 'test2',
succeeded: false,
errorMessage: '',
type: '',
} as FieldValidationResult,
});
});
});
});
50 changes: 33 additions & 17 deletions lib/src/validationsResultBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,62 @@
import { FieldValidationResult, FormValidationResult } from './entities';
import { consts } from './consts';
import set from 'lodash.set';

export class ValidationsResultBuilder {
buildFormValidationsResult(fieldValidationResults: Array<FieldValidationResult>): FormValidationResult {
buildFormValidationsResult(
fieldValidationResults: Array<FieldValidationResult>
): FormValidationResult {
let formValidationResult = new FormValidationResult();

if (fieldValidationResults && fieldValidationResults.length > 0) {
let filteredFieldValidationResults = this.removeUndefinedValidationResults(fieldValidationResults);
let filteredFieldValidationResults = this.removeUndefinedValidationResults(
fieldValidationResults
);
this.setGlobalKeyToEmptyKeys(filteredFieldValidationResults);

formValidationResult.succeeded = filteredFieldValidationResults.every(fvr => fvr.succeeded);
const fieldValidationResultList = filteredFieldValidationResults.filter(fvr => fvr.key !== consts.globalFormValidationId);
formValidationResult.fieldErrors = this.mapFieldValidationResultListToFieldErrorsObject(fieldValidationResultList);
formValidationResult.formGlobalErrors = filteredFieldValidationResults.filter(fvr => fvr.key === consts.globalFormValidationId);
formValidationResult.succeeded = filteredFieldValidationResults.every(
fvr => fvr.succeeded
);
const fieldValidationResultList = filteredFieldValidationResults.filter(
fvr => fvr.key !== consts.globalFormValidationId
);
formValidationResult.fieldErrors = this.mapFieldValidationResultListToFieldErrorsObject(
fieldValidationResultList
);
formValidationResult.formGlobalErrors = filteredFieldValidationResults.filter(
fvr => fvr.key === consts.globalFormValidationId
);
}

return formValidationResult;
}

private removeUndefinedValidationResults(fieldValidationResults: Array<FieldValidationResult>): Array<FieldValidationResult> {
private removeUndefinedValidationResults(
fieldValidationResults: Array<FieldValidationResult>
): Array<FieldValidationResult> {
return fieldValidationResults.filter(
fvr => fvr !== undefined &&
fvr !== null
fvr => fvr !== undefined && fvr !== null
);
}

private setGlobalKeyToEmptyKeys(fieldValidationResults: Array<FieldValidationResult>) {
private setGlobalKeyToEmptyKeys(
fieldValidationResults: Array<FieldValidationResult>
) {
fieldValidationResults.forEach(fieldValidationResult => {
if (!fieldValidationResult.key) {
fieldValidationResult.key = consts.globalFormValidationId;
}
});
}

private mapFieldValidationResultListToFieldErrorsObject = (fieldValidationResultList: FieldValidationResult[]): { [key: string]: FieldValidationResult } => (
fieldValidationResultList.reduce((errors, result) => set(errors, result.key, result), {})
)
private mapFieldValidationResultListToFieldErrorsObject = (
fieldValidationResultList: FieldValidationResult[]
): { [key: string]: FieldValidationResult } =>
fieldValidationResultList.reduce((errors, result) => {
errors[result.key] = result;
return errors;
}, {});
}

let validationsResultBuilder = new ValidationsResultBuilder();

export {
validationsResultBuilder
}
export { validationsResultBuilder };