36 lines
1.1 KiB
TypeScript
36 lines
1.1 KiB
TypeScript
import { z } from "zod"
|
|
|
|
export * from "./rbac"
|
|
|
|
export const postStatusSchema = z.enum(["draft", "published"])
|
|
|
|
const postMutableFieldsSchema = z.object({
|
|
title: z.string().min(3).max(180),
|
|
slug: z.string().min(3).max(180),
|
|
excerpt: z.string().max(320).optional(),
|
|
body: z.string().min(1),
|
|
status: postStatusSchema,
|
|
})
|
|
|
|
export const postSchema = z.object({
|
|
id: z.string().uuid(),
|
|
...postMutableFieldsSchema.shape,
|
|
createdAt: z.date(),
|
|
updatedAt: z.date(),
|
|
})
|
|
|
|
export const createPostInputSchema = postMutableFieldsSchema
|
|
export const updatePostInputSchema = postMutableFieldsSchema
|
|
.partial()
|
|
.refine((value) => Object.keys(value).length > 0, {
|
|
message: "At least one field is required for an update.",
|
|
})
|
|
|
|
// Backward-compatible alias while migrating callers to create/update-specific schemas.
|
|
export const upsertPostSchema = createPostInputSchema
|
|
|
|
export type Post = z.infer<typeof postSchema>
|
|
export type CreatePostInput = z.infer<typeof createPostInputSchema>
|
|
export type UpdatePostInput = z.infer<typeof updatePostInputSchema>
|
|
export type UpsertPostInput = z.infer<typeof upsertPostSchema>
|