| Crates.io | kotoba-schema |
| lib.rs | kotoba-schema |
| version | 0.1.22 |
| created_at | 2025-09-17 16:02:18.391297+00 |
| updated_at | 2025-09-19 18:45:39.416039+00 |
| description | Graph Schema Definition and Validation for Kotoba |
| homepage | https://github.com/com-junkawasaki/kotoba |
| repository | https://github.com/com-junkawasaki/kotoba |
| max_upload_size | |
| id | 1843539 |
| size | 202,732 |
Graph Schema Definition and Validation for Kotoba - プロセスネットワーク as GTS(DPO)+OpenGraph with Merkle DAG & PG view
kotoba-schema provides a comprehensive schema system for defining and validating graph structures in the Kotoba graph database. It supports:
use kotoba_schema::{GraphSchema, VertexTypeSchema, EdgeTypeSchema, PropertySchema, PropertyType};
use std::collections::HashMap;
// Create a new schema
let mut schema = GraphSchema::new(
"social_graph".to_string(),
"Social Network Graph Schema".to_string(),
"1.0.0".to_string(),
);
// Define User vertex type
let mut user_props = HashMap::new();
user_props.insert(
"name".to_string(),
PropertySchema {
name: "name".to_string(),
property_type: PropertyType::String,
description: Some("User's full name".to_string()),
required: true,
default_value: None,
constraints: vec![PropertyConstraint::MinLength(1)],
},
);
let user_vertex = VertexTypeSchema {
name: "User".to_string(),
description: Some("User account vertex".to_string()),
required_properties: vec!["name".to_string()],
properties: user_props,
inherits: vec![],
constraints: vec![],
};
schema.add_vertex_type(user_vertex);
// Validate the schema
let validation = schema.validate_schema();
assert!(validation.is_valid);
use kotoba_schema::SchemaManager;
use kotoba_storage::StorageManager;
// Create storage backend
let storage_manager = StorageManager::default().await?;
let backend = Arc::new(storage_manager.backend().clone());
// Create schema manager
let mut schema_manager = SchemaManager::new(backend);
// Register schema
schema_manager.register_schema(schema).await?;
// Validate graph data
let graph_data = serde_json::json!({
"vertices": [{
"id": "user1",
"labels": ["User"],
"properties": {
"name": "John Doe"
}
}]
});
let validation = schema_manager.validate_graph_data("social_graph", &graph_data).await?;
assert!(validation.is_valid);
use kotoba_schema::{SchemaOperations, MigrationRule, MigrationRuleType};
// Create migration rules
let migration_rules = HashMap::from([
("rename_username".to_string(), MigrationRule {
rule_type: MigrationRuleType::RenameProperty,
source_path: "username".to_string(),
target_path: "name".to_string(),
transformation: None,
})
]);
// Migrate schema
let result = schema_manager.migrate_schema(
"social_graph_v1",
"social_graph_v2",
migration_rules
).await?;
println!("Migrated {} graphs", result.migrated_count);
let user_vertex = VertexTypeSchema {
name: "User".to_string(),
description: Some("User account".to_string()),
required_properties: vec!["id".to_string(), "name".to_string()],
properties: HashMap::from([
("id".to_string(), PropertySchema {
name: "id".to_string(),
property_type: PropertyType::String,
required: true,
constraints: vec![PropertyConstraint::Pattern(r"^[a-zA-Z0-9_-]+$".to_string())],
..
}),
("name".to_string(), PropertySchema {
name: "name".to_string(),
property_type: PropertyType::String,
required: true,
constraints: vec![PropertyConstraint::MinLength(1), PropertyConstraint::MaxLength(100)],
..
}),
("email".to_string(), PropertySchema {
name: "email".to_string(),
property_type: PropertyType::String,
required: false,
constraints: vec![PropertyConstraint::Pattern(r"^[^@]+@[^@]+\.[^@]+$".to_string())],
..
}),
("age".to_string(), PropertySchema {
name: "age".to_string(),
property_type: PropertyType::Integer,
required: false,
constraints: vec![PropertyConstraint::MinValue(0), PropertyConstraint::MaxValue(150)],
..
}),
]),
inherits: vec![],
constraints: vec![],
};
let friendship_edge = EdgeTypeSchema {
name: "FRIENDS_WITH".to_string(),
description: Some("Friendship relationship".to_string()),
source_types: vec!["User".to_string()],
target_types: vec!["User".to_string()],
required_properties: vec![],
properties: HashMap::from([
("since".to_string(), PropertySchema {
name: "since".to_string(),
property_type: PropertyType::DateTime,
required: false,
..
}),
]),
directed: true,
constraints: vec![],
};
String: Text dataInteger: 64-bit signed integerFloat: 64-bit floating pointBoolean: True/false valuesDateTime: ISO 8601 date/timeJson: Arbitrary JSON dataArray<T>: Array of type TMap: String-keyed map of valuesMinLength(n): Minimum string lengthMaxLength(n): Maximum string lengthMinValue(n): Minimum numeric valueMaxValue(n): Maximum numeric valuePattern(regex): Regular expression patternEnum(values): Allowed values listCustom(name): Custom validation ruleUniqueProperty: Unique property valuesCardinality: Edge multiplicity constraintsPathConstraint: Complex path patternsCustom: Custom schema rulesThis crate integrates with the Kotoba process network as defined in dag.jsonnet:
'schema_validator': {
name: 'schema_validator',
path: 'crates/kotoba-schema/src/validator.rs',
type: 'schema',
description: 'Graph schema validation engine',
dependencies: ['types', 'graph_core'],
provides: ['SchemaValidator', 'ValidationResult'],
status: 'completed',
build_order: 4,
},
kotoba-schema/
├── schema.rs # Core schema definitions
├── validator.rs # Validation engine
├── manager.rs # Schema management
├── registry.rs # Schema registry
├── migration.rs # Schema migration
└── export.rs # JSON Schema export
cargo test -p kotoba-schema
cargo bench -p kotoba-schema
MIT OR Apache-2.0