UNPKG

@syngrisi/syngrisi

Version:
112 lines (108 loc) 5.22 kB
// src/server/schemas/User.schema.ts import { extendZodWithOpenApi as extendZodWithOpenApi3 } from "@asteasolutions/zod-to-openapi"; import { z as z3 } from "zod"; // src/server/schemas/utils/commonValidations.ts import { z as z2 } from "zod"; import { extendZodWithOpenApi as extendZodWithOpenApi2 } from "@asteasolutions/zod-to-openapi"; // src/server/schemas/common/Version.schema.ts import { z } from "zod"; import { extendZodWithOpenApi } from "@asteasolutions/zod-to-openapi"; extendZodWithOpenApi(z); var VersionBaseSchema = z.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 extendZodWithOpenApi2(z2); var mongooseIdRegex = /^[0-9a-fA-F]{24}$/; var id = z2.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: z2.string().refine((value) => { const num = Number(value); return Number.isInteger(num) && num >= 0; }, { message: "String must be a positive number or 0" }), password: z2.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: z2.string().min(1).openapi({ example: "john.doe@example.com" }), // TODO: workaround TBD date: z2.string().refine((val) => { const date = new Date(val); return !isNaN(date.getTime()); }, { message: "Invalid date format" }), paramsId: { params: z2.object({ id }) }, paramsTestId: { params: z2.object({ testid: id }) }, success: z2.object({ message: z2.literal("success") }) }; // src/server/schemas/User.schema.ts extendZodWithOpenApi3(z3); var UserSchema = z3.object({ username: z3.string().min(1, "UserSchema: the username name is empty").openapi({ example: "johndoe@example.com" }), firstName: z3.string().min(1, "UserSchema: the firstName name is empty").openapi({ example: "John" }), lastName: z3.string().min(1, "UserSchema: the lastName name is empty").openapi({ example: "Doe" }), role: z3.enum(["admin", "reviewer", "user"]), password: z3.string().optional(), token: z3.string().optional(), apiKey: z3.string().optional(), createdDate: z3.date().optional().openapi({ example: "2024-05-25T15:23:21.150Z" }), updatedDate: z3.date().optional().openapi({ example: "2024-05-26T15:23:21.150Z" }), expiration: z3.date().optional(), meta: z3.record(z3.string(), z3.any()).optional(), _id: commonValidations.id.openapi({ example: "6bbF35cAB3C59dA969edAe79" }), id: commonValidations.id.openapi({ example: "6bbF35cAB3C59dA969edAe79" }) }); var UserCreateReqSchema = z3.object({ username: z3.string().min(1, "UserSchema: the username name is empty").openapi({ example: "johndoe@example.com" }), firstName: z3.string().min(1, "UserSchema: the firstName name is empty").openapi({ example: "John" }), lastName: z3.string().min(1, "UserSchema: the lastName name is empty").openapi({ example: "Doe" }), role: z3.enum(["admin", "reviewer", "user"]), email: z3.string().optional(), password: z3.string() }); var UserCurrentRespSchema = z3.object({ _id: commonValidations.id.openapi({ example: "6bbF35cAB3C59dA969edAe79" }), id: commonValidations.id.openapi({ example: "6bbF35cAB3C59dA969edAe79" }), username: z3.string().min(1, "UserSchema: the username name is empty").openapi({ example: "johndoe@example.com" }), firstName: z3.string().min(1, "UserSchema: the firstName name is empty").openapi({ example: "John" }), lastName: z3.string().min(1, "UserSchema: the lastName name is empty").openapi({ example: "Doe" }), role: z3.enum(["admin", "reviewer", "user"]) }); var UserGetRespSchema = UserCurrentRespSchema; var UserCreateRespSchema = z3.object({ username: z3.string().min(1, "UserSchema: the username name is empty").openapi({ example: "johndoe@example.com" }), firstName: z3.string().min(1, "UserSchema: the firstName name is empty").openapi({ example: "John" }), lastName: z3.string().min(1, "UserSchema: the lastName name is empty").openapi({ example: "Doe" }), role: z3.enum(["admin", "reviewer", "user"]), createdDate: z3.date().optional().openapi({ example: "2024-05-25T15:23:21.150Z" }), updatedDate: z3.date().optional().openapi({ example: "2024-05-26T15:23:21.150Z" }), _id: commonValidations.id.openapi({ example: "6bbF35cAB3C59dA969edAe79" }), id: commonValidations.id.openapi({ example: "6bbF35cAB3C59dA969edAe79" }) }); export { UserCreateReqSchema, UserCreateRespSchema, UserCurrentRespSchema, UserGetRespSchema, UserSchema }; //# sourceMappingURL=User.schema.js.map