Skip to content

Commit ab68138

Browse files
committed
Return promise from validateForm() and validateField()
These have returned void, so it's not a breaking change. isFieldValid() returns boolean; changing that to Promise<boolean> would be a breaking change, so it's left as-is for now.
1 parent c838a40 commit ab68138

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

src/index.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export type ValidationProvider = (value: string, element: ValidatableElement, pa
8282
export type ValidatedCallback = (success: boolean) => void;
8383

8484
interface ValidationEventCallback<TEvent extends Event = Event> {
85-
(e?: TEvent, callback?: ValidatedCallback): void;
85+
(e?: TEvent, callback?: ValidatedCallback): Promise<boolean>;
8686
debounced?: (e?: TEvent, callback?: ValidatedCallback) => void;
8787
remove?: () => void;
8888
}
@@ -729,29 +729,29 @@ export class ValidationService {
729729
* Fires off validation for elements within the provided form and then calls the callback
730730
* @param form The form to validate.
731731
* @param callback Receives true or false indicating validity after all validation is complete.
732+
* @returns Promise that resolves to true or false indicating validity after all validation is complete.
732733
*/
733-
validateForm = (form: HTMLFormElement, callback?: ValidatedCallback) => {
734+
validateForm = async (form: HTMLFormElement, callback?: ValidatedCallback) => {
734735
if (!(form instanceof HTMLFormElement)) {
735736
throw new Error('validateForm() can only be called on <form> elements');
736737
}
737738
let formUID = this.getElementUID(form);
738739
let formValidationEvent = this.formEvents[formUID];
739-
if (formValidationEvent) {
740-
formValidationEvent(undefined, callback);
741-
}
740+
return !formValidationEvent ||
741+
await formValidationEvent(undefined, callback);
742742
}
743743

744744
/**
745745
* Fires off validation for the provided element and then calls the callback
746746
* @param field The element to validate.
747747
* @param callback Receives true or false indicating validity after all validation is complete.
748+
* @returns Promise that resolves to true or false indicating validity after all validation is complete
748749
*/
749-
validateField = (field: ValidatableElement, callback?: ValidatedCallback) => {
750+
validateField = async (field: ValidatableElement, callback?: ValidatedCallback) => {
750751
let fieldUID = this.getElementUID(field);
751752
let fieldValidationEvent = this.inputEvents[fieldUID];
752-
if (fieldValidationEvent) {
753-
fieldValidationEvent(undefined, callback);
754-
}
753+
return !fieldValidationEvent ||
754+
await fieldValidationEvent(undefined, callback);
755755
}
756756

757757
/**
@@ -931,11 +931,11 @@ export class ValidationService {
931931
let cb: ValidationEventCallback<SubmitEvent> = (e, callback) => {
932932
// Prevent recursion
933933
if (validationTask) {
934-
return;
934+
return validationTask;
935935
}
936936

937937
if (!this.shouldValidate(e)) {
938-
return;
938+
return Promise.resolve(true);
939939
}
940940

941941
validationTask = this.getFormValidationTask(formUID);
@@ -947,11 +947,11 @@ export class ValidationService {
947947

948948
this.logger.log('Validating', form);
949949

950-
validationTask.then(async success => {
950+
return validationTask.then(async success => {
951951
this.logger.log('Validated (success = %s)', success, form);
952952
if (callback) {
953953
callback(success);
954-
return;
954+
return success;
955955
}
956956

957957
const validationEvent = new CustomEvent('validation',
@@ -963,8 +963,10 @@ export class ValidationService {
963963
// Firefox fix: redispatch 'submit' after finished handling this event
964964
await new Promise(resolve => setTimeout(resolve, 0));
965965
this.handleValidated(form, success, e);
966+
return success;
966967
}).catch(error => {
967968
this.logger.log('Validation error', error);
969+
return false;
968970
}).finally(() => {
969971
validationTask = null;
970972
});
@@ -1062,15 +1064,17 @@ export class ValidationService {
10621064

10631065
const cb: ValidationEventCallback = async (event, callback) => {
10641066
let validate = this.validators[uid];
1065-
if (!validate) return;
1067+
if (!validate) return true;
10661068

10671069
this.logger.log('Validating', { event });
10681070
try {
10691071
const success = await validate();
10701072
callback(success);
1073+
return success;
10711074
}
10721075
catch (error) {
10731076
this.logger.log('Validation error', error);
1077+
return false;
10741078
}
10751079
};
10761080

0 commit comments

Comments
 (0)