oxirs-star 0.2.4

RDF-star and SPARQL-star grammar support for quoted triples
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
//! Comprehensive validation framework for RDF-star data
//!
//! This module provides extensive validation capabilities for RDF-star graphs,
//! including syntax validation, semantic validation, SHACL-star constraints,
//! custom rules, and performance validation.

use crate::model::{StarGraph, StarTerm, StarTriple};
use crate::shacl_star::ShaclStarValidator;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use thiserror::Error;
use tracing::{debug, info};

/// Validation errors
#[derive(Error, Debug)]
pub enum ValidationError {
    #[error("Validation failed: {0}")]
    ValidationFailed(String),

    #[error("Invalid configuration: {0}")]
    InvalidConfig(String),

    #[error("Constraint violation: {0}")]
    ConstraintViolation(String),
}

/// Validation level
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum ValidationLevel {
    /// Syntax validation only
    Syntax,
    /// Syntax + basic semantic checks
    Basic,
    /// Full semantic validation
    Semantic,
    /// Complete validation (syntax + semantics + constraints)
    Complete,
    /// Strict validation with all optional checks
    Strict,
}

/// Validation severity
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum ValidationSeverity {
    Info,
    Warning,
    Error,
    Critical,
}

/// Validation issue
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ValidationIssue {
    /// Issue severity
    pub severity: ValidationSeverity,

    /// Issue category
    pub category: ValidationCategory,

    /// Issue message
    pub message: String,

    /// Location (triple index, line number, etc.)
    pub location: Option<String>,

    /// Suggestion for fixing
    pub suggestion: Option<String>,

    /// Related rule/constraint ID
    pub rule_id: Option<String>,
}

/// Validation category
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ValidationCategory {
    Syntax,
    Semantics,
    Performance,
    DataQuality,
    Consistency,
    BestPractices,
    Security,
    Compliance,
}

/// Validation configuration
#[derive(Debug, Clone)]
pub struct ValidationConfig {
    /// Validation level
    pub level: ValidationLevel,

    /// Maximum nesting depth allowed
    pub max_nesting_depth: usize,

    /// Maximum graph size (number of triples)
    pub max_graph_size: Option<usize>,

    /// Enable IRI validation
    pub validate_iris: bool,

    /// Enable literal datatype validation
    pub validate_datatypes: bool,

    /// Enable SHACL-star validation
    pub enable_shacl: bool,

    /// Custom validation rules
    pub custom_rules: Vec<CustomValidationRule>,

    /// Stop on first error
    pub fail_fast: bool,

    /// Maximum issues to collect
    pub max_issues: Option<usize>,
}

impl Default for ValidationConfig {
    fn default() -> Self {
        Self {
            level: ValidationLevel::Complete,
            max_nesting_depth: 10,
            max_graph_size: None,
            validate_iris: true,
            validate_datatypes: true,
            enable_shacl: true,
            custom_rules: Vec::new(),
            fail_fast: false,
            max_issues: Some(100),
        }
    }
}

/// Custom validation rule
#[derive(Debug, Clone)]
pub struct CustomValidationRule {
    /// Rule ID
    pub id: String,

    /// Rule description
    pub description: String,

    /// Validation function
    pub validator: fn(&StarTriple) -> Option<String>,

    /// Severity if violated
    pub severity: ValidationSeverity,
}

/// Validation result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ValidationResult {
    /// Is valid
    pub is_valid: bool,

    /// Validation level used
    pub level: ValidationLevel,

    /// Issues found
    pub issues: Vec<ValidationIssue>,

    /// Statistics
    pub statistics: ValidationStatistics,

    /// Timestamp
    pub validated_at: DateTime<Utc>,

    /// Duration (milliseconds)
    pub duration_ms: u64,
}

impl ValidationResult {
    /// Get issues by severity
    pub fn get_issues_by_severity(&self, severity: ValidationSeverity) -> Vec<&ValidationIssue> {
        self.issues
            .iter()
            .filter(|i| i.severity == severity)
            .collect()
    }

    /// Get issues by category
    pub fn get_issues_by_category(&self, category: ValidationCategory) -> Vec<&ValidationIssue> {
        self.issues
            .iter()
            .filter(|i| i.category == category)
            .collect()
    }

    /// Get critical issues
    pub fn get_critical_issues(&self) -> Vec<&ValidationIssue> {
        self.get_issues_by_severity(ValidationSeverity::Critical)
    }

    /// Get errors
    pub fn get_errors(&self) -> Vec<&ValidationIssue> {
        self.get_issues_by_severity(ValidationSeverity::Error)
    }

    /// Get warnings
    pub fn get_warnings(&self) -> Vec<&ValidationIssue> {
        self.get_issues_by_severity(ValidationSeverity::Warning)
    }

    /// Has critical issues
    pub fn has_critical_issues(&self) -> bool {
        self.issues
            .iter()
            .any(|i| i.severity == ValidationSeverity::Critical)
    }

    /// Has errors
    pub fn has_errors(&self) -> bool {
        self.issues
            .iter()
            .any(|i| i.severity == ValidationSeverity::Error)
    }
}

/// Validation statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ValidationStatistics {
    pub total_triples: usize,
    pub quoted_triples: usize,
    pub max_nesting_depth_found: usize,
    pub unique_subjects: usize,
    pub unique_predicates: usize,
    pub unique_objects: usize,
    pub blank_nodes: usize,
    pub literals: usize,
    pub iris: usize,
}

/// Comprehensive validator
pub struct RdfStarValidator {
    /// Configuration
    config: ValidationConfig,

    /// SHACL validator (if enabled)
    shacl_validator: Option<ShaclStarValidator>,
}

impl RdfStarValidator {
    /// Create a new validator
    pub fn new(config: ValidationConfig) -> Self {
        let shacl_validator = if config.enable_shacl {
            Some(ShaclStarValidator::new())
        } else {
            None
        };

        Self {
            config,
            shacl_validator,
        }
    }

    /// Validate a graph
    pub fn validate(&self, graph: &StarGraph) -> Result<ValidationResult, ValidationError> {
        let start = std::time::Instant::now();
        info!("Starting validation of graph with {} triples", graph.len());

        let mut issues = Vec::new();

        // Level-based validation
        match self.config.level {
            ValidationLevel::Syntax => {
                self.validate_syntax(graph, &mut issues)?;
            }
            ValidationLevel::Basic => {
                self.validate_syntax(graph, &mut issues)?;
                self.validate_basic_semantics(graph, &mut issues)?;
            }
            ValidationLevel::Semantic => {
                self.validate_syntax(graph, &mut issues)?;
                self.validate_basic_semantics(graph, &mut issues)?;
                self.validate_semantics(graph, &mut issues)?;
            }
            ValidationLevel::Complete | ValidationLevel::Strict => {
                self.validate_syntax(graph, &mut issues)?;
                self.validate_basic_semantics(graph, &mut issues)?;
                self.validate_semantics(graph, &mut issues)?;
                self.validate_constraints(graph, &mut issues)?;
                self.validate_performance(graph, &mut issues)?;

                if self.config.level == ValidationLevel::Strict {
                    self.validate_best_practices(graph, &mut issues)?;
                }
            }
        }

        // Custom rules
        self.apply_custom_rules(graph, &mut issues)?;

        // Compute statistics
        let statistics = self.compute_statistics(graph);

        let duration_ms = start.elapsed().as_millis() as u64;

        let is_valid = !issues.iter().any(|i| {
            matches!(
                i.severity,
                ValidationSeverity::Error | ValidationSeverity::Critical
            )
        });

        let result = ValidationResult {
            is_valid,
            level: self.config.level.clone(),
            issues,
            statistics,
            validated_at: Utc::now(),
            duration_ms,
        };

        info!(
            "Validation complete: {} issues found in {}ms",
            result.issues.len(),
            duration_ms
        );

        Ok(result)
    }

    /// Validate syntax
    fn validate_syntax(
        &self,
        graph: &StarGraph,
        issues: &mut Vec<ValidationIssue>,
    ) -> Result<(), ValidationError> {
        debug!("Validating syntax");

        for (idx, triple) in graph.iter().enumerate() {
            // Check nesting depth
            let depth = self.get_nesting_depth(triple);
            if depth >= self.config.max_nesting_depth {
                issues.push(ValidationIssue {
                    severity: ValidationSeverity::Error,
                    category: ValidationCategory::Syntax,
                    message: format!(
                        "Nesting depth {} exceeds or equals maximum {}",
                        depth, self.config.max_nesting_depth
                    ),
                    location: Some(format!("triple {}", idx)),
                    suggestion: Some(
                        "Reduce nesting depth or increase max_nesting_depth".to_string(),
                    ),
                    rule_id: Some("MAX_NESTING_DEPTH".to_string()),
                });

                if self.config.fail_fast {
                    break;
                }
            }

            // Validate IRIs if enabled
            if self.config.validate_iris {
                self.validate_term_iris(&triple.subject, "subject", idx, issues);
                self.validate_term_iris(&triple.predicate, "predicate", idx, issues);
                self.validate_term_iris(&triple.object, "object", idx, issues);
            }

            if let Some(max_issues) = self.config.max_issues {
                if issues.len() >= max_issues {
                    break;
                }
            }
        }

        Ok(())
    }

    /// Validate basic semantics
    fn validate_basic_semantics(
        &self,
        graph: &StarGraph,
        issues: &mut Vec<ValidationIssue>,
    ) -> Result<(), ValidationError> {
        debug!("Validating basic semantics");

        // Check graph size
        if let Some(max_size) = self.config.max_graph_size {
            if graph.len() > max_size {
                issues.push(ValidationIssue {
                    severity: ValidationSeverity::Warning,
                    category: ValidationCategory::Performance,
                    message: format!(
                        "Graph size {} exceeds recommended maximum {}",
                        graph.len(),
                        max_size
                    ),
                    location: None,
                    suggestion: Some(
                        "Consider splitting the graph or increasing max_graph_size".to_string(),
                    ),
                    rule_id: Some("MAX_GRAPH_SIZE".to_string()),
                });
            }
        }

        Ok(())
    }

    /// Validate semantics
    fn validate_semantics(
        &self,
        _graph: &StarGraph,
        _issues: &mut Vec<ValidationIssue>,
    ) -> Result<(), ValidationError> {
        debug!("Validating semantics");

        // Check for common semantic issues
        for (idx, triple) in _graph.iter().enumerate() {
            // Predicate should be a NamedNode
            if !matches!(triple.predicate, StarTerm::NamedNode(_)) {
                _issues.push(ValidationIssue {
                    severity: ValidationSeverity::Error,
                    category: ValidationCategory::Semantics,
                    message: "Predicate must be an IRI".to_string(),
                    location: Some(format!("triple {}", idx)),
                    suggestion: Some("Use an IRI for the predicate".to_string()),
                    rule_id: Some("PREDICATE_IRI".to_string()),
                });
            }
        }

        Ok(())
    }

    /// Validate constraints (SHACL-star)
    fn validate_constraints(
        &self,
        _graph: &StarGraph,
        _issues: &mut Vec<ValidationIssue>,
    ) -> Result<(), ValidationError> {
        debug!("Validating constraints");

        if let Some(ref _validator) = self.shacl_validator {
            // SHACL validation would be integrated here
            debug!("SHACL-star validation enabled");
        }

        Ok(())
    }

    /// Validate performance characteristics
    fn validate_performance(
        &self,
        graph: &StarGraph,
        issues: &mut Vec<ValidationIssue>,
    ) -> Result<(), ValidationError> {
        debug!("Validating performance characteristics");

        // Check for performance anti-patterns
        let blank_node_count = graph
            .iter()
            .filter(|t| matches!(t.subject, StarTerm::BlankNode(_)))
            .count();

        if blank_node_count > graph.len() / 2 {
            issues.push(ValidationIssue {
                severity: ValidationSeverity::Warning,
                category: ValidationCategory::Performance,
                message: format!(
                    "High blank node usage ({}/{} triples) may impact performance",
                    blank_node_count,
                    graph.len()
                ),
                location: None,
                suggestion: Some(
                    "Consider using IRIs instead of blank nodes where possible".to_string(),
                ),
                rule_id: Some("HIGH_BLANK_NODE_USAGE".to_string()),
            });
        }

        Ok(())
    }

    /// Validate best practices
    fn validate_best_practices(
        &self,
        graph: &StarGraph,
        issues: &mut Vec<ValidationIssue>,
    ) -> Result<(), ValidationError> {
        debug!("Validating best practices");

        // Check for common best practice violations
        let mut predicate_usage: HashMap<String, usize> = HashMap::new();

        for triple in graph.iter() {
            if let StarTerm::NamedNode(nn) = &triple.predicate {
                *predicate_usage.entry(nn.iri.clone()).or_insert(0) += 1;
            }
        }

        // Check for very low predicate reuse
        let single_use_predicates: Vec<_> = predicate_usage
            .iter()
            .filter(|(_, &count)| count == 1)
            .collect();

        if single_use_predicates.len() > predicate_usage.len() / 2 {
            issues.push(ValidationIssue {
                severity: ValidationSeverity::Info,
                category: ValidationCategory::BestPractices,
                message: format!(
                    "Many predicates ({}/{}) are used only once",
                    single_use_predicates.len(),
                    predicate_usage.len()
                ),
                location: None,
                suggestion: Some("Consider reusing predicates where appropriate".to_string()),
                rule_id: Some("LOW_PREDICATE_REUSE".to_string()),
            });
        }

        Ok(())
    }

    /// Apply custom validation rules
    fn apply_custom_rules(
        &self,
        graph: &StarGraph,
        issues: &mut Vec<ValidationIssue>,
    ) -> Result<(), ValidationError> {
        if self.config.custom_rules.is_empty() {
            return Ok(());
        }

        debug!("Applying {} custom rules", self.config.custom_rules.len());

        for (idx, triple) in graph.iter().enumerate() {
            for rule in &self.config.custom_rules {
                if let Some(violation_msg) = (rule.validator)(triple) {
                    issues.push(ValidationIssue {
                        severity: rule.severity.clone(),
                        category: ValidationCategory::Compliance,
                        message: violation_msg,
                        location: Some(format!("triple {}", idx)),
                        suggestion: None,
                        rule_id: Some(rule.id.clone()),
                    });

                    if self.config.fail_fast {
                        return Ok(());
                    }
                }
            }
        }

        Ok(())
    }

    /// Compute validation statistics
    fn compute_statistics(&self, graph: &StarGraph) -> ValidationStatistics {
        let mut unique_subjects = HashSet::new();
        let mut unique_predicates = HashSet::new();
        let mut unique_objects = HashSet::new();
        let mut blank_nodes = 0;
        let mut literals = 0;
        let mut iris = 0;
        let mut quoted_triples = 0;
        let mut max_depth = 0;

        for triple in graph.iter() {
            // Count term types
            match &triple.subject {
                StarTerm::NamedNode(nn) => {
                    unique_subjects.insert(nn.iri.clone());
                    iris += 1;
                }
                StarTerm::BlankNode(bn) => {
                    unique_subjects.insert(format!("_:{}", bn.id));
                    blank_nodes += 1;
                }
                StarTerm::QuotedTriple(_) => {
                    quoted_triples += 1;
                }
                _ => {}
            }

            if let StarTerm::NamedNode(nn) = &triple.predicate {
                unique_predicates.insert(nn.iri.clone());
                iris += 1;
            }

            match &triple.object {
                StarTerm::NamedNode(nn) => {
                    unique_objects.insert(nn.iri.clone());
                    iris += 1;
                }
                StarTerm::Literal(lit) => {
                    unique_objects.insert(lit.value.clone());
                    literals += 1;
                }
                StarTerm::BlankNode(bn) => {
                    unique_objects.insert(format!("_:{}", bn.id));
                    blank_nodes += 1;
                }
                StarTerm::QuotedTriple(_) => {
                    quoted_triples += 1;
                }
                _ => {}
            }

            let depth = self.get_nesting_depth(triple);
            if depth > max_depth {
                max_depth = depth;
            }
        }

        ValidationStatistics {
            total_triples: graph.len(),
            quoted_triples,
            max_nesting_depth_found: max_depth,
            unique_subjects: unique_subjects.len(),
            unique_predicates: unique_predicates.len(),
            unique_objects: unique_objects.len(),
            blank_nodes,
            literals,
            iris,
        }
    }

    /// Get nesting depth of a triple
    fn get_nesting_depth(&self, triple: &StarTriple) -> usize {
        let subject_depth = self.get_term_depth(&triple.subject);
        let object_depth = self.get_term_depth(&triple.object);
        subject_depth.max(object_depth)
    }

    /// Get nesting depth of a term
    fn get_term_depth(&self, term: &StarTerm) -> usize {
        match term {
            StarTerm::QuotedTriple(qt) => 1 + self.get_nesting_depth(qt),
            _ => 0,
        }
    }

    /// Validate IRIs in a term
    fn validate_term_iris(
        &self,
        term: &StarTerm,
        position: &str,
        triple_idx: usize,
        issues: &mut Vec<ValidationIssue>,
    ) {
        if let StarTerm::NamedNode(nn) = term {
            if !self.is_valid_iri(&nn.iri) {
                issues.push(ValidationIssue {
                    severity: ValidationSeverity::Error,
                    category: ValidationCategory::Syntax,
                    message: format!("Invalid IRI in {}: {}", position, nn.iri),
                    location: Some(format!("triple {}", triple_idx)),
                    suggestion: Some("Ensure IRI is properly formatted".to_string()),
                    rule_id: Some("INVALID_IRI".to_string()),
                });
            }
        }

        // Recurse into quoted triples
        if let StarTerm::QuotedTriple(qt) = term {
            self.validate_term_iris(
                &qt.subject,
                &format!("{}->subject", position),
                triple_idx,
                issues,
            );
            self.validate_term_iris(
                &qt.predicate,
                &format!("{}->predicate", position),
                triple_idx,
                issues,
            );
            self.validate_term_iris(
                &qt.object,
                &format!("{}->object", position),
                triple_idx,
                issues,
            );
        }
    }

    /// Check if IRI is valid (basic check)
    fn is_valid_iri(&self, iri: &str) -> bool {
        // Basic IRI validation
        iri.starts_with("http://") || iri.starts_with("https://") || iri.starts_with("urn:")
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::StarTerm;

    #[test]
    fn test_validator_creation() {
        let config = ValidationConfig::default();
        let validator = RdfStarValidator::new(config);

        assert!(validator.shacl_validator.is_some());
    }

    #[test]
    fn test_basic_validation() -> Result<(), Box<dyn std::error::Error>> {
        let config = ValidationConfig {
            level: ValidationLevel::Basic,
            ..Default::default()
        };

        let validator = RdfStarValidator::new(config);

        let mut graph = StarGraph::new();
        graph.insert(StarTriple::new(
            StarTerm::iri("http://example.org/s")?,
            StarTerm::iri("http://example.org/p")?,
            StarTerm::literal("object")?,
        ))?;

        let result = validator.validate(&graph)?;

        assert!(result.is_valid);
        assert_eq!(result.statistics.total_triples, 1);

        Ok(())
    }

    #[test]
    fn test_nesting_depth_validation() -> Result<(), Box<dyn std::error::Error>> {
        let config = ValidationConfig {
            level: ValidationLevel::Syntax,
            max_nesting_depth: 2,
            ..Default::default()
        };

        let validator = RdfStarValidator::new(config);

        let mut graph = StarGraph::new();

        // Create nested quoted triple (depth 1)
        let inner = StarTriple::new(
            StarTerm::iri("http://example.org/s1")?,
            StarTerm::iri("http://example.org/p1")?,
            StarTerm::literal("obj1")?,
        );

        let middle = StarTriple::new(
            StarTerm::quoted_triple(inner),
            StarTerm::iri("http://example.org/p2")?,
            StarTerm::literal("obj2")?,
        );

        let outer = StarTriple::new(
            StarTerm::quoted_triple(middle),
            StarTerm::iri("http://example.org/p3")?,
            StarTerm::literal("obj3")?,
        );

        graph.insert(outer)?;

        let result = validator.validate(&graph)?;

        // Should have error because depth is 2, which equals max
        assert!(!result.is_valid);
        assert!(result.has_errors());

        Ok(())
    }

    #[test]
    fn test_custom_validation_rule() -> Result<(), Box<dyn std::error::Error>> {
        let mut config = ValidationConfig::default();

        // Add custom rule: predicate must not be "http://example.org/forbidden"
        config.custom_rules.push(CustomValidationRule {
            id: "NO_FORBIDDEN_PREDICATE".to_string(),
            description: "Predicate must not be forbidden".to_string(),
            validator: |triple| {
                if let StarTerm::NamedNode(nn) = &triple.predicate {
                    if nn.iri == "http://example.org/forbidden" {
                        return Some("Forbidden predicate used".to_string());
                    }
                }
                None
            },
            severity: ValidationSeverity::Error,
        });

        let validator = RdfStarValidator::new(config);

        let mut graph = StarGraph::new();
        graph.insert(StarTriple::new(
            StarTerm::iri("http://example.org/s")?,
            StarTerm::iri("http://example.org/forbidden")?,
            StarTerm::literal("obj")?,
        ))?;

        let result = validator.validate(&graph)?;

        assert!(!result.is_valid);
        assert_eq!(result.get_errors().len(), 1);

        Ok(())
    }
}