Skip to content

Commit 83391c9

Browse files
committed
fixed lint issue
1 parent 78db98a commit 83391c9

File tree

5 files changed

+137
-97
lines changed

5 files changed

+137
-97
lines changed

examples/snippets/event-handler/rest/validation_basic.ts

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,40 @@ const userResponseSchema = z.object({
1818
});
1919

2020
// Validate request body
21-
app.post('/users', async () => {
22-
return {
23-
id: '123',
24-
name: 'John Doe',
25-
26-
createdAt: new Date().toISOString(),
27-
};
28-
}, {
29-
validation: { req: { body: createUserSchema } },
30-
});
21+
app.post(
22+
'/users',
23+
async () => {
24+
return {
25+
id: '123',
26+
name: 'John Doe',
27+
28+
createdAt: new Date().toISOString(),
29+
};
30+
},
31+
{
32+
validation: { req: { body: createUserSchema } },
33+
}
34+
);
3135

3236
// Validate both request and response
33-
app.get('/users/:id', async (reqCtx) => {
34-
const { id } = reqCtx.params;
35-
36-
return {
37-
id,
38-
name: 'John Doe',
39-
40-
createdAt: new Date().toISOString(),
41-
};
42-
}, {
43-
validation: {
44-
req: { path: z.object({ id: z.string().uuid() }) },
45-
res: { body: userResponseSchema },
37+
app.get(
38+
'/users/:id',
39+
async (reqCtx) => {
40+
const { id } = reqCtx.params;
41+
42+
return {
43+
id,
44+
name: 'John Doe',
45+
46+
createdAt: new Date().toISOString(),
47+
};
4648
},
47-
});
49+
{
50+
validation: {
51+
req: { path: z.object({ id: z.string().uuid() }) },
52+
res: { body: userResponseSchema },
53+
},
54+
}
55+
);
4856

4957
export const handler = app.resolve.bind(app);

examples/snippets/event-handler/rest/validation_error_handling.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@ const userSchema = z.object({
99
age: z.number().int().positive('Age must be positive'),
1010
});
1111

12-
app.post('/users', async () => {
13-
return {
14-
id: '123',
15-
name: 'John Doe',
16-
17-
age: 30,
18-
};
19-
}, {
20-
validation: { req: { body: userSchema } },
21-
});
12+
app.post(
13+
'/users',
14+
async () => {
15+
return {
16+
id: '123',
17+
name: 'John Doe',
18+
19+
age: 30,
20+
};
21+
},
22+
{
23+
validation: { req: { body: userSchema } },
24+
}
25+
);
2226

2327
export const handler = app.resolve.bind(app);

examples/snippets/event-handler/rest/validation_query_headers.ts

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,52 +10,64 @@ const listUsersQuerySchema = z.object({
1010
sort: z.enum(['name', 'email', 'createdAt']).optional(),
1111
});
1212

13-
app.get('/users', async () => {
14-
return {
15-
users: [],
16-
pagination: {
17-
page: 1,
18-
limit: 10,
19-
},
20-
};
21-
}, {
22-
validation: { req: { query: listUsersQuerySchema } },
23-
});
13+
app.get(
14+
'/users',
15+
async () => {
16+
return {
17+
users: [],
18+
pagination: {
19+
page: 1,
20+
limit: 10,
21+
},
22+
};
23+
},
24+
{
25+
validation: { req: { query: listUsersQuerySchema } },
26+
}
27+
);
2428

2529
// Validate headers
2630
const apiKeyHeaderSchema = z.object({
2731
'x-api-key': z.string().min(32),
2832
'content-type': z.string().optional(),
2933
});
3034

31-
app.post('/protected', async () => {
32-
return { message: 'Access granted' };
33-
}, {
34-
validation: { req: { headers: apiKeyHeaderSchema } },
35-
});
35+
app.post(
36+
'/protected',
37+
async () => {
38+
return { message: 'Access granted' };
39+
},
40+
{
41+
validation: { req: { headers: apiKeyHeaderSchema } },
42+
}
43+
);
3644

3745
// Validate multiple components
38-
app.post('/users/:id/posts', async (reqCtx) => {
39-
const { id } = reqCtx.params;
40-
41-
return {
42-
postId: '456',
43-
userId: id,
44-
title: 'New Post',
45-
};
46-
}, {
47-
validation: {
48-
req: {
49-
path: z.object({ id: z.string().uuid() }),
50-
body: z.object({
51-
title: z.string().min(1).max(200),
52-
content: z.string().min(1),
53-
}),
54-
headers: z.object({
55-
'content-type': z.literal('application/json'),
56-
}),
57-
},
46+
app.post(
47+
'/users/:id/posts',
48+
async (reqCtx) => {
49+
const { id } = reqCtx.params;
50+
51+
return {
52+
postId: '456',
53+
userId: id,
54+
title: 'New Post',
55+
};
5856
},
59-
});
57+
{
58+
validation: {
59+
req: {
60+
path: z.object({ id: z.string().uuid() }),
61+
body: z.object({
62+
title: z.string().min(1).max(200),
63+
content: z.string().min(1),
64+
}),
65+
headers: z.object({
66+
'content-type': z.literal('application/json'),
67+
}),
68+
},
69+
},
70+
}
71+
);
6072

6173
export const handler = app.resolve.bind(app);

packages/event-handler/src/rest/middleware/validation.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,21 @@ export const createValidationMiddleware = (
5353
// Validate response
5454
if (resSchemas) {
5555
const response = reqCtx.res;
56-
56+
5757
if (resSchemas.body) {
5858
const clonedResponse = response.clone();
5959
const contentType = response.headers.get('content-type');
60-
60+
6161
let bodyData: unknown;
6262
if (contentType?.includes('application/json')) {
6363
bodyData = await clonedResponse.json();
6464
} else {
6565
bodyData = await clonedResponse.text();
6666
}
67-
67+
6868
await validateComponent(resSchemas.body, bodyData, 'body', false);
6969
}
70-
70+
7171
if (resSchemas.headers) {
7272
const headers = Object.fromEntries(response.headers.entries());
7373
await validateComponent(resSchemas.headers, headers, 'headers', false);
@@ -83,11 +83,11 @@ async function validateComponent(
8383
isRequest: boolean
8484
): Promise<void> {
8585
const result = await schema['~standard'].validate(data);
86-
86+
8787
if ('issues' in result) {
8888
const message = `Validation failed for ${isRequest ? 'request' : 'response'} ${component}`;
8989
const error = new Error('Validation failed');
90-
90+
9191
if (isRequest) {
9292
throw new RequestValidationError(message, component, error);
9393
}

packages/event-handler/tests/unit/rest/middleware/validation.test.ts

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,9 @@ describe('Validation Middleware', () => {
9797
// Prepare
9898
const pathSchema = z.object({ id: z.string() });
9999
const validateSpy = vi.spyOn(pathSchema['~standard'], 'validate');
100-
app.get(
101-
'/users/:id',
102-
async (reqCtx) => ({ id: reqCtx.params.id }),
103-
{
104-
validation: { req: { path: pathSchema } },
105-
}
106-
);
100+
app.get('/users/:id', async (reqCtx) => ({ id: reqCtx.params.id }), {
101+
validation: { req: { path: pathSchema } },
102+
});
107103
const event = {
108104
...createTestEvent('/users/123', 'GET'),
109105
pathParameters: { id: '123' },
@@ -154,7 +150,10 @@ describe('Validation Middleware', () => {
154150

155151
// Assess
156152
expect(result.statusCode).toBe(HttpStatusCodes.OK);
157-
expect(validateSpy).toHaveBeenCalledExactlyOnceWith({ page: '1', limit: '10' });
153+
expect(validateSpy).toHaveBeenCalledExactlyOnceWith({
154+
page: '1',
155+
limit: '10',
156+
});
158157
});
159158

160159
it('returns 422 on query parameters validation failure', async () => {
@@ -218,7 +217,9 @@ describe('Validation Middleware', () => {
218217
const unvalidatedResult = await app.resolve(unvalidatedEvent, context);
219218

220219
// Assess
221-
expect(validatedResult.statusCode).toBe(HttpStatusCodes.UNPROCESSABLE_ENTITY);
220+
expect(validatedResult.statusCode).toBe(
221+
HttpStatusCodes.UNPROCESSABLE_ENTITY
222+
);
222223
expect(unvalidatedResult.statusCode).toBe(HttpStatusCodes.OK);
223224
expect(validateSpy).toHaveBeenCalledExactlyOnceWith({ invalid: 'data' });
224225
});
@@ -262,7 +263,10 @@ describe('Validation Middleware', () => {
262263

263264
// Assess
264265
expect(result.statusCode).toBe(HttpStatusCodes.OK);
265-
expect(validateSpy).toHaveBeenCalledExactlyOnceWith({ id: '123', name: 'John' });
266+
expect(validateSpy).toHaveBeenCalledExactlyOnceWith({
267+
id: '123',
268+
name: 'John',
269+
});
266270
});
267271

268272
it('validates response headers successfully', async () => {
@@ -286,13 +290,17 @@ describe('Validation Middleware', () => {
286290
// Prepare
287291
const responseSchema = z.string();
288292
const validateSpy = vi.spyOn(responseSchema['~standard'], 'validate');
289-
app.get('/text', () => {
290-
return new Response('plain text', {
291-
headers: { 'content-type': 'text/plain' },
292-
});
293-
}, {
294-
validation: { res: { body: responseSchema } },
295-
});
293+
app.get(
294+
'/text',
295+
() => {
296+
return new Response('plain text', {
297+
headers: { 'content-type': 'text/plain' },
298+
});
299+
},
300+
{
301+
validation: { res: { body: responseSchema } },
302+
}
303+
);
296304
const event = createTestEvent('/text', 'GET');
297305

298306
// Act
@@ -330,7 +338,10 @@ describe('Validation Middleware', () => {
330338
const requestSchema = z.object({ name: z.string() });
331339
const responseSchema = z.object({ id: z.string(), name: z.string() });
332340
const requestValidateSpy = vi.spyOn(requestSchema['~standard'], 'validate');
333-
const responseValidateSpy = vi.spyOn(responseSchema['~standard'], 'validate');
341+
const responseValidateSpy = vi.spyOn(
342+
responseSchema['~standard'],
343+
'validate'
344+
);
334345
app.post('/users', async () => ({ id: '123', name: 'John' }), {
335346
validation: {
336347
req: { body: requestSchema },
@@ -349,7 +360,12 @@ describe('Validation Middleware', () => {
349360

350361
// Assess
351362
expect(result.statusCode).toBe(HttpStatusCodes.OK);
352-
expect(requestValidateSpy).toHaveBeenCalledExactlyOnceWith({ name: 'John' });
353-
expect(responseValidateSpy).toHaveBeenCalledExactlyOnceWith({ id: '123', name: 'John' });
363+
expect(requestValidateSpy).toHaveBeenCalledExactlyOnceWith({
364+
name: 'John',
365+
});
366+
expect(responseValidateSpy).toHaveBeenCalledExactlyOnceWith({
367+
id: '123',
368+
name: 'John',
369+
});
354370
});
355371
});

0 commit comments

Comments
 (0)