JSON Schema Import
JSON Schema Import
Section titled “JSON Schema Import”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);Overloads
Section titled “Overloads”| 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.
Supported keywords
Section titled “Supported keywords”FromJsonSchemaParser (namespace ZodSharp.JsonSchema) maps:
type—string/number/integer/boolean/null/object/array.enum→ZodUnionof literals (a single member becomes a literal);const→ literal.anyOf/oneOf→ZodUnion;allOf→ first schema.- String constraints —
minLength,maxLength,pattern, andformat(email,uri,uuid). Theuuid/guidformat maps to the versionless.UUID(); JSON Schema has no versioneduuidformat, so a versioned.UUID(UuidVersion.V7)schema exports back as plainformat: "uuid". - Numeric constraints —
minimum,maximum,multipleOf;integeradditionally applies.Int(). - Objects —
requiredand optional fields viaZ.Object().Field(...). - Arrays —
items,minItems,maxItems.
Limitations
Section titled “Limitations”$refis supported only for local references (#/...); external$reftargets throwNotSupportedException.- The options type is currently empty; behaviour is fixed by the supported keyword set above.
Cross-platform reuse
Section titled “Cross-platform reuse”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.