1 | import { Request, ValidationError } from './base';
|
2 |
|
3 |
|
4 |
|
5 | export type ErrorFormatter<T = any> = (error: ValidationError) => T;
|
6 | type ToArrayOptions = {
|
7 | |
8 |
|
9 |
|
10 |
|
11 | onlyFirstError?: boolean;
|
12 | };
|
13 | export type ResultFactory<T> = (req: Request) => Result<T>;
|
14 | interface ResultFactoryBuilderOptions<T = any> {
|
15 | |
16 |
|
17 |
|
18 |
|
19 | formatter: ErrorFormatter<T>;
|
20 | }
|
21 |
|
22 |
|
23 |
|
24 | export declare const validationResult: ResultFactory<ValidationError> & {
|
25 | withDefaults: typeof withDefaults;
|
26 | };
|
27 |
|
28 |
|
29 |
|
30 | export declare class Result<T = any> {
|
31 | private formatter;
|
32 | private readonly errors;
|
33 | constructor(formatter: ErrorFormatter<T>, errors: readonly ValidationError[]);
|
34 | /**
|
35 | * Gets the validation errors as an array.
|
36 | *
|
37 | * @param options.onlyFirstError whether only the first error of each
|
38 | */
|
39 | array(options?: ToArrayOptions): T[];
|
40 | /**
|
41 | * Gets the validation errors as an object.
|
42 | * If a field has more than one error, only the first one is set in the resulting object.
|
43 | *
|
44 | * @returns an object from field name to error
|
45 | */
|
46 | mapped(): Record<string, T>;
|
47 | /**
|
48 | * Specifies a function to format errors with.
|
49 | * @param formatter the function to use for formatting errors
|
50 | * @returns A new {@link Result} instance with the given formatter
|
51 | */
|
52 | formatWith<T2>(formatter: ErrorFormatter<T2>): Result<T2>;
|
53 | |
54 |
|
55 |
|
56 | isEmpty(): boolean;
|
57 | |
58 |
|
59 |
|
60 | throw(): void;
|
61 | }
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | declare function withDefaults<T = any>(options?: Partial<ResultFactoryBuilderOptions<T>>): ResultFactory<T>;
|
67 | export {};
|