Getting Started
Getting Started
Section titled “Getting Started”Install
Section titled “Install”dotnet add package Purview.ZodSharpAdd the integration packages you need:
# System.Text.Json integration + JSON Schema importdotnet add package Purview.ZodSharp.SystemTextJson
# Newtonsoft.Json integration + JSON Schema importdotnet add package Purview.ZodSharp.NewtonsoftJson
# ASP.NET Core ProblemDetails integrationdotnet add package Purview.ZodSharp.AspNetCorePurview.ZodSharp— core library, source generator, and JSON Schema export.Purview.ZodSharp.SystemTextJson— System.Text.Json deserialize-and-validate, validating converters, and JSON Schema import.Purview.ZodSharp.NewtonsoftJson— Newtonsoft.Json deserialize-and-validate, validating converters, and JSON Schema import.Purview.ZodSharp.AspNetCore— failed validation results converted to standardProblemDetails/HttpValidationProblemDetailspayloads.
First schema
Section titled “First schema”using ZodSharp;
var nameSchema = Z.String().Min(3).Max(50);var result = nameSchema.Validate("John");
if (result.IsSuccess) Console.WriteLine($"Valid name: {result.Value}");Validate, SafeParse, and Parse
Section titled “Validate, SafeParse, and Parse”Validatereturns aValidationResult<T>— no exceptions.SafeParseis an alias ofValidate.ParsethrowsZodExceptionon failure.
var value = nameSchema.Parse("AB"); // throws ZodException
var result = nameSchema.SafeParse("AB"); // non-throwingif (!result.IsSuccess){ foreach (var error in result.Errors) Console.WriteLine($" - {string.Join(".", error.Path)}: {error.Message}");}Object validation
Section titled “Object validation”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();
var userData = new Dictionary<string, object?>{ { "name", "John Doe" }, { "age", 30.0 },};
var result = userSchema.Validate(userData);Source-generated validators
Section titled “Source-generated validators”Mark a class, struct, or record with [ZodSchema] and a zero-allocation static 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 failureSee the Source Generator and Source Generator DataAnnotations pages for the full feature set.
JSON integration
Section titled “JSON integration”// System.Text.Json or Newtonsoft.Jsonvar json = """{ "name": "John", "age": 30 }""";var result = userSchema.DeserializeAndValidate(json);// JSON Schema export (core)var jsonSchema = Z.ToJsonSchema(userSchema, new ToJsonSchemaOptions { Title = "User" });
// JSON Schema import (requires an integration package)var imported = Z.FromJsonSchema(jsonSchemaString);See System.Text.Json Integration, Newtonsoft.Json Integration, JSON Schema Export, and JSON Schema Import.