diff --git a/lib/src/baseFormValidation.ts b/lib/src/baseFormValidation.ts index dd06abf..9f60656 100644 --- a/lib/src/baseFormValidation.ts +++ b/lib/src/baseFormValidation.ts @@ -52,11 +52,11 @@ export class BaseFormValidation implements FormValidation { } } - private parseFieldValidations(constraint: string, fieldValidationConstraints: FieldValidationConstraint[]) { + private parseFieldValidations(field: string, fieldValidationConstraints: FieldValidationConstraint[]) { if (fieldValidationConstraints instanceof Array) { fieldValidationConstraints.forEach((fieldValidationConstraint) => { if (fieldValidationConstraint && typeof fieldValidationConstraint === 'object') { - this.addFieldValidation(constraint, fieldValidationConstraint); + this.addFieldValidation(field, fieldValidationConstraint); } }); } @@ -73,7 +73,7 @@ export class BaseFormValidation implements FormValidation { } validateField(vm: any, key: string, value: any, eventsFilter?: ValidationEventsFilter): Promise { - return this.validationEngine.triggerFieldValidation(vm, key, value, eventsFilter); + return this.validationEngine.validateField(vm, key, value, eventsFilter); } validateForm(vm: any): Promise { diff --git a/lib/src/consts.ts b/lib/src/consts.ts index 99c1134..abf74cc 100644 --- a/lib/src/consts.ts +++ b/lib/src/consts.ts @@ -1,4 +1,4 @@ export const consts = { globalFormValidationId: "_GLOBAL_FORM_", - defaultFilter: { OnChange: true } + defaultFilter: { onChange: true } } diff --git a/lib/src/entities.ts b/lib/src/entities.ts index 68c246d..8dc0db5 100644 --- a/lib/src/entities.ts +++ b/lib/src/entities.ts @@ -40,11 +40,7 @@ export interface FormValidationFunction { } export interface FieldValidationFunction { - (value: any, vm: any, customParams: any): Promise |ValidationResult; -} - -export interface AsyncFieldValidationFunction { - (value: any, vm: any, customParams: any): Promise; + (value: any, vm: any, customParams: any): ValidationResult; } export interface FieldValidationConstraint { diff --git a/lib/src/spec/baseFormValidation.spec.ts b/lib/src/spec/baseFormValidation.spec.ts index e458961..763ad07 100644 --- a/lib/src/spec/baseFormValidation.spec.ts +++ b/lib/src/spec/baseFormValidation.spec.ts @@ -34,10 +34,10 @@ describe('formValidation tests', () => { expect(isFormDirty.calledOnce).to.be.true; })); - it('Spec#3 => should have an exposed method "validateField" that calls ValidationEngine.validateSingleField', sinon.test(function () { + it('Spec#3 => should have an exposed method "validateField" that calls ValidationEngine.fireFieldValidations', sinon.test(function () { // Arrange const sinon: sinon.SinonStatic = this; - const validateSingleField = sinon.stub(ValidationEngine.prototype, 'validateSingleField', () => { }); + const fireFieldValidations = sinon.stub(ValidationEngine.prototype, 'fireFieldValidations', () => { }); const validationConstraints = {}; const viewModel = {}; const key = 'fullname'; @@ -49,7 +49,7 @@ describe('formValidation tests', () => { formValidation.validateField(viewModel, key, value, eventsFilter); // Assert - expect(validateSingleField.calledOnce).to.be.true; + expect(fireFieldValidations.calledOnce).to.be.true; })); it('Spec#4 => should have an exposed method "validateForm" that calls ValidationEngine.validateForm', sinon.test(function () { @@ -353,7 +353,7 @@ describe('formValidation tests', () => { const addFieldValidation = sinon.stub(ValidationEngine.prototype, 'addFieldValidation', () => { }); const validation1 = () => new FieldValidationResult(); const customParams = { foo: 'bar' }; - const eventsFilter = { OnBlur: true }; + const eventsFilter = { onBlur: true }; const validationConstraints: ValidationConstraints = { fields: { property1: [ diff --git a/lib/src/spec/fieldValidationEventFilter.spec.ts b/lib/src/spec/fieldValidationEventFilter.spec.ts index 0cb2dba..f57aee7 100644 --- a/lib/src/spec/fieldValidationEventFilter.spec.ts +++ b/lib/src/spec/fieldValidationEventFilter.spec.ts @@ -4,42 +4,42 @@ import { fieldValidationEventFilter } from '../fieldValidationEventFilter' describe('FieldValidationEventFilter ', () => { describe('when calling filter ', () => { - it('should returns new Array having only one elmenent matching the eventsFilter {OnChange: true} ' + - 'when passing a list of validations containg only one Element and having OnChange true for that element', () => { + it('should returns new Array having only one element matching the eventsFilter {onChange: true} ' + + 'when passing a list of validations containg only one Element and having onChange true for that element', () => { //Arrange const allValidations: FieldValidation[] = [ { validationFn: (vm, value) => { return null }, - eventsFilter: { OnChange: true }, + eventsFilter: { onChange: true }, customParams: {} } ]; - const eventFilter = { OnChange: true }; + const eventFilter = { onChange: true }; //Act const result = fieldValidationEventFilter.filter(allValidations, eventFilter); //Assert expect(result.length).to.be.equal(1); - expect(result[0].eventsFilter.OnChange).to.be.true; + expect(result[0].eventsFilter).to.have.property('onChange').that.is.true; }); }); describe('when calling filter ', () => { - it('should returns new Array having zero elmenent matching the eventsFilter {OnBlur: true} ' + - 'when passing a list of validations containg only one Element and having OnChange true for that element', () => { + it('should returns new Array having zero elements matching the eventsFilter {onBlur: true} ' + + 'when passing a list of validations containg only one Element and having onChange true for that element', () => { //Arrange const allValidations: Array = [ { validationFn: (vm, value) => { return null }, - eventsFilter: { OnChange: true }, + eventsFilter: { onChange: true }, customParams: {} } ]; - const eventsFilter = { OnBlur: true }; + const eventsFilter = { onBlur: true }; //Act const result = fieldValidationEventFilter.filter(allValidations, eventsFilter); @@ -51,84 +51,84 @@ describe('FieldValidationEventFilter ', () => { describe('FieldValidationEventFilter ', () => { describe('when calling filter ', () => { - it('should returns new Array having only one elmenent matching the eventsFilter {OnChange: true} ' + - 'when passing a list of validations containg two elements, one OnChange the other OnBlur', () => { + it('should returns new Array having only one element matching the eventsFilter {onChange: true} ' + + 'when passing a list of validations containg two elements, one onChange the other onBlur', () => { //Arrange const allValidations: Array = [ { validationFn: (vm, value) => { return null }, - eventsFilter: { OnChange: true }, + eventsFilter: { onChange: true }, customParams: {} }, { validationFn: (vm, value) => { return null }, - eventsFilter: { OnBlur: true }, + eventsFilter: { onBlur: true }, customParams: {} } ]; - const eventsFilter = { OnChange: true }; + const eventsFilter = { onChange: true }; //Act const result = fieldValidationEventFilter.filter(allValidations, eventsFilter); //Assert expect(result.length).to.be.equal(1); - expect(result[0].eventsFilter.OnChange).to.be.true; + expect(result[0].eventsFilter).to.have.property('onChange').that.is.true; }); }); }); describe('FieldValidationEventFilter ', () => { describe('when calling filter ', () => { - it('should returns new Array having two elements matching the eventsFilter {OnChange: true} ' + - 'when passing a list of validations containg three elements, two OnChange the other OnBlur', () => { + it('should returns new Array having two elements matching the eventsFilter {onChange: true} ' + + 'when passing a list of validations containg three elements, two onChange the other onBlur', () => { //Arrange const allValidations: Array = [ { validationFn: (vm, value) => { return null }, - eventsFilter: { OnChange: true }, + eventsFilter: { onChange: true }, customParams: {} }, { validationFn: (vm, value) => { return null }, - eventsFilter: { OnBlur: true }, + eventsFilter: { onBlur: true }, customParams: {} }, { validationFn: (vm, value) => { return null }, - eventsFilter: { OnChange: true }, + eventsFilter: { onChange: true }, customParams: {} } ]; - const eventsFilter = { OnChange: true }; + const eventsFilter = { onChange: true }; //Act const result = fieldValidationEventFilter.filter(allValidations, eventsFilter); //Assert expect(result.length).to.be.equal(2); - expect(result[0].eventsFilter.OnChange).to.be.true; - expect(result[1].eventsFilter.OnChange).to.be.true; + expect(result[0].eventsFilter).to.have.property('onChange').that.is.true; + expect(result[1].eventsFilter).to.have.property('onChange').that.is.true; }); }); }); describe('FieldValidationEventFilter ', () => { describe('when calling filter ', () => { - it('should returns new Array having two elements matching the eventsFilter {OnChange: true, OnBlur: true} (OR) ' + - 'when passing a list of validations containg three elements, one OnChange the other OnBlur, the other OnWhatever', () => { + it('should returns new Array having two elements matching the eventsFilter {onChange: true, onBlur: true} (OR) ' + + 'when passing a list of validations containg three elements, one onChange the other onBlur, the other OnWhatever', () => { //Arrange const allValidations: Array = [ { validationFn: (vm, value) => { return null }, - eventsFilter: { OnChange: true }, + eventsFilter: { onChange: true }, customParams: {} }, { validationFn: (vm, value) => { return null }, - eventsFilter: { OnBlur: true }, + eventsFilter: { onBlur: true }, customParams: {} }, { @@ -138,15 +138,15 @@ describe('FieldValidationEventFilter ', () => { } ]; - const eventsFilter = { OnChange: true, OnBlur: true }; + const eventsFilter = { onChange: true, onBlur: true }; //Act const result = fieldValidationEventFilter.filter(allValidations, eventsFilter); //Assert expect(result.length).to.be.equal(2); - expect(result[0].eventsFilter.OnChange).to.be.true; - expect(result[1].eventsFilter.OnBlur).to.be.true; + expect(result[0].eventsFilter).to.have.property('onChange').that.is.true; + expect(result[1].eventsFilter).to.have.property('onBlur').that.is.true; }); }); }); @@ -154,17 +154,17 @@ describe('FieldValidationEventFilter ', () => { describe('FieldValidationEventFilter ', () => { describe('when calling filter ', () => { it('should returns new Array having three elements matching the eventsFilter null ' + - 'when passing a list of validations containg three elements, one OnChange the other OnBlur, the other OnWhatever', () => { + 'when passing a list of validations containg three elements, one onChange the other onBlur, the other OnWhatever', () => { //Arrange const allValidations: Array = [ { validationFn: (vm, value) => { return null }, - eventsFilter: { OnChange: true }, + eventsFilter: { onChange: true }, customParams: {} }, { validationFn: (vm, value) => { return null }, - eventsFilter: { OnBlur: true }, + eventsFilter: { onBlur: true }, customParams: {} }, { @@ -181,9 +181,9 @@ describe('FieldValidationEventFilter ', () => { //Assert expect(result.length).to.be.equal(3); - expect(result[0].eventsFilter.OnChange).to.be.true; - expect(result[1].eventsFilter.OnBlur).to.be.true; - expect(result[2].eventsFilter.OnWhatever).to.be.true; + expect(result[0].eventsFilter).to.have.property('onChange').that.is.true; + expect(result[1].eventsFilter).to.have.property('onBlur').that.is.true; + expect(result[2].eventsFilter).to.have.property('OnWhatever').that.is.true; }); }); }); diff --git a/lib/src/spec/validationEngine.spec.ts b/lib/src/spec/validationEngine.spec.ts index 2080723..db89be0 100644 --- a/lib/src/spec/validationEngine.spec.ts +++ b/lib/src/spec/validationEngine.spec.ts @@ -1,7 +1,6 @@ import { ValidationEngine } from '../validationEngine'; import { FieldValidationResult } from '../entities'; -//TODO: Implement Issue #20 describe('ValidationEngine tests', () => { it('should return isFormPristine true after initialization', () => { // Arrange @@ -17,7 +16,7 @@ describe('ValidationEngine tests', () => { const viewModel = [{ formFieldName: 'nameId', vmFieldName: 'name' }]; formValidationBase - .triggerFieldValidation(viewModel, 'nameId', 'newContent') + .validateField(viewModel, 'nameId', 'newContent') .then((errors) => { // Assert expect(formValidationBase.isFormPristine()).to.be.false; @@ -39,7 +38,7 @@ describe('ValidationEngine tests', () => { const viewModel = [{ formFieldName: 'nameId', vmFieldName: 'name' }]; formValidationBase - .triggerFieldValidation(viewModel, 'nameId', 'newContent') + .validateField(viewModel, 'nameId', 'newContent') .then((errors) => { // Assert expect(formValidationBase.isFormDirty()).to.be.true; @@ -62,7 +61,7 @@ describe('ValidationEngine tests', () => { // Act formValidationBase - .triggerFieldValidation(viewModel, 'nameId', 'newContent') + .validateField(viewModel, 'nameId', 'newContent') .then((errors) => { // Assert expect(formValidationBase.isValidationInProgress()).to.be.false; diff --git a/lib/src/spec/validationEngineValidateForm.spec.ts b/lib/src/spec/validationEngineValidateForm.spec.ts index c0d0dec..a4e38a2 100644 --- a/lib/src/spec/validationEngineValidateForm.spec.ts +++ b/lib/src/spec/validationEngineValidateForm.spec.ts @@ -2,7 +2,6 @@ import { ValidationEngine } from '../validationEngine'; import { FieldValidationResult, FormValidationResult } from '../entities'; import { consts } from '../consts'; -//TODO: Implement Issue #20 describe('ValidationEngine Validate Form', () => { describe('Group #1 => When calling validateForm and addFieldValidation', () => { it('Spec #1 => should return a failed FormValidationResult with one fieldErrors equals ' + @@ -480,8 +479,8 @@ describe('ValidationEngine Validate Form', () => { const vm = { fullname: '' }; // Act - validationEngine.addFieldValidation('fullname', validationFn1Spy, { OnChange: true, OnBlur: true }); - validationEngine.addFieldValidation('fullname', validationFn2Spy, { OnBlur: true }); + validationEngine.addFieldValidation('fullname', validationFn1Spy, { onChange: true, onBlur: true }); + validationEngine.addFieldValidation('fullname', validationFn2Spy, { onBlur: true }); validationEngine.validateForm(vm).then((validationResult) => { // Assert diff --git a/lib/src/spec/validationEngineValidateSingleField.spec.ts b/lib/src/spec/validationEngineValidateSingleField.spec.ts index 30803c1..d849a73 100644 --- a/lib/src/spec/validationEngineValidateSingleField.spec.ts +++ b/lib/src/spec/validationEngineValidateSingleField.spec.ts @@ -1,7 +1,6 @@ import { ValidationEngine } from '../validationEngine'; import { FieldValidationResult } from '../entities'; -//TODO: Implement Issue #20 (Take into account if it returns Promise.resolve(undefined)) describe('lcFormValidation simple form', () => { it('should return isValidationInProgress true if validations are inProgress', (done) => { // Arrange @@ -28,7 +27,7 @@ describe('lcFormValidation simple form', () => { ); formValidationBase - .triggerFieldValidation(viewModel, 'fullname', 'newContent') + .validateField(viewModel, 'fullname', 'newContent') .then((errors) => { // Assert expect(formValidationBase.isValidationInProgress()).to.be.false; @@ -66,7 +65,7 @@ describe('lcFormValidation simple form', () => { ); formValidationBase - .triggerFieldValidation(viewModel, 'fullname', '') + .validateField(viewModel, 'fullname', '') .then((fieldValidationResult: FieldValidationResult) => { // Assert expect(fieldValidationResult.key).to.be.equal('fullname'); @@ -103,7 +102,7 @@ describe('lcFormValidation simple form', () => { ); formValidationBase - .triggerFieldValidation(viewModel, 'fullname', 'john') + .validateField(viewModel, 'fullname', 'john') .then((fieldValidationResult: FieldValidationResult) => { // Assert @@ -128,7 +127,7 @@ describe('lcFormValidation simple form', () => { } ); - const promise = formValidationBase.triggerFieldValidation(viewModel, 'fullname', ''); + const promise = formValidationBase.validateField(viewModel, 'fullname', ''); //Assert expect(promise).to.eventually.be.rejected.and.notify(done); diff --git a/lib/src/validationEngine.ts b/lib/src/validationEngine.ts index 9533f95..280a456 100644 --- a/lib/src/validationEngine.ts +++ b/lib/src/validationEngine.ts @@ -17,8 +17,7 @@ export interface IValidationEngine { isFormPristine(): boolean; isValidationInProgress(): boolean; validateForm(vm: any): Promise; - triggerFieldValidation(vm: any, key: string, value: any, eventsFilter?: ValidationEventsFilter): Promise; - // TODO: Implement Issue #15 + validateField(vm: any, key: string, value: any, eventsFilter?: ValidationEventsFilter): Promise; addFieldValidation(key: string, validation: FieldValidationFunction, eventsFilter?: ValidationEventsFilter): void; addFormValidation(validation: FormValidationFunction): void; isValidationInProgress(): boolean; @@ -53,7 +52,7 @@ export class ValidationEngine implements IValidationEngine { let fieldValidationResults: ValidationResult[] = validationsDispatcher.fireAllFieldsValidations( viewModel, Object.keys(this.validationsPerField), - this.validateSingleField.bind(this) + this.fireFieldValidations.bind(this) ); // Let's add GlobalFormValidations @@ -61,8 +60,6 @@ export class ValidationEngine implements IValidationEngine { fieldValidationResults = [...fieldValidationResults, ...this.validateGlobalFormValidations(viewModel)]; } - // TODO: Implement Issue #16 - Error handling - // Once all the single field validations have been resolved // resolve the fullFormValidatePromise Promise.all(fieldValidationResults) @@ -97,18 +94,18 @@ export class ValidationEngine implements IValidationEngine { return globalFieldResultValidations; } - triggerFieldValidation(vm: any, key: string, value: any, filters: ValidationEventsFilter = consts.defaultFilter): Promise { + validateField(vm: any, key: string, value: any, filters: ValidationEventsFilter = consts.defaultFilter): Promise { // updated dirty flag and perform validation this.isFormChanged = false; - return this.validateSingleField(vm, key, value, filters); + return this.fireFieldValidations(vm, key, value, filters); } // if filter is null all validations are returned (fullform validation case) - private validateSingleField(vm: any, key: string, value: any, filters: ValidationEventsFilter = null): Promise { + private fireFieldValidations(vm: any, key: string, value: any, filters: ValidationEventsFilter = null): Promise { this.asyncValidationInProgressCount++; const fieldValidationResultPromise = new Promise((resolve, reject) => { - // TODO: this should be encapsulated into two separate functions, Issue #26 + // TODO: this should be encapsulated into two separate functions if (!this.isFieldKeyMappingDefined(key)) { this.asyncValidationInProgressCount--; resolve(); diff --git a/samples/react/typescript/01 SignupForm/src/components/sampleSignupForm/sampleSignupForm.tsx b/samples/react/typescript/01 SignupForm/src/components/sampleSignupForm/sampleSignupForm.tsx index 1db0004..b471073 100644 --- a/samples/react/typescript/01 SignupForm/src/components/sampleSignupForm/sampleSignupForm.tsx +++ b/samples/react/typescript/01 SignupForm/src/components/sampleSignupForm/sampleSignupForm.tsx @@ -40,7 +40,7 @@ export class SampleSignupForm extends React.Component { this.applyFieldValidation(event); }} onBlur={(event) => { - this.applyFieldValidation(event, { OnBlur: true }); + this.applyFieldValidation(event, { onBlur: true }); }} error={(this.props.errors.login) ? this.props.errors.login.errorMessage : ''} /> diff --git a/samples/react/typescript/01 SignupForm/src/components/sampleSignupForm/validations/signupFormValidation.ts b/samples/react/typescript/01 SignupForm/src/components/sampleSignupForm/validations/signupFormValidation.ts index d1fbdc9..b729b82 100644 --- a/samples/react/typescript/01 SignupForm/src/components/sampleSignupForm/validations/signupFormValidation.ts +++ b/samples/react/typescript/01 SignupForm/src/components/sampleSignupForm/validations/signupFormValidation.ts @@ -49,11 +49,11 @@ const signupValidationConstraints: ValidationConstraints = { login: [ { validator: Validators.required, - eventsFilter: { OnChange: true, OnBlur: true }, + eventsFilter: { onChange: true, onBlur: true }, }, { validator: loginExistOnGitHubValidationHandler, - eventsFilter: { OnBlur: true } + eventsFilter: { onBlur: true } }, ] }