Crate json_structure

Crate json_structure 

Source
Expand description

§JSON Structure

A Rust implementation of the JSON Structure schema validation library.

JSON Structure is a type-oriented schema language for JSON, designed for defining data structures that can be validated and mapped to programming language types.

§Features

  • Schema Validation: Validate JSON Structure schema documents for correctness
  • Instance Validation: Validate JSON instances against JSON Structure schemas
  • Source Location Tracking: Line/column tracking for error messages
  • Extension Support: Support for validation, composition, and other extensions

§Quick Start

use json_structure::{SchemaValidator, InstanceValidator};

// Validate a schema
let schema_json = r#"{
    "$id": "https://example.com/person",
    "name": "Person",
    "type": "object",
    "properties": {
        "name": { "type": "string" },
        "age": { "type": "int32" }
    },
    "required": ["name"]
}"#;

let schema_validator = SchemaValidator::new();
let schema_result = schema_validator.validate(schema_json);
assert!(schema_result.is_valid());

// Validate an instance
let schema: serde_json::Value = serde_json::from_str(schema_json).unwrap();
let instance_json = r#"{"name": "Alice", "age": 30}"#;

let instance_validator = InstanceValidator::new();
let instance_result = instance_validator.validate(instance_json, &schema);
assert!(instance_result.is_valid());

§Supported Types

§Primitive Types

  • string, boolean, null
  • number, integer (alias for int32)
  • int8, int16, int32, int64, int128
  • uint8, uint16, uint32, uint64, uint128
  • float, float8, double, decimal
  • date, time, datetime, duration
  • uuid, uri, binary, jsonpointer

§Compound Types

  • object - JSON object with typed properties
  • array - Homogeneous list
  • set - Unique homogeneous list
  • map - Dictionary with string keys
  • tuple - Fixed-length typed array
  • choice - Discriminated union
  • any - Any JSON value

§Extensions

Extensions can be enabled via the $uses keyword in schemas:

  • JSONStructureValidation - Validation constraints (minLength, pattern, etc.)
  • JSONStructureConditionalComposition - Conditional composition (allOf, oneOf, etc.)
  • JSONStructureImport - Schema imports
  • JSONStructureAlternateNames - Alternate property names
  • JSONStructureUnits - Unit annotations

Structs§

InstanceValidator
Validates JSON instances against JSON Structure schemas.
InstanceValidatorOptions
Options for instance validation.
JsonLocation
Location in the source JSON document.
JsonSourceLocator
Locates positions in JSON source text.
SchemaValidator
Validates JSON Structure schema documents.
SchemaValidatorOptions
Options for schema validation.
ValidationError
A validation error with code, message, path, and location.
ValidationResult
Result of validation containing errors and warnings.

Enums§

InstanceErrorCode
Error codes for instance validation errors.
SchemaErrorCode
Error codes for schema validation errors.
Severity
Severity level for validation messages.

Constants§

COMPOSITION_KEYWORDS
Conditional composition keywords that require JSONStructureConditionalComposition.
COMPOUND_TYPES
Compound types in JSON Structure.
INTEGER_TYPES
Integer types in JSON Structure.
KNOWN_EXTENSIONS
Known extension names.
NUMERIC_TYPES
Numeric types in JSON Structure.
PRIMITIVE_TYPES
Primitive types in JSON Structure.
SCHEMA_KEYWORDS
Core schema keywords.
VALIDATION_EXTENSION_KEYWORDS
Validation extension keywords that require JSONStructureValidation.

Functions§

is_compound_type
Returns true if the given type name is a compound type.
is_integer_type
Returns true if the given type name is an integer type.
is_numeric_type
Returns true if the given type name is a numeric type.
is_primitive_type
Returns true if the given type name is a primitive type.
is_valid_type
Returns true if the given type name is a valid JSON Structure type.