Skip to content
Purview Dev logo

ZodSharp

Stable

Zero-allocation schema validation for C# — a Zod port.

aspnetcore c-sharp csharp dotnet json-schema newtonsoft-json schema source-generator system-text-json validation zero-allocation zod

The problem

A high-performance schema validation library for C#, ported from TypeScript Zod by way of the guinhx/ZodSharp C# port. Struct-based rules, a fluent API, JSON Schema export and import, source-generated validators, and zero-allocation validation on every valid input path. Integrates with System.Text.Json, Newtonsoft.Json, and ASP.NET Core ProblemDetails.

From the repository

High-performance, zero-allocation schema validation for C# — a Zod port with struct-based rules, a fluent API, JSON Schema interop, and compile-time source generation.

Why it became a project

Schema validation was repeatedly needed at application boundaries, but existing approaches did not combine a fluent Zod-style model with predictable, allocation-conscious execution.

Acknowledgments

ZodSharp builds on the work of others.

  • Zod

    The original JavaScript/TypeScript schema validation library.

  • ZodSharp (guinhx)

    The C# port of Zod, and the fork this project is based on.

Where it fits

Use it when

You want composable C# schemas, compile-time validator generation, or JSON Schema interop with tight control over validation behaviour and allocations.

It may not fit when

Data annotations or an existing validation library already cover your requirements, or Zod-style schemas would add an unnecessary second validation model.

Use cases

Concrete examples of what ZodSharp does, and who each one is for.

Developer

Validate input without exceptions or allocations

You need to validate values at an application boundary and prefer a result you can inspect over exception-driven control flow.

csharp
var nameSchema = Z.String().Min(3).Max(50);
var result = nameSchema.Validate("John");

if (result.IsSuccess)
    Console.WriteLine($"Valid name: {result.Value}");

What you get: Validate/SafeParse return a ValidationResult<T> with no exceptions, while Parse still throws ZodException when you want that behaviour.

Validation is zero-allocation on every valid input path, so the performance claim is a property of the code rather than an aspiration.

Read the guide →
Team lead

Return standard ProblemDetails for invalid requests

You want failed validation in ASP.NET Core to surface as standard ProblemDetails payloads rather than a bespoke error shape that clients must learn.

bash
dotnet add package Purview.ZodSharp.AspNetCore

What you get: The AspNetCore adapter converts failed validation results into the standard ProblemDetails / HttpValidationProblemDetails payloads.

One adapter package, and API consumers keep the error contract they already expect.

Read the guide →

Packages

4 packages

Install

Purview.ZodSharp primary

.NET CLI
dotnet add package Purview.ZodSharp

Purview.ZodSharp.SystemTextJson

.NET CLI
dotnet add package Purview.ZodSharp.SystemTextJson

Purview.ZodSharp.NewtonsoftJson

.NET CLI
dotnet add package Purview.ZodSharp.NewtonsoftJson

Purview.ZodSharp.AspNetCore

.NET CLI
dotnet add package Purview.ZodSharp.AspNetCore