Purview.Results.ZodSharp
2.0.0
dotnet add package Purview.Results.ZodSharp --version 2.0.0
NuGet\Install-Package Purview.Results.ZodSharp -Version 2.0.0
<PackageReference Include="Purview.Results.ZodSharp" Version="2.0.0" />
<PackageVersion Include="Purview.Results.ZodSharp" Version="2.0.0" />
<PackageReference Include="Purview.Results.ZodSharp" />
paket add Purview.Results.ZodSharp --version 2.0.0
#r "nuget: Purview.Results.ZodSharp, 2.0.0"
#:package Purview.Results.ZodSharp@2.0.0
#addin nuget:?package=Purview.Results.ZodSharp&version=2.0.0
#tool nuget:?package=Purview.Results.ZodSharp&version=2.0.0
Purview.Results.ZodSharp
Bridges ZodSharp ValidationResult<T> values into
Purview.Results, so validation outcomes flow through the
same result pipeline as every other expected outcome instead of throwing.
Installation
dotnet add package Purview.Results.ZodSharp
Quick start
ZodSharp validation never throws: Validate returns a ValidationResult<T> carrying the validated value on
success and every ValidationError on failure. ToResult translates that into a result whose error type the
caller chooses:
return RepositoryReconciliationResultSchema
.Validate(reconciliationResult)
.ToResult<RepositoryReconciliationResult, ReconciliationError>(errors =>
new ReconciliationResultInvalid(reconciliationResult, errors)
);
When the surrounding method's success type differs from the validated type, produce the failure with the
generated AsFailure<TValue>() helper instead, because the validated value is not the value the method
succeeds with:
var validated = ProviderConnectionIdSchema.Validate(providerConnectionId);
if (!validated.IsSuccess)
return new ProviderConnectionInvalid(providerConnectionId, validated.Errors)
.AsFailure<RepositoryReconciliationResult>();
return await ReconcileCoreAsync(validated.Value, repositories, cancellationToken);
API
| Member | Purpose |
|---|---|
ValidationResult<TValue>.ToResult<TValue, TError>(Func<ImmutableArray<ValidationError>, TError> onFailure) |
Success carries the validated value; failure carries the created error. onFailure runs only when validation failed, so a successful validation allocates no error |
ValidationResult<TValue>.ToUnitResult<TValue, TError>(Func<ImmutableArray<ValidationError>, TError> onFailure) |
The value-discarding counterpart: success is a unit Result<TError> and failure carries the created error. Use it when the method reports only why the input was rejected |
ValueTask<ValidationResult<TValue>>.ToResultAsync<TValue, TError>(Func<ImmutableArray<ValidationError>, TError> onFailure) |
The asynchronous counterpart, for a schema whose validation awaits |
ValueTask<ValidationResult<TValue>>.ToUnitResultAsync<TValue, TError>(Func<ImmutableArray<ValidationError>, TError> onFailure) |
The value-discarding asynchronous counterpart |
Task<ValidationResult<TValue>>.ToResultAsync<TValue, TError>(...) / .ToUnitResultAsync<TValue, TError>(...) |
The same, for APIs that return a Task - Purview.ZodSharp.Http's request and response validation |
IValidationErrorCarrier |
Implemented by an error value that carries ZodSharp validation errors, so an HTTP layer can turn it into a validation problem without knowing the error type |
The error type must be named explicitly when it is a union, as in
ToResult<RepositoryReconciliationResult, ReconciliationError>(...), because a union case does not carry the
union type that contains it. ToUnitResult (and the async forms) still name the validated value type for the same
reason, even when the value is discarded.
A factory returning a result rather than an error is deliberately not offered: for a lambda returning
Result<TValue, TError> the compiler prefers a Func<..., TError> parameter and would silently nest the
results. Naming the error type explicitly keeps the intent unambiguous.
Async validation
ValidateAsync — on the generated schema, the DI validator adapter, or a
[ZodSchema(CustomValidationMethodName = ...)] rule — returns a ValueTask<ValidationResult<T>>, and
ToResultAsync/ToUnitResultAsync fold that into a result exactly as the synchronous methods fold Validate:
return await TenantInputSchemaValidator
.ValidateAsync(input, cancellationToken)
.ToUnitResultAsync<TenantInput, TenantError>(errors => new TenantInputInvalid(input, errors));
Validation over HTTP
ToResultAsync and ToUnitResultAsync also accept a Task<ValidationResult<T>>, the shape
Purview.ZodSharp.Http returns. A client's request and response
validation therefore folds into a result like a local Validate, and a server's validation problem reads back into
the same case:
// Validated before it is sent: an invalid body is never sent, and its errors become the case.
var sent = await http
.PostAsJsonValidatedAsync("/api/tenants", input, validator, AppJsonContext.Default.TenantInput, services)
.ToResultAsync<HttpResponseMessage, TenantError>(errors => new TenantInputInvalid(input, errors));
// Rejected by the server: the problem Purview.ZodSharp.AspNetCore wrote reads back into ZodSharp errors.
if (sent.TryGetValue(out var response) && await response.TryReadZodValidationProblemAsync() is { } problem)
return new TenantInputInvalid(input, problem.ToValidationErrors()).AsFailure<Tenant>();
validator is the generated {Type}SchemaValidator. This package takes no dependency on Purview.ZodSharp.Http;
reference it in the client. Both are trim-, Native AOT- and Blazor WebAssembly-safe.
Examples
src/src/Examples.Zod is a
runnable console example that validates a [ZodSchema] TenantInput and turns the outcome into a
Result<Tenant, TenantError>, with the rejection carrying its reported ValidationErrors.
dotnet run --project src/src/Examples.Zod
The repository README lists the Basic, ASP.NET Core, ASP.NET Core + Zod, value-objects composition and Blazor WebAssembly/Server examples too.
Related packages
| Package | Purpose |
|---|---|
Purview.Results.ZodSharp.AspNetCore |
Renders the validation errors an IValidationErrorCarrier error carries as HttpValidationProblemDetails |
Agent skills
The purview-results-zodsharp-validation agent skill covers this package. No package ships skills: install them, with every other Purview.Results skill, from the git repository with the skills CLI:
npx skills add purview-dev/results
License
MIT — see LICENSE.md.
| 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
- Purview.Results (>= 2.0.0)
- Purview.ZodSharp (>= 3.0.0)
-
net11.0
- Purview.Results (>= 2.0.0)
- Purview.ZodSharp (>= 3.0.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Purview.Results.ZodSharp:
| Package | Downloads |
|---|---|
|
Purview.Results.ZodSharp.AspNetCore
Maps Purview result failures that carry ZodSharp validation errors onto ASP.NET Core HttpValidationProblemDetails responses. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 2.0.0 | 96 | 11/16/2026 |
| 2.0.0-prerelease.1 | 75 | 11/12/2026 |
| 1.1.0-prerelease.3 | 65 | 11/8/2026 |
| 1.1.0-prerelease.2 | 95 | 11/3/2026 |
| 1.0.4 | 126 | 10/26/2026 |
| 1.0.3 | 139 | 10/22/2026 |
| 1.0.2 | 117 | 10/16/2026 |
| 1.0.0.1 | 164 | 10/8/2026 |
| 1.0.0 | 108 | 10/7/2026 |
| 1.0.0-prerelease.2 | 99 | 9/30/2026 |
| 1.0.0-prerelease.1 | 77 | 9/30/2026 |