Skip to content

Instantly share code, notes, and snippets.

@josephdburdick
Created April 1, 2025 21:55
Show Gist options
  • Save josephdburdick/42f05b9bb881de1af25880a7ba9e2b9c to your computer and use it in GitHub Desktop.
Save josephdburdick/42f05b9bb881de1af25880a7ba9e2b9c to your computer and use it in GitHub Desktop.

Revisions

  1. josephdburdick created this gist Apr 1, 2025.
    30 changes: 30 additions & 0 deletions Scene.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    import { z } from "zod"
    import camelcaseKeys from "camelcase-keys"

    export const SceneSchema = z
    .object({
    id: z.string().optional(),
    movieId: z.string(),
    type: z.string(),
    })
    .transform((item) => {
    return {
    ...item,
    toJson: () => {
    return Object.fromEntries(
    Object.entries(item).map(([key, value]) => [
    key.replace(/([A-Z])/g, "_$1").toLowerCase(),
    value,
    ]),
    )
    },
    }
    })

    export type Scene = z.infer<typeof SceneSchema> & {
    toJson(): Object
    }

    export const createScene = (data: Record<string, unknown>): Scene => {
    return SceneSchema.parse(camelcaseKeys(data, { deep: true }))
    }