API Reference
Core Functions
Struct2JSONSchema.generate_schema — Function
generate_schema(T; ctx=SchemaContext(), simplify=true, inline_all_defs=false)Variant of generate_schema! that clones the provided context before generation. The original ctx is unaffected.
If simplify=true (default), the schema is simplified by removing unused definitions, inlining single-use references, and sorting definitions.
If inline_all_defs=true, all $ref references are expanded inline and the $defs section is removed entirely (except for recursive definitions which must remain in $defs). This option takes precedence over simplify.
Struct2JSONSchema.generate_schema! — Function
generate_schema!(T; ctx=SchemaContext(), simplify=true, inline_all_defs=false)Materialize a schema for T using the provided mutable context. ctx is updated in-place and the function returns a named tuple with doc (the JSON schema document) and unknowns (newly encountered unsupported types).
If simplify=true (default), the schema is simplified by removing unused definitions, inlining single-use references, and sorting definitions.
If inline_all_defs=true, all $ref references are expanded inline and the $defs section is removed entirely (except for recursive definitions which must remain in $defs). This option takes precedence over simplify.
Context Management
Struct2JSONSchema.SchemaContext — Type
SchemaContextHolds all state required while generating JSON Schemas. The context tracks $defs, previously computed keys, the current traversal path, override registrations, and knobs for auto-detecting optional fields. Construct a context with SchemaContext() and pass it to the API helpers.
Struct2JSONSchema.UnknownEntry — Type
UnknownEntryRepresents a type that could not be processed, with a reason.
Fields
type::Any: The type that could not be processedpath::Tuple{Vararg{Symbol}}: The path where the type was encounteredreason::String: The reason why processing failed
Type Registration
Struct2JSONSchema.override_abstract! — Function
override_abstract!(ctx, A; variants, discr_key, tag_value, require_discr=true)Register a discriminator schema for the abstract type A. variants must be a vector of concrete subtypes, discr_key the discriminator field name, and tag_value a Dict mapping each variant to a JSON scalar tag. When require_discr is true the discriminator field is marked as required.
Struct2JSONSchema.override! — Function
override!(generator, ctx)Append a generic override function. generator receives the live SchemaContext and must return a replacement schema Dict or nothing to allow later overrides/default generation to run. Exceptions are caught; when ctx.verbose is true a warning is logged before falling back to the next candidate.
Struct2JSONSchema.override_type! — Function
override_type!(generator, ctx, T)Convenience wrapper over override! that only fires when the currently generated type is exactly T. The supplied generator should return the full replacement schema for that type.
Struct2JSONSchema.override_field! — Function
override_field!(generator, ctx, T, field)Register a context-aware override for the field field on struct T. The override runs while the field is visited and may return any schema Dict. Returning nothing falls back to downstream overrides or the default $ref.
Struct2JSONSchema.optional! — Function
optional!(ctx, T, fields...)Mark specific fields on T as optional regardless of their declared types. fields may be supplied as a collection of Symbols or as varargs.
Struct2JSONSchema.describe! — Function
describe!(ctx, T, field, description)Register a description for the field field on struct T. The description will be added to the JSON Schema as the description property. Manual registration takes priority over automatic extraction via REPL.fielddoc.
Optional Fields
Struct2JSONSchema.auto_optional_nothing! — Function
Enable automatic Union{T,Nothing} → optional field detection.
Struct2JSONSchema.auto_optional_missing! — Function
Enable automatic Union{T,Missing} → optional field detection.
Struct2JSONSchema.auto_optional_null! — Function
auto_optional_null!(ctx)Helper that enables both Union{T,Nothing} and Union{T,Missing} detection in one call.
Field Filtering
Struct2JSONSchema.skip! — Function
skip!(ctx, T, fields...)Mark specific fields on T to be completely skipped (excluded from schema generation). Skipped fields will not appear in properties or required. fields may be supplied as a collection of Symbols or as varargs.
Struct2JSONSchema.only! — Function
only!(ctx, T, fields...)Mark that only the specified fields on T should be included in the schema. All other fields will be skipped. This is the inverse of skip!. fields may be supplied as a collection of Symbols or as varargs.
Default Values
Struct2JSONSchema.defaultvalue! — Function
defaultvalue!(ctx::SchemaContext, instance) -> NothingRegister default values for all fields of a struct instance (recursively).
Example
ctx = SchemaContext()
defaultvalue!(ctx, ServerConfig("localhost", 8080))Struct2JSONSchema.defaultvalue_serializer! — Function
defaultvalue_serializer!(ctx::SchemaContext, serializer::Function) -> NothingRegister a custom serializer for default values.
Serializers are evaluated in FIFO order. Return nothing to fall through.
Example
defaultvalue_serializer!(ctx) do field_type, value, ctx
value isa MyType ? serialize_my_type(value) : nothing
endStruct2JSONSchema.defaultvalue_type_serializer! — Function
defaultvalue_type_serializer!(ctx::SchemaContext, T::Type, serializer::Function) -> NothingRegister a custom serializer for all values of type T.
Example
defaultvalue_type_serializer!(ctx, Color) do value, ctx
"#" * join(string.(value.r, value.g, value.b), base=16, pad=2)
endStruct2JSONSchema.defaultvalue_field_serializer! — Function
defaultvalue_field_serializer!(ctx::SchemaContext, Parent::Type, field::Symbol, serializer::Function) -> NothingRegister a custom serializer for a specific field.
Example
defaultvalue_field_serializer!(ctx, Metrics, :created_at) do value, ctx
Int(datetime2unix(value))
endIndex
Struct2JSONSchema.SchemaContextStruct2JSONSchema.UnknownEntryStruct2JSONSchema.auto_optional_missing!Struct2JSONSchema.auto_optional_nothing!Struct2JSONSchema.auto_optional_null!Struct2JSONSchema.defaultvalue!Struct2JSONSchema.defaultvalue_field_serializer!Struct2JSONSchema.defaultvalue_serializer!Struct2JSONSchema.defaultvalue_type_serializer!Struct2JSONSchema.describe!Struct2JSONSchema.generate_schemaStruct2JSONSchema.generate_schema!Struct2JSONSchema.only!Struct2JSONSchema.optional!Struct2JSONSchema.override!Struct2JSONSchema.override_abstract!Struct2JSONSchema.override_field!Struct2JSONSchema.override_type!Struct2JSONSchema.skip!