JSON Schema validation for MoonBit, draft-04 to 2020-12.
moon add moonbitstack/moonschemafn main raise {
let schema = @moonschema.Schema::new(
@json.parse(
(
#|{ "type": "object",
#| "properties": { "port": { "type": "integer", "minimum": 1 } },
#| "required": ["port"] }
),
),
)
println(schema.valid(@json.parse("{\"port\": 8080}")))
for fault in schema.faults(@json.parse("{\"port\": 0}")) {
println(fault.to_string())
}
}true
/port: below the minimum (minimum at /properties/port/minimum)let remote = @json.parse("{ \"$ref\": \"https://example.test/int\" }")
let schema = @moonschema.Schema::new(
remote,
remotes={
"https://example.test/int": @json.parse("{ \"type\": \"integer\" }"),
},
)
println(schema.valid(Json::number(1.0))) // truepub struct Schema {
// private fields
}let schema = @moonschema.Schema::new(
@json.parse("{ \"type\": \"integer\", \"minimum\": 1 }"),
)
for fault in schema.faults(@json.parse("0")) {
println(fault.to_string())
}
// the instance: below the minimum (minimum at /minimum)let schema = @moonschema.Schema::new(
@json.parse("{ \"type\": \"object\", \"required\": [\"id\"] }"),
)
schema.valid(@json.parse("{\"id\": 1}")) // truelet schema = @moonschema.Schema::new(@json.parse("{ \"type\": \"integer\" }"))
schema.valid(@json.parse("7")) // true
schema.valid(@json.parse("\"seven\"")) // falseInstall
Download zipJSON Schema validation for MoonBit, draft-04 to 2020-12.