MCPcopy Create free account
hub / github.com/fastify/fastify / validate

Function validate

lib/validation.js:146–210  ·  view source on GitHub ↗
(context, request, execution)

Source from the content-addressed store, hash-verified

144}
145
146function validate (context, request, execution) {
147 const runExecution = execution === undefined
148
149 if (runExecution &&
150 context[paramsSchema] === undefined &&
151 context[bodySchema] === undefined &&
152 context[querystringSchema] === undefined &&
153 context[headersSchema] === undefined) {
154 // the route has no validation schemas
155 return false
156 }
157
158 if (runExecution || !execution.skipParams) {
159 const params = validateParam(context[paramsSchema], request, 'params')
160 if (params) {
161 if (typeof params.then !== 'function') {
162 return wrapValidationError(params, 'params', context.schemaErrorFormatter)
163 } else {
164 return validateAsyncParams(params, context, request)
165 }
166 }
167 }
168
169 if (runExecution || !execution.skipBody) {
170 let validatorFunction = null
171 if (typeof context[bodySchema] === 'function') {
172 validatorFunction = context[bodySchema]
173 } else if (context[bodySchema]) {
174 const contentSchema = context[bodySchema][request.mediaType]
175 if (contentSchema) {
176 validatorFunction = contentSchema
177 }
178 }
179 const body = validateParam(validatorFunction, request, 'body')
180 if (body) {
181 if (typeof body.then !== 'function') {
182 return wrapValidationError(body, 'body', context.schemaErrorFormatter)
183 } else {
184 return validateAsyncBody(body, context, request)
185 }
186 }
187 }
188
189 if (runExecution || !execution.skipQuery) {
190 const query = validateParam(context[querystringSchema], request, 'query')
191 if (query) {
192 if (typeof query.then !== 'function') {
193 return wrapValidationError(query, 'querystring', context.schemaErrorFormatter)
194 } else {
195 return validateAsyncQuery(query, context, request)
196 }
197 }
198 }
199
200 const headers = validateParam(context[headersSchema], request, 'headers')
201 if (headers) {
202 if (typeof headers.then !== 'function') {
203 return wrapValidationError(headers, 'headers', context.schemaErrorFormatter)

Callers 5

validateAsyncParamsFunction · 0.70
validateAsyncBodyFunction · 0.70
validateAsyncQueryFunction · 0.70
validateInitialConfigFunction · 0.70
request.jsFile · 0.70

Calls 6

validateParamFunction · 0.85
wrapValidationErrorFunction · 0.85
validateAsyncParamsFunction · 0.85
validateAsyncBodyFunction · 0.85
validateAsyncQueryFunction · 0.85
validateAsyncHeadersFunction · 0.85

Tested by

no test coverage detected