| 55 | } |
| 56 | |
| 57 | function compileSchemasForValidation (context, compile, isCustom) { |
| 58 | const { schema } = context |
| 59 | if (!schema) { |
| 60 | return |
| 61 | } |
| 62 | |
| 63 | const { method, url } = context.config || {} |
| 64 | |
| 65 | const headers = schema.headers |
| 66 | // the or part is used for backward compatibility |
| 67 | if (headers && (isCustom || Object.getPrototypeOf(headers) !== Object.prototype)) { |
| 68 | // do not mess with schema when custom validator applied, e.g. Joi, Typebox |
| 69 | context[headersSchema] = compile({ schema: headers, method, url, httpPart: 'headers' }) |
| 70 | } else if (headers) { |
| 71 | // The header keys are case insensitive |
| 72 | // https://datatracker.ietf.org/doc/html/rfc2616#section-4.2 |
| 73 | const headersSchemaLowerCase = {} |
| 74 | Object.keys(headers).forEach(k => { headersSchemaLowerCase[k] = headers[k] }) |
| 75 | if (headersSchemaLowerCase.required instanceof Array) { |
| 76 | headersSchemaLowerCase.required = headersSchemaLowerCase.required.map(h => h.toLowerCase()) |
| 77 | } |
| 78 | if (headers.properties) { |
| 79 | headersSchemaLowerCase.properties = {} |
| 80 | Object.keys(headers.properties).forEach(k => { |
| 81 | headersSchemaLowerCase.properties[k.toLowerCase()] = headers.properties[k] |
| 82 | }) |
| 83 | } |
| 84 | context[headersSchema] = compile({ schema: headersSchemaLowerCase, method, url, httpPart: 'headers' }) |
| 85 | } else if (Object.hasOwn(schema, 'headers')) { |
| 86 | FSTWRN001('headers', method, url) |
| 87 | } |
| 88 | |
| 89 | if (schema.body) { |
| 90 | const contentProperty = schema.body.content |
| 91 | if (contentProperty) { |
| 92 | const contentTypeSchemas = {} |
| 93 | for (const contentType of Object.keys(contentProperty)) { |
| 94 | const contentSchema = contentProperty[contentType].schema |
| 95 | contentTypeSchemas[contentType] = compile({ schema: contentSchema, method, url, httpPart: 'body', contentType }) |
| 96 | } |
| 97 | context[bodySchema] = contentTypeSchemas |
| 98 | } else { |
| 99 | context[bodySchema] = compile({ schema: schema.body, method, url, httpPart: 'body' }) |
| 100 | } |
| 101 | } else if (Object.hasOwn(schema, 'body')) { |
| 102 | FSTWRN001('body', method, url) |
| 103 | } |
| 104 | |
| 105 | if (schema.querystring) { |
| 106 | context[querystringSchema] = compile({ schema: schema.querystring, method, url, httpPart: 'querystring' }) |
| 107 | } else if (Object.hasOwn(schema, 'querystring')) { |
| 108 | FSTWRN001('querystring', method, url) |
| 109 | } |
| 110 | |
| 111 | if (schema.params) { |
| 112 | context[paramsSchema] = compile({ schema: schema.params, method, url, httpPart: 'params' }) |
| 113 | } else if (Object.hasOwn(schema, 'params')) { |
| 114 | FSTWRN001('params', method, url) |