Zoi.JSONSchema (Zoi v0.10.7)
View SourceJSON Schema is a declarative language for annotating and validating JSON document's structure, constraints, and data types. It helps you standardize and define expectations for JSON data.
Zoi provides functionality to convert its type definitions into JSON Schema format, enabling seamless integration with systems that utilize JSON Schema for data validation and documentation.
Example
iex> schema = Zoi.object(%{name: Zoi.string(), age: Zoi.integer()})
iex> Zoi.to_json_schema(schema)
%{
"$schema": "https://json-schema.org/draft/2020-12/schema",
type: :object,
properties: %{
name: %{type: :string},
age: %{type: :integer}
},
required: [:name, :age],
additionalProperties: false
}Supported Types
The following Zoi types are supported for conversion to JSON Schema:
Zoi.string/0Zoi.integer/0Zoi.float/0Zoi.number/0Zoi.decimal/0- (converted to JSON Schemanumber)Zoi.boolean/0Zoi.literal/1Zoi.null/0Zoi.array/1Zoi.tuple/1Zoi.enum/1Zoi.map/0Zoi.object/1Zoi.intersection/1Zoi.union/1Zoi.nullable/1Zoi.date/0andZoi.ISO.date/0Zoi.datetime/0andZoi.ISO.datetime/0Zoi.naive_datetime/0andZoi.ISO.naive_datetime/0Zoi.time/0andZoi.ISO.time/0
Metadata
Zoi.to_json_schema/1 can also incorporate description and example metadata into the resulting JSON Schema:
iex> schema = Zoi.string(description: "A simple string", example: "Hello, World!")
iex> Zoi.to_json_schema(schema)
%{
"$schema": "https://json-schema.org/draft/2020-12/schema",
type: :string,
description: "A simple string",
example: "Hello, World!"
}Limitations
- Complex types or custom types not listed above will raise an error during conversion.
- Some advanced
Zoifeatures may not have direct equivalents in JSON Schema. - Refinements are partially supported, primarily for string patterns and length constraints.
- Additional properties in objects are disallowed by default (
additionalProperties: false).
References
Summary
Functions
@spec encode(Zoi.schema()) :: map()