Skip to content

Latest commit

 

History

History
65 lines (44 loc) · 2.05 KB

README.md

File metadata and controls

65 lines (44 loc) · 2.05 KB

Purview.ZodSharp.SystemTextJson

NuGet version Release

System.Text.Json integration for Purview.ZodSharp NuGet version. 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);

Validating converter

var converter = userSchema.CreateValidatingConverter();
var options = new JsonSerializerOptions { Converters = { converter } };
var value = JsonSerializer.Deserialize<User>(json, options);

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.