Beispiel-Konvertierung: SQL DDL → Zod Schema
Eingabe (SQL DDL)
CREATE TABLE products (
id INT PRIMARY KEY,
sku VARCHAR(50) NOT NULL,
price DECIMAL(10,2) NOT NULL,
in_stock BOOLEAN DEFAULT true
); Ausgabe (Zod Schema)
import { z } from "zod";
export const productSchema = z.object({
id: z.number().int(),
sku: z.string().max(50),
price: z.number().positive(),
inStock: z.boolean().default(true),
});
export type Product = z.infer<typeof productSchema>; Häufig gestellte Fragen (FAQ)
Warum sollte man Datenbankeingaben mit Zod-Schemas validieren?
Zod bietet eine Laufzeit-Typvalidierung für API-Request-Bodys und Formulareingaben, bevor Daten in Ihre SQL-Datenbank geschrieben werden.