Compile-once JSON Schema 2020-12 validator for MoonBit: exact decimal arithmetic, precise pattern semantics, no silent schema errors, no hidden I/O.
///|
let document = @json-schema.parse(
"{\"type\":\"object\",\"required\":[\"amount\"],\"properties\":{\"amount\":{\"type\":\"number\",\"multipleOf\":0.1}},\"additionalProperties\":false}",
)
///|
let schema = @json-schema.Schema::compile(document)
///|
let instance = @json-schema.parse("{\"amount\":0.3}")
///|
let valid = schema.valid(instance)
///|
let failures = schema.faults(instance)///|
test "preloaded resource references" {
let resources : Map[String, Json] = Map::from_array([
(
"https://example.com/schemas/positive",
{ "type": "integer", "minimum": 1 },
),
])
let schema = @json-schema.Schema::compile(
{ "$ref": "positive" },
base_uri="https://example.com/schemas/request",
resources~,
)
assert_true(schema.valid(Json::number(2)))
assert_false(schema.valid(Json::number(0)))
}moon check --target native --deny-warn --output-json
moon test --target native --deny-warn --output-json
moon check --target js --deny-warn --output-json
moon test --target js --deny-warn --output-json
moon check --target wasm --deny-warn --output-json
moonx benchmarks/run-campaign.mbtxpub(all) suberror CompileError {
InvalidKeyword(path~ : String, reason~ : String)
UnknownDialect(String)
UnsupportedVocabulary(dialect~ : String, vocabulary~ : String)
UnsupportedKeyword(path~ : String, keyword~ : String)
UnsupportedPattern(path~ : String, pattern~ : String, reason~ : String)
DepthLimit(Int)
NumericBudget(path~ : String)
} derive(Debug)pub(all) suberror ParseError {
InvalidJson(position~ : Int, reason~ : String)
DepthLimit(Int)
} derive(Debug)pub struct CompiledPattern {
// private fields
}pub struct Schema {
// private fields
}Install
Download zipCompile-once JSON Schema 2020-12 validator for MoonBit: exact decimal arithmetic, precise pattern semantics, no silent schema errors, no hidden I/O.