scirs2-core 0.4.2

Core utilities and common functionality for SciRS2 (scirs2-core)
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
//! Comprehensive examples for the data validation system
//!
//! This example demonstrates all the validation features including:
//! - Basic data type validation
//! - Constraint validation (temporal, statistical, shape)
//! - Custom validation rules
//! - Schema composition
//! - Array validation
//! - Performance optimizations

#[cfg(feature = "data_validation")]
use scirs2_core::validation::data::*;
#[cfg(feature = "data_validation")]
use serde_json::Value as JsonValue;
#[cfg(feature = "data_validation")]
use std::time::Duration;

#[cfg(feature = "data_validation")]
#[allow(dead_code)]
fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("=== Comprehensive Data Validation Examples ===\n");

    // Example 1: Basic data type validation
    basic_validation_example()?;

    // Example 2: Temporal constraint validation
    temporal_validation_example()?;

    // Example 3: Statistical constraint validation
    statistical_validation_example()?;

    // Example 4: Shape constraint validation
    shape_validation_example()?;

    // Example 5: Complex schema composition
    complex_schema_example()?;

    // Example 6: Array validation with ndarray
    array_validation_example()?;

    // Example 7: Custom validation rules
    custom_validation_example()?;

    Ok(())
}

#[cfg(feature = "data_validation")]
#[allow(dead_code)]
fn basic_validation_example() -> Result<(), Box<dyn std::error::Error>> {
    println!("1. Basic Data Type Validation");
    println!("-----------------------------");

    let config = ValidationConfig::default();
    let validator = Validator::new(config)?;

    // Create a schema for user data
    let schema = ValidationSchema::new()
        .name("user_profile")
        .require_field("username", DataType::String)
        .require_field("age", DataType::Integer)
        .require_field("email", DataType::String)
        .optional_field("bio", DataType::String)
        .require_field("scores", DataType::Array(Box::new(DataType::Float64)));

    // Valid data

    {
        use serde_json::json;

        let valid_data = json!({
            "username": "john_doe",
            "age": 30,
            "email": "john@example.com",
            "bio": "Software developer",
            "scores": [85.5, 92.0, 78.5, 94.5]
        });

        let result = validator.validate(&valid_data, &schema)?;
        println!(
            "Valid data validation: {}",
            if result.is_valid() {
                "PASSED"
            } else {
                "FAILED"
            }
        );

        // Invalid data (missing required field)
        let invalid_data = json!({
            "username": "jane_doe",
            "email": "jane@example.com"
            // Missing required fields: age, scores
        });

        let result = validator.validate(&invalid_data, &schema)?;
        println!(
            "Invalid data validation: {}",
            if !result.is_valid() {
                "CORRECTLY FAILED"
            } else {
                "INCORRECTLY PASSED"
            }
        );
        if !result.is_valid() {
            for error in result.errors() {
                println!("  - Error: {}", error.message);
            }
        }
    }

    println!();
    Ok(())
}

#[cfg(feature = "data_validation")]
#[allow(dead_code)]
fn temporal_validation_example() -> Result<(), Box<dyn std::error::Error>> {
    println!("2. Temporal Constraint Validation");
    println!("---------------------------------");

    let config = ValidationConfig::default();
    let validator = Validator::new(config)?;

    // Create temporal constraints for time series data
    let time_constraints = TimeConstraints::new()
        .with_min_interval(Duration::from_secs(1))
        .with_max_interval(Duration::from_secs(300)) // 5 minutes
        .require_monotonic()
        .disallow_duplicates();

    let schema = ValidationSchema::new()
        .name("sensor_data")
        .require_field("timestamps", DataType::Array(Box::new(DataType::Float64)))
        .require_field("values", DataType::Array(Box::new(DataType::Float64)))
        .add_constraint("timestamps", Constraint::Temporal(time_constraints));

    {
        use serde_json::json;

        // Valid time series data
        let valid_data = json!({
            "timestamps": [1000, 2000, 3500, 7000, 10000], // Monotonic with valid intervals
            "values": [23.5, 24.1, 23.8, 24.5, 25.0]
        });

        let result = validator.validate(&valid_data, &schema)?;
        println!(
            "Valid temporal data: {}",
            if result.is_valid() {
                "PASSED"
            } else {
                "FAILED"
            }
        );

        // Invalid time series data (non-monotonic)
        let invalid_data = json!({
            "timestamps": [1000, 3000, 2000, 4000, 5000], // 2000 breaks monotonic order
            "values": [23.5, 24.1, 23.8, 24.5, 25.0]
        });

        let result = validator.validate(&invalid_data, &schema)?;
        println!(
            "Non-monotonic timestamps: {}",
            if !result.is_valid() {
                "CORRECTLY FAILED"
            } else {
                "INCORRECTLY PASSED"
            }
        );
    }

    println!();
    Ok(())
}

#[cfg(feature = "data_validation")]
#[allow(dead_code)]
fn statistical_validation_example() -> Result<(), Box<dyn std::error::Error>> {
    println!("3. Statistical Constraint Validation");
    println!("-----------------------------------");

    let config = ValidationConfig::default();
    let validator = Validator::new(config)?;

    // Create statistical constraints for measurement data
    let stats_constraints = StatisticalConstraints::new()
        .with_mean_range(20.0, 30.0)  // Expected temperature range
        .with_std_range(0.5, 5.0)     // Expected variability
        .with_distribution("normal"); // Expected distribution

    let schema = ValidationSchema::new()
        .name("temperature_readings")
        .require_field("measurements", DataType::Array(Box::new(DataType::Float64)))
        .add_constraint("measurements", Constraint::Statistical(stats_constraints));

    {
        use serde_json::json;

        // Valid measurements (mean ~25, std ~2)
        let valid_data = json!({
            "measurements": [23.0, 24.5, 25.0, 26.5, 27.0, 24.0, 25.5, 26.0, 23.5, 25.0]
        });

        let result = validator.validate(&valid_data, &schema)?;
        println!(
            "Valid statistical data: {}",
            if result.is_valid() {
                "PASSED"
            } else {
                "FAILED"
            }
        );

        // Invalid measurements (mean too high)
        let invalid_data = json!({
            "measurements": [35.0, 36.5, 37.0, 38.5, 39.0, 36.0, 37.5, 38.0, 35.5, 37.0]
        });

        let result = validator.validate(&invalid_data, &schema)?;
        println!(
            "Out-of-range mean: {}",
            if !result.is_valid() {
                "CORRECTLY FAILED"
            } else {
                "INCORRECTLY PASSED"
            }
        );
    }

    println!();
    Ok(())
}

#[cfg(feature = "data_validation")]
#[allow(dead_code)]
fn shape_validation_example() -> Result<(), Box<dyn std::error::Error>> {
    println!("4. Shape Constraint Validation");
    println!("------------------------------");

    let config = ValidationConfig::default();
    let validator = Validator::new(config)?;

    // Create shape constraints for matrix data
    let shape_constraints = ShapeConstraints::new()
        .with_dimensions(vec![Some(10), Some(10)])  // Expect 10x10 matrix
        .require_square();

    let schema = ValidationSchema::new()
        .name("correlation_matrix")
        .require_field(
            "matrix",
            DataType::Array(Box::new(DataType::Array(Box::new(DataType::Float64)))),
        )
        .add_constraint("matrix", Constraint::Shape(shape_constraints));

    {
        use serde_json::json;

        // Valid 3x3 matrix (smaller for example)
        let shape_3x3 = ShapeConstraints::new()
            .with_dimensions(vec![Some(3), Some(3)])
            .require_square();

        let schema_3x3 = ValidationSchema::new()
            .name("smallmatrix")
            .require_field(
                "matrix",
                DataType::Array(Box::new(DataType::Array(Box::new(DataType::Float64)))),
            )
            .add_constraint("matrix", Constraint::Shape(shape_3x3));

        let valid_data = json!({
            "matrix": [
                [1.0, 0.5, 0.3],
                [0.5, 1.0, 0.7],
                [0.3, 0.7, 1.0]
            ]
        });

        let result = validator.validate(&valid_data, &schema_3x3)?;
        println!(
            "Valid square matrix: {}",
            if result.is_valid() {
                "PASSED"
            } else {
                "FAILED"
            }
        );

        // Invalid matrix (not square)
        let invalid_data = json!({
            "matrix": [
                [1.0, 0.5, 0.3],
                [0.5, 1.0, 0.7]
                // Only 2x3, not square
            ]
        });

        let result = validator.validate(&invalid_data, &schema_3x3)?;
        println!(
            "Non-square matrix: {}",
            if !result.is_valid() {
                "CORRECTLY FAILED"
            } else {
                "INCORRECTLY PASSED"
            }
        );
    }

    println!();
    Ok(())
}

#[cfg(feature = "data_validation")]
#[allow(dead_code)]
fn complex_schema_example() -> Result<(), Box<dyn std::error::Error>> {
    println!("5. Complex Schema Composition");
    println!("-----------------------------");

    let config = ValidationConfig::default();
    let validator = Validator::new(config)?;

    // Create a complex schema for scientific experiment data
    let time_constraints = TimeConstraints::new()
        .with_min_interval(Duration::from_millis(100))
        .require_monotonic();

    let measurement_constraints = StatisticalConstraints::new()
        .with_mean_range(0.0, 100.0)
        .with_std_range(0.1, 50.0);

    let schema = ValidationSchema::new()
        .name("experiment_data")
        .require_field("experiment_id", DataType::String)
        .require_field("timestamps", DataType::Array(Box::new(DataType::Float64)))
        .require_field("measurements", DataType::Array(Box::new(DataType::Float64)))
        .require_field("metadata", DataType::Object)
        .add_constraint("timestamps", Constraint::Temporal(time_constraints))
        .add_constraint(
            "measurements",
            Constraint::Statistical(measurement_constraints),
        )
        .add_constraint(
            "measurements",
            Constraint::Range {
                min: -100.0,
                max: 200.0,
            },
        );

    {
        use serde_json::json;

        let experiment_data = json!({
            "experiment_id": "EXP-2025-001",
            "timestamps": [0.0, 100.0, 200.0, 300.0, 400.0],
            "measurements": [23.5, 24.1, 25.3, 24.8, 25.5],
            "metadata": {
                "instrument": "Thermometer-X100",
                "location": "Lab A",
                "operator": "Dr. Smith"
            }
        });

        let result = validator.validate(&experiment_data, &schema)?;
        println!(
            "Complex experiment data validation: {}",
            if result.is_valid() {
                "PASSED"
            } else {
                "FAILED"
            }
        );
    }

    println!();
    Ok(())
}

#[cfg(feature = "data_validation")]
#[allow(dead_code)]
fn array_validation_example() -> Result<(), Box<dyn std::error::Error>> {
    println!("6. Array Validation with ndarray");
    println!("--------------------------------");

    use ndarray::{Array1, Array2};

    let config = ValidationConfig::default();
    let validator = Validator::new(config.clone())?;

    // Validate a 1D array
    let data_1d = Array1::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0]);
    let constraints_1d = ArrayValidationConstraints::new()
        .withshape(vec![5])
        .check_numeric_quality();

    let result = validator.validate_ndarray(&data_1d, &constraints_1d, &config)?;
    println!(
        "1D array validation: {}",
        if result.is_valid() {
            "PASSED"
        } else {
            "FAILED"
        }
    );

    // Validate a 2D array
    let data_2d =
        Array2::from_shape_vec((3, 3), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0])?;

    let constraints_2d = ArrayValidationConstraints::new()
        .withshape(vec![3, 3])
        .check_numeric_quality();

    let result = validator.validate_ndarray(&data_2d, &constraints_2d, &config)?;
    println!(
        "2D array validation: {}",
        if result.is_valid() {
            "PASSED"
        } else {
            "FAILED"
        }
    );

    // Test with invalid data (contains NaN)
    let invalid_data = Array1::from_vec(vec![1.0, 2.0, f64::NAN, 4.0, 5.0]);
    let result = validator.validate_ndarray(&invalid_data, &constraints_1d, &config)?;
    println!(
        "Array with NaN: {}",
        if !result.is_valid() {
            "CORRECTLY FAILED"
        } else {
            "INCORRECTLY PASSED"
        }
    );
    if result.is_valid() {
        println!("  - Expected validation to fail for NaN, but it passed");
        println!("  - Note: NaN checking might need to be explicitly enabled in constraints");
    }

    println!();
    Ok(())
}

#[cfg(feature = "data_validation")]
#[allow(dead_code)]
fn custom_validation_example() -> Result<(), Box<dyn std::error::Error>> {
    println!("7. Custom Validation Rules");
    println!("--------------------------");

    // Create a custom validation rule
    struct EmailValidator;

    impl ValidationRule for EmailValidator {
        fn validate(&self, value: &JsonValue, _fieldpath: &str) -> Result<(), String> {
            if let Some(email) = value.as_str() {
                if email.contains('@') && email.contains('.') {
                    Ok(())
                } else {
                    Err("Invalid email format".to_string())
                }
            } else {
                Err("Expected string value for email".to_string())
            }
        }

        fn name(&self) -> &str {
            "email_format"
        }

        fn description(&self) -> &str {
            "Validates email format (must contain @ and .)"
        }
    }

    let config = ValidationConfig::default();
    let mut validator = Validator::new(config)?;

    // Add the custom rule
    validator.add_custom_rule("email_format".to_string(), Box::new(EmailValidator));

    // Create a schema that uses the custom rule
    // Note: Custom rules need to be added through field definition, not constraints
    let mut schema = ValidationSchema::new().name("contact_info");

    // Add the email field with custom validation rule
    let email_field = FieldDefinition::new(DataType::String)
        .required()
        .with_validation_rule("email_format");

    // Add the field to the schema
    schema.fields.insert("email".to_string(), email_field);

    {
        use serde_json::json;

        // Valid email
        let valid_data = json!({
            "email": "user@example.com"
        });

        let result = validator.validate(&valid_data, &schema)?;
        println!(
            "Valid email: {}",
            if result.is_valid() {
                "PASSED"
            } else {
                "FAILED"
            }
        );

        // Invalid email
        let invalid_data = json!({
            "email": "invalid-email"
        });

        let result = validator.validate(&invalid_data, &schema)?;
        println!(
            "Invalid email: {}",
            if !result.is_valid() {
                "CORRECTLY FAILED"
            } else {
                "INCORRECTLY PASSED"
            }
        );
        if result.is_valid() {
            println!("  - Expected validation to fail, but it passed");
        } else {
            for error in result.errors() {
                println!("  - Error: {}", error.message);
            }
        }
    }

    println!();
    Ok(())
}

#[cfg(not(feature = "data_validation"))]
#[allow(dead_code)]
fn main() {
    println!("This example requires the 'data_validation' feature to be enabled.");
    println!("Run with: cargo run --example validation_comprehensive --features data_validation");
}