JSON Structure SDK for Rust
A Rust implementation of the JSON Structure schema validation library.
Installation
Add to your Cargo.toml:
[]
= "0.1"
Quick Start
use ;
Features
Schema Validation
The SchemaValidator validates JSON Structure schema documents:
use ;
// With default options
let validator = new;
// With custom options
let options = SchemaValidatorOptions ;
let validator = with_options;
let result = validator.validate;
Instance Validation
The InstanceValidator validates JSON instances against schemas:
use ;
// With default options
let validator = new;
// With extended validation (enables constraint keywords)
let options = InstanceValidatorOptions ;
let validator = with_options;
let result = validator.validate;
Source Location Tracking
Errors include line and column information:
for error in result.errors
Supported Types
Primitive Types
| Type | Description |
|---|---|
string |
UTF-8 string |
boolean |
true or false |
null |
Null value |
number |
Any JSON number |
integer |
Alias for int32 |
int8 - int128 |
Signed integers |
uint8 - uint128 |
Unsigned integers |
float, double, decimal |
Floating-point numbers |
date |
Date (YYYY-MM-DD) |
time |
Time (HH:MM:SS) |
datetime |
RFC 3339 datetime |
duration |
ISO 8601 duration |
uuid |
UUID string |
uri |
URI string |
binary |
Base64-encoded bytes |
jsonpointer |
JSON Pointer |
Compound Types
| Type | Description | Required Keywords |
|---|---|---|
object |
Typed properties | properties |
array |
Homogeneous list | items |
set |
Unique list | items |
map |
String-keyed dictionary | values |
tuple |
Fixed-length array | properties + tuple |
choice |
Discriminated union | choices + selector |
any |
Any value | (none) |
Extensions
Enable extensions using $uses in your schema:
Available Extensions
- JSONStructureValidation: Validation constraints (
minLength,maxLength,pattern,minimum,maximum, etc.) - JSONStructureConditionalComposition: Composition keywords (
allOf,anyOf,oneOf,not,if/then/else) - JSONStructureImport: Schema imports (
$import,$importdefs) - JSONStructureAlternateNames: Alternate property names (
altnames) - JSONStructureUnits: Unit annotations (
unit)
Error Handling
use ;
Using Default Trait
Both validators implement Default:
use ;
let schema_validator = default;
let instance_validator = default;
Error as std::error::Error
ValidationError implements std::error::Error for integration with Rust's error handling:
use ValidationError;
License
MIT License - see LICENSE for details.
Command Line Interface
The SDK includes jstruct, a CLI tool for validating schemas and instances.
Installation
Or build from source:
Commands
Check Schema(s)
Validate one or more JSON Structure schema files:
# Check a single schema
# Check multiple schemas
# Use quiet mode (no output, just exit code)
# Output as JSON
# Output as TAP (Test Anything Protocol)
Validate Instance(s)
Validate JSON instances against a schema:
# Validate instance against schema
# Validate multiple instances
# With extended validation (constraint keywords)
# Output as JSON
Exit Codes
| Code | Meaning |
|---|---|
| 0 | All files valid |
| 1 | One or more files invalid |
| 2 | Error (file not found, etc.) |
Output Formats
Text (default):
✓ schema.json: valid
✗ bad-schema.json: invalid
- /$id: Missing required property "$id"
JSON:
TAP:
1..2
ok 1 - schema.json
not ok 2 - bad-schema.json
- /$id: Missing required property "$id"