json/blueprint/codegen/schema
Generate native records and ordinary typed codecs from a closed-object schema.
This development-time module returns source; the caller writes, formats and
compiles it. Generated applications need only json_blueprint. Retain the
root codec when reusing it, as with a handwritten codec.
Supported children are strings, booleans, integers, exact numbers, inclusive
ranges, objects, lists, pairs and nullable values. Optional nullable fields
use Option(Option(a)) to distinguish missing, null and a supplied value.
Enums, unions and unconstrained values return located errors. Native Int
projection retains the runtime codec’s limits. BEAM-only integer bounds
produce source that must be checked for the intended target; JavaScript
cannot safely use those bounds. Exact Number does not use
floating-point conversion.
Only the root <name>_codec() accessor is public. Root and nested records
are public. Wire names are preserved; native names normalize codepoints.
Uppercase ASCII letters each start an underscore-separated word. Delimiter
runs collapse, digit-leading names gain field_, and keywords gain _.
Empty names and collisions fail rather than silently renaming fields.
import json/blueprint/codegen/schema
import json/blueprint/contract
import json/blueprint/value
pub fn generate_order() {
let assert Ok(contract) =
contract.parse(
"{\"$schema\":\"https://json-schema.org/draft/2020-12/schema\",\"type\":\"object\",\"properties\":{\"orderId\":{\"type\":\"string\"}},\"required\":[\"orderId\"],\"additionalProperties\":false}",
value.default_limits(),
)
schema.compile("generated/order", "order", contract.schema(contract))
}
Types
Generation refuses unsupported shapes or source names before returning source. Paths locate the canonical rendered schema, not the original document bytes.
pub type CompileError {
InvalidModuleName(module_path: String, reason: String)
InvalidRootName(name: String, reason: String)
InvalidFieldName(
path: List(codec.PathSegment),
wire_name: String,
)
NameCollision(
name: String,
first: NameOrigin,
second: NameOrigin,
)
UnsupportedShape(
path: List(codec.PathSegment),
kind: UnsupportedKind,
)
}
Constructors
-
InvalidModuleName(module_path: String, reason: String) -
InvalidRootName(name: String, reason: String) -
InvalidFieldName( path: List(codec.PathSegment), wire_name: String, ) -
NameCollision( name: String, first: NameOrigin, second: NameOrigin, ) -
UnsupportedShape( path: List(codec.PathSegment), kind: UnsupportedKind, )
Logical canonical schema locations, or a language/imported type name.
pub type NameOrigin {
SchemaLocation(List(codec.PathSegment))
ReservedName
}
Constructors
-
SchemaLocation(List(codec.PathSegment)) -
ReservedName
A shape outside the native generation profile.
pub type UnsupportedKind {
NonObjectRoot
StringEnum
TaggedUnion
AnyValue
UnknownShape
}
Constructors
-
NonObjectRoot -
StringEnum -
TaggedUnion -
AnyValue -
UnknownShape
Values
pub fn compile(
module_path: String,
name: String,
schema: codec.Schema,
) -> Result(codegen.GeneratedModule, CompileError)
Return deterministic source and its SHA-1 content fingerprint.
Object properties are ordered by Unicode codepoint order of their wire names. The input must
already be an admitted Schema; this function does not load JSON Schema.