Skip to content

Commit 4564494

Browse files
rs-prasaditz-PrathamMittal
authored andcommitted
BZ-42256: feat: Add support for asynchronous validator in input
- Add support for asynchronous input validator. - Change return type of custom validator for input component.
1 parent 8e0dcd9 commit 4564494

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

src/lib/Input/Input.svelte

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@
99
export let properties: InputProperties = defaultInputProperties;
1010
let inputElement: HTMLInputElement | HTMLTextAreaElement;
1111
12-
$: state = getValidationState(properties) as ValidationState;
12+
let state: ValidationState = 'InProgress';
13+
14+
$: validateState(properties);
15+
1316
1417
// For making this function reactive, prop was passed as param
15-
function getValidationState(prop: InputProperties): ValidationState {
16-
const valueValidation: ValidationState = validateInput(
18+
async function validateState(prop: InputProperties): Promise<void> {
19+
const valueValidation: ValidationState = await validateInput(
1720
prop.value,
1821
prop.dataType,
1922
prop.validationPattern,
@@ -26,9 +29,9 @@
2629
inputElement &&
2730
inputElement !== document.activeElement
2831
) {
29-
return 'Invalid';
32+
state = 'Invalid';
3033
} else {
31-
return valueValidation;
34+
state = valueValidation;
3235
}
3336
}
3437

src/lib/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export type AutoCompleteType =
2626
export type CustomValidator = (
2727
inputValue: string,
2828
currentValidationState: ValidationState
29-
) => ValidationState;
29+
) => ValidationState | Promise<ValidationState>;
3030

3131
export type TextTransformer = (text: string) => string;
3232

src/lib/utils.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ import type { CustomValidator, InputDataType, ValidationState } from '$lib/types
1010
* @returns ValidationState : InProgress | Valid | Invalid
1111
*/
1212

13-
export function validateInput(
13+
export async function validateInput(
1414
inputValue: string,
1515
dataType: InputDataType,
1616
validPattern: RegExp | null,
1717
inProgressPattern: RegExp | null,
1818
customValidators: CustomValidator[]
19-
): ValidationState {
19+
): Promise<ValidationState> {
2020
let validationResult: ValidationState = 'Valid';
2121

2222
switch (dataType) {
@@ -40,17 +40,19 @@ export function validateInput(
4040
break;
4141
}
4242

43-
customValidators.forEach((validator: CustomValidator) => {
44-
const currentResult = validator(inputValue, validationResult);
43+
for (const validator of customValidators) {
44+
const currentResult = await Promise.resolve(validator(inputValue, validationResult));
4545
if (currentResult === 'Invalid') {
4646
validationResult = 'Invalid';
47+
break;
4748
} else if (currentResult === 'InProgress') {
4849
validationResult = 'InProgress';
50+
break;
4951
} else {
5052
validationResult =
5153
validationResult === 'Valid' && currentResult === 'Valid' ? 'Valid' : validationResult;
5254
}
53-
});
55+
}
5456

5557
return validationResult;
5658
}

0 commit comments

Comments
 (0)