Skip to content

Commit 5c06656

Browse files
rs-prasadmurdore
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 5c06656

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

src/lib/Input/Input.svelte

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,16 @@
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+
getValidationState(properties).then((value) => {
15+
state = value;
16+
});
17+
}
1318
1419
// For making this function reactive, prop was passed as param
15-
function getValidationState(prop: InputProperties): ValidationState {
16-
const valueValidation: ValidationState = validateInput(
20+
async function getValidationState(prop: InputProperties): Promise<ValidationState> {
21+
const valueValidation: ValidationState = await validateInput(
1722
prop.value,
1823
prop.dataType,
1924
prop.validationPattern,

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: 5 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,8 +40,8 @@ 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';
4747
} else if (currentResult === 'InProgress') {
@@ -50,7 +50,7 @@ export function validateInput(
5050
validationResult =
5151
validationResult === 'Valid' && currentResult === 'Valid' ? 'Valid' : validationResult;
5252
}
53-
});
53+
}
5454

5555
return validationResult;
5656
}

0 commit comments

Comments
 (0)