data-validation-patterns

Validate external data against Zod schemas with safeParse fallbacks.

2|Updated Apr 9, 2023
One-click install
npx skills add https://github.com/Esdeveniments/esdeveniments-frontend --skill data-validation-patterns
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: data-validation-patterns
Source: https://github.com/Esdeveniments/esdeveniments-frontend/tree/main/.github/skills/data-validation-patterns
Command: npx skills add https://github.com/Esdeveniments/esdeveniments-frontend --skill data-validation-patterns

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill standardizes and enforces runtime data integrity by validating all external data against Zod schemas before use, preventing type errors and downstream bugs.

Core Features & Use Cases

  • Centralized validation: place schemas in lib/validation/ and reuse across API clients and UI layers.
  • Safe parsing with fallbacks: use safeParse to avoid exceptions and provide safe defaults or null when validation fails.
  • End-to-end observability: capture validation failures (e.g., in Sentry) to surface data quality issues in production.
  • Use cases include validating API responses, webhook payloads, user input, and URL/query parameters across client and server boundaries.

Quick Start

Create Zod schemas in lib/validation/, write parse helpers with safeParse, and wire them into your API wrappers or UI fetch logic. Example usage involves validating a fetched payload before transforming it into domain objects.

Frequently Asked Questions about data-validation-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I validate API responses with Zod before using the data?

To validate API responses with Zod, define schemas in a centralized directory and use safeParse within your API wrappers to check payloads before transforming them into domain objects, preventing downstream type errors.

What is the best way to handle webhook payload validation without crashing the application?

Handling webhook payload validation safely requires using Zod's safeParse method. safeParse avoids throwing exceptions on invalid data, allowing you to provide explicit fallbacks or null values when validation fails.

How do I capture Zod validation failures for observability in production?

Capture Zod validation failures for observability by integrating safeParse checks into wrapper functions that log errors to monitoring tools like Sentry, surfacing runtime data quality issues across frontend and backend boundaries.

Does this data validation approach work across both frontend and backend boundaries?

Yes, this data validation approach works across frontend and backend boundaries. Place Zod schemas in a shared validation directory and reuse them to validate user input, URL parameters, and API client fetch logic.

When should I use safeParse instead of parse for external user input?

Use safeParse instead of parse for external user input whenever invalid data is anticipated. safeParse returns a success object with fallbacks rather than throwing an error, ensuring application stability when external data quality is poor.