37 lines
876 B
TypeScript
37 lines
876 B
TypeScript
|
import { z } from "zod";
|
||
|
import { storySchema } from "app/ui/forms/schemas";
|
||
|
import { Story } from "@prisma/client";
|
||
|
|
||
|
//schemas
|
||
|
|
||
|
const schema = storySchema.omit({ id: true, genres: true })
|
||
|
const genreSchema = z.object({ id: z.number() })
|
||
|
const genresSchema = z.array(genreSchema)
|
||
|
|
||
|
export async function prepStoryData(data: Story & { genres: number[] }): Promise<{ title: string, word_count: number }> {
|
||
|
const storyData = {
|
||
|
title: data.title,
|
||
|
word_count: data.word_count,
|
||
|
}
|
||
|
//prepare schemas
|
||
|
|
||
|
//throw an error if validation fails
|
||
|
schema.safeParse(storyData)
|
||
|
|
||
|
return storyData
|
||
|
}
|
||
|
|
||
|
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
|
||
|
|
||
|
}
|