UNPKG

@syngrisi/syngrisi

Version:
100 lines (95 loc) 3.85 kB
// src/server/schemas/common/RequestPagination.schema.ts import { extendZodWithOpenApi as extendZodWithOpenApi4 } from "@asteasolutions/zod-to-openapi"; import { z as z4 } from "zod"; // src/server/schemas/common/requestQueryFilterSchema.schema.ts import { extendZodWithOpenApi } from "@asteasolutions/zod-to-openapi"; import { z } from "zod"; extendZodWithOpenApi(z); var requestQueryFilterSchema = z.string().optional().refine((data) => { if (!data) return false; try { const parsed = JSON.parse(data); const valueSchema = z.lazy(() => z.union([ z.string(), z.number(), z.boolean(), z.array(z.any()), z.record(z.string(), z.any()) ])); const schema = z.record(z.string(), valueSchema); schema.parse(parsed); return true; } catch (e) { return false; } }, { message: "Invalid JSON string or does not match the required schema" }).openapi({ example: '{"key1": "value1", "key2": 123, "key3": true, "$and":[{"name":"CheckName"}]}' }); // src/server/schemas/utils/commonValidations.ts import { z as z3 } from "zod"; import { extendZodWithOpenApi as extendZodWithOpenApi3 } from "@asteasolutions/zod-to-openapi"; // src/server/schemas/common/Version.schema.ts import { z as z2 } from "zod"; import { extendZodWithOpenApi as extendZodWithOpenApi2 } from "@asteasolutions/zod-to-openapi"; extendZodWithOpenApi2(z2); var VersionBaseSchema = z2.string().regex(/^\d+\.\d+\.\d+$/, 'Version must be in the format "x.y.z"'); var VersionSchema = VersionBaseSchema.transform((value) => { const parts = value.split("."); return { major: parseInt(parts[0]), minor: parseInt(parts[1]), patch: parseInt(parts[2]) }; }); // src/server/schemas/utils/commonValidations.ts extendZodWithOpenApi3(z3); var mongooseIdRegex = /^[0-9a-fA-F]{24}$/; var id = z3.string().regex(mongooseIdRegex, { message: "Invalid Mongoose ObjectId format: /^[0-9a-fA-F]{24}$/" }).openapi({ description: "baseline ID", example: "6bbF35cAB3C59dA969edAe79" }); var commonValidations = { id, version: VersionBaseSchema.openapi({ example: "1.1.2" }), positiveNumberString: z3.string().refine((value) => { const num = Number(value); return Number.isInteger(num) && num >= 0; }, { message: "String must be a positive number or 0" }), password: z3.string().min(6).regex(/(?=.*[0-9])/, "Password must include a number").regex(/(?=.*[a-z])/, "Password must include a lowercase letter").regex(/(?=.*[A-Z])/, "Password must include an uppercase letter").refine((value) => { return /(?=.*[!@#$%^&*(),.?":{}|<>-])/.test(value); }, { message: "Password must include a special symbol" }).openapi({ example: "Aa1!IJASSNOJ" }), username: z3.string().min(1).openapi({ example: "john.doe@example.com" }), // TODO: workaround TBD date: z3.string().refine((val) => { const date = new Date(val); return !isNaN(date.getTime()); }, { message: "Invalid date format" }), paramsId: { params: z3.object({ id }) }, paramsTestId: { params: z3.object({ testid: id }) }, success: z3.object({ message: z3.literal("success") }) }; // src/server/schemas/common/RequestPagination.schema.ts extendZodWithOpenApi4(z4); var RequestPaginationSchema = z4.object({ filter: requestQueryFilterSchema.optional(), limit: commonValidations.positiveNumberString.optional().openapi({ example: "10" }), page: commonValidations.positiveNumberString.optional().openapi({ example: "1" }), sortBy: z4.string().optional().openapi({ example: "name:desc" }), populate: z4.string().optional().openapi({ example: "test" }), includeUsage: z4.string().optional().openapi({ example: "true" }), baselineSnapshotId: z4.string().optional().openapi({ example: "656dd9e1a9f9dcd4a0c1beef" }) }); export { RequestPaginationSchema }; //# sourceMappingURL=RequestPagination.schema.js.map