Sign in

    smithy-jsonschema

    Convert assembled Smithy models to JSON Schema

    smithy
    json-schema
    moonbit
    Download zip
    Author
    Version
    0.6.0
    License
    Apache-2.0
    Last updated
    4 hours ago
    Downloads
    42

    #Smithy JSON Schema

    Converts an assembled Smithy model into a JSON Schema document.

    #Library

    ///|
    test {
    let loaded = @smithy.Loader::new()
    .add_idl(
    "example.smithy", "$version: \"2\"\nnamespace example\nstructure Value { name: String }",
    )
    .assemble()
    let document = @jsonschema.convert(loaded.model.unwrap())
    assert_true(document is { "definitions": { "Value": _, .. }, .. })
    }

    Pass service="example#Api" to keep the service's connected value shapes. Services, operations, resources and prelude definitions are omitted. Mixins are flattened before conversion. Simple shapes and lists are inlined where the reference converter inlines them; shared aggregate shapes use $ref. Conflicting exported names and invalid configuration raise JsonSchemaError.

    Protocol converters can prepare SchemaIndex::new(model, service~, config~) once, then use document(), shape(id), schema_for_member(id) and structure(id, selected_members) with the same contextual references. Filtering a structure happens before required properties are computed. definition(name) resolves component names within the selected service closure.

    Protocol exporters can opt into SchemaIndex::for_values or convert_values. This mode replaces target constraints with explicit member overrides, retains intrinsic integer/float bounds, closes structures, limits unions to exactly one known member, and allows null collection values only where sparse or Document permits them. Shared data and constrained member variants use definitions so recursive collections remain finite. It does not change the reference-compatible defaults. Wire-specific limitations such as decoded blob lengths and timestamp policies still require the exporting protocol's diagnostics and extensions. Value conversion requires union_strategy="oneOf", map_strategy="propertyNames" and support_non_numeric_floats=false. Other settings for these options raise InvalidConfiguration because they discard runtime member, map-key, sparse-value or numeric-bound contracts.

    #Build plugin

    The Smithy CLI registers the jsonSchema plugin. Put this in smithy-build.json, then run smithy build:

    { "version": "1.0", "sources": ["model"], "projections": { "schema": { "plugins": { "jsonSchema": { "service": "example#Api", "useJsonName": true } } } } }

    The document is written to build/smithy/schema/jsonSchema/schema.json.

    Plugin settingLibrary Config::new argumentDefault
    useJsonNameuse_json_namefalse
    unionStrategyunion_strategyoneOf; also object, structure
    enumStrategyenum_strategyenum; also oneOf
    mapStrategymap_strategypropertyNames; also patternProperties
    defaultTimestampFormatdefault_timestamp_formatdate-time; also http-date, epoch-seconds
    supportNonNumericFloatssupport_non_numeric_floatsfalse
    definitionPointerdefinition_pointer#/definitions
    schemaDocumentExtensionsschema_document_extensions{}
    addReferenceDescriptionsadd_reference_descriptionsfalse
    disableDefaultValuesdisable_default_valuesfalse
    disableIntEnumsdisable_int_enumsfalse
    useIntegerTypeuse_integer_typefalse
    alphanumericOnlyRefsalphanumeric_only_refsfalse; sanitize names before collision detection

    definitionPointer starts with #/ and supports nested paths such as #/components/schemas. As in the reference, pointer escaping is unsupported. The plugin's jsonSchemaVersion accepts draft07 or draft2020-12; the latter defaults the definition pointer to #/$defs.

    JsonSchemaError

    pub(all) suberror JsonSchemaError {
    ConflictingShapeName(String)
    InvalidConfiguration(String)
    } derive(
    Debug
    )

    Config

    pub(all) struct Config {
    map_strategy : String
    union_strategy : String
    enum_strategy : String
    use_json_name : Bool
    default_timestamp_format : String
    support_non_numeric_floats : Bool
    schema_document_extensions : Json
    definition_pointer : String
    add_reference_descriptions : Bool
    disable_default_values : Bool
    disable_int_enums : Bool
    use_integer_type : Bool
    alphanumeric_only_refs : Bool
    }

    Conversion settings shared with the JSON Schema build plugin.

    Config::new

    fn Config::new(map_strategy? : String, union_strategy? : String, enum_strategy? : String, use_json_name? : Bool, default_timestamp_format? : String, support_non_numeric_floats? : Bool, schema_document_extensions? : Json, definition_pointer? : String, add_reference_descriptions? : Bool, disable_default_values? : Bool, disable_int_enums? : Bool, use_integer_type? : Bool, alphanumeric_only_refs? : Bool) -> Config

    SchemaIndex

    pub struct SchemaIndex {
    shapes : Map[String, Json]
    names : Map[String, String]
    config : Config
    document : Json
    definitions : Map[String,
    ShapeId
    ]
    // private fields
    }

    A prepared conversion context for protocol converters that need the complete schema document plus schemas for individual members or filtered structures. The encoded model and contextual names are prepared once, not per binding.

    SchemaIndex::definition

    fn SchemaIndex::definition(self : SchemaIndex, name : String) ->
    ShapeId
    ?

    Resolve a contextual component name within the selected service closure.

    SchemaIndex::document

    fn SchemaIndex::document(self : SchemaIndex) -> Json

    SchemaIndex::for_values

    fn SchemaIndex::for_values(model :
    Model
    , service? : String, config? : Config) -> SchemaIndex raise

    Prepare value-compatible schemas without changing reference conversion.

    SchemaIndex::name

    SchemaIndex::new

    fn SchemaIndex::new(model :
    Model
    , service? : String, config? : Config) -> SchemaIndex raise

    SchemaIndex::schema_for_member

    fn SchemaIndex::schema_for_member(self : SchemaIndex, id :
    ShapeId
    , config? : Config?) -> Json raise JsonSchemaError

    Convert a member using the same inline/reference rules as a full document.

    SchemaIndex::shape

    Convert a value shape inline, retaining the shared contextual references.

    SchemaIndex::structure

    Convert only the selected members of a structure. Filtering occurs before required/property generation, so omitted HTTP bindings cannot leave stale required properties in synthesized document payloads.

    convert

    fn convert(model :
    Model
    , service? : String, config? : Config) -> Json raise

    Convert value shapes in an assembled model to a JSON Schema document.

    convert_values

    fn convert_values(model :
    Model
    , service? : String, config? : Config) -> Json raise

    Convert with runtime value-contract semantics while retaining the existing reference-compatible convert signature and Config record.

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io