-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.d.ts
51 lines (44 loc) · 1.56 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
type ValidationResultErrors<TData> = {
[K in keyof TData]: TData[K] extends object
? ValidationResultErrors<TData[K]>
: string[];
}
type ValidatorResult<TData> = {
errorCount: number;
errors: ValidationResultErrors<TData>
}
type FieldRule<TData> = {
errorMessage: (key: string, data: TData) => string;
} & (
{ name: string } | { rule: (key: string, data: TData) => (Promise<boolean> | boolean) }
)
type ValidatorRules<TData> = {
[K in keyof TData]: string | (string | FieldRule<TData>)[]
}
type Validator = {
/**
* Runs validation using the supplied set of rules
* @param data the data to be validated
* @param rules the rules to apply
* @returns the result of the validation
*/
validate<TData> (data: TData, rules: ValidatorRules<TData>): Promise<ValidatorResult<TData>>;
/**
* Allows more rules to be defined.
* @param name the name of the rule to add; if it already exists it will be overwritten
* @param ruleDefinition the rule definition - return true if it passes, otherwise false
*/
addRule<TData> (name: string, ruleDefinition: {
rule: (key: string, data: TData) => Promise<boolean>,
errorMessage: (key: string, data: TData) => string,
}): void;
/**
* Allows an error handler (for outputting error messages) to be redefined
* @param name the name of the rule the handler applies to
* @param handler the error handler
*/
setErrorHandler<TData> (name: string, handler: (key: string, data: TData) => string): void;
};
declare module '@garrypas/gp-validator' {
export const create: () => Validator;
}