slices-lexicon#
Rust implementation of AT Protocol lexicon validation.
Overview#
This validation engine can be used in any project that needs AT Protocol lexicon validation. It provides high-performance, spec-compliant validation of AT Protocol lexicon documents and data records. It can also be compiled to WebAssembly for use in JavaScript/TypeScript environments.
Architecture#
This package serves as the core validation engine and is typically consumed by higher-level packages:
@slices/lexicon- TypeScript/Deno package with ergonomic APIs@slices/cli- Deno command-line tool for lexicon/appview managementlexicon-intellisense- VS Code extension for lexicon development
Features#
- High-performance lexicon validation implemented in Rust
- Comprehensive error reporting with detailed context
- Full AT Protocol lexicon specification compliance
- Support for all lexicon types: records, queries, procedures, subscriptions
- Optional WebAssembly compilation for JavaScript/TypeScript environments
- Zero JavaScript runtime dependencies when using WebAssembly
Direct Usage#
Rust Library#
Add to your Cargo.toml:
[dependencies]
slices-lexicon = "0.2"
Basic validation:
use slices_lexicon::{validate, validate_record, is_valid_nsid};
use serde_json::json;
// Define lexicon documents
let lexicons = vec![
json!({
"id": "com.example.post",
"lexicon": 1,
"defs": {
"main": {
"type": "record",
"key": "tid",
"record": {
"type": "object",
"required": ["text"],
"properties": {
"text": { "type": "string", "maxLength": 300 }
}
}
}
}
})
];
// Validate lexicon documents
match validate(lexicons.clone()) {
Ok(()) => println!("All lexicons are valid"),
Err(errors) => {
for (lexicon_id, error_list) in errors {
println!("Errors in {}: {:?}", lexicon_id, error_list);
}
}
}
// Validate a data record against the schema
let record = json!({ "text": "Hello, world!" });
validate_record(lexicons, "com.example.post", record)?;
// Validate NSID format
let is_valid = is_valid_nsid("com.example.post");
println!("NSID valid: {}", is_valid);
WebAssembly#
Build the WASM module:
wasm-pack build --target web --features wasm
Use in JavaScript environments:
import init, { WasmLexiconValidator } from "./pkg/slices_lexicon.js";
await init();
// Validate lexicons
const lexicons = [{
id: "com.example.post",
lexicon: 1,
defs: {
main: {
type: "record",
key: "tid",
record: {
type: "object",
required: ["text"],
properties: {
text: { type: "string", maxLength: 300 },
},
},
},
},
}];
const validator = new WasmLexiconValidator(JSON.stringify(lexicons));
const errorsJson = validator.validate_lexicons();
const errors = JSON.parse(errorsJson);
if (Object.keys(errors).length > 0) {
console.log("Validation errors:", errors);
} else {
console.log("All lexicons valid");
}
validator.free(); // Clean up WASM resources
JavaScript/TypeScript Usage#
If you're using JavaScript or TypeScript, use the higher-level packages instead of consuming this library directly:
- TypeScript/JavaScript: Use
@slices/lexiconfor ergonomic APIs with automatic resource management - VS Code Development: Install the
lexicon-intellisenseextension - CLI Tools: Use the Slices CLI for lexicon management tasks
Supported Types#
- Objects with required/optional fields
- Arrays with min/max length constraints
- Strings with format validation (datetime, URI, DID, handle, etc.)
- Integers with range constraints
- Booleans with const values
- Bytes (base64 encoded)
- Unions (open and closed)
- References (local and cross-lexicon)
- CID links
- Tokens (unit types for type discrimination)
- Unknown types
String Formats#
datetime- RFC3339/ISO8601 datetimeuri- Generic URIat-uri- AT Protocol URIdid- Decentralized Identifierhandle- AT Protocol handleat-identifier- DID or handlensid- Name Spaced Identifiercid- Content Identifierlanguage- BCP47 language tagtid- Timestamp-based Identifierrecord-key- Record key format
License#
MIT