Skip to content

Fluent Schema API

Preview ZodSharp Reviewed 2026-09-16 purview-dev/zodsharp aspnetcore c-sharp csharp dotnet json-schema newtonsoft-json schema source-generator system-text-json validation zero-allocation zod

public static class Z (namespace ZodSharp) is the entry point for creating schemas.

Method Signature Returns
String() Z.String() ZodString
Number() Z.Number() ZodNumber
Boolean() Z.Boolean() ZodBoolean
Null() Z.Null() ZodNull
Array<T> Z.Array<T>(IZodSchema<T, T> elementSchema) ZodArray<T>
Optional<T> Z.Optional<T>(IZodSchema<T, T> schema)T : class ZodOptional<T>
Nullable<T> Z.Nullable<T>(IZodSchema<T, T> schema)T : struct ZodNullable<T>
Object() Z.Object() ZodObjectBuilder
Union (untyped) Z.Union(params IZodSchema<object, object>[] options) ZodUnion
Union<T1,T2> (typed) Z.Union<T1, T2>(IZodSchema<T1, T1>, IZodSchema<T2, T2>) ZodTypedUnion<T1, T2>
Intersection<T> Z.Intersection<T>(IZodSchema<T, T> left, IZodSchema<T, T> right) ZodIntersection<T>
Literal<T> Z.Literal<T>(T value)T : IEquatable<T> ZodLiteral<T>
Lazy<T> Z.Lazy<T>(Func<IZodSchema<T, T>> schemaGetter) ZodLazy<T>
DiscriminatedUnion Z.DiscriminatedUnion(string discriminator) ZodDiscriminatedUnionBuilder
Enum (string values) Z.Enum(params string[] values) ZodEnum
Enum<TEnum> (native) Z.Enum<TEnum>()TEnum : struct, Enum ZodNativeEnum<TEnum>
Record<TValue> Z.Record<TValue>(IZodSchema<TValue, TValue> valueSchema) ZodRecord<TValue>
Tuple<T1,T2> Z.Tuple<T1, T2>(IZodSchema<T1, T1>, IZodSchema<T2, T2>) ZodTuple<T1, T2>
Tuple<T1,T2,T3> Z.Tuple<T1, T2, T3>(IZodSchema<T1, T1>, IZodSchema<T2, T2>, IZodSchema<T3, T3>) ZodTuple<T1, T2, T3>
ToJsonSchema<T> Z.ToJsonSchema<T>(IZodSchema<T, T> schema, ToJsonSchemaOptions? options = null) JsonSchemaDefinition

Each schema type has its own page:

  • IZodSchema<TOutput, TInput>Validate / ValidateAsync; IZodSchema<T> is the convenience form where input equals output.
  • IZodSchemaValidator (marker) and IZodSchemaValidator<T> — the DI-facing adapter surface (see Dependency Injection).
  • IValidationRule<T> — the rule contract implemented by every struct rule.

Because composition is implemented on the base ZodType, every schema can chain .Describe(...), .Transform(...), .Refine(...), .SuperRefine(...), .Pipe(...), .Catch(...), .Prefault(...), .Default(...), .And(...), and .Or(...). See Composition and Transforms.