1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//! JSON Schema validation module
//!
//! This module provides optional JSON Schema validation for documents.
//! Schemas can be attached to collections and validation modes control
//! how strictly they are enforced.
use jsonschema::validator_for;
use serde_json::Value;
/// Validation mode for schema enforcement
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum SchemaValidationMode {
/// No validation (default)
#[default]
Off,
/// Strict validation - reject any document that doesn't match schema
Strict,
/// Lenient validation - accept document but log warnings for violations
Lenient,
}
/// Schema metadata stored in RocksDB
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct CollectionSchema {
/// Schema name (for future versioning support)
pub name: String,
/// JSON Schema document
pub schema: Value,
/// Validation mode
pub validation_mode: SchemaValidationMode,
}
impl CollectionSchema {
/// Create a new JSON schema
pub fn new(name: String, schema: Value, validation_mode: SchemaValidationMode) -> Self {
Self {
name,
schema,
validation_mode,
}
}
/// Get validation mode
pub fn validation_mode(&self) -> SchemaValidationMode {
self.validation_mode
}
/// Check if validation is enabled
pub fn is_enabled(&self) -> bool {
self.validation_mode != SchemaValidationMode::Off
}
}
/// Compiled schema validator
#[derive(Clone)]
pub struct SchemaValidator {
schema: CollectionSchema,
validator: Option<jsonschema::Validator>,
}
impl SchemaValidator {
/// Create a new schema validator
pub fn new(schema: CollectionSchema) -> Result<Self, SchemaCompilationError> {
let validator = if schema.is_enabled() {
Some(
validator_for(&schema.schema)
.map_err(|e| SchemaCompilationError::InvalidSchema(e.to_string()))?,
)
} else {
None
};
Ok(Self { schema, validator })
}
/// Validate a document against the schema
pub fn validate(&self, document: &Value) -> Result<(), SchemaValidationError> {
if let Some(ref validator) = self.validator {
match self.schema.validation_mode {
SchemaValidationMode::Off => Ok(()),
SchemaValidationMode::Strict => {
let mut violations = Vec::new();
for error in validator.iter_errors(document) {
violations.push(ValidationViolation {
instance_path: error.instance_path().to_string(),
schema_path: error.schema_path().to_string(),
error: error.to_string(),
});
}
if violations.is_empty() {
Ok(())
} else {
Err(SchemaValidationError::SchemaViolations(violations))
}
}
SchemaValidationMode::Lenient => {
for error in validator.iter_errors(document) {
tracing::warn!(
schema = %self.schema.name,
path = %error.instance_path(),
violation = %error,
"Schema validation warning (lenient mode)"
);
}
Ok(())
}
}
} else {
Ok(())
}
}
/// Get the underlying schema
pub fn schema(&self) -> &CollectionSchema {
&self.schema
}
}
/// Schema compilation error (when setting an invalid schema)
#[derive(Debug, thiserror::Error)]
pub enum SchemaCompilationError {
#[error("Invalid JSON Schema: {0}")]
InvalidSchema(String),
}
/// Schema validation error (when a document doesn't match schema)
#[derive(Debug, thiserror::Error, serde::Serialize)]
pub enum SchemaValidationError {
#[error("Schema validation failed with violations")]
SchemaViolations(Vec<ValidationViolation>),
}
/// Individual schema violation detail
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ValidationViolation {
/// JSON Path to the violating instance
pub instance_path: String,
/// JSON Path to the schema keyword that failed
pub schema_path: String,
/// Human-readable error message
pub error: String,
}
/// Schema validation result
#[derive(Debug, Clone)]
pub struct ValidationResult {
/// Whether validation passed
pub is_valid: bool,
/// List of violations (empty if valid)
pub violations: Vec<ValidationViolation>,
}
impl ValidationResult {
/// Create a successful result
pub fn valid() -> Self {
Self {
is_valid: true,
violations: Vec::new(),
}
}
/// Create a failed result
pub fn invalid(violations: Vec<ValidationViolation>) -> Self {
Self {
is_valid: false,
violations,
}
}
}