refactor: add server side validation schema for fileOperations (#4989)
* refactor: move files to subfolder * refactor: schema for fileOperations.info * refactor: schema for fileOperations.list * refactor: schema for fileOperations.delete * refactor: schema for fileOperations.redirect
This commit is contained in:
committed by
GitHub
parent
c039501035
commit
e2429f6d85
67
server/routes/api/fileOperations/schema.ts
Normal file
67
server/routes/api/fileOperations/schema.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
import { isEmpty } from "lodash";
|
||||
import z from "zod";
|
||||
import { FileOperationType } from "@shared/types";
|
||||
import { FileOperation } from "@server/models";
|
||||
import BaseSchema from "../BaseSchema";
|
||||
|
||||
const CollectionsSortParamsSchema = z.object({
|
||||
/** The attribute to sort by */
|
||||
sort: z
|
||||
.string()
|
||||
.refine((val) => Object.keys(FileOperation.getAttributes()).includes(val), {
|
||||
message: "Invalid sort parameter",
|
||||
})
|
||||
.default("createdAt"),
|
||||
|
||||
/** The direction of the sorting */
|
||||
direction: z
|
||||
.string()
|
||||
.optional()
|
||||
.transform((val) => (val !== "ASC" ? "DESC" : val)),
|
||||
});
|
||||
|
||||
export const FileOperationsInfoSchema = BaseSchema.extend({
|
||||
body: z.object({
|
||||
/** Id of the file operation to be retrieved */
|
||||
id: z.string().uuid(),
|
||||
}),
|
||||
});
|
||||
|
||||
export type FileOperationsInfoReq = z.infer<typeof FileOperationsInfoSchema>;
|
||||
|
||||
export const FileOperationsListSchema = BaseSchema.extend({
|
||||
body: CollectionsSortParamsSchema.extend({
|
||||
/** File Operation Type */
|
||||
type: z.nativeEnum(FileOperationType),
|
||||
}),
|
||||
});
|
||||
|
||||
export type FileOperationsListReq = z.infer<typeof FileOperationsListSchema>;
|
||||
|
||||
export const FileOperationsRedirectSchema = BaseSchema.extend({
|
||||
body: z.object({
|
||||
/** Id of the file operation to access */
|
||||
id: z.string().uuid().optional(),
|
||||
}),
|
||||
query: z.object({
|
||||
/** Id of the file operation to access */
|
||||
id: z.string().uuid().optional(),
|
||||
}),
|
||||
}).refine((req) => !(isEmpty(req.body.id) && isEmpty(req.query.id)), {
|
||||
message: "id is required",
|
||||
});
|
||||
|
||||
export type FileOperationsRedirectReq = z.infer<
|
||||
typeof FileOperationsRedirectSchema
|
||||
>;
|
||||
|
||||
export const FileOperationsDeleteSchema = BaseSchema.extend({
|
||||
body: z.object({
|
||||
/** Id of the file operation to delete */
|
||||
id: z.string().uuid(),
|
||||
}),
|
||||
});
|
||||
|
||||
export type FileOperationsDeleteReq = z.infer<
|
||||
typeof FileOperationsDeleteSchema
|
||||
>;
|
||||
Reference in New Issue
Block a user