Crate slices_lexicon

Crate slices_lexicon 

Source
Expand description

§AT Protocol Lexicon Validator

This crate provides validation for AT Protocol Lexicon schema documents and data records. Lexicon is a schema definition language used to describe atproto records, HTTP endpoints (XRPC), and event stream messages.

§Features

  • Schema validation: Validate lexicon documents against the lexicon specification
  • Data validation: Validate data records against their lexicon schemas
  • Cross-reference checking: Ensure all references between lexicons can be resolved
  • Format validation: Support for AT Protocol string formats (DIDs, handles, NSIDs, etc.)
  • WebAssembly support: Optional WASM bindings for browser/JS usage

§Basic Usage

use slices_lexicon::{validate, ValidationError};
use serde_json::json;

let lexicons = vec![
    json!({
        "lexicon": 1,
        "id": "com.example.post",
        "defs": {
            "main": {
                "type": "record",
                "key": "tid",
                "record": {
                    "type": "object",
                    "required": ["text"],
                    "properties": {
                        "text": { "type": "string", "maxLength": 300 }
                    }
                }
            }
        }
    })
];

match validate(lexicons) {
    Ok(_) => println!("All lexicons are valid!"),
    Err(errors) => {
        for (lexicon_id, error_list) in errors {
            println!("Errors in {}: {:?}", lexicon_id, error_list);
        }
    }
}

§Advanced Usage with Validation Context

use slices_lexicon::validation::{ValidationContext, primary::RecordValidator, Validator};
use serde_json::json;

let lexicons = vec![
    json!({
        "lexicon": 1,
        "id": "com.example.post",
        "defs": {
            "main": {
                "type": "record",
                "key": "tid",
                "record": {
                    "type": "object",
                    "required": ["text"],
                    "properties": {
                        "text": { "type": "string" }
                    }
                }
            }
        }
    })
];

let ctx = ValidationContext::builder()
    .with_lexicons(lexicons)?
    .build()?;

let validator = RecordValidator;
let record_def = json!({
    "type": "record",
    "key": "tid",
    "record": {
        "type": "object",
        "required": ["text"],
        "properties": {
            "text": { "type": "string" }
        }
    }
});
validator.validate(&record_def, &ctx)?;

Re-exports§

pub use errors::ValidationError;
pub use types::LexiconDoc;
pub use types::StringFormat;
pub use validation::ValidationContext;
pub use validation::ValidationContextBuilder;
pub use validation::Validator;

Modules§

errors
types
validation
Validation module for Lexicon schemas and data

Functions§

is_valid_nsid
Utility function to check if a string is a valid NSID (Namespaced Identifier)
validate
Validates a set of lexicon documents
validate_record
Validates a single data record against its lexicon schema