Code Snippets
A collection of my go-to code patterns and snippets. Click to copy.
Hono patterns - Schema, Repository, Services, Routes
Zod Schema with Validation
Type-safe schema validation with Zod
import { z } from "zod";
// Entity
export const blockSchema = z.object({
id: z.string().uuid(),
blockerUserId: z.string().uuid(),
blockedUserId: z.string().uuid(),
reason: z.string().nullable(),
createdAt: z.coerce.date(),
isDeleted: z.boolean(),
deletedAt: z.coerce.date().nullable(),
unblockedBy: z.string().uuid().nullable(),
});
// Input schemas
export const getBlocksQuerySchema = z.object({
cursor: z.string().optional(),
limit: z.coerce.number().int().min(1).max(100).default(20),
});
export const createBlockInputSchema = z.object({
blockedUserId: z.string().uuid(),
reason: z.string().max(500).optional(),
});
// Output schemas
export const blockResponseSchema = z.object({
id: z.string(),
user: z.object({
id: z.string(),
username: z.string(),
image: z.string().nullable(),
age: z.number().optional(),
}),
reason: z.string().nullable(),
blockedAt: z.coerce.date(),
});
export const paginatedBlocksResponseSchema = z.object({
blocks: z.array(blockResponseSchema),
pagination: z.object({
limit: z.number(),
nextCursor: z.string().nullable(),
}),
});
// Types
export type Block = z.infer<typeof blockSchema>;
export type GetBlocksQuery = z.infer<typeof getBlocksQuerySchema>;
export type CreateBlockInput = z.infer<typeof createBlockInputSchema>;
export type BlockResponse = z.infer<typeof blockResponseSchema>;
export type PaginatedBlocksResponse = z.infer<typeof paginatedBlocksResponseSchema>;