sdc4-validator 0.1.0

High-performance XML Schema validator with Semantic Data Charter (SDC4) support
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
# Developer Guide and Architecture Documentation

This document provides detailed information for developers and AI assistants working on sdcvalidatorRust.

## Project Overview

**sdcvalidatorRust** is a high-performance XML Schema validator implementing the Semantic Data Charter (SDC4) specification. It's designed to provide fast, memory-efficient validation with a unique "quarantine-and-tag" recovery mode that preserves invalid data rather than rejecting it.

### Key Goals

1. **Performance**: Leverage Rust's zero-cost abstractions for optimal speed
2. **Memory Efficiency**: Streaming parsing and validation where possible
3. **SDC4 Compliance**: Full implementation of SDC4 specification
4. **Correctness**: Comprehensive testing and formal verification
5. **Usability**: Intuitive API for both CLI and library usage

## Architecture

### High-Level Architecture

```
┌─────────────────────────────────────────────────────────────┐
│                     CLI Interface                            │
│                    (src/cli/mod.rs)                          │
└─────────────────────┬───────────────────────────────────────┘
┌─────────────────────▼───────────────────────────────────────┐
│                  Public API Layer                            │
│            (src/lib.rs, src/api/mod.rs)                      │
└─────────────────────┬───────────────────────────────────────┘
        ┌─────────────┼─────────────┐
        │             │             │
┌───────▼─────┐  ┌───▼─────┐  ┌───▼──────────┐
│   Parser    │  │ Schema  │  │   Recovery   │
│   Engine    │  │ Engine  │  │    Engine    │
│             │  │         │  │              │
│ src/parser/ │  │src/     │  │src/recovery/ │
│             │  │schema/  │  │              │
└─────────────┘  └─────────┘  └──────────────┘
        │             │             │
        └─────────────┼─────────────┘
        ┌─────────────┴─────────────┐
        │                           │
┌───────▼──────┐          ┌─────────▼────────┐
│    Error     │          │  ExceptionalValue│
│  Classifier  │          │     Injector     │
│              │          │                  │
│src/errors/   │          │src/recovery/ev/  │
└──────────────┘          └──────────────────┘
```

### Core Components

#### 1. Parser Engine (`src/parser/`)

Responsible for parsing XML documents and schemas.

**Modules**:
- `xml.rs` - XML document parsing
- `xsd.rs` - XSD schema parsing
- `streaming.rs` - Streaming parser for large documents
- `types.rs` - XML type definitions

**Key Design Decisions**:
- Use streaming parser for memory efficiency
- Support both DOM-style and SAX-style parsing
- Handle XML namespaces correctly
- Preserve document structure for recovery mode

#### 2. Schema Engine (`src/schema/`)

Validates XML documents against XSD schemas.

**Modules**:
- `validator.rs` - Main validation logic
- `xsd10.rs` - XSD 1.0 validation
- `xsd11.rs` - XSD 1.1 validation (assertions, etc.)
- `datatypes.rs` - Built-in XML Schema datatypes
- `sdc4.rs` - SDC4-specific validation extensions

**Key Design Decisions**:
- Compile schemas once, validate many documents
- Support both XSD 1.0 and 1.1
- Efficient error collection
- Extensible for SDC4 features

#### 3. Recovery Engine (`src/recovery/`)

Implements the "quarantine-and-tag" pattern.

**Modules**:
- `mod.rs` - Recovery orchestration
- `ev.rs` - ExceptionalValue injection
- `classifier.rs` - Error to EV type mapping
- `xml_rewriter.rs` - XML document modification

**Key Design Decisions**:
- Preserve original data structure
- Insert ExceptionalValue elements at appropriate locations
- Map validation errors to ISO 21090 EV types
- Maintain document validity after recovery

#### 4. Error Classifier (`src/errors/`)

Maps validation errors to appropriate ExceptionalValue types.

**Modules**:
- `types.rs` - Error type definitions
- `classifier.rs` - Classification logic
- `mapping.rs` - Error to EV type mapping rules

**Key Design Decisions**:
- Use pattern matching for error classification
- Support custom classification rules
- Provide detailed error information
- Enable error aggregation

### Data Flow

#### Standard Validation

```
XML Document → Parser → Schema Validator → Validation Result
                  ↓           ↓
              XML Tree    Schema Model
```

#### Recovery Mode

```
XML Document → Parser → Schema Validator → Error Classifier
                  ↓           ↓                  ↓
              XML Tree    Schema Model    ExceptionalValue Types
                  ↓                              ↓
                  └──────→ Recovery Engine ──────┘
                        Modified XML Document
```

## Implementation Plan

### Phase 1: Core Infrastructure (Q2 2026)

**Week 1-2: Project Setup**
- Set up Cargo workspace
- Configure CI/CD
- Set up testing framework
- Define core types and traits

**Week 3-4: XML Parser**
- Implement basic XML parsing
- Add namespace support
- Add streaming parser
- Write parser tests

**Week 5-6: Schema Parser**
- Parse XSD schemas
- Build schema model
- Handle includes/imports
- Write schema parser tests

**Week 7-8: Basic Validation**
- Implement element validation
- Implement type validation
- Add error reporting
- Write validation tests

### Phase 2: Feature Completion (Q3 2026)

**Week 1-2: Advanced Validation**
- XSD 1.1 assertions
- Complex type derivation
- Schema component constraints
- Identity constraints

**Week 3-4: Recovery Engine**
- Error classification
- ExceptionalValue injection
- XML rewriting
- Recovery tests

**Week 5-6: SDC4 Features**
- SDC4-specific validation rules
- Custom ExceptionalValue handling
- SDC4 metadata preservation

**Week 7-8: CLI Interface**
- Command-line argument parsing
- File I/O
- Progress reporting
- Error formatting

### Phase 3: Production Ready (Q4 2026)

**Week 1-2: Performance Optimization**
- Profiling and benchmarking
- Optimize hot paths
- Reduce allocations
- Parallel validation

**Week 3-4: Documentation**
- API documentation
- User guides
- Examples
- Architecture docs

**Week 5-6: Testing & QA**
- Comprehensive test suite
- Integration tests
- Fuzzing
- Security audit

**Week 7-8: Release Preparation**
- Package for crates.io
- Release notes
- Migration guide
- Version 1.0 release

## Code Style and Conventions

### Naming Conventions

- **Modules**: `snake_case` (e.g., `xml_parser`)
- **Types**: `PascalCase` (e.g., `ValidationResult`)
- **Functions**: `snake_case` (e.g., `validate_element`)
- **Constants**: `SCREAMING_SNAKE_CASE` (e.g., `MAX_DEPTH`)
- **Lifetimes**: Single lowercase letter (e.g., `'a`, `'input`)

### Module Organization

```rust
// src/validator.rs

//! Module-level documentation
//!
//! Describes the purpose and usage of this module.

use std::collections::HashMap;
use crate::schema::Schema;

// Private constants
const DEFAULT_MAX_DEPTH: usize = 100;

// Public types
pub struct Validator {
    schema: Schema,
    options: ValidationOptions,
}

// Public implementation
impl Validator {
    /// Creates a new validator from a schema file.
    ///
    /// # Arguments
    ///
    /// * `path` - Path to the XSD schema file
    ///
    /// # Errors
    ///
    /// Returns an error if the schema file cannot be read or parsed.
    pub fn from_file(path: &str) -> Result<Self, Error> {
        // Implementation
    }
}

// Private implementation
impl Validator {
    fn validate_internal(&self, xml: &str) -> Result<(), Error> {
        // Implementation
    }
}

// Tests
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_validator_creation() {
        // Test implementation
    }
}
```

### Error Handling

Use custom error types with `thiserror`:

```rust
use thiserror::Error;

#[derive(Error, Debug)]
pub enum ValidatorError {
    #[error("Failed to parse XML: {0}")]
    XmlParseError(String),

    #[error("Schema validation failed: {0}")]
    ValidationError(String),

    #[error("I/O error: {0}")]
    IoError(#[from] std::io::Error),
}

pub type Result<T> = std::result::Result<T, ValidatorError>;
```

### Testing Strategy

```rust
#[cfg(test)]
mod tests {
    use super::*;

    // Unit tests - test individual functions
    #[test]
    fn test_parse_simple_xml() {
        let xml = "<root/>";
        let result = parse_xml(xml);
        assert!(result.is_ok());
    }

    // Integration tests - test components together
    #[test]
    fn test_validation_workflow() {
        let schema = Schema::from_file("test.xsd").unwrap();
        let validator = Validator::new(schema);
        let result = validator.validate("<root/>");
        assert!(result.is_valid());
    }

    // Edge case tests
    #[test]
    fn test_empty_document() {
        let result = parse_xml("");
        assert!(result.is_err());
    }
}
```

## Dependencies

### Core Dependencies

- **quick-xml**: Fast XML parsing
- **roxmltree**: XML tree representation
- **thiserror**: Error handling
- **anyhow**: Error context
- **clap**: CLI argument parsing

### Development Dependencies

- **criterion**: Benchmarking
- **proptest**: Property-based testing
- **pretty_assertions**: Better test output

### Minimizing Dependencies

- Prefer standard library when possible
- Evaluate dependency security and maintenance
- Document why each dependency is needed
- Keep dependency tree shallow

## Testing

### Test Categories

1. **Unit Tests**: Test individual functions and methods
2. **Integration Tests**: Test component interactions
3. **Property Tests**: Test invariants with random inputs
4. **Benchmark Tests**: Measure performance
5. **Example Tests**: Verify examples compile and run

### Test Data

Store test files in `tests/fixtures/`:
```
tests/
├── fixtures/
│   ├── schemas/
│   │   ├── simple.xsd
│   │   ├── complex.xsd
│   │   └── sdc4.xsd
│   └── documents/
│       ├── valid/
│       │   ├── simple.xml
│       │   └── complex.xml
│       └── invalid/
│           ├── type_error.xml
│           └── structure_error.xml
├── integration_tests.rs
└── validation_tests.rs
```

## Performance Considerations

### Optimization Targets

1. **Parsing**: < 100ms for typical documents (< 1MB)
2. **Validation**: < 10ms overhead over parsing
3. **Recovery**: < 50ms for typical error injection
4. **Memory**: < 10x document size

### Profiling

```bash
# CPU profiling
cargo build --release
CARGO_PROFILE_RELEASE_DEBUG=true cargo flamegraph --bin sdcvalidator

# Memory profiling
valgrind --tool=massif target/release/sdcvalidator

# Benchmarking
cargo bench
```

## Security Considerations

### Input Validation

- Validate all external input
- Set resource limits (memory, depth, time)
- Disable dangerous features by default (external entities)
- Sanitize error messages

### Fuzzing

```bash
# Install cargo-fuzz
cargo install cargo-fuzz

# Run fuzzer
cargo fuzz run parser_fuzzer
```

## Release Checklist

- [ ] All tests pass
- [ ] Documentation updated
- [ ] CHANGELOG.md updated
- [ ] Version bumped in Cargo.toml
- [ ] Security audit completed
- [ ] Performance benchmarks run
- [ ] Examples tested
- [ ] Release notes written
- [ ] Git tag created
- [ ] Published to crates.io
- [ ] GitHub release created

## Resources

### Standards

- [XML 1.0 Specification]https://www.w3.org/TR/xml/
- [XML Schema 1.0]https://www.w3.org/TR/xmlschema-1/
- [XML Schema 1.1]https://www.w3.org/TR/xmlschema11-1/
- [ISO 21090]https://www.iso.org/standard/35646.html

### Related Projects

- [sdcvalidator]https://github.com/SemanticDataCharter/sdcvalidator - Python implementation
- [sdcvalidatorJS]https://github.com/SemanticDataCharter/sdcvalidatorJS - TypeScript implementation
- [SDCRM]https://github.com/SemanticDataCharter/SDCRM - Reference model

### Rust Resources

- [The Rust Book]https://doc.rust-lang.org/book/
- [Rust API Guidelines]https://rust-lang.github.io/api-guidelines/
- [Rust Performance Book]https://nnethercote.github.io/perf-book/

## AI Assistant Guidelines

When working on this project:

1. **Follow the architecture**: Maintain separation of concerns
2. **Write tests first**: TDD approach preferred
3. **Document as you go**: Update docs with code changes
4. **Consider performance**: Profile before optimizing
5. **Security first**: Validate inputs, handle errors safely
6. **Keep it simple**: Prefer clarity over cleverness
7. **Ask questions**: Clarify requirements before implementing

### Common Tasks

**Adding a new validation rule**:
1. Add test case in `tests/validation_tests.rs`
2. Implement rule in `src/schema/validator.rs`
3. Update error classifier if needed
4. Document in API docs

**Adding a new ExceptionalValue type**:
1. Add variant to `ExceptionalValueType` enum
2. Update classifier mapping
3. Add injection logic
4. Write tests

**Performance optimization**:
1. Profile current performance
2. Identify bottleneck
3. Implement optimization
4. Benchmark to verify improvement
5. Ensure tests still pass

## Contact

For questions or clarifications:
- Open a GitHub discussion
- Check existing documentation
- Review similar code in Python/TypeScript implementations
- Ask maintainers for guidance

---

This document is a living guide. Update it as the architecture evolves!