Skip to content

Commit 1f44dff

Browse files
committed
Merge branch 'feature/84-Improve-get-formValidationResult.fieldErrors'
2 parents a8ed619 + bd68022 commit 1f44dff

File tree

21 files changed

+305
-203
lines changed

21 files changed

+305
-203
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ lib/coverage/
1010
yarn.lock
1111
.vscode/
1212
npm-debug.log
13-
lib/package-lock\.json
13+
package-lock\.json

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ customerFormValidation
4343
console.log(validationResult.success); // true
4444
console.log(validationResult.formGlobalErrors); // []
4545
console.log(validationResult.fieldErrors);
46-
/*[
47-
{ succeeded: true, type: "REQUIRED", key: "firstName", errorMessage: "" },
48-
{ succeeded: true, type: "REQUIRED", key: "lastName", errorMessage: "" }
49-
]*/
46+
/*{
47+
firstName: { succeeded: true, type: "REQUIRED", key: "firstName", errorMessage: "" },
48+
lastName: { succeeded: true, type: "REQUIRED", key: "lastName", errorMessage: "" }
49+
}*/
5050
})
5151
.catch((error) => {
5252
// handle unexpected errors
@@ -240,7 +240,7 @@ testFormValidation
240240
.then((validationResult) => {
241241
console.log(validationResult.succeeded); // true
242242
console.log(validationResult.formGlobalErrors) // []
243-
console.log(validationResult.fieldErrors); // []
243+
console.log(validationResult.fieldErrors); // {}
244244
})
245245
.catch((error) => {
246246
// handle unexpected errors

lib/lc-form-validation.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export class FieldValidationResult {
77

88
export class FormValidationResult {
99
succeeded: boolean;
10-
fieldErrors: Array<FieldValidationResult>;
10+
fieldErrors: { [key: string]: FieldValidationResult };
1111
formGlobalErrors: Array<FieldValidationResult>;
1212
}
1313

lib/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "lc-form-validation",
3-
"version": "1.0.3",
3+
"version": "2.0.0",
44
"description": "lcFormValidation is an async form validation library heavily based on JavaScript (no HTML attributes or annotations). lcFormValidation is third party / framework agnostic so it can be easily integrated with frameworks like React.",
55
"main": "dist/lc-form-validation.js",
66
"scripts": {
@@ -84,6 +84,7 @@
8484
],
8585
"dependencies": {
8686
"es6-promise": "4.1.0",
87-
"lodash.get": "^4.4.2"
87+
"lodash.get": "^4.4.2",
88+
"lodash.set": "^4.3.2"
8889
}
8990
}

lib/src/entities.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ export class FieldValidationResult {
2424

2525
export class FormValidationResult {
2626
succeeded: boolean;
27-
fieldErrors: FieldValidationResult[];
27+
fieldErrors: { [key: string]: FieldValidationResult };
2828
formGlobalErrors: FieldValidationResult[];
2929

3030
constructor() {
3131
this.succeeded = false;
32-
this.fieldErrors = [];
32+
this.fieldErrors = {};
3333
}
3434
}
3535

lib/src/spec/validationEngineValidateForm.spec.ts

Lines changed: 168 additions & 116 deletions
Large diffs are not rendered by default.

lib/src/spec/validationsResultBuilder.spec.ts

Lines changed: 75 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ describe('ValidationsResultBuilder ', () => {
104104
expect(result.succeeded).to.be.true;
105105
});
106106

107-
it('Spec #8 => should returns new FormValidationResult equals { fieldErrors: [] } ' +
107+
it('Spec #8 => should returns new FormValidationResult equals { fieldErrors: {} } ' +
108108
'when passing fieldValidationResults equals undefined', () => {
109109
//Arrange
110110
let fieldValidationResults = undefined;
@@ -113,10 +113,10 @@ describe('ValidationsResultBuilder ', () => {
113113
let result = validationsResultBuilder.buildFormValidationsResult(fieldValidationResults);
114114

115115
//Assert
116-
expect(result.fieldErrors).to.be.empty;
116+
expect(result.fieldErrors).to.deep.equals({});
117117
});
118118

119-
it('Spec #9 => should returns new FormValidationResult equals { fieldErrors: [] } ' +
119+
it('Spec #9 => should returns new FormValidationResult equals { fieldErrors: {} } ' +
120120
'when passing fieldValidationResults equals null', () => {
121121
//Arrange
122122
let fieldValidationResults = null;
@@ -125,10 +125,10 @@ describe('ValidationsResultBuilder ', () => {
125125
let result = validationsResultBuilder.buildFormValidationsResult(fieldValidationResults);
126126

127127
//Assert
128-
expect(result.fieldErrors).to.be.empty;
128+
expect(result.fieldErrors).to.deep.equals({});
129129
});
130130

131-
it('Spec #10 => should returns new FormValidationResult equals { fieldErrors: [] } ' +
131+
it('Spec #10 => should returns new FormValidationResult equals { fieldErrors: {} } ' +
132132
'when passing fieldValidationResults equals empty', () => {
133133
//Arrange
134134
let fieldValidationResults = [];
@@ -137,11 +137,11 @@ describe('ValidationsResultBuilder ', () => {
137137
let result = validationsResultBuilder.buildFormValidationsResult(fieldValidationResults);
138138

139139
//Assert
140-
expect(result.fieldErrors).to.be.empty;
140+
expect(result.fieldErrors).to.deep.equals({});
141141
});
142142

143143
it('Spec #11 => should returns new FormValidationResult equals ' +
144-
'{ fieldErrors: [{ key: "test" }] } with length equals 1 ' +
144+
'{ fieldErrors: { test: { key: "test", succeeded: false, errorMessage: "", type: "" } } }' +
145145
'when passing fieldValidationResults with one item equals { key: "test" }', () => {
146146
//Arrange
147147
let fieldValidationResult = new FieldValidationResult();
@@ -152,12 +152,18 @@ describe('ValidationsResultBuilder ', () => {
152152
let result = validationsResultBuilder.buildFormValidationsResult(fieldValidationResults);
153153

154154
//Assert
155-
expect(result.fieldErrors).to.have.length(1);
156-
expect(result.fieldErrors[0].key).to.be.equal('test');
155+
expect(result.fieldErrors).to.deep.equal({
156+
test: {
157+
key: 'test',
158+
succeeded: false,
159+
errorMessage: '',
160+
type: '',
161+
} as FieldValidationResult,
162+
});
157163
});
158164

159165
it('Spec #12 => should returns new FormValidationResult equals ' +
160-
'{ fieldErrors: [] } with length equals 0 ' +
166+
'{ fieldErrors: {} } without properties ' +
161167
'when passing fieldValidationResults with one item equals { key: null }', () => {
162168
//Arrange
163169
let fieldValidationResult = new FieldValidationResult();
@@ -168,11 +174,11 @@ describe('ValidationsResultBuilder ', () => {
168174
let result = validationsResultBuilder.buildFormValidationsResult(fieldValidationResults);
169175

170176
//Assert
171-
expect(result.fieldErrors).to.have.length(0);
177+
expect(result.fieldErrors).to.deep.equals({});
172178
});
173179

174180
it('Spec #13 => should returns new FormValidationResult equals ' +
175-
'{ fieldErrors: [] } with length equals 0 ' +
181+
'{ fieldErrors: {} } without properties ' +
176182
'when passing fieldValidationResults with one item equals { key: undefined }', () => {
177183
//Arrange
178184
let fieldValidationResult = new FieldValidationResult();
@@ -183,11 +189,11 @@ describe('ValidationsResultBuilder ', () => {
183189
let result = validationsResultBuilder.buildFormValidationsResult(fieldValidationResults);
184190

185191
//Assert
186-
expect(result.fieldErrors).to.have.length(0);
192+
expect(result.fieldErrors).to.deep.equals({});
187193
});
188194

189195
it('Spec #14 => should returns new FormValidationResult equals ' +
190-
'{ fieldErrors: [] } with length equals 0 ' +
196+
'{ fieldErrors: {} } without properties ' +
191197
'when passing fieldValidationResults with one item equals { key: "" }', () => {
192198
//Arrange
193199
let fieldValidationResult = new FieldValidationResult();
@@ -198,11 +204,11 @@ describe('ValidationsResultBuilder ', () => {
198204
let result = validationsResultBuilder.buildFormValidationsResult(fieldValidationResults);
199205

200206
//Assert
201-
expect(result.fieldErrors).to.have.length(0);
207+
expect(result.fieldErrors).to.deep.equals({});
202208
});
203209

204210
it('Spec #15 => should returns new FormValidationResult equals ' +
205-
'{ fieldErrors: [] } with length equals 0 ' +
211+
'{ fieldErrors: {} } without properties ' +
206212
'when passing fieldValidationResults with one item equals { key: "_GLOBAL_FORM_" }', () => {
207213
//Arrange
208214
let fieldValidationResult = new FieldValidationResult();
@@ -213,7 +219,7 @@ describe('ValidationsResultBuilder ', () => {
213219
let result = validationsResultBuilder.buildFormValidationsResult(fieldValidationResults);
214220

215221
//Assert
216-
expect(result.fieldErrors).to.have.length(0);
222+
expect(result.fieldErrors).to.deep.equals({});
217223
});
218224

219225
it('Spec #16 => should returns new FormValidationResult equals ' +
@@ -296,7 +302,7 @@ describe('ValidationsResultBuilder ', () => {
296302
});
297303

298304
it('Spec #21 => should returns new FormValidationResult equals ' +
299-
'{ fieldErrors: [{ key: "test" }] } with length equals 1 ' +
305+
'{ fieldErrors: { test: { key: "test" } } } ' +
300306
'when passing fieldValidationResults with two item first equals { key: "test" }' +
301307
'and second equals null', () => {
302308
//Arrange
@@ -311,12 +317,18 @@ describe('ValidationsResultBuilder ', () => {
311317
let result = validationsResultBuilder.buildFormValidationsResult(fieldValidationResults);
312318

313319
//Assert
314-
expect(result.fieldErrors).to.have.length(1);
315-
expect(result.fieldErrors[0].key).to.be.equal('test');
320+
expect(result.fieldErrors).to.deep.equal({
321+
test: {
322+
key: 'test',
323+
succeeded: false,
324+
errorMessage: '',
325+
type: '',
326+
} as FieldValidationResult,
327+
});
316328
});
317329

318330
it('Spec #22 => should returns new FormValidationResult equals ' +
319-
'{ fieldErrors: [{ key: "test" }] } with length equals 1 ' +
331+
'{ fieldErrors: { test: { key: "test" } } } ' +
320332
'when passing fieldValidationResults with two item first equals { key: "test" }' +
321333
'and second equals undefined', () => {
322334
//Arrange
@@ -331,11 +343,50 @@ describe('ValidationsResultBuilder ', () => {
331343
let result = validationsResultBuilder.buildFormValidationsResult(fieldValidationResults);
332344

333345
//Assert
334-
expect(result.fieldErrors).to.have.length(1);
335-
expect(result.fieldErrors[0].key).to.be.equal('test');
346+
expect(result.fieldErrors).to.deep.equal({
347+
test: {
348+
key: 'test',
349+
succeeded: false,
350+
errorMessage: '',
351+
type: '',
352+
} as FieldValidationResult,
353+
});
336354
});
337355

338356
it('Spec #23 => should returns new FormValidationResult equals ' +
357+
'{ fieldErrors: { test1: { key: "test1" }, test2: { key: "test2" } } } ' +
358+
'when passing fieldValidationResults with two item first equals { key: "test1" }' +
359+
'and second equals { key: "test2" }', () => {
360+
//Arrange
361+
const fieldValidationResult1 = new FieldValidationResult();
362+
fieldValidationResult1.key = 'test1';
363+
364+
const fieldValidationResult2 = new FieldValidationResult();
365+
fieldValidationResult2.key = 'test2';
366+
367+
const fieldValidationResults = [fieldValidationResult1, fieldValidationResult2];
368+
369+
//Act
370+
const result = validationsResultBuilder.buildFormValidationsResult(fieldValidationResults);
371+
372+
//Assert
373+
expect(result.fieldErrors).to.deep.equal({
374+
test1: {
375+
key: 'test1',
376+
succeeded: false,
377+
errorMessage: '',
378+
type: '',
379+
} as FieldValidationResult,
380+
test2: {
381+
key: 'test2',
382+
succeeded: false,
383+
errorMessage: '',
384+
type: '',
385+
} as FieldValidationResult,
386+
});
387+
});
388+
389+
it('Spec #24 => should returns new FormValidationResult equals ' +
339390
'{ formGlobalErrors: [{ key: "_GLOBAL_FORM_" }] } with length equals 1 ' +
340391
'when passing fieldValidationResults with two item first equals { key: "_GLOBAL_FORM_" }' +
341392
'and second equals null', () => {
@@ -355,7 +406,7 @@ describe('ValidationsResultBuilder ', () => {
355406
expect(result.formGlobalErrors[0].key).to.be.equal('_GLOBAL_FORM_');
356407
});
357408

358-
it('Spec #24 => should returns new FormValidationResult equals ' +
409+
it('Spec #25 => should returns new FormValidationResult equals ' +
359410
'{ formGlobalErrors: [{ key: "_GLOBAL_FORM_" }] } with length equals 1 ' +
360411
'when passing fieldValidationResults with two item first equals { key: "_GLOBAL_FORM_" }' +
361412
'and second equals undefined', () => {

lib/src/validationsResultBuilder.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { FieldValidationResult, FormValidationResult } from './entities';
22
import { consts } from './consts';
3+
import set from 'lodash.set';
34

45
export class ValidationsResultBuilder {
56
buildFormValidationsResult(fieldValidationResults: Array<FieldValidationResult>): FormValidationResult {
@@ -10,7 +11,8 @@ export class ValidationsResultBuilder {
1011
this.setGlobalKeyToEmptyKeys(filteredFieldValidationResults);
1112

1213
formValidationResult.succeeded = filteredFieldValidationResults.every(fvr => fvr.succeeded);
13-
formValidationResult.fieldErrors = filteredFieldValidationResults.filter(fvr => fvr.key !== consts.globalFormValidationId);
14+
const fieldValidationResultList = filteredFieldValidationResults.filter(fvr => fvr.key !== consts.globalFormValidationId);
15+
formValidationResult.fieldErrors = this.mapFieldValidationResultListToFieldErrorsObject(fieldValidationResultList);
1416
formValidationResult.formGlobalErrors = filteredFieldValidationResults.filter(fvr => fvr.key === consts.globalFormValidationId);
1517
}
1618

@@ -31,6 +33,10 @@ export class ValidationsResultBuilder {
3133
}
3234
});
3335
}
36+
37+
private mapFieldValidationResultListToFieldErrorsObject = (fieldValidationResultList: FieldValidationResult[]): { [key: string]: FieldValidationResult } => (
38+
fieldValidationResultList.reduce((errors, result) => set(errors, result.key, result), {})
39+
)
3440
}
3541

3642
let validationsResultBuilder = new ValidationsResultBuilder();

samples/jquery/00 ShoppingForm/src/modules/app/app.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import $ from 'jquery';
22
import { productsService } from '../../services/productsService';
33
import { productsFormValidation } from './validation/formProductValidationService';
44

5-
let storedProducts = [];
65
let $selBrands, $selProducts, $formProducts, $txtNif, $txtDiscount;
76

87
class App {
@@ -94,7 +93,9 @@ class App {
9493
productsFormValidation
9594
.validateForm(vm)
9695
.then(validationResult => {
97-
validationResult.fieldErrors.forEach(this.handleFieldValidationResult($form));
96+
Object.keys(validationResult.fieldErrors).forEach((key) => {
97+
this.handleFieldValidationResult($form)(validationResult.fieldErrors[key]);
98+
});
9899
if (validationResult.succeeded) {
99100
console.log('Form is sent');
100101
}

samples/react/es6/00 SimpleForm/src/reducers/customer.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,11 @@ function customerProcessUIInputCompleted(state, action) {
4343
}
4444

4545
function customerSaveCompleted(state, action) {
46-
const newCustomerErrors = { ...state.customerErrors };
47-
48-
action.formValidationResult.fieldErrors.forEach(fieldValidationResult => {
49-
newCustomerErrors[fieldValidationResult.key] = fieldValidationResult;
50-
});
51-
5246
return {
5347
...state,
54-
customerErrors: newCustomerErrors
48+
customerErrors: {
49+
...state.customerErrors,
50+
...action.formValidationResult.fieldErrors,
51+
},
5552
};
5653
}

0 commit comments

Comments
 (0)