jsonschema validation
$ moon add mizchi/jsonschemamizchi/jsonschema/
types/ # Schema type definitions
parser/ # JSON Schema parser (opt-in)
validate/ # Validation implementation
codegen/ # Code generation
exports/ # Main exports for JSlet schema = object_schema(
properties=Some({
"name": string_schema(min_length=Some(1)),
"age": integer_schema(minimum=Some(0)),
}),
required=Some(["name"]),
)
let result = validate_schema(schema, { "name": "John", "age": 30 })let raw : Json = {
"type": "object",
"properties": {
"name": { "type": "string" },
"age": { "type": "integer" }
},
"required": ["name"]
}
let validator = build_validator(raw)
let result = validator.validate({ "name": "John", "age": 30 })| Operation | Time |
|---|---|
| String validation | 0.01 µs |
| Object validation (2 props) | 0.08 µs |
| Array validation (10 items) | 0.14 µs |
| Nested object (depth 2) | 0.17 µs |
| Full validation with pre-built schema | 0.14 µs |
| JSON Schema parsing | 1.25 µs |
// Fast: schema is pre-built
let schema = object_schema(properties=Some({ "name": string_schema() }))
let result = validate_schema(schema, input)
// Slower: requires parsing JSON Schema
let validator = build_validator({ "type": "object", ... })let validator = build_validator(schema_json) // Build once
for input in inputs {
validator.validate(input) // Reuse
}