Skip to content

Commit d8bf1d9

Browse files
authored
Merge pull request #15 from devforth/add-zod-schemas
Add zod schemas
2 parents f0b205b + f4c18ba commit d8bf1d9

3 files changed

Lines changed: 37 additions & 16 deletions

File tree

index.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,20 @@ import type {
55
} from "adminforth";
66
import clone from 'clone';
77

8-
import { AdminForthPlugin, AdminForthResourcePages, suggestIfTypo } from "adminforth";
8+
import { AdminForthPlugin, parseBody, AdminForthResourcePages, suggestIfTypo } from "adminforth";
99
import { PluginOptions } from "./types.js";
1010
import { interpretResource, ActionCheckSource } from "adminforth";
11+
import { z } from "zod";
12+
13+
const startBulkActionBodySchema = z.object({
14+
resourceId: z.string(),
15+
actionId: z.union([z.string(), z.number()]),
16+
recordIds: z.array(z.union([z.string(), z.number()])),
17+
}).strict();
18+
19+
const getDefaultFiltersBodySchema = z.object({
20+
record: z.record(z.string(), z.any()).nullish(),
21+
}).strict();
1122

1223
export default class ForeignInlineListPlugin extends AdminForthPlugin {
1324
foreignResource: AdminForthResource;
@@ -56,8 +67,11 @@ export default class ForeignInlineListPlugin extends AdminForthPlugin {
5667
server.endpoint({
5768
method: 'POST',
5869
path: `/plugin/${this.pluginInstanceId}/start_bulk_action`,
59-
handler: async ({ body, adminUser, tr }) => {
60-
const { resourceId, actionId, recordIds } = body;
70+
handler: async ({ body, adminUser, tr, response: httpResponse }) => {
71+
const parsed = parseBody(startBulkActionBodySchema, body, httpResponse);
72+
if ('error' in parsed) return parsed.error;
73+
const data = parsed.data;
74+
const { resourceId, actionId, recordIds } = data;
6175
const resource = this.adminforth.config.resources.find((res) => res.resourceId == resourceId);
6276
if (!resource) {
6377
return { error: await tr(`Resource {resourceId} not found`, 'errors', { resourceId }) };
@@ -102,11 +116,14 @@ export default class ForeignInlineListPlugin extends AdminForthPlugin {
102116
server.endpoint({
103117
method: 'POST',
104118
path: `/plugin/${this.pluginInstanceId}/get_default_filters`,
105-
handler: async ({body}) => {
119+
handler: async ({ body, response }) => {
106120
if (!this.options.defaultFilters) {
107121
return { error: 'No default filters function defined', ok: false };
108122
}
109-
const record = body.record;
123+
const parsed = parseBody(getDefaultFiltersBodySchema, body, response);
124+
if ('error' in parsed) return parsed.error;
125+
const data = parsed.data;
126+
const record = data.record;
110127
if (!record) {
111128
return { error: 'No record provided in request body', ok: false };
112129
}

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"license": "MIT",
2525
"devDependencies": {
2626
"@types/node": "^22.10.7",
27-
"adminforth": "^3.6.21",
27+
"adminforth": "^3.7.1",
2828
"semantic-release": "^24.2.1",
2929
"semantic-release-slack-bot": "^4.0.2",
3030
"typescript": "^5.7.3"
@@ -55,9 +55,10 @@
5555
]
5656
},
5757
"dependencies": {
58-
"clone": "^2.1.2"
58+
"clone": "^2.1.2",
59+
"zod": "^4.3.6"
5960
},
6061
"peerDependencies": {
61-
"adminforth": "^3.6.21"
62+
"adminforth": "^3.7.1"
6263
}
6364
}

pnpm-lock.yaml

Lines changed: 11 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)