Skip to content

Commit 91c2397

Browse files
authored
Merge pull request #1 from devforth/add-zod-schemas
Add zod schemas
2 parents 8281794 + 0a3d650 commit 91c2397

3 files changed

Lines changed: 115 additions & 98 deletions

File tree

index.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
import { AdminForthPlugin, AdminForthDataTypes, AdminForthResourcePages, Filters} from "adminforth";
1+
import { AdminForthPlugin, parseBody, AdminForthDataTypes, AdminForthResourcePages, Filters} from "adminforth";
22
import type { IAdminForth, IHttpServer, AdminForthResourceColumn, AdminForthResource, IAdminForthHttpResponse, AdminUser, AdminForthComponentDeclaration } from "adminforth";
33
import type { PluginOptions } from './types.js';
44
import { error } from "console";
5-
5+
import { z } from "zod";
6+
7+
const deactivateUserBodySchema = z.object({
8+
record: z.record(z.string(), z.unknown()),
9+
}).strict();
10+
611
export default class UserSoftDelete extends AdminForthPlugin {
712
options: PluginOptions;
813
allowDisableFunc: Function | null | boolean = null;
@@ -108,7 +113,12 @@ export default class UserSoftDelete extends AdminForthPlugin {
108113
server.endpoint({
109114
method: 'POST',
110115
path: `/plugin/${this.pluginInstanceId}/deactivateUser`,
111-
handler: async ({ adminUser,body }) => {
116+
handler: async ({ adminUser, body, response }) => {
117+
console.log(`UserSoftDelete: deactivateUser endpoint called by ${adminUser.dbUser[this.resourceConfig.columns.find((col) => col.primaryKey).name]}`);
118+
const parsed = parseBody(deactivateUserBodySchema, body, response);
119+
console.log(`UserSoftDelete: parsed body: ${JSON.stringify(parsed)}`);
120+
if ('error' in parsed) return parsed.error;
121+
const data = parsed.data;
112122
let isAllowedToDeactivate = false;
113123
if ( typeof this.allowDisableFunc === "function" ) {
114124
isAllowedToDeactivate = await this.allowDisableFunc(adminUser);
@@ -118,10 +128,10 @@ export default class UserSoftDelete extends AdminForthPlugin {
118128
}
119129

120130
const primaryKeyColumn = this.resourceConfig.columns.find((col) => col.primaryKey);
121-
if (!body.record) {
131+
if (!data.record) {
122132
return { ok: false, error: "No record provided" };
123133
}
124-
const id = body.record[primaryKeyColumn.name];
134+
const id = data.record[primaryKeyColumn.name];
125135
if (id === undefined || id === null) {
126136
return { ok: false, error: "No record id provided" };
127137
}

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,18 @@
1515
"author": "DevForth (https://devforth.io)",
1616
"license": "MIT",
1717
"description": "",
18+
"dependencies": {
19+
"zod": "^4.3.6"
20+
},
1821
"devDependencies": {
1922
"@types/node": "latest",
20-
"adminforth": "^3.6.21",
23+
"adminforth": "^3.7.1",
2124
"semantic-release": "^24.2.1",
2225
"semantic-release-slack-bot": "^4.0.2",
2326
"typescript": "^5.7.3"
2427
},
2528
"peerDependencies": {
26-
"adminforth": "^3.6.21"
29+
"adminforth": "^3.7.1"
2730
},
2831
"release": {
2932
"plugins": [

0 commit comments

Comments
 (0)