Skip to content

Commit ad26813

Browse files
committed
Parse the format response instead of assuming it's valid
1 parent cd8c762 commit ad26813

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

ui/frontend/reducers/output/format.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit';
2+
import * as z from 'zod';
23

34
import { adaptFetchError, jsonPost, routes } from '../../actions';
45
import { formatRequestSelector } from '../../selectors';
@@ -21,19 +22,22 @@ interface FormatRequestBody {
2122
edition: string;
2223
}
2324

24-
interface FormatResponseBody {
25-
success: boolean;
26-
code: string;
27-
stdout: string;
28-
stderr: string;
29-
}
25+
const FormatResponseBody = z.object({
26+
success: z.boolean(),
27+
code: z.string(),
28+
stdout: z.string(),
29+
stderr: z.string(),
30+
});
31+
32+
type FormatResponseBody = z.infer<typeof FormatResponseBody>;
3033

3134
export const performFormat = createAsyncThunk<FormatResponseBody, void, { state: RootState }>(
3235
sliceName,
3336
async (_arg: void, { getState }) => {
3437
const body: FormatRequestBody = formatRequestSelector(getState());
3538

36-
return adaptFetchError(() => jsonPost<FormatResponseBody>(routes.format, body));
39+
const d = await adaptFetchError(() => jsonPost(routes.format, body));
40+
return FormatResponseBody.parseAsync(d);
3741
},
3842
);
3943

0 commit comments

Comments
 (0)