P
💻DeveloppementIntermediateAll AIs

Automatically Generate Zod Schemas from a REST API

Automatically generates Zod validation schemas and their inferred TypeScript types from a REST API JSON response example.

Paste in your AI

Paste this prompt in ChatGPT, Claude or Gemini and customize the variables in brackets.

Tu es un expert TypeScript spécialisé en validation de données avec Zod. À partir de la réponse JSON suivante provenant de l'endpoint [ENDPOINT_URL] de l'API [NOM_API], génère les schémas Zod complets et optimisés.

Voici un exemple de réponse JSON de l'API :

[EXEMPLE_REPONSE_JSON]

Consignes précises :

  1. Analyse chaque champ du JSON : type, nullabilité, format (date, email, UUID, URL…), et valeurs possibles (enum).
  2. Génère un schéma Zod principal et des sous-schémas pour chaque objet imbriqué.
  3. Utilise les bons validateurs Zod : z.string().uuid(), z.string().email(), z.string().datetime(), z.coerce.date(), z.enum([...]), z.discriminatedUnion(), etc.
  4. Ajoute des .describe() sur chaque champ pour documenter le schéma.
  5. Exporte le type TypeScript inféré avec z.infer<typeof schema>.
  6. Si la réponse est paginée, crée un schéma générique de pagination réutilisable.
  7. Génère une fonction de parsing typesafe pour valider les réponses fetch :
    • Gestion d'erreur avec ZodError formaté
    • Version safe (safeParse) et stricte (parse)
  8. Ajoute des transformations .transform() si des champs nécessitent une conversion (string → Date, string → number).

Format de sortie attendu :

  • Un fichier schemas/[nom-ressource].schema.ts avec les schémas
  • Un fichier types/[nom-ressource].types.ts avec les types inférés
  • Un fichier lib/api-parser.ts avec les fonctions de parsing
  • Des commentaires JSDoc sur chaque export

Personalize this prompt with Léa

Léa rewrites this prompt for your job and your exact goal — 3 quick questions.

Why this prompt works

<p>This prompt transforms any API JSON response into robust, typed <strong>Zod</strong> schemas. Instead of manually writing validators, you simply provide a response example and the AI analyzes each field to choose the most precise validator: <strong>z.string().uuid()</strong> for identifiers, <strong>z.string().datetime()</strong> for ISO dates, <strong>z.enum()</strong> for finite values, etc.</p><p>The approach is structured in three separate files to respect <strong>separation of concerns</strong>: Zod schemas in a dedicated file, TypeScript types inferred via <strong>z.infer</strong> in another, and parsing functions in a utility module. This organization facilitates maintenance and type sharing between frontend and backend.</p><p>To maximize results, provide a JSON response that is <strong>as complete as possible</strong>, including edge cases: nullable fields, empty arrays, nested objects and paginated responses. The richer the example, the more precise the generated schemas will be and the better they will cover production error cases.</p>

Use Cases

Type responses from a third-party API without OpenAPI documentationMigrate a JavaScript project to TypeScript with runtime validationCreate a validation layer for a fetch or axios API client

Expected Output

Three ready-to-use TypeScript files: a Zod schema file with precise validators and descriptions, an exportable inferred types file, and a typesafe parsing module with formatted error handling.

Improve this prompt

Run this prompt through the Optimizer to strengthen its context, constraints and expected format.

Improve this prompt with the Optimizer

Comments

  • LéaAI

    Pour un résultat plus fiable, enrichissez le prompt avec plusieurs exemples de réponses (cas nominaux, champs nuls, valeurs limites). Indiquez aussi les contraintes métier non visibles dans le JSON (regex, longueurs, relations). Sinon, le schéma Zod sera trop permissif et basé uniquement sur l'échantillon fourni.

📬 Get new prompts every week

Join our newsletter and never miss a prompt.

Go further

Similar Prompts

💻DeveloppementIntermediateAll AIs

Create a Complete and Optimized Database Schema

A comprehensive prompt to generate a professional database schema with DDL, relationships, indexes and test data.

0382
💻DeveloppementIntermediateAll AIs

Helm Chart for Application

Create a Helm chart

0299
💻DeveloppementAdvancedAll AIs

Progressive Rollout Strategy

Progressive deployment of a new model

0270
💻DeveloppementBeginnerAll AIs

Complete Curriculum-Aligned Lesson Plan

Quickly prepare a structured lesson

0311