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 Schema | CompiledSchema |
|---|---|
title | schema name (falls back to file stem) |
root object with properties | a model named after the title |
$defs / definitions entry | a model, or a union if it is a string enum |
properties.X | a field, optional unless listed in required |
$ref: "#/$defs/Foo" | CompiledType::Ref { name: "Foo" } |
type: [T, "null"] | type T, field marked optional |
oneOf / anyOf | CompiledType::Union |
items | CompiledType::Array |
inline type: object | CompiledType::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§
- Json
Schema Load Error - 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.