-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwithSpecValidation.ts
More file actions
142 lines (118 loc) · 4.3 KB
/
withSpecValidation.ts
File metadata and controls
142 lines (118 loc) · 4.3 KB
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 type { IRequest, RequestHandler } from 'itty-router';
import { error } from 'itty-router';
import type { StandardSchemaV1 } from '@standard-schema/spec';
import type {
ContractAugmentedRequest,
ContractOperationParameters,
ContractOperationQuery,
} from '../types.js';
import { validateSchema, defineProp } from '../utils.js';
import {
extractPathParamsFromUrl,
getContentType,
parseBodyByContentType,
normalizeHeaders,
validateHeadersWithFallback,
} from './utils.js';
type ContractOperation = NonNullable<ContractAugmentedRequest['__contractOperation']>;
export const withSpecValidation: RequestHandler<IRequest> = async (request: IRequest) => {
const operation = (request as ContractAugmentedRequest).__contractOperation;
if (!operation) return;
// Path params
const params = await resolveAndValidatePathParams(request, operation);
defineProp(request, 'validatedParams', params);
// Query params
const query = await resolveAndValidateQuery(request, operation);
defineProp(request, 'validatedQuery', query);
// Headers
const validatedHeaders = await resolveAndValidateHeaders(request, operation);
defineProp(request, 'validatedHeaders', validatedHeaders);
// Body
const validatedBody = await resolveAndValidateBody(request, operation);
defineProp(request, 'validatedBody', validatedBody);
};
async function resolveAndValidatePathParams(request: IRequest, operation: ContractOperation) {
const requestParams = extractPathParamsFromUrl(operation.path, request.url);
return operation.pathParams
? await validateSchema<ContractOperationParameters<ContractOperation>>(
operation.pathParams,
requestParams
)
: requestParams;
}
async function resolveAndValidateQuery(
request: IRequest,
operation: ContractOperation
): Promise<Record<string, unknown>> {
if (operation.query) {
return validateSchema<ContractOperationQuery<ContractOperation>>(
operation.query,
request.query
);
}
return {};
}
async function resolveAndValidateHeaders(
request: IRequest,
operation: ContractOperation
): Promise<Headers | undefined> {
if (operation.headers) {
const normalizedHeaders = normalizeHeaders(request.headers);
const validatedHeaders = await validateHeadersWithFallback(
operation.headers,
normalizedHeaders
);
return new Headers(validatedHeaders as Record<string, string>);
}
return undefined;
}
async function tryReadRequestText(
request: IRequest
): Promise<{ ok: true; text: string } | { ok: false }> {
try {
const text = await request.text();
return { ok: true, text };
} catch {
return { ok: false };
}
}
function findRequestSchemaEntry(
requests: Record<string, unknown>,
contentType: string
): [normalizedContentType: string, schema: unknown] | undefined {
// Slightly more robust than the original comment implied:
// - normalizes the incoming content type for matching (adds acceptance, doesn’t remove)
const normalized = contentType.toLowerCase();
const entry = Object.entries(requests).find(([key]) => key.toLowerCase() === normalized);
if (!entry) return;
return [normalized, entry[1]];
}
async function resolveAndValidateBody(
request: IRequest,
operation: ContractOperation
): Promise<unknown> {
// Preserve existing behavior: if no request schemas defined, set empty body.
if (!operation.requests) return {};
// Preserve existing behavior: body read failures become empty body.
const read = await tryReadRequestText(request);
if (!read.ok) return {};
const bodyText = read.text;
if (!bodyText.trim()) return {};
const contentType = getContentType(request);
if (!contentType) {
throw error(400, 'Content-Type header is required');
}
const entry = findRequestSchemaEntry(operation.requests, contentType);
if (!entry) {
throw error(
400,
`Unsupported Content-Type: ${contentType}. Supported types: ${Object.keys(operation.requests).join(', ')}`
);
}
const [normalizedContentType, requestSchema] = entry;
if (!requestSchema || typeof requestSchema !== 'object' || !('body' in requestSchema)) {
throw error(500, 'Invalid request schema configuration');
}
const bodyData = parseBodyByContentType(normalizedContentType, bodyText);
return await validateSchema((requestSchema as { body: StandardSchemaV1 }).body, bodyData);
}