Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save noahzgordon/40a7c6c4e0b7f85b570d84ad621bd2ae to your computer and use it in GitHub Desktop.
Save noahzgordon/40a7c6c4e0b7f85b570d84ad621bd2ae to your computer and use it in GitHub Desktop.
const newTableViewBaseSchema = z.object({
name: z.string(),
icon: z.string().nullable(),
});
const basicVitallyObjectTypes = Object.values(VitallyObjectType).filter((type) => (
type !== VitallyObjectType.customObject &&
type !== VitallyObjectType.surveyResponse &&
type !== VitallyObjectType.goal
)).map(type => z.literal(type)) as unknown as readonly [z.ZodTypeAny, z.ZodTypeAny, ...z.ZodTypeAny[]];
const newTableViewSchema = z.union([
z.object({
type: z.literal(VitallyObjectType.customObject),
customObjectId: z.string(),
}).merge(newTableViewBaseSchema),
z.object({
type: z.literal(VitallyObjectType.surveyResponse),
surveyId: z.string(),
}).merge(newTableViewBaseSchema),
z.object({
type: z.literal(VitallyObjectType.goal),
goalTemplateId: z.string().nullable(),
}).merge(newTableViewBaseSchema),
// catch-all for other object types
z.object({ type: z.union(basicVitallyObjectTypes) }).merge(newTableViewBaseSchema),
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment