forked from gcanti/tcomb-validation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.d.ts
93 lines (85 loc) · 2.08 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import * as t from 'tcomb';
export * from 'tcomb';
// Augment 'tcomb': Add getValidationErrorMessage.
declare module 'tcomb' {
export interface Type<T> {
/**
* Allows customization of the error message for a type.
*
* (Extension from tcomb-validation)
*
* @param actual Current value
* @param path Path to validate
* @param context Additional metadata.
*/
getValidationErrorMessage(actual: T, path: Path, context: any): string;
}
}
/**
* Defines a path through the properties of an
* object (string, property name) or array (number, index).
*/
type Path = Array<string | number>;
type Predicate<T> = (value: T) => boolean;
export interface ValidationError {
/**
* Error message.
*/
message: string;
/**
* Current value.
*/
actual: any;
/**
* Expected type.
*/
expected: t.Type<any>;
/**
* Path to the property/index that failed validation.
*/
path: Path;
}
/**
* Result of a validation.
*/
export interface ValidationResult {
/**
* True if there are no validation errors. False otherwise.
*/
isValid(): boolean;
/**
* Returns the first error, if any. Null otherwise.
*/
firstError(): ValidationError | null;
/**
* Contains the validation errors, if any. Empty if none.
*/
errors: Array<ValidationError>;
}
/**
* Options for the validate function.
*/
interface ValidateOptions {
/**
* Path prefix for validation.
*/
path?: Path;
/**
* Data passed to getValidationErrorMessage.
*/
context?: any;
/**
* If true, no additional properties are allowed
* when validating structs.
*
* Defaults to false.
*/
strict?: boolean;
}
/**
* Validates an object and returns the validation result.
* @param value The value to validate.
* @param type The type to validate against.
* @param options Validation options. Optional.
*/
export function validate<T>(value: any, type: t.Type<T>, options?: ValidateOptions): ValidationResult;