Skip to main content

Module json_schema

Module json_schema 

Source
Expand description

JSON Schema loader

Loads a standard JSON Schema document into a CompiledSchema, so schemas that already exist elsewhere become ODK inputs without being rewritten in ODK’s own format. The motivating case is Rust types that derive their schema via schemars (for example Resonance’s ProjectDocument), where the Rust type is the source of truth and the JSON Schema is generated from it.

Draft 2020-12 ($defs) and draft-07 (definitions) are both accepted; they differ here only in where named subschemas live.

§Mapping

JSON SchemaCompiledSchema
titleschema name (falls back to file stem)
root object with propertiesa model named after the title
$defs / definitions entrya model, or a union if it is a string enum
properties.Xa field, optional unless listed in required
$ref: "#/$defs/Foo"CompiledType::Ref { name: "Foo" }
type: [T, "null"]type T, field marked optional
oneOf / anyOfCompiledType::Union
itemsCompiledType::Array
inline type: objectCompiledType::Object
minLength, maximum, pattern, …FieldConstraints

§Not represented

Keywords with no ODK equivalent are dropped rather than approximated: multi-branch allOf composition, not, conditional if/then/else, patternProperties, and additionalProperties schemas. A schema relying on them loads, but the constraint is not carried into the IR.

A single-branch allOf is unwrapped, because that is how schemars emits a $ref on a field that also has a description, and treating it as unrepresentable would silently drop the reference.

Enums§

JsonSchemaLoadError
Error type for JSON Schema loading.

Functions§

is_json_schema
Whether a JSON document looks like a JSON Schema rather than an ODK schema.
load_json_schema
Load a JSON Schema document from a file.
models_by_name
Named lookup of a schema’s models, for tests and callers that want one.
parse_json_schema
Convert a JSON Schema document into a CompiledSchema.