Logo

dev-resources.site

for different kinds of informations.

Using Zod's z.union: An Important Pitfall to Avoid

Published at
11/7/2024
Categories
webdev
schema
zod
typescript
Author
jenchen
Categories
4 categories in total
webdev
open
schema
open
zod
open
typescript
open
Author
7 person written this
jenchen
open
Using Zod's z.union: An Important Pitfall to Avoid

When using the schema z.union([z.object({}), anotherSchema.partial()]);, Zod checks if the object matches the empty object {} first.

Since an empty object can always be valid, Zod might decide that the object matches {} and stops there, ignoring the other options in the union.

To fix this, you should ensure that Zod prioritizes the more specific schema (anotherSchema.partial()) over the empty object. You can achieve this by reversing the order in the union.

For example:

import { z } from "zod";

// Define two schemas
const emptySchema = z.object({});
const anotherSchema = z.object({
  name: z.string(),
  age: z.number().optional(),
});

// Create a union where the empty schema is checked first
const problematicSchema = z.union([emptySchema, anotherSchema.partial()]);

// Test with an object that should match `anotherSchema.partial()`
const data = { name: "myName" };

const result = problematicSchema.safeParse(data);
console.log(result);

Enter fullscreen mode Exit fullscreen mode

The output is


{
  success: true,
  data: {}
}
Enter fullscreen mode Exit fullscreen mode

Instead of

{
  success: true,
  data: { name: "myName" }
}
Enter fullscreen mode Exit fullscreen mode
schema Article's
30 articles in total
Favicon
Schema Markup can boost your click-through rates by up to 30%?
Favicon
Custom schema specific Supabase Server Component clients in Grida Form workspace
Favicon
Zod for TypeScript Schema Validation: A Comprehensive Guide
Favicon
Database schema design of Splitwise application
Favicon
Validating JSON Schema with Fixed and User-Defined Keys in Python
Favicon
Using Zod's z.union: An Important Pitfall to Avoid
Favicon
Desvendando o Atlas: Gerencie Seus Esquemas de Banco de Dados com EstiloπŸš€
Favicon
Customize Schema with @extend_schema_view
Favicon
Hotel reservation Schema design (PostgreSQL)
Favicon
How to Insert Data into Specific Table of PostgreSQL Schemas with Node.js
Favicon
Typesafe Supabase Flutter Queries
Favicon
Using yup to build schema with value parsing and validation.
Favicon
Designing an Optimal Database Schema for a Followers-Following System in a Blog-Post App
Favicon
JobPosting from Schema.org
Favicon
Unlocking the Power of Mongoose Schemas: Enhancements for Better Data Handling
Favicon
Unraveling the Power of Schema Markup: Elevating Your Website's Visibility and Performance
Favicon
How to handle complex json schema
Favicon
Navigating Django Schemas with Ease: How Django-schema-viewer Helps Developers
Favicon
🐣Your First Database Schema Change in 5 Minutes with Bytebase
Favicon
Multiplos schemas no mesmo tΓ³pico Kafka na linha de comando
Favicon
How pgroll works under the hood
Favicon
Automating API Documentation: A Journey from TypeScript to OpenAPI and Schema Governence with Optic
Favicon
Master schema validation in TypeScript with Zod
Favicon
πŸš› Deploy Database Schema Migrations with Bytebase
Favicon
Random Data Generator Website
Favicon
Xata's JSON Column Type
Favicon
Introducing pgroll: Zero-downtime, Reversible, Schema Migrations for Postgres
Favicon
Designing Your Database Schema
Favicon
WHAT ARE THE TYPES OF SCHEMA MARKEUP?
Favicon
Iceberg Schema Evolution in Trino

Featured ones: