UNPKG

@overture-stack/lyric

Version:
253 lines (252 loc) 7.74 kB
import { z } from 'zod'; import { isAuditEventValid, isSubmissionActionTypeValid } from './auditUtils.js'; import { parseSQON } from './convertSqonToQuery.js'; import { isValidDateFormat, isValidIdNumber } from './formatUtils.js'; import { VIEW_TYPE } from './types.js'; const auditEventTypeSchema = z .string() .trim() .min(1) .refine((value) => isAuditEventValid(value), 'invalid Event Type'); const booleanSchema = z .string() .toLowerCase() .refine((value) => value === 'true' || value === 'false'); const viewSchema = z.string().toLowerCase().trim().min(1).pipe(VIEW_TYPE); const categoryIdSchema = z .string() .trim() .min(1) .refine((value) => { const parsed = parseInt(value); return isValidIdNumber(parsed); }, 'invalid category ID'); const endDateSchema = z .string() .trim() .min(1) .refine((value) => isValidDateFormat(value), 'invalid `endDate` parameter'); const entityNameSchema = z.string().trim().min(1); const organizationSchema = z.string().trim().min(1); const pageSizeSchema = z.string().superRefine((value, ctx) => { const parsed = parseInt(value); if (isNaN(parsed)) { ctx.addIssue({ code: z.ZodIssueCode.invalid_type, expected: 'number', received: 'nan', }); } if (parsed < 1) { ctx.addIssue({ code: z.ZodIssueCode.too_small, minimum: 1, inclusive: true, type: 'number', }); } }); const indexIntegerSchema = z.string().superRefine((value, ctx) => { const parsed = parseInt(value); if (isNaN(parsed)) { ctx.addIssue({ code: z.ZodIssueCode.invalid_type, expected: 'number', received: 'nan', }); } }); const positiveInteger = z.string().superRefine((value, ctx) => { const parsed = parseInt(value); if (isNaN(parsed)) { ctx.addIssue({ code: z.ZodIssueCode.invalid_type, expected: 'number', received: 'nan', }); } if (parsed < 1) { ctx.addIssue({ code: z.ZodIssueCode.too_small, minimum: 1, inclusive: true, type: 'number', }); } }); const sqonSchema = z.custom((value) => { try { parseSQON(value); return true; } catch (error) { return false; } }, 'invalid SQON format'); const startDateSchema = z .string() .trim() .min(1) .refine((value) => isValidDateFormat(value), 'invalid `startDate` parameter'); const submissionActionTypeSchema = z .string() .trim() .min(1) .refine((value) => isSubmissionActionTypeValid(value), 'invalid Submission Action Type'); const submissionIdSchema = z .string() .trim() .min(1) .refine((value) => { const parsed = parseInt(value); return isValidIdNumber(parsed); }, 'invalid submission ID'); const stringNotEmpty = z.string().trim().min(1); export const categoryPathParamsSchema = z.object({ categoryId: categoryIdSchema, }); export const categoryOrganizationPathParamsSchema = z.object({ categoryId: categoryIdSchema, organization: organizationSchema, }); const submissionIdPathParamSchema = z.object({ submissionId: submissionIdSchema, }); const paginationQuerySchema = z.object({ page: positiveInteger.optional(), pageSize: pageSizeSchema.optional(), }); const auditQuerySchema = z .object({ entityName: entityNameSchema.optional(), eventType: auditEventTypeSchema.optional(), systemId: stringNotEmpty.optional(), startDate: startDateSchema.optional(), endDate: endDateSchema.optional(), }) .merge(paginationQuerySchema); export const auditByCatAndOrgRequestSchema = { query: auditQuerySchema, pathParams: categoryOrganizationPathParamsSchema, }; // Category Request export const cagegoryDetailsRequestSchema = { pathParams: categoryPathParamsSchema, }; export const dictionaryRegisterRequestSchema = { body: z.object({ categoryName: stringNotEmpty, dictionaryName: stringNotEmpty, dictionaryVersion: stringNotEmpty, defaultCentricEntity: stringNotEmpty.optional(), }), }; export const submissionsByCategoryRequestSchema = { query: z.object({ onlyActive: booleanSchema.default('false'), organization: organizationSchema.optional(), username: stringNotEmpty.optional(), }), pathParams: categoryPathParamsSchema, }; export const submissionByIdRequestSchema = { pathParams: submissionIdPathParamSchema, }; export const submissionActiveByOrganizationRequestSchema = { pathParams: categoryOrganizationPathParamsSchema, }; export const submissionCommitRequestSchema = { pathParams: z.object({ categoryId: categoryIdSchema, submissionId: submissionIdSchema, }), }; export const submissionDeleteRequestSchema = { pathParams: submissionIdPathParamSchema, }; export const submissionDeleteEntityNameRequestSchema = { query: z.object({ entityName: entityNameSchema, index: indexIntegerSchema.optional(), }), pathParams: z.object({ actionType: submissionActionTypeSchema, submissionId: submissionIdSchema, }), }; export const uploadSubmissionRequestSchema = { body: z.record(z.unknown()).array(), pathParams: categoryPathParamsSchema, query: z.object({ entityName: entityNameSchema, organization: organizationSchema, }), }; export const dataDeleteBySystemIdRequestSchema = { pathParams: z.object({ systemId: stringNotEmpty, categoryId: categoryIdSchema, }), }; export const dataEditRequestSchema = { body: z.record(z.unknown()).array(), pathParams: categoryPathParamsSchema, query: z.object({ entityName: entityNameSchema, organization: organizationSchema, }), }; export const dataGetByCategoryRequestSchema = { query: z .object({ entityName: z.union([entityNameSchema, entityNameSchema.array()]).optional(), view: viewSchema.optional(), }) .merge(paginationQuerySchema) .superRefine((data, ctx) => { if (data.view === VIEW_TYPE.Values.compound && data.entityName && data.entityName?.length > 0) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'is incompatible with `compound` view', path: ['entityName'], }); } }), pathParams: categoryPathParamsSchema, }; export const dataGetByOrganizationRequestSchema = { query: z .object({ entityName: z.union([entityNameSchema, entityNameSchema.array()]).optional(), view: viewSchema.optional(), }) .merge(paginationQuerySchema) .superRefine((data, ctx) => { if (data.view === VIEW_TYPE.Values.compound && data.entityName && data.entityName?.length > 0) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'is incompatible with `compound` view', path: ['entityName'], }); } }), pathParams: categoryOrganizationPathParamsSchema, }; export const dataGetByQueryRequestSchema = { body: sqonSchema, query: z .object({ entityName: z.union([entityNameSchema, entityNameSchema.array()]).optional(), }) .merge(paginationQuerySchema), pathParams: categoryOrganizationPathParamsSchema, }; export const dataGetBySystemIdRequestSchema = { query: z.object({ view: viewSchema.optional(), }), pathParams: z.object({ systemId: stringNotEmpty, categoryId: categoryIdSchema, }), };