Purview.ZodSharp.SystemTextJson
3.0.0-prerelease.2
dotnet add package Purview.ZodSharp.SystemTextJson --version 3.0.0-prerelease.2
NuGet\Install-Package Purview.ZodSharp.SystemTextJson -Version 3.0.0-prerelease.2
<PackageReference Include="Purview.ZodSharp.SystemTextJson" Version="3.0.0-prerelease.2" />
<PackageVersion Include="Purview.ZodSharp.SystemTextJson" Version="3.0.0-prerelease.2" />
<PackageReference Include="Purview.ZodSharp.SystemTextJson" />
paket add Purview.ZodSharp.SystemTextJson --version 3.0.0-prerelease.2
#r "nuget: Purview.ZodSharp.SystemTextJson, 3.0.0-prerelease.2"
#:package Purview.ZodSharp.SystemTextJson@3.0.0-prerelease.2
#addin nuget:?package=Purview.ZodSharp.SystemTextJson&version=3.0.0-prerelease.2&prerelease
#tool nuget:?package=Purview.ZodSharp.SystemTextJson&version=3.0.0-prerelease.2&prerelease
Purview.ZodSharp.SystemTextJson
System.Text.Json integration for Purview.ZodSharp . Deserialize-and-validate JSON in a single pass, create validating
JsonConverter<T> instances, and import JSON Schema definitions (Z.FromJsonSchema).
Installation
dotnet add package Purview.ZodSharp.SystemTextJson
Deserialize and validate
using ZodSharp;
var userSchema = Z.Object()
.Field("name", Z.String().Min(3))
.Field("age", Z.Number().Min(0).Int())
.Build();
var json = """{ "name": "John", "age": 30 }""";
var result = userSchema.DeserializeAndValidate(json);
if (result.IsSuccess)
Console.WriteLine($"Valid: {result.Value}");
Async stream overload:
await using var stream = File.OpenRead("user.json");
var result = await userSchema.DeserializeAndValidateAsync(stream);
In a trimmed, Native AOT or Blazor WebAssembly app, pass the contract from a source-generated JsonSerializerContext
(the JsonTypeInfo<T> overloads); the overloads above resolve it reflectively and are annotated so the build warns.
var result = schema.DeserializeAndValidate(json, AppJsonContext.Default.User);
Service-aware rules (an async uniqueness check that resolves a repository) run when a generated validator is given the caller's services:
var result = await new UserSchemaValidator().DeserializeAndValidateAsync(
stream, AppJsonContext.Default.User, services, cancellationToken);
var written = await new UserSchemaValidator().ValidateAndSerializeAsync(
user, output, AppJsonContext.Default.User, services, cancellationToken);
For HttpClient request and response bodies, see
Purview.ZodSharp.Http, which builds on these.
Validating converter
var converter = userSchema.CreateValidatingConverter();
var options = new JsonSerializerOptions { Converters = { converter } };
var value = JsonSerializer.Deserialize<User>(json, options);
Errors
A failure to read the JSON (malformed text, a null document, a value the serializer cannot convert, or a
NotSupportedException/InvalidOperationException thrown while deserializing) is reported as one error with code
invalid_json (SystemTextJsonExtensions.InvalidJsonCode), category invalid_type and the fixed message
"The JSON value could not be converted to {expected}.". The serializer's path becomes the error's FullPath
($.items[1].age becomes items[1].age) and its line and byte position become the line and position
parameters. The serializer's own message is never surfaced, because it names CLR types and can quote the input.
The validating converter throws ZodJsonValidationException (namespace ZodSharp.Json, a JsonException) carrying the
schema's full Errors, located under the converter's JSON path; its message still starts with "Validation failed: ",
so existing catch (JsonException) blocks keep working. DeserializeAndValidate unwraps it into the result.
UTC DateTimeOffset contract
The default PreserveOffset schema is exact and needs no converter. When the generated JSON Schema uses
DateTimeOffsetSerialization.Utc, add the matching converter so the serializer writes UTC and rejects a value
without an explicit Z/zero offset (or with a non-zero offset) on read:
using ZodSharp.Json.Serialization.SystemTextJson;
var options = new JsonSerializerOptions
{
Converters = { new UtcDateTimeOffsetConverter() },
};
PreserveOffsetDateTimeOffsetConverter is available to opt a type or property back out of a project-wide UTC
converter. See JSON Schema Export.
Import from JSON Schema
using ZodSharp;
using ZodSharp.JsonSchema.SystemTextJson;
var schema = Z.FromJsonSchema(jsonSchemaString);
var result = schema.Validate(data);
This lets you share schemas defined in TypeScript/Zod with your .NET backend. (Export via Z.ToJsonSchema lives in the core package.)
Documentation
License
MIT — see the package metadata in Purview.ZodSharp.SystemTextJson on NuGet.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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. net11.0 is compatible. |
-
net10.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.14)
- Microsoft.Extensions.Options (>= 10.0.14)
- Purview.ZodSharp (>= 3.0.0-prerelease.2)
-
net11.0
- Purview.ZodSharp (>= 3.0.0-prerelease.2)
NuGet packages (3)
Showing the top 3 NuGet packages that depend on Purview.ZodSharp.SystemTextJson:
| Package | Downloads |
|---|---|
|
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. |
|
|
Purview.ZodSharp.Http
HttpClient integration for ZodSharp: validates request bodies before they are sent and response bodies as they are read, and reads the validation problems Purview.ZodSharp.AspNetCore writes back into ZodSharp errors. Trim-, Native AOT- and Blazor WebAssembly-safe. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 3.0.0-prerelease.2 | 85 | 11/2/2026 |
| 2.4.2 | 170 | 10/26/2026 |
| 2.4.1 | 139 | 10/23/2026 |
| 2.4.0 | 273 | 10/22/2026 |
| 2.3.0 | 100 | 10/22/2026 |
| 2.2.0 | 110 | 10/18/2026 |
| 2.1.0 | 103 | 10/16/2026 |
| 2.0.2 | 121 | 10/8/2026 |
| 2.0.1.1 | 108 | 10/7/2026 |
| 2.0.1 | 104 | 10/7/2026 |
| 2.0.0 | 106 | 9/29/2026 |
| 2.0.0-prerelease.28 | 85 | 9/28/2026 |
| 2.0.0-prerelease.27 | 77 | 9/27/2026 |
| 2.0.0-prerelease.26 | 77 | 9/27/2026 |
| 2.0.0-prerelease.25 | 76 | 9/27/2026 |
| 2.0.0-prerelease.24 | 74 | 9/25/2026 |
| 2.0.0-prerelease.23 | 89 | 9/25/2026 |
| 2.0.0-prerelease.22 | 73 | 9/24/2026 |
| 2.0.0-prerelease.21 | 74 | 9/24/2026 |
| 2.0.0-prerelease.20 | 93 | 9/23/2026 |