graphlite 0.0.1

GraphLite - A lightweight ISO GQL Graph Database
Documentation
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
// Copyright (c) 2024-2025 DeepGraph Inc.
// SPDX-License-Identifier: Apache-2.0
//
// Schema validator implementation

use parking_lot::RwLock;
use serde_json::Value;
use std::collections::HashMap;
use std::sync::Arc;

use crate::catalog::manager::CatalogManager;
use crate::catalog::operations::{CatalogResponse, QueryType};
use crate::schema::types::{Constraint, DataType, GraphTypeDefinition, NodeTypeDefinition};

/// Schema validation errors
#[derive(Debug, thiserror::Error)]
pub enum ValidationError {
    #[error("Unknown label: {0}")]
    UnknownLabel(String),

    #[error("Unknown property '{property}' for label '{label}'")]
    #[allow(dead_code)] // ROADMAP v0.4.0 - Schema validation error for undefined properties
    UnknownProperty { label: String, property: String },

    #[error("Missing required property '{property}' for label '{label}'")]
    MissingRequiredProperty { label: String, property: String },

    #[error("Invalid property type for '{property}': expected {expected}, got {got}")]
    InvalidPropertyType {
        property: String,
        expected: String,
        got: String,
    },

    #[error("Constraint violation: {constraint} - {message}")]
    ConstraintViolation { constraint: String, message: String },

    #[error("No graph type associated with graph '{0}'")]
    NoGraphType(String),

    #[error("Catalog error: {0}")]
    CatalogError(String),

    #[error("Invalid value: {0}")]
    InvalidValue(String),
}

/// Schema validator for validating nodes, edges, and indexes against graph type definitions
#[allow(dead_code)] // ROADMAP v0.4.0 - Schema validation system for graph type enforcement
pub struct SchemaValidator {
    #[allow(dead_code)] // ROADMAP v0.4.0 - Catalog integration for schema definitions
    catalog_manager: Arc<RwLock<CatalogManager>>,
    #[allow(dead_code)] // ROADMAP v0.4.0 - Flexible validation for schema evolution
    allow_unknown_properties: bool,
}

impl SchemaValidator {
    /// Create a new schema validator
    pub fn new(catalog_manager: Arc<RwLock<CatalogManager>>) -> Self {
        Self {
            catalog_manager,
            allow_unknown_properties: false, // Default to strict validation
        }
    }

    /// Create a new schema validator with custom configuration
    #[allow(dead_code)] // ROADMAP v0.4.0 - Configurable validation for different use cases
    pub fn with_config(
        catalog_manager: Arc<RwLock<CatalogManager>>,
        allow_unknown_properties: bool,
    ) -> Self {
        Self {
            catalog_manager,
            allow_unknown_properties,
        }
    }

    /// Validate that a node matches the graph type schema (with graph name)
    #[allow(dead_code)] // ROADMAP v0.4.0 - Node validation for graph type DDL enforcement at INSERT time
    pub fn validate_node(
        &self,
        graph_name: &str,
        label: &str,
        properties: &HashMap<String, Value>,
    ) -> Result<(), ValidationError> {
        // Get graph type for graph
        let graph_type = self.get_graph_type_for_graph(graph_name)?;
        self.validate_node_with_type(&graph_type, label, properties)
    }

    /// Validate that a node matches the graph type schema (with graph type directly)
    pub fn validate_node_with_type(
        &self,
        graph_type: &GraphTypeDefinition,
        label: &str,
        properties: &HashMap<String, Value>,
    ) -> Result<(), ValidationError> {
        // Find node type definition
        let node_type = graph_type
            .node_types
            .iter()
            .find(|nt| nt.label == label)
            .ok_or_else(|| ValidationError::UnknownLabel(label.to_string()))?;

        // Validate required properties
        for prop_def in &node_type.properties {
            if prop_def.required && !properties.contains_key(&prop_def.name) {
                return Err(ValidationError::MissingRequiredProperty {
                    label: label.to_string(),
                    property: prop_def.name.clone(),
                });
            }
        }

        // Validate property types
        for (prop_name, prop_value) in properties {
            if let Some(prop_def) = node_type.properties.iter().find(|p| p.name == *prop_name) {
                self.validate_property_type(prop_value, &prop_def.data_type)?;
            }
        }

        // Validate constraints
        self.validate_node_constraints(node_type)?;

        Ok(())
    }

    /// Validate partial node update (only validates provided properties)
    #[allow(dead_code)] // ROADMAP v0.4.0 - Partial node validation for UPDATE/SET operations with graph type enforcement
    pub fn validate_partial_node(
        &self,
        graph_type: &GraphTypeDefinition,
        label: &str,
        properties: &HashMap<String, Value>,
    ) -> Result<(), ValidationError> {
        // Find node type definition
        let node_type = graph_type
            .node_types
            .iter()
            .find(|nt| nt.label == label)
            .ok_or_else(|| ValidationError::UnknownLabel(label.to_string()))?;

        // Only validate the properties that are being updated
        for (prop_name, prop_value) in properties {
            if let Some(prop_def) = node_type.properties.iter().find(|p| p.name == *prop_name) {
                self.validate_property_type(prop_value, &prop_def.data_type)?;

                // Validate individual property constraints
                for constraint in &prop_def.constraints {
                    self.validate_property_constraint(prop_value, constraint)?;
                }
            } else if !self.allow_unknown_properties {
                return Err(ValidationError::UnknownProperty {
                    label: label.to_string(),
                    property: prop_name.clone(),
                });
            }
        }

        Ok(())
    }

    /// Validate that an edge matches the graph type schema
    #[allow(dead_code)] // ROADMAP v0.4.0 - Edge validation for graph type DDL enforcement (endpoint constraints, edge properties)
    pub fn validate_edge(
        &self,
        graph_type: &GraphTypeDefinition,
        edge_type: &str,
        from_label: &str,
        to_label: &str,
        properties: &HashMap<String, Value>,
    ) -> Result<(), ValidationError> {
        // Find edge type definition
        let edge_def = graph_type
            .edge_types
            .iter()
            .find(|et| et.type_name == edge_type)
            .ok_or_else(|| ValidationError::UnknownLabel(edge_type.to_string()))?;

        // Validate from/to node types
        if !edge_def.from_node_types.contains(&from_label.to_string()) {
            return Err(ValidationError::InvalidValue(format!(
                "Edge type '{}' cannot originate from node type '{}'",
                edge_type, from_label
            )));
        }

        if !edge_def.to_node_types.contains(&to_label.to_string()) {
            return Err(ValidationError::InvalidValue(format!(
                "Edge type '{}' cannot terminate at node type '{}'",
                edge_type, to_label
            )));
        }

        // Validate required properties
        for prop_def in &edge_def.properties {
            if prop_def.required && !properties.contains_key(&prop_def.name) {
                return Err(ValidationError::MissingRequiredProperty {
                    label: edge_type.to_string(),
                    property: prop_def.name.clone(),
                });
            }
        }

        // Validate property types
        for (prop_name, prop_value) in properties {
            if let Some(prop_def) = edge_def.properties.iter().find(|p| p.name == *prop_name) {
                self.validate_property_type(prop_value, &prop_def.data_type)?;
            }
        }

        Ok(())
    }

    /// Validate that an index references valid schema elements
    #[allow(dead_code)] // ROADMAP v0.4.0 - Index schema validation for CREATE INDEX on graph type properties
    pub fn validate_index_schema(
        &self,
        graph_name: &str,
        label: &str,
        properties: &[String],
    ) -> Result<(), ValidationError> {
        let graph_type = self.get_graph_type_for_graph(graph_name)?;

        // Check label exists
        let node_type = graph_type
            .node_types
            .iter()
            .find(|nt| nt.label == label)
            .ok_or_else(|| ValidationError::UnknownLabel(label.to_string()))?;

        // Check properties exist
        for property in properties {
            if !node_type.properties.iter().any(|p| p.name == *property) {
                return Err(ValidationError::UnknownProperty {
                    label: label.to_string(),
                    property: property.clone(),
                });
            }
        }

        Ok(())
    }

    /// Get the graph type definition for a graph
    #[allow(dead_code)] // ROADMAP v0.4.0 - Helper to resolve graph type from graph name for validation
    fn get_graph_type_for_graph(
        &self,
        graph_name: &str,
    ) -> Result<GraphTypeDefinition, ValidationError> {
        // Query catalog to get graph's associated graph type
        let catalog_manager = self.catalog_manager.read();

        // Get graph metadata to find the associated graph type name
        let graph_metadata = catalog_manager
            .query_read_only(
                "graph",
                QueryType::Get,
                serde_json::json!({ "name": graph_name }),
            )
            .map_err(|e| ValidationError::CatalogError(e.to_string()))?;

        // Extract graph type name from metadata
        let graph_type_name = match graph_metadata {
            CatalogResponse::Success { data: Some(data) } => data
                .get("graph_type")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string())
                .ok_or_else(|| ValidationError::NoGraphType(graph_name.to_string()))?,
            CatalogResponse::Query { results } => results
                .get("graph_type")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string())
                .ok_or_else(|| ValidationError::NoGraphType(graph_name.to_string()))?,
            _ => {
                return Err(ValidationError::NoGraphType(graph_name.to_string()));
            }
        };

        // Get graph type definition
        let graph_type_response = catalog_manager
            .query_read_only(
                "graph_type",
                QueryType::GetGraphType,
                serde_json::json!({ "name": graph_type_name }),
            )
            .map_err(|e| ValidationError::CatalogError(e.to_string()))?;

        match graph_type_response {
            CatalogResponse::Success { data: Some(data) } => serde_json::from_value(data)
                .map_err(|e| ValidationError::CatalogError(e.to_string())),
            CatalogResponse::Query { results } => serde_json::from_value(results)
                .map_err(|e| ValidationError::CatalogError(e.to_string())),
            _ => Err(ValidationError::NoGraphType(graph_name.to_string())),
        }
    }

    /// Validate property type
    fn validate_property_type(
        &self,
        value: &Value,
        expected_type: &DataType,
    ) -> Result<(), ValidationError> {
        let valid = match (expected_type, value) {
            (DataType::String | DataType::Text, Value::String(_)) => true,
            (DataType::Integer, Value::Number(n)) => n.is_i64(),
            (DataType::BigInt, Value::Number(n)) => n.is_i64() || n.is_u64(),
            (DataType::Float | DataType::Double, Value::Number(n)) => n.is_f64() || n.is_i64(),
            (DataType::Boolean, Value::Bool(_)) => true,
            (DataType::Json, _) => true, // JSON can hold any value
            (DataType::Array(_), Value::Array(_)) => true, // TODO: Validate element types
            (DataType::UUID, Value::String(s)) => {
                // Basic UUID format validation
                s.len() == 36 && s.chars().filter(|c| *c == '-').count() == 4
            }
            _ => false,
        };

        if !valid {
            return Err(ValidationError::InvalidPropertyType {
                property: "".to_string(), // TODO: Pass property name through
                expected: format!("{:?}", expected_type),
                got: format!("{}", value),
            });
        }

        Ok(())
    }

    /// Validate node constraints
    #[allow(dead_code)] // ROADMAP v0.4.0 - Property constraint validation (UNIQUE, CHECK, RANGE) for graph types
    fn validate_property_constraint(
        &self,
        value: &Value,
        constraint: &Constraint,
    ) -> Result<(), ValidationError> {
        match constraint {
            Constraint::MinLength(min) => {
                if let Value::String(s) = value {
                    if s.len() < *min {
                        return Err(ValidationError::ConstraintViolation {
                            constraint: "MinLength".to_string(),
                            message: format!(
                                "String length {} is less than minimum {}",
                                s.len(),
                                min
                            ),
                        });
                    }
                }
            }
            Constraint::MaxLength(max) => {
                if let Value::String(s) = value {
                    if s.len() > *max {
                        return Err(ValidationError::ConstraintViolation {
                            constraint: "MaxLength".to_string(),
                            message: format!("String length {} exceeds maximum {}", s.len(), max),
                        });
                    }
                }
            }
            Constraint::Pattern(pattern) => {
                if let Value::String(s) = value {
                    let regex = regex::Regex::new(pattern)
                        .map_err(|e| ValidationError::InvalidValue(e.to_string()))?;
                    if !regex.is_match(s) {
                        return Err(ValidationError::ConstraintViolation {
                            constraint: "Pattern".to_string(),
                            message: format!("Value '{}' does not match pattern '{}'", s, pattern),
                        });
                    }
                }
            }
            Constraint::MinValue(min) => {
                if let Value::Number(n) = value {
                    if let Some(num_val) = n.as_f64() {
                        if num_val < *min {
                            return Err(ValidationError::ConstraintViolation {
                                constraint: "MinValue".to_string(),
                                message: format!("Value {} is less than minimum {}", num_val, min),
                            });
                        }
                    }
                }
            }
            Constraint::MaxValue(max) => {
                if let Value::Number(n) = value {
                    if let Some(num_val) = n.as_f64() {
                        if num_val > *max {
                            return Err(ValidationError::ConstraintViolation {
                                constraint: "MaxValue".to_string(),
                                message: format!("Value {} exceeds maximum {}", num_val, max),
                            });
                        }
                    }
                }
            }
            Constraint::Unique => {
                // Unique constraint is handled at storage level
            }
            _ => {
                // Other constraints are handled at different levels
            }
        }
        Ok(())
    }

    fn validate_node_constraints(
        &self,
        node_type: &NodeTypeDefinition,
    ) -> Result<(), ValidationError> {
        for constraint in &node_type.constraints {
            match constraint {
                Constraint::NotNull => {
                    // Already handled in required property validation
                }
                Constraint::Unique => {
                    // TODO: Check uniqueness against existing data
                }
                Constraint::MinLength(_) => {
                    // TODO: Validate string lengths
                }
                Constraint::MaxLength(_) => {
                    // TODO: Validate string lengths
                }
                Constraint::Pattern(_) => {
                    // TODO: Validate against regex pattern
                }
                _ => {
                    // TODO: Implement other constraint validations
                }
            }
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {

    #[test]
    fn test_validate_property_type() {
        // TODO: Add comprehensive tests for property type validation
    }

    #[test]
    fn test_validate_node_constraints() {
        // TODO: Add tests for constraint validation
    }
}