All files / src/schemas AudioBook.ts

0% Statements 0/14
0% Branches 0/6
0% Functions 0/2
0% Lines 0/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61                                                                                                                         
import z from "zod";
import { CharacterSchema, CharacterSchemaWithVoice } from "./Character";
import { SceneSchema } from "./Scene";
import { DialogueSchema } from "./Dialogue";
import { EpisodeSchema } from "./Episode";
 
export const AudioBookSchema = z
    .object({
        name: z.string(),
        plot: z.string(),
        genre: z.array(z.string()),
        characters: z.array(CharacterSchema).describe("List of all characters appearing in the audiobook"),
        episodes: z.array(EpisodeSchema).describe("List of episodes that structure the audiobook content"),
        scenes: z.array(SceneSchema).describe("List of scenes that make up the audiobook narrative"),
        dialogues: z.array(DialogueSchema).describe("Sequential list of all dialogue lines in the audiobook"),
    })
    .superRefine((data, ctx) => {
        const numCharacters = data.characters.length;
 
        /* Validate character indices in dialogues */
        data.dialogues.forEach((dialogue, i) => {
            if (dialogue.character < 0 || dialogue.character >= numCharacters) {
                ctx.addIssue({
                    code: "custom",
                    message: `'dialogue' at index ${i} references invalid 'character' index ${dialogue.character}; Valid range: [0, ${numCharacters})`,
                    path: ["dialogues", i, "character"],
                });
            }
        });
 
        /* Validate scene's dialogue indices are continuous */
        for (let i = 0; i < data.scenes.length - 1; i++) {
            const curr = data.scenes[i];
            const next = data.scenes[i + 1];
 
            if (curr.dialogueEnd !== next.dialogueBegin) {
                ctx.addIssue({
                    code: "custom",
                    message: `'scene' at index ${i} ends at 'dialogue' index ${curr.dialogueEnd} but it's next 'scene' at index ${i + 1} begins at 'dialogue' index ${next.dialogueBegin}; They should be equal`,
                    path: ["scenes", i + 1, "dialogueBegin"],
                });
            }
        }
 
        /* Validate scene dialogue indices are within bounds */
        {
            const firstScene = data.scenes.at(0);
 
            const lastScene = data.scenes.at(-1);
        }
 
        /* TODO: finish these */
    });
 
export const AudioBookWithCharacterVoicesSchema = AudioBookSchema.safeExtend({
    characters: z.array(CharacterSchemaWithVoice).describe("List of all characters appearing in the audiobook"),
});
 
export type AudioBook = z.infer<typeof AudioBookSchema>;
export type AudioBookWithCharacterVoices = z.infer<typeof AudioBookWithCharacterVoicesSchema>;