Skip to content

Instantly share code, notes, and snippets.

@PiotrJander
Last active October 4, 2022 13:33
Show Gist options
  • Save PiotrJander/b72b9c4586f653b840f34ff749d171be to your computer and use it in GitHub Desktop.
Save PiotrJander/b72b9c4586f653b840f34ff749d171be to your computer and use it in GitHub Desktop.
import { z } from "zod";
// utilities
const stringDate = z.string().regex(/\d{4}-\d{2}-\d{2}/);
// shared
const OrderBaseBaseSchema = z.object({
id: z.string(),
orderNumber: z.string(),
candidateId: z.string(),
requestedDate: stringDate,
interviewDate: stringDate.optional(),
returnedDate: stringDate.optional(),
requestedByUserId: z.string(),
interviewerId: z.string().optional(),
intakeFormUrl: z.string().optional(),
notes: z.string().optional(),
});
// Report orders
const OrderBaseSchema = OrderBaseBaseSchema.extend({
kind: "report1",
basicScore: z.string().optional(),
advancedScore: z.string().optional(),
backgroundCheckReport: z.string().optional(),
basicScoreReport: z.string().optional(),
advancedScoreReport: z.string().optional(),
specialCaseLetter: z.string().optional(),
});
const OrderCancelledSchema = OrderBaseSchema.extend({
status: z.literal("Status1"),
});
const OrderRequestedSchema = OrderBaseSchema.extend({
status: z.literal("Status2"),
});
const OrderInterviewPrerequisitesRequestedSchema =
OrderRequestedSchema.extend({
status: z.literal("Status3"),
});
const OrderBackgroundCheckCompletedSchema = OrderRequestedSchema.extend({
status: z.literal("Status4"),
backgroundCheckReport: z.string(),
});
const OrderInterviewScheduledSchema =
OrderBackgroundCheckCompletedSchema.extend({
status: z.literal("Status5"),
interviewDate: stringDate,
});
const OrderInterviewCompletedSchema = OrderInterviewScheduledSchema.extend({
status: z.literal("Status6"),
interviewerId: z.string(),
intakeFormUrl: z.string(),
});
const OrderScoresAssignedSchema = OrderInterviewCompletedSchema.extend({
status: z.literal("Status7"),
basicScore: z.string(),
advancedScore: z.string(),
});
const OrderReportGeneratedSchema = OrderScoresAssignedSchema.extend({
status: z.literal("Status8"),
basicScoreReport: z.string(),
advancedScoreReport: z.string(),
});
const OrderReportReturnedSchema = OrderReportGeneratedSchema.extend({
status: z.literal("Status9"),
returnedDate: stringDate,
});
export const ReportOrderSchema = z.discriminatedUnion("status", [
OrderCancelledSchema,
OrderRequestedSchema,
OrderInterviewPrerequisitesRequestedSchema,
OrderBackgroundCheckCompletedSchema,
OrderInterviewScheduledSchema,
OrderScoresAssignedSchema,
OrderReportGeneratedSchema,
OrderReportReturnedSchema,
]);
export type ReportOrder = z.infer<typeof ReportOrderSchema>;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment