-
Notifications
You must be signed in to change notification settings - Fork 816
/
Copy pathindex.ts
142 lines (129 loc) · 4.77 KB
/
index.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { ValidationError } from './validation/ValidationError';
import { ValidatorOptions } from './validation/ValidatorOptions';
import { ValidationSchema } from './validation-schema/ValidationSchema';
import { getMetadataStorage } from './metadata/MetadataStorage';
import { Validator } from './validation/Validator';
import { getFromContainer } from './container';
// -------------------------------------------------------------------------
// Export everything api users needs
// -------------------------------------------------------------------------
export * from './container';
export * from './decorator/decorators';
export * from './decorator/ValidationOptions';
export * from './validation/ValidatorConstraintInterface';
export * from './validation/ValidationError';
export * from './validation/ValidatorOptions';
export * from './validation/ValidationArguments';
export * from './validation/ValidationTypes';
export * from './validation/Validator';
export * from './validation-schema/ValidationSchema';
export * from './register-decorator';
export * from './metadata/MetadataStorage';
// -------------------------------------------------------------------------
// Shortcut methods for api users
// -------------------------------------------------------------------------
/**
* Validates given object.
*/
export function validate(object: object, validatorOptions?: ValidatorOptions): Promise<ValidationError[]>;
/**
* Validates given object by a given validation schema.
*/
export function validate(
schemaName: string,
object: object,
validatorOptions?: ValidatorOptions
): Promise<ValidationError[]>;
/**
* Validates given object by object's decorators or given validation schema.
*/
export function validate(
schemaNameOrObject: object | string,
objectOrValidationOptions?: object | ValidatorOptions,
maybeValidatorOptions?: ValidatorOptions
): Promise<ValidationError[]> {
if (typeof schemaNameOrObject === 'string') {
return getFromContainer(Validator).validate(
schemaNameOrObject,
objectOrValidationOptions as object,
maybeValidatorOptions
);
} else {
return getFromContainer(Validator).validate(schemaNameOrObject, objectOrValidationOptions as ValidatorOptions);
}
}
/**
* Validates given object and reject on error.
*/
export function validateOrReject(object: object, validatorOptions?: ValidatorOptions): Promise<void>;
/**
* Validates given object by a given validation schema and reject on error.
*/
export function validateOrReject(
schemaName: string,
object: object,
validatorOptions?: ValidatorOptions
): Promise<void>;
/**
* Validates given object by object's decorators or given validation schema and reject on error.
*/
export function validateOrReject(
schemaNameOrObject: object | string,
objectOrValidationOptions?: object | ValidatorOptions,
maybeValidatorOptions?: ValidatorOptions
): Promise<void> {
if (typeof schemaNameOrObject === 'string') {
return getFromContainer(Validator).validateOrReject(
schemaNameOrObject,
objectOrValidationOptions as object,
maybeValidatorOptions
);
} else {
return getFromContainer(Validator).validateOrReject(
schemaNameOrObject,
objectOrValidationOptions as ValidatorOptions
);
}
}
/**
* Performs sync validation of the given object.
* Note that this method completely ignores async validations.
* If you want to properly perform validation you need to call validate method instead.
*/
export function validateSync(object: object, validatorOptions?: ValidatorOptions): ValidationError[];
/**
* Validates given object by a given validation schema.
* Note that this method completely ignores async validations.
* If you want to properly perform validation you need to call validate method instead.
*/
export function validateSync(
schemaName: string,
object: object,
validatorOptions?: ValidatorOptions
): ValidationError[];
/**
* Validates given object by object's decorators or given validation schema.
* Note that this method completely ignores async validations.
* If you want to properly perform validation you need to call validate method instead.
*/
export function validateSync(
schemaNameOrObject: object | string,
objectOrValidationOptions?: object | ValidatorOptions,
maybeValidatorOptions?: ValidatorOptions
): ValidationError[] {
if (typeof schemaNameOrObject === 'string') {
return getFromContainer(Validator).validateSync(
schemaNameOrObject,
objectOrValidationOptions as object,
maybeValidatorOptions
);
} else {
return getFromContainer(Validator).validateSync(schemaNameOrObject, objectOrValidationOptions as ValidatorOptions);
}
}
/**
* Registers a new validation schema.
*/
export function registerSchema(schema: ValidationSchema): void {
getMetadataStorage().addValidationSchema(schema);
}