Skip to content

JSON Schema Import

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

Import a JSON Schema into a Purview.ZodSharp schema with Z.FromJsonSchema. This API is provided by the JSON integration packages — reference either Purview.ZodSharp.SystemTextJson or Purview.ZodSharp.NewtonsoftJson (both expose the same surface).

using ZodSharp;
var jsonSchemaString = """
{
"type": "object",
"properties": {
"name": { "type": "string", "minLength": 3 },
"email": { "type": "string", "format": "email" }
},
"required": ["name", "email"]
}
""";
var userSchema = Z.FromJsonSchema(jsonSchemaString);
var result = userSchema.Validate(userData);
Signature Notes
IZodSchema<object, object> FromJsonSchema(string jsonSchema, FromJsonSchemaOptions? options = null) parses the JSON string into a JsonSchemaDefinition, then into a schema
IZodSchema<object, object> FromJsonSchema(JsonSchemaDefinition schema, FromJsonSchemaOptions? options = null) import from an already-deserialized definition

FromJsonSchemaOptions is currently an empty placeholder reserved for future options.

FromJsonSchemaParser (namespace ZodSharp.JsonSchema) maps:

  • typestring / number / integer / boolean / null / object / array.
  • enumZodUnion of literals (a single member becomes a literal); const → literal.
  • anyOf / oneOfZodUnion; allOf → first schema.
  • String constraints — minLength, maxLength, pattern, and format (email, uri, uuid). The uuid/guid format maps to the versionless .UUID(); JSON Schema has no versioned uuid format, so a versioned .UUID(UuidVersion.V7) schema exports back as plain format: "uuid".
  • Numeric constraints — minimum, maximum, multipleOf; integer additionally applies .Int().
  • Objects — required and optional fields via Z.Object().Field(...).
  • Arrays — items, minItems, maxItems.
  • $ref is supported only for local references (#/...); external $ref targets throw NotSupportedException.
  • The options type is currently empty; behaviour is fixed by the supported keyword set above.

Export a TypeScript/Zod schema to JSON Schema (Zod v4+ z.toJSONSchema) and import it on the backend:

import { z } from "zod";
const UserSchema = z.object({ username: z.string().min(3), email: z.string().email() });
const jsonSchema = z.toJSONSchema(UserSchema);
var userSchema = Z.FromJsonSchema(jsonSchemaString);
var result = userSchema.Validate(incomingData);

See Cross-Platform Interop for the repository’s fixture-based verification of this loop.