-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathexpress-joi-validation.d.ts
125 lines (116 loc) · 3.33 KB
/
express-joi-validation.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
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
import * as Joi from 'joi'
import * as express from 'express'
import { IncomingHttpHeaders } from 'http'
import { ParsedQs } from 'qs'
/**
* Creates an instance of this module that can be used to generate middleware
* @param cfg
*/
export function createValidator(cfg?: ExpressJoiConfig): ExpressJoiInstance
/**
* These are the named properties on an express.Request that this module can
* validate, e.g "body" or "query"
*/
export enum ContainerTypes {
Body = 'body',
Query = 'query',
Headers = 'headers',
Fields = 'fields',
Params = 'params'
}
/**
* Use this in you express error handler if you've set *passError* to true
* when calling *createValidator*
*/
export type ExpressJoiError<TSchema = any> = Extract<
Joi.ValidationResult<TSchema>,
{ error: Joi.ValidationError }
> & {
type: ContainerTypes;
};
/**
* A schema that developers should extend to strongly type the properties
* (query, body, etc.) of incoming express.Request passed to a request handler.
*/
export type ValidatedRequestSchema = Record<ContainerTypes, any>
/**
* Use this in conjunction with *ValidatedRequestSchema* instead of
* express.Request for route handlers. This ensures *req.query*,
* *req.body* and others are strongly typed using your
* *ValidatedRequestSchema*
*/
export interface ValidatedRequest<T extends ValidatedRequestSchema>
extends express.Request {
body: T[ContainerTypes.Body]
query: T[ContainerTypes.Query] & ParsedQs
headers: T[ContainerTypes.Headers]
params: T[ContainerTypes.Params]
}
/**
* Use this in conjunction with *ValidatedRequestSchema* instead of
* express.Request for route handlers. This ensures *req.query*,
* *req.body* and others are strongly typed using your *ValidatedRequestSchema*
*
* This will also allow you to access the original body, params, etc. as they
* were before validation.
*/
export interface ValidatedRequestWithRawInputsAndFields<
T extends ValidatedRequestSchema
> extends express.Request {
body: T[ContainerTypes.Body]
query: T[ContainerTypes.Query]
headers: T[ContainerTypes.Headers]
params: T[ContainerTypes.Params]
fields: T[ContainerTypes.Fields]
originalBody: any
originalQuery: any
originalHeaders: IncomingHttpHeaders
originalParams: any
originalFields: any
}
/**
* Configuration options supported by *createValidator(config)*
*/
export interface ExpressJoiConfig {
statusCode?: number
passError?: boolean
joi?: object
}
/**
* Configuration options supported by middleware, e.g *validator.body(config)*
*/
export interface ExpressJoiContainerConfig {
joi?: Joi.ValidationOptions
statusCode?: number
passError?: boolean
}
/**
* A validator instance that can be used to generate middleware. Is returned by
* calling *createValidator*
*/
export interface ExpressJoiInstance {
body(
schema: Joi.Schema,
cfg?: ExpressJoiContainerConfig
): express.RequestHandler
query(
schema: Joi.Schema,
cfg?: ExpressJoiContainerConfig
): express.RequestHandler
params(
schema: Joi.Schema,
cfg?: ExpressJoiContainerConfig
): express.RequestHandler
headers(
schema: Joi.Schema,
cfg?: ExpressJoiContainerConfig
): express.RequestHandler
fields(
schema: Joi.Schema,
cfg?: ExpressJoiContainerConfig
): express.RequestHandler
response(
schema: Joi.Schema,
cfg?: ExpressJoiContainerConfig
): express.RequestHandler
}