Purview.ZodSharp
2.0.0-prerelease.15
See the version list below for details.
dotnet add package Purview.ZodSharp --version 2.0.0-prerelease.15
NuGet\Install-Package Purview.ZodSharp -Version 2.0.0-prerelease.15
<PackageReference Include="Purview.ZodSharp" Version="2.0.0-prerelease.15" />
<PackageVersion Include="Purview.ZodSharp" Version="2.0.0-prerelease.15" />
<PackageReference Include="Purview.ZodSharp" />
paket add Purview.ZodSharp --version 2.0.0-prerelease.15
#r "nuget: Purview.ZodSharp, 2.0.0-prerelease.15"
#:package Purview.ZodSharp@2.0.0-prerelease.15
#addin nuget:?package=Purview.ZodSharp&version=2.0.0-prerelease.15&prerelease
#tool nuget:?package=Purview.ZodSharp&version=2.0.0-prerelease.15&prerelease
ZodSharp
A high-performance schema validation library for C#, ported from TypeScript Zod. It features zero-allocation validation, struct-based rules, a fluent API, and a compile-time source generator for maximum performance.
This package is the core library. It also ships the [ZodSchema] source generator and JSON Schema export (Z.ToJsonSchema). JSON Schema import (Z.FromJsonSchema) and JSON serialization integrations are available in the companion packages:
Installation
dotnet add package Purview.ZodSharp
Quick start
using ZodSharp;
var nameSchema = Z.String().Min(3).Max(50);
var result = nameSchema.Validate("John");
if (result.IsSuccess)
Console.WriteLine($"Valid name: {result.Value}");
Composable schemas for objects, arrays, unions, discriminators, records, tuples and more:
var userSchema = Z.Object()
.Field("name", Z.String().Min(1))
.Field("age", Z.Number().Min(0).Max(120).Int())
.Field("email", Z.String().Email())
.Build();
Source generator
Mark a class, struct, or record with [ZodSchema] and a zero-allocation validator is generated at compile time:
using System.ComponentModel.DataAnnotations;
using ZodSharp;
[ZodSchema]
public class User
{
[Required]
[StringLength(50, MinimumLength = 3)]
public string Name { get; set; } = string.Empty;
[Range(0, 120)]
public int Age { get; set; }
[EmailAddress]
public string? Email { get; set; }
}
var result = UserSchema.Validate(user);
var validated = UserSchema.Parse(user); // throws on failure
// Value-first composition methods (enable via EnableComposition, on by default):
var adult = UserSchema.ApplyRefine(user, u => u.Age >= 18, "Must be adult");
DataAnnotations attributes such as [Required], [Length], [StringLength], [MinLength], [MaxLength], [Range], [RegularExpression], [AllowedValues], [DeniedValues], [EmailAddress], and [Compare] are validated with direct, typed codegen (no reflection).
Error factory
ErrorType lets you define a user-facing error (code, optional category, description, optional
message template with named placeholders, and — purely for convenience — an HTTP status code) and the
bundled source generator turns each [ErrorType] field in a partial class into strongly typed
Create/Throw helpers:
using ZodSharp.Core;
public static partial class ConcurrentErrorType
{
[ErrorType]
public static readonly ErrorType SaveFailed = new(
Code: "aggregate_save_failed",
Category: "invalid_value",
Description: "The aggregate could not be saved.",
HttpStatus: 409,
MessageFormat: "Aggregate '{AggregateId}' (of type {AggregateType}) failed to save",
Parameters:
[
new("AggregateId", typeof(string)),
ErrorType.Param<string>("AggregateType")
]);
}
// Returns a ValidationError with the code, category, the formatted message, and the typed parameters:
var error = ConcurrentErrorType.CreateSaveFailed("agg-123", "Invoice");
// Throws a ZodException carrying the same ValidationError:
ConcurrentErrorType.ThrowSaveFailed("agg-123", "Invoice");
The generated Create sets ValidationError.Category from the error type's Category — a broad
grouping that can span many specific codes (for example code invalid_tenant_id with category
invalid_value).
The bundled ZODSASP001/ZODSASP002/ZODSASP003 analyzers warn when a MessageFormat placeholder
is not declared in Parameters, when the containing class is not partial, or when the field is not
static readonly. The Purview.ZodSharp.AspNetCore package consumes this factory to map errors to
ProblemDetails responses.
Dependency injection
AddZodSharpFactory registers IZodSchemaFactory as a singleton. The factory is registered only if
one is not already present — the first call wins and later calls (including their configure
callbacks) are ignored, so state is never overwritten:
builder.Services.AddZodSharpFactory(factory => factory.RegisterFromAssembly(typeof(User).Assembly));
AddZodSchemaOptionsValidator<T> registers an IValidateOptions<T> that resolves the factory and
validates T when options are instantiated. The factory must already be registered. When no validator
exists for T, the default MissingValidatorBehavior.Throw throws via ResolveRequired<T>; pass
MissingValidatorBehavior.Ignore to pass through untouched:
builder.Services.AddZodSharpFactory(factory => factory.RegisterFromAssembly(typeof(UserOptions).Assembly));
builder.Services.AddZodSchemaOptionsValidator<UserOptions>();
// Or chain through the options builder, optionally failing at startup:
builder.Services.AddOptions<UserOptions>().AddZodSchemaValidator().ValidateOnStart();
The Purview.ZodSharp.AspNetCore package offers AddZodSharp with assembly auto-discovery.
JSON Schema export
var jsonSchema = Z.ToJsonSchema(userSchema, new ToJsonSchemaOptions { Title = "User" });
Documentation
License
MIT — see the package metadata in Purview.ZodSharp on NuGet.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.12)
- Microsoft.Extensions.Options (>= 10.0.12)
-
net8.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.12)
- Microsoft.Extensions.Options (>= 10.0.12)
-
net9.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.12)
- Microsoft.Extensions.Options (>= 10.0.12)
NuGet packages (6)
Showing the top 5 NuGet packages that depend on Purview.ZodSharp:
| Package | Downloads |
|---|---|
|
Purview.ZodSharp.AspNetCore
ASP.NET Core ProblemDetails integration for ZodSharp validation results. |
|
|
Purview.ZodSharp.SystemTextJson
A high-performance C# port of Zod schema validation library. Features zero-allocation validation, struct-based rules, fluent API, and source generator support for maximum performance. |
|
|
Purview.ZodSharp.NewtonsoftJson
A high-performance C# port of Zod schema validation library. Features zero-allocation validation, struct-based rules, fluent API, and source generator support for maximum performance. |
|
|
Purview.EventSourcing.Admin.API
ASP.NET Minimal API endpoints for Purview EventSourcing Admin Portal with OpenAPI documentation. |
|
|
Purview.EventSourcing.Admin.Site
Ready-to-use Razor Pages admin dashboard for Purview EventSourcing Admin Portal. Provides web UI for aggregate search, event inspection, and point-in-time projection queries. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 2.0.0-prerelease.16 | 0 | 9/21/2026 |
| 2.0.0-prerelease.15 | 0 | 9/21/2026 |
| 2.0.0-prerelease.14 | 0 | 9/21/2026 |
| 2.0.0-prerelease.13 | 38 | 9/20/2026 |
| 2.0.0-prerelease.12 | 44 | 9/20/2026 |
| 2.0.0-prerelease.11 | 43 | 9/19/2026 |
| 2.0.0-prerelease.10 | 42 | 9/19/2026 |
| 2.0.0-prerelease.9 | 59 | 9/18/2026 |
| 2.0.0-prerelease.8 | 48 | 9/17/2026 |
| 2.0.0-prerelease.7 | 60 | 9/17/2026 |
| 2.0.0-prerelease.6 | 116 | 9/14/2026 |
| 2.0.0-prerelease.5 | 96 | 9/12/2026 |
| 2.0.0-prerelease.4 | 67 | 9/12/2026 |
| 2.0.0-prerelease.3 | 107 | 9/8/2026 |
| 2.0.0-prerelease.2 | 82 | 9/6/2026 |
| 2.0.0-prerelease.1 | 80 | 9/5/2026 |