40 lines
1.1 KiB
TypeScript
40 lines
1.1 KiB
TypeScript
import { z } from "zod";
|
|
import { storySchema } from "app/ui/forms/schemas";
|
|
import { Pub, Story } from "@prisma/client";
|
|
import { pubSchema } from "app/ui/forms/schemas";
|
|
import { StoryWithGenres } from "app/story/page";
|
|
|
|
//schemas
|
|
|
|
const storySchemaTrimmed = storySchema.omit({ genres: true })
|
|
const pubSchemaTrimmed = pubSchema.omit({ genres: true })
|
|
const genreSchema = z.object({ id: z.number() })
|
|
const genresSchema = z.array(genreSchema)
|
|
|
|
export async function prepStoryData(data: Story): Promise<{ title: string, word_count: number }> {
|
|
const storyData = structuredClone(data)
|
|
//throw an error if validation fails
|
|
storySchemaTrimmed.safeParse(storyData)
|
|
return storyData
|
|
}
|
|
|
|
export async function prepPubData(data: Pub): Promise<Pub> {
|
|
const pubData = structuredClone(data)
|
|
pubSchemaTrimmed.safeParse(pubData)
|
|
return pubData
|
|
}
|
|
|
|
export async function prepGenreData(data: number[]): Promise<{ id: number }[]> {
|
|
"use server"
|
|
|
|
//prepare data
|
|
const genresArray = data.map((e) => { return { id: e } })
|
|
|
|
//prepare schemas
|
|
|
|
//throws error if validation fails
|
|
genresSchema.safeParse(genresArray)
|
|
return genresArray
|
|
|
|
}
|