Skip to content

Commit 870946f

Browse files
committed
Draft: async validation rules
1 parent 25c017e commit 870946f

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

src/Rule.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export type RuleObject = {
1212
/**
1313
* Returns whether the rule passed with the given element(s)
1414
*/
15-
passed(elements: HTMLElement[], ...args: string[]): boolean;
15+
passed(elements: HTMLElement[], ...args: string[]): boolean | Promise<void>;
1616
/**
1717
* Message shown when the rule doesn't pass
1818
*/

src/Validator.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,13 @@ export class Validator {
105105
/**
106106
* Validate the elements
107107
*/
108-
public validate(): boolean {
108+
public async validate(): Promise<boolean> {
109109
this.errors = [];
110110

111111
if (this.hasValidatableElements()) {
112-
return !this.getRuleList()
113-
.map((rule: string) => this.validateRule(rule))
112+
return !(await Promise.all(this.getRuleList().map((rule: string) => this.validateRule(rule))))
114113
.filter((passed: boolean) => !passed)
115-
.length;
114+
.length
116115
}
117116

118117
return true;
@@ -128,7 +127,7 @@ export class Validator {
128127
/**
129128
* Validate a specific rule
130129
*/
131-
private validateRule(rule: string): boolean {
130+
private async validateRule(rule: string): Promise<boolean> {
132131
const [ruleName, ruleArgs = ''] = rule.split(':');
133132

134133
if (Validator.ruleExists(ruleName)) {

0 commit comments

Comments
 (0)