Skip to content

Commit 8e95996

Browse files
authored
Merge pull request #71 from Lemoncode/#58-fix-pending-changes
Pending feedback from#58
2 parents 55d81c6 + 9bd2ea6 commit 8e95996

11 files changed

Lines changed: 63 additions & 73 deletions

lib/src/baseFormValidation.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ export class BaseFormValidation implements FormValidation {
5252
}
5353
}
5454

55-
private parseFieldValidations(constraint: string, fieldValidationConstraints: FieldValidationConstraint[]) {
55+
private parseFieldValidations(field: string, fieldValidationConstraints: FieldValidationConstraint[]) {
5656
if (fieldValidationConstraints instanceof Array) {
5757
fieldValidationConstraints.forEach((fieldValidationConstraint) => {
5858
if (fieldValidationConstraint && typeof fieldValidationConstraint === 'object') {
59-
this.addFieldValidation(constraint, fieldValidationConstraint);
59+
this.addFieldValidation(field, fieldValidationConstraint);
6060
}
6161
});
6262
}
@@ -73,7 +73,7 @@ export class BaseFormValidation implements FormValidation {
7373
}
7474

7575
validateField(vm: any, key: string, value: any, eventsFilter?: ValidationEventsFilter): Promise<FieldValidationResult> {
76-
return this.validationEngine.triggerFieldValidation(vm, key, value, eventsFilter);
76+
return this.validationEngine.validateField(vm, key, value, eventsFilter);
7777
}
7878

7979
validateForm(vm: any): Promise<FormValidationResult> {

lib/src/consts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export const consts = {
22
globalFormValidationId: "_GLOBAL_FORM_",
3-
defaultFilter: { OnChange: true }
3+
defaultFilter: { onChange: true }
44
}

lib/src/entities.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,7 @@ export interface FormValidationFunction {
4040
}
4141

4242
export interface FieldValidationFunction {
43-
(value: any, vm: any, customParams: any): Promise<FieldValidationResult> |ValidationResult;
44-
}
45-
46-
export interface AsyncFieldValidationFunction {
47-
(value: any, vm: any, customParams: any): Promise<FieldValidationResult>;
43+
(value: any, vm: any, customParams: any): ValidationResult;
4844
}
4945

5046
export interface FieldValidationConstraint {

lib/src/spec/baseFormValidation.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ describe('formValidation tests', () => {
3434
expect(isFormDirty.calledOnce).to.be.true;
3535
}));
3636

37-
it('Spec#3 => should have an exposed method "validateField" that calls ValidationEngine.validateSingleField', sinon.test(function () {
37+
it('Spec#3 => should have an exposed method "validateField" that calls ValidationEngine.fireFieldValidations', sinon.test(function () {
3838
// Arrange
3939
const sinon: sinon.SinonStatic = this;
40-
const validateSingleField = sinon.stub(ValidationEngine.prototype, 'validateSingleField', () => { });
40+
const fireFieldValidations = sinon.stub(ValidationEngine.prototype, 'fireFieldValidations', () => { });
4141
const validationConstraints = {};
4242
const viewModel = {};
4343
const key = 'fullname';
@@ -49,7 +49,7 @@ describe('formValidation tests', () => {
4949
formValidation.validateField(viewModel, key, value, eventsFilter);
5050

5151
// Assert
52-
expect(validateSingleField.calledOnce).to.be.true;
52+
expect(fireFieldValidations.calledOnce).to.be.true;
5353
}));
5454

5555
it('Spec#4 => should have an exposed method "validateForm" that calls ValidationEngine.validateForm', sinon.test(function () {
@@ -353,7 +353,7 @@ describe('formValidation tests', () => {
353353
const addFieldValidation = sinon.stub(ValidationEngine.prototype, 'addFieldValidation', () => { });
354354
const validation1 = () => new FieldValidationResult();
355355
const customParams = { foo: 'bar' };
356-
const eventsFilter = { OnBlur: true };
356+
const eventsFilter = { onBlur: true };
357357
const validationConstraints: ValidationConstraints = {
358358
fields: {
359359
property1: [

lib/src/spec/fieldValidationEventFilter.spec.ts

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,42 @@ import { fieldValidationEventFilter } from '../fieldValidationEventFilter'
44
describe('FieldValidationEventFilter ', () => {
55

66
describe('when calling filter ', () => {
7-
it('should returns new Array<FieldValidation> having only one elmenent matching the eventsFilter {OnChange: true} ' +
8-
'when passing a list of validations containg only one Element and having OnChange true for that element', () => {
7+
it('should returns new Array<FieldValidation> having only one element matching the eventsFilter {onChange: true} ' +
8+
'when passing a list of validations containg only one Element and having onChange true for that element', () => {
99
//Arrange
1010
const allValidations: FieldValidation[] = [
1111
{
1212
validationFn: (vm, value) => { return null },
13-
eventsFilter: { OnChange: true },
13+
eventsFilter: { onChange: true },
1414
customParams: {}
1515
}
1616
];
1717

18-
const eventFilter = { OnChange: true };
18+
const eventFilter = { onChange: true };
1919

2020
//Act
2121
const result = fieldValidationEventFilter.filter(allValidations, eventFilter);
2222

2323
//Assert
2424
expect(result.length).to.be.equal(1);
25-
expect(result[0].eventsFilter.OnChange).to.be.true;
25+
expect(result[0].eventsFilter).to.have.property('onChange').that.is.true;
2626
});
2727

2828
});
2929

3030
describe('when calling filter ', () => {
31-
it('should returns new Array<FieldValidation> having zero elmenent matching the eventsFilter {OnBlur: true} ' +
32-
'when passing a list of validations containg only one Element and having OnChange true for that element', () => {
31+
it('should returns new Array<FieldValidation> having zero elements matching the eventsFilter {onBlur: true} ' +
32+
'when passing a list of validations containg only one Element and having onChange true for that element', () => {
3333
//Arrange
3434
const allValidations: Array<FieldValidation> = [
3535
{
3636
validationFn: (vm, value) => { return null },
37-
eventsFilter: { OnChange: true },
37+
eventsFilter: { onChange: true },
3838
customParams: {}
3939
}
4040
];
4141

42-
const eventsFilter = { OnBlur: true };
42+
const eventsFilter = { onBlur: true };
4343

4444
//Act
4545
const result = fieldValidationEventFilter.filter(allValidations, eventsFilter);
@@ -51,84 +51,84 @@ describe('FieldValidationEventFilter ', () => {
5151

5252
describe('FieldValidationEventFilter ', () => {
5353
describe('when calling filter ', () => {
54-
it('should returns new Array<FieldValidation> having only one elmenent matching the eventsFilter {OnChange: true} ' +
55-
'when passing a list of validations containg two elements, one OnChange the other OnBlur', () => {
54+
it('should returns new Array<FieldValidation> having only one element matching the eventsFilter {onChange: true} ' +
55+
'when passing a list of validations containg two elements, one onChange the other onBlur', () => {
5656
//Arrange
5757
const allValidations: Array<FieldValidation> = [
5858
{
5959
validationFn: (vm, value) => { return null },
60-
eventsFilter: { OnChange: true },
60+
eventsFilter: { onChange: true },
6161
customParams: {}
6262
},
6363
{
6464
validationFn: (vm, value) => { return null },
65-
eventsFilter: { OnBlur: true },
65+
eventsFilter: { onBlur: true },
6666
customParams: {}
6767
}
6868
];
6969

70-
const eventsFilter = { OnChange: true };
70+
const eventsFilter = { onChange: true };
7171

7272
//Act
7373
const result = fieldValidationEventFilter.filter(allValidations, eventsFilter);
7474

7575
//Assert
7676
expect(result.length).to.be.equal(1);
77-
expect(result[0].eventsFilter.OnChange).to.be.true;
77+
expect(result[0].eventsFilter).to.have.property('onChange').that.is.true;
7878
});
7979
});
8080
});
8181

8282
describe('FieldValidationEventFilter ', () => {
8383
describe('when calling filter ', () => {
84-
it('should returns new Array<FieldValidation> having two elements matching the eventsFilter {OnChange: true} ' +
85-
'when passing a list of validations containg three elements, two OnChange the other OnBlur', () => {
84+
it('should returns new Array<FieldValidation> having two elements matching the eventsFilter {onChange: true} ' +
85+
'when passing a list of validations containg three elements, two onChange the other onBlur', () => {
8686
//Arrange
8787
const allValidations: Array<FieldValidation> = [
8888
{
8989
validationFn: (vm, value) => { return null },
90-
eventsFilter: { OnChange: true },
90+
eventsFilter: { onChange: true },
9191
customParams: {}
9292
},
9393
{
9494
validationFn: (vm, value) => { return null },
95-
eventsFilter: { OnBlur: true },
95+
eventsFilter: { onBlur: true },
9696
customParams: {}
9797
},
9898
{
9999
validationFn: (vm, value) => { return null },
100-
eventsFilter: { OnChange: true },
100+
eventsFilter: { onChange: true },
101101
customParams: {}
102102
}
103103
];
104104

105-
const eventsFilter = { OnChange: true };
105+
const eventsFilter = { onChange: true };
106106

107107
//Act
108108
const result = fieldValidationEventFilter.filter(allValidations, eventsFilter);
109109

110110
//Assert
111111
expect(result.length).to.be.equal(2);
112-
expect(result[0].eventsFilter.OnChange).to.be.true;
113-
expect(result[1].eventsFilter.OnChange).to.be.true;
112+
expect(result[0].eventsFilter).to.have.property('onChange').that.is.true;
113+
expect(result[1].eventsFilter).to.have.property('onChange').that.is.true;
114114
});
115115
});
116116
});
117117

118118
describe('FieldValidationEventFilter ', () => {
119119
describe('when calling filter ', () => {
120-
it('should returns new Array<FieldValidation> having two elements matching the eventsFilter {OnChange: true, OnBlur: true} (OR) ' +
121-
'when passing a list of validations containg three elements, one OnChange the other OnBlur, the other OnWhatever', () => {
120+
it('should returns new Array<FieldValidation> having two elements matching the eventsFilter {onChange: true, onBlur: true} (OR) ' +
121+
'when passing a list of validations containg three elements, one onChange the other onBlur, the other OnWhatever', () => {
122122
//Arrange
123123
const allValidations: Array<FieldValidation> = [
124124
{
125125
validationFn: (vm, value) => { return null },
126-
eventsFilter: { OnChange: true },
126+
eventsFilter: { onChange: true },
127127
customParams: {}
128128
},
129129
{
130130
validationFn: (vm, value) => { return null },
131-
eventsFilter: { OnBlur: true },
131+
eventsFilter: { onBlur: true },
132132
customParams: {}
133133
},
134134
{
@@ -138,33 +138,33 @@ describe('FieldValidationEventFilter ', () => {
138138
}
139139
];
140140

141-
const eventsFilter = { OnChange: true, OnBlur: true };
141+
const eventsFilter = { onChange: true, onBlur: true };
142142

143143
//Act
144144
const result = fieldValidationEventFilter.filter(allValidations, eventsFilter);
145145

146146
//Assert
147147
expect(result.length).to.be.equal(2);
148-
expect(result[0].eventsFilter.OnChange).to.be.true;
149-
expect(result[1].eventsFilter.OnBlur).to.be.true;
148+
expect(result[0].eventsFilter).to.have.property('onChange').that.is.true;
149+
expect(result[1].eventsFilter).to.have.property('onBlur').that.is.true;
150150
});
151151
});
152152
});
153153

154154
describe('FieldValidationEventFilter ', () => {
155155
describe('when calling filter ', () => {
156156
it('should returns new Array<FieldValidation> having three elements matching the eventsFilter null ' +
157-
'when passing a list of validations containg three elements, one OnChange the other OnBlur, the other OnWhatever', () => {
157+
'when passing a list of validations containg three elements, one onChange the other onBlur, the other OnWhatever', () => {
158158
//Arrange
159159
const allValidations: Array<FieldValidation> = [
160160
{
161161
validationFn: (vm, value) => { return null },
162-
eventsFilter: { OnChange: true },
162+
eventsFilter: { onChange: true },
163163
customParams: {}
164164
},
165165
{
166166
validationFn: (vm, value) => { return null },
167-
eventsFilter: { OnBlur: true },
167+
eventsFilter: { onBlur: true },
168168
customParams: {}
169169
},
170170
{
@@ -181,9 +181,9 @@ describe('FieldValidationEventFilter ', () => {
181181

182182
//Assert
183183
expect(result.length).to.be.equal(3);
184-
expect(result[0].eventsFilter.OnChange).to.be.true;
185-
expect(result[1].eventsFilter.OnBlur).to.be.true;
186-
expect(result[2].eventsFilter.OnWhatever).to.be.true;
184+
expect(result[0].eventsFilter).to.have.property('onChange').that.is.true;
185+
expect(result[1].eventsFilter).to.have.property('onBlur').that.is.true;
186+
expect(result[2].eventsFilter).to.have.property('OnWhatever').that.is.true;
187187
});
188188
});
189189
});

lib/src/spec/validationEngine.spec.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { ValidationEngine } from '../validationEngine';
22
import { FieldValidationResult } from '../entities';
33

4-
//TODO: Implement Issue #20
54
describe('ValidationEngine tests', () => {
65
it('should return isFormPristine true after initialization', () => {
76
// Arrange
@@ -17,7 +16,7 @@ describe('ValidationEngine tests', () => {
1716
const viewModel = [{ formFieldName: 'nameId', vmFieldName: 'name' }];
1817

1918
formValidationBase
20-
.triggerFieldValidation(viewModel, 'nameId', 'newContent')
19+
.validateField(viewModel, 'nameId', 'newContent')
2120
.then((errors) => {
2221
// Assert
2322
expect(formValidationBase.isFormPristine()).to.be.false;
@@ -39,7 +38,7 @@ describe('ValidationEngine tests', () => {
3938
const viewModel = [{ formFieldName: 'nameId', vmFieldName: 'name' }];
4039

4140
formValidationBase
42-
.triggerFieldValidation(viewModel, 'nameId', 'newContent')
41+
.validateField(viewModel, 'nameId', 'newContent')
4342
.then((errors) => {
4443
// Assert
4544
expect(formValidationBase.isFormDirty()).to.be.true;
@@ -62,7 +61,7 @@ describe('ValidationEngine tests', () => {
6261

6362
// Act
6463
formValidationBase
65-
.triggerFieldValidation(viewModel, 'nameId', 'newContent')
64+
.validateField(viewModel, 'nameId', 'newContent')
6665
.then((errors) => {
6766
// Assert
6867
expect(formValidationBase.isValidationInProgress()).to.be.false;

lib/src/spec/validationEngineValidateForm.spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { ValidationEngine } from '../validationEngine';
22
import { FieldValidationResult, FormValidationResult } from '../entities';
33
import { consts } from '../consts';
44

5-
//TODO: Implement Issue #20
65
describe('ValidationEngine Validate Form', () => {
76
describe('Group #1 => When calling validateForm and addFieldValidation', () => {
87
it('Spec #1 => should return a failed FormValidationResult with one fieldErrors equals ' +
@@ -480,8 +479,8 @@ describe('ValidationEngine Validate Form', () => {
480479
const vm = { fullname: '' };
481480

482481
// Act
483-
validationEngine.addFieldValidation('fullname', validationFn1Spy, { OnChange: true, OnBlur: true });
484-
validationEngine.addFieldValidation('fullname', validationFn2Spy, { OnBlur: true });
482+
validationEngine.addFieldValidation('fullname', validationFn1Spy, { onChange: true, onBlur: true });
483+
validationEngine.addFieldValidation('fullname', validationFn2Spy, { onBlur: true });
485484
validationEngine.validateForm(vm).then((validationResult) => {
486485

487486
// Assert

lib/src/spec/validationEngineValidateSingleField.spec.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { ValidationEngine } from '../validationEngine';
22
import { FieldValidationResult } from '../entities';
33

4-
//TODO: Implement Issue #20 (Take into account if it returns Promise.resolve(undefined))
54
describe('lcFormValidation simple form', () => {
65
it('should return isValidationInProgress true if validations are inProgress', (done) => {
76
// Arrange
@@ -28,7 +27,7 @@ describe('lcFormValidation simple form', () => {
2827
);
2928

3029
formValidationBase
31-
.triggerFieldValidation(viewModel, 'fullname', 'newContent')
30+
.validateField(viewModel, 'fullname', 'newContent')
3231
.then((errors) => {
3332
// Assert
3433
expect(formValidationBase.isValidationInProgress()).to.be.false;
@@ -66,7 +65,7 @@ describe('lcFormValidation simple form', () => {
6665
);
6766

6867
formValidationBase
69-
.triggerFieldValidation(viewModel, 'fullname', '')
68+
.validateField(viewModel, 'fullname', '')
7069
.then((fieldValidationResult: FieldValidationResult) => {
7170
// Assert
7271
expect(fieldValidationResult.key).to.be.equal('fullname');
@@ -103,7 +102,7 @@ describe('lcFormValidation simple form', () => {
103102
);
104103

105104
formValidationBase
106-
.triggerFieldValidation(viewModel, 'fullname', 'john')
105+
.validateField(viewModel, 'fullname', 'john')
107106
.then((fieldValidationResult: FieldValidationResult) => {
108107

109108
// Assert
@@ -128,7 +127,7 @@ describe('lcFormValidation simple form', () => {
128127
}
129128
);
130129

131-
const promise = formValidationBase.triggerFieldValidation(viewModel, 'fullname', '');
130+
const promise = formValidationBase.validateField(viewModel, 'fullname', '');
132131

133132
//Assert
134133
expect(promise).to.eventually.be.rejected.and.notify(done);

0 commit comments

Comments
 (0)