MoonBit-native RFC 8927 JSON Type Definition validator and code generator
moon add ppyj663/moonjtd@0.1.1git clone https://github.com/ppyj663/MoonJTD.git
cd MoonJTD
moon test --target js
moon run examples/quickstart --target jslet document = @jtd.parse_checked_schema(
"{\"properties\":{\"id\":{\"type\":\"uint32\"}}}",
)
match document {
Ok(schema) => {
let errors = @jtd.validate(schema, Json::object(Map([
("id", Json::number(7.0)),
])))
println(errors.length())
}
Err(errors) => for error in errors { println(error.to_string()) }
}///|
let user = @jtd.jtd_object()
.required("id", @jtd.jtd_uint32())
.required("name", @jtd.jtd_string())
.optional("email", @jtd.jtd_string())
.build()moon run examples/quickstart --target js
moon run examples/codegen --target jsmoonjtd --help
moonjtd check schema.jtd.json
moonjtd validate schema.jtd.json instance.json
moonjtd generate schema.jtd.json RootType output.mbt
moonjtd inspect schema.jtd.json
moonjtd format schema.jtd.json output.jsonMoonJTD/
├── *.mbt, moon.pkg # portable core library package
├── cmd/main/ # JavaScript CLI entry point
├── cmd/conformance/ # upstream RFC 8927 corpus runner
├── examples/ # runnable quickstart and code generation demos
├── fixtures/ # small, authored CLI smoke-test inputs
├── docs/ # design, standards, provenance, and history
├── scripts/ # CLI smoke checks, coverage, conformance, and audit
└── .github/workflows/ # multi-target continuous integrationmoon build --target js
moon build --target wasm-gc
moon fmt --check
moon check --target js
moon test --target js
moon check --target wasm-gc
moon test --target wasm-gc
moon check --target native
moon test --target native
moon coverage analyze
pwsh ./scripts/conformance.ps1
pwsh ./scripts/audit.ps1
pwsh ./scripts/cli-smoke.ps1pub(all) suberror CodecError {
CodecInvalidSchema(Array[Diagnostic])
CodecInvalidJson(String)
CodecValidationFailed(Array[Diagnostic])
} derive(Eq, Debug)fn CodegenOptions::new(root_name? : String, public_types? : Bool, derive_eq? : Bool, derive_debug? : Bool, header? : Bool) -> CodegenOptionspub(all) struct Diagnostic {
code : DiagnosticCode
message : String
instance_path : JsonPointer
schema_path : JsonPointer
} derive(Eq, Debug)fn Diagnostic::new(code : DiagnosticCode, message : String, instance_path? : JsonPointer, schema_path? : JsonPointer) -> Diagnosticpub(all) enum DiagnosticCode {
InvalidJson
ExpectedObject
UnknownKeyword
MixedSchemaForms
InvalidMemberType
InvalidTypeName
EmptyEnum
DuplicateEnumValue
UnknownReference
NestedDefinitions
PropertyOverlap
InvalidDiscriminatorMapping
DuplicateDiscriminatorProperty
ValidationMismatch
MissingProperty
AdditionalProperty
InvalidDiscriminatorTag
UnknownDiscriminatorValue
ResourceLimitExceeded
InvalidIdentifier
GenerationFailure
} derive(Eq, Debug)fn DiscriminatorSchemaBuilder::branch(self : DiscriminatorSchemaBuilder, tag_value : String, schema : Schema) -> DiscriminatorSchemaBuilderfn DiscriminatorSchemaBuilder::build(self : DiscriminatorSchemaBuilder) -> Result[Schema, Diagnostic]fn DiscriminatorSchemaBuilder::metadata_entry(self : DiscriminatorSchemaBuilder, name : String, value : Json) -> DiscriminatorSchemaBuilderfn DiscriminatorSchemaBuilder::nullable(self : DiscriminatorSchemaBuilder, nullable : Bool) -> DiscriminatorSchemaBuilderfn DocumentBuilder::definition(self : DocumentBuilder, name : String, schema : Schema) -> DocumentBuilderfn JtdCodec::decode_batch(self : JtdCodec, values : Array[Json]) -> Array[Result[ValidatedJson, CodecError]]fn JtdCodec::decode_json_array(self : JtdCodec, text : StringView) -> Result[Array[ValidatedJson], CodecError]fn JtdCodec::encode_json(self : JtdCodec, value : Json, escape_slash? : Bool) -> Result[String, CodecError]fn JtdCodec::encode_validated(self : JtdCodec, value : ValidatedJson, escape_slash? : Bool) -> Stringfn JtdCodec::new(document : SchemaDocument, options? : ValidationOptions) -> Result[JtdCodec, CodecError]fn ObjectSchemaBuilder::allow_additional(self : ObjectSchemaBuilder, allowed : Bool) -> ObjectSchemaBuilderfn ObjectSchemaBuilder::metadata_entry(self : ObjectSchemaBuilder, name : String, value : Json) -> ObjectSchemaBuilderfn ObjectSchemaBuilder::nullable(self : ObjectSchemaBuilder, nullable : Bool) -> ObjectSchemaBuilderfn ObjectSchemaBuilder::optional(self : ObjectSchemaBuilder, name : String, schema : Schema) -> ObjectSchemaBuilderfn ObjectSchemaBuilder::required(self : ObjectSchemaBuilder, name : String, schema : Schema) -> ObjectSchemaBuilderpub(all) struct Schema {
form : SchemaForm
nullable : Bool
metadata : Map[String, Json]
} derive(Debug)fn SchemaCheckOptions::new(max_depth? : Int, max_nodes? : Int, max_errors? : Int) -> SchemaCheckOptionspub(all) struct SchemaNodeInfo {
path : JsonPointer
form : String
shape : String
nullable : Bool
child_count : Int
} derive(Eq, Debug)pub(all) struct SchemaStats {
node_count : Int
max_depth : Int
definition_count : Int
reference_count : Int
nullable_count : Int
empty_count : Int
type_count : Int
enum_count : Int
elements_count : Int
properties_count : Int
values_count : Int
discriminator_count : Int
required_property_count : Int
optional_property_count : Int
discriminator_branch_count : Int
} derive(Eq, Debug)fn ValidationOptions::new(max_errors? : Int, max_depth? : Int, max_ref_depth? : Int, max_nodes? : Int) -> ValidationOptionspub(all) struct ValidationReport {
errors : Array[Diagnostic]
visited_nodes : Int
truncated : Bool
} derive(Debug)fn codec_from_schema(schema_text : StringView, options? : ValidationOptions) -> Result[JtdCodec, CodecError]fn generate_moonbit_types(document : SchemaDocument, root_name? : String) -> Result[String, CodegenError]fn generate_moonbit_types_with(document : SchemaDocument, options : CodegenOptions) -> Result[String, CodegenError]fn is_rfc3339_timestamp(text : String) -> Boolfn moon_field_name(name : String) -> Stringfn moon_type_name(name : String) -> Stringfn validate_json_text(document : SchemaDocument, text : StringView) -> Result[Array[Diagnostic], Diagnostic]fn validate_with(document : SchemaDocument, value : Json, options : ValidationOptions) -> ValidationReportfn version_banner() -> StringInstall
Download zipMoonBit-native RFC 8927 JSON Type Definition validator and code generator