-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
205 lines (182 loc) · 5.8 KB
/
index.js
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
const fp = require('fastify-plugin')
const {
ErrorPrefix,
getErrorMessage,
hasProperties,
initialExcludes,
isSchemaTypeExcluded,
SCHEMA_TYPES,
isSchemaDisabled
} = require('./utils')
function FastifyEnforceSchema (fastify, opts, done) {
if (!Object.prototype.hasOwnProperty.call(opts, 'disabled')) {
opts.disabled = []
}
if (
opts.disabled === true ||
(Array.isArray(opts.disabled) && isSchemaDisabled(opts.disabled))
) {
done()
return
}
if (!Object.prototype.hasOwnProperty.call(opts, 'exclude')) {
opts.exclude = []
}
const { disabled, exclude } = opts
fastify.addHook('onRoute', (routeOptions) => {
if (
routeOptions.path === '*' ||
!routeOptions.path ||
routeOptions.schema === false
) {
done()
return
}
const excludedEntity = [...initialExcludes, ...exclude].find(({ url }) =>
new RegExp(url).test(routeOptions.path)
)
const hasSchemas =
typeof excludedEntity === 'object' &&
Object.prototype.hasOwnProperty.call(excludedEntity, 'excludedSchemas')
if (excludedEntity && !hasSchemas) {
done()
return
}
if (!routeOptions?.schema) {
throw new Error(getErrorMessage({ schema: true }, routeOptions))
}
if (
Object.prototype.hasOwnProperty.call(routeOptions.schema, 'disabled') &&
!Array.isArray(routeOptions.schema.disabled)
) {
throw new Error(
getErrorMessage(
{ message: 'schema.disabled must be an array' },
routeOptions
)
)
}
if (
routeOptions?.schema?.response !== false &&
!routeOptions?.schema?.disabled?.includes(SCHEMA_TYPES.response) &&
!isSchemaTypeExcluded(excludedEntity, SCHEMA_TYPES.response) &&
disabled.indexOf(SCHEMA_TYPES.response) === -1
) {
const responseKeys = Object.keys(routeOptions?.schema?.response || {})
if (!routeOptions?.schema?.response) {
throw new Error(
getErrorMessage({ schemaType: SCHEMA_TYPES.response }, routeOptions)
)
}
if (routeOptions?.schema?.response && !responseKeys.length) {
throw new Error(
getErrorMessage(
{
message:
'no HTTP status codes were provided in the response schema'
},
routeOptions
)
)
}
responseKeys.forEach((value) => {
if (
value === 'default' ||
['1xx', '2xx', '3xx', '4xx', '5xx'].includes(value) ||
(value >= 100 && value <= 599)
) {
if (hasProperties(routeOptions, SCHEMA_TYPES.response, value)) {
done()
return
}
throw new Error(
getErrorMessage(
{
message: `schema ${SCHEMA_TYPES.response} key "${value}" must be a non-empty object`
},
routeOptions
)
)
}
if (Number.isInteger(parseInt(value, 10))) {
throw new Error(
getErrorMessage(
{
message: `schema ${SCHEMA_TYPES.response} key "${value}" must be a valid HTTP code which ranges from 100 - 599`
},
routeOptions
)
)
}
throw new Error(
getErrorMessage(
{
message: `"${value}" is not \`default\` or a supported HTTP status code`
},
routeOptions
)
)
})
}
// NOTE: Deprecation will be removed in the next version
if (routeOptions?.schema?.body === false) {
process.emitWarning(
`[${ErrorPrefix}] Setting "schema.body" to false is deprecated. Please use the "schema.disabled" option instead.`,
'DeprecationWarning'
)
if (Array.isArray(routeOptions.schema.disabled)) {
if (routeOptions.schema.disabled.indexOf(SCHEMA_TYPES.body) === -1) {
routeOptions.schema.disabled.push(SCHEMA_TYPES.body)
}
} else {
routeOptions.schema.disabled = [SCHEMA_TYPES.body]
}
delete routeOptions?.schema.body
}
if (
!routeOptions?.schema?.disabled?.includes(SCHEMA_TYPES.body) &&
!isSchemaTypeExcluded(excludedEntity, SCHEMA_TYPES.body) &&
['POST', 'PUT', 'PATCH'].includes(routeOptions.method) &&
disabled.indexOf(SCHEMA_TYPES.body) === -1 &&
!hasProperties(routeOptions, SCHEMA_TYPES.body)
) {
throw new Error(
getErrorMessage({ schemaType: SCHEMA_TYPES.body }, routeOptions)
)
}
// NOTE: Deprecation will be removed in the next version
if (routeOptions?.schema?.params === false) {
process.emitWarning(
`[${ErrorPrefix}] Setting "schema.params" to false is deprecated. Please use the "schema.disabled" option instead.`,
'DeprecationWarning'
)
if (Array.isArray(routeOptions.schema.disabled)) {
if (routeOptions.schema.disabled.indexOf(SCHEMA_TYPES.params) === -1) {
routeOptions.schema.disabled.push(SCHEMA_TYPES.params)
}
} else {
routeOptions.schema.disabled = [SCHEMA_TYPES.params]
}
delete routeOptions?.schema.params
}
if (
!routeOptions?.schema?.disabled?.includes(SCHEMA_TYPES.params) &&
/:\w+/.test(routeOptions.url) &&
!isSchemaTypeExcluded(excludedEntity, SCHEMA_TYPES.params) &&
disabled.indexOf(SCHEMA_TYPES.params) === -1 &&
!hasProperties(routeOptions, SCHEMA_TYPES.params)
) {
throw new Error(
getErrorMessage({ schemaType: SCHEMA_TYPES.params }, routeOptions)
)
}
})
done()
}
const _fastifyEnforceSchema = fp(FastifyEnforceSchema, {
fastify: '4.x',
name: 'fastify-enforce-schema'
})
module.exports = _fastifyEnforceSchema
module.exports.FastifyEnforceSchema = _fastifyEnforceSchema
module.exports.default = _fastifyEnforceSchema