| Crates.io | slices-lexicon |
| lib.rs | slices-lexicon |
| version | 0.3.1 |
| created_at | 2025-09-18 20:31:32.198823+00 |
| updated_at | 2025-10-30 00:20:24.513696+00 |
| description | AT Protocol lexicon validation library for Slices |
| homepage | |
| repository | https://tangled.org/@slices.network/slices/tree/main/crates/slices-lexicon |
| max_upload_size | |
| id | 1845285 |
| size | 520,194 |
Rust implementation of AT Protocol lexicon validation.
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.
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 developmentAdd 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);
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
If you're using JavaScript or TypeScript, use the higher-level packages instead of consuming this library directly:
@slices/lexicon for ergonomic APIs with
automatic resource managementlexicon-intellisense extensiondatetime - 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 formatMIT