rust-yaml 0.0.5

A fast, safe YAML 1.2 library for Rust
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
//! Comprehensive integration tests for schema validation

use regex::Regex;
use rust_yaml::{Schema, SchemaRule, SchemaValidator, Value, ValueType, Yaml};
use std::collections::HashMap;

#[test]
fn test_schema_validation_integration_with_yaml_api() {
    let yaml = Yaml::new();

    // Create a comprehensive user schema
    let mut user_properties = HashMap::new();
    user_properties.insert(
        "name".to_string(),
        Schema::with_type(ValueType::String).rule(SchemaRule::Length {
            min: Some(2),
            max: Some(50),
        }),
    );
    user_properties.insert(
        "email".to_string(),
        Schema::with_type(ValueType::String).rule(SchemaRule::Pattern(
            Regex::new(r"^[^@]+@[^@]+\.[^@]+$").unwrap(),
        )),
    );
    user_properties.insert(
        "age".to_string(),
        Schema::with_type(ValueType::Integer).rule(SchemaRule::Range {
            min: Some(18.0),
            max: Some(100.0),
        }),
    );

    let user_schema = Schema::with_type(ValueType::Object)
        .rule(SchemaRule::Properties(user_properties))
        .rule(SchemaRule::Required(vec![
            "name".to_string(),
            "email".to_string(),
        ]))
        .info("User Schema", "Validates user registration data");

    // Test valid YAML input
    let valid_yaml = r#"
name: "Alice Johnson"
email: "alice@example.com"
age: 30
active: true
"#;

    let result = yaml.load_str_with_schema(valid_yaml, &user_schema);
    assert!(result.is_ok(), "Valid user YAML should pass validation");

    let user_value = result.unwrap();
    if let Value::Mapping(map) = user_value {
        assert_eq!(
            map.get(&Value::String("name".to_string())),
            Some(&Value::String("Alice Johnson".to_string()))
        );
        assert_eq!(
            map.get(&Value::String("email".to_string())),
            Some(&Value::String("alice@example.com".to_string()))
        );
        assert_eq!(
            map.get(&Value::String("age".to_string())),
            Some(&Value::Int(30))
        );
    } else {
        panic!("Expected mapping for user data");
    }

    // Test invalid YAML input - missing required field
    let invalid_yaml = r#"
name: "Bob"
age: 25
"#;

    let result = yaml.load_str_with_schema(invalid_yaml, &user_schema);
    assert!(result.is_err(), "Invalid user YAML should fail validation");

    let error_message = result.unwrap_err().to_string();
    assert!(
        error_message.contains("email"),
        "Error should mention missing email field"
    );

    // Test invalid YAML input - invalid email format
    let invalid_email_yaml = r#"
name: "Charlie"
email: "not-an-email"
age: 35
"#;

    let result = yaml.load_str_with_schema(invalid_email_yaml, &user_schema);
    assert!(
        result.is_err(),
        "Invalid email format should fail validation"
    );

    let error_message = result.unwrap_err().to_string();
    assert!(
        error_message.contains("pattern"),
        "Error should mention pattern validation failure"
    );

    // Test invalid YAML input - age out of range
    let invalid_age_yaml = r#"
name: "David"
email: "david@example.com"
age: 15
"#;

    let result = yaml.load_str_with_schema(invalid_age_yaml, &user_schema);
    assert!(result.is_err(), "Age under 18 should fail validation");

    let error_message = result.unwrap_err().to_string();
    assert!(
        error_message.contains("minimum"),
        "Error should mention minimum age validation"
    );
}

#[test]
fn test_multi_document_schema_validation() {
    let yaml = Yaml::new();

    // Schema for simple configuration entries
    let config_schema = Schema::with_type(ValueType::Object)
        .rule(SchemaRule::Properties({
            let mut props = HashMap::new();
            props.insert("name".to_string(), Schema::with_type(ValueType::String));
            props.insert(
                "value".to_string(),
                Schema::new().rule(SchemaRule::AnyOf(vec![
                    Schema::with_type(ValueType::String),
                    Schema::with_type(ValueType::Integer),
                    Schema::with_type(ValueType::Boolean),
                ])),
            );
            props
        }))
        .rule(SchemaRule::Required(vec![
            "name".to_string(),
            "value".to_string(),
        ]));

    // Test multi-document YAML
    let multi_doc_yaml = r#"
name: "debug"
value: true
---
name: "port"
value: 8080
---
name: "host"
value: "localhost"
"#;

    let result = yaml.load_all_str_with_schema(multi_doc_yaml, &config_schema);
    assert!(
        result.is_ok(),
        "All valid config documents should pass validation"
    );

    let docs = result.unwrap();
    assert_eq!(docs.len(), 3, "Should have parsed 3 documents");

    // Test multi-document with one invalid
    let invalid_multi_doc_yaml = r#"
name: "debug"
value: true
---
name: "port"
# Missing required value field
---
name: "host"
value: "localhost"
"#;

    let result = yaml.load_all_str_with_schema(invalid_multi_doc_yaml, &config_schema);
    assert!(result.is_err(), "Invalid document should fail validation");
}

#[test]
fn test_complex_nested_schema_validation() {
    let yaml = Yaml::new();

    // Create nested schema for a web service configuration
    let mut server_props = HashMap::new();
    server_props.insert("host".to_string(), Schema::with_type(ValueType::String));
    server_props.insert(
        "port".to_string(),
        Schema::with_type(ValueType::Integer).rule(SchemaRule::Range {
            min: Some(1.0),
            max: Some(65535.0),
        }),
    );
    server_props.insert("ssl".to_string(), Schema::with_type(ValueType::Boolean));

    let server_schema = Schema::with_type(ValueType::Object)
        .rule(SchemaRule::Properties(server_props))
        .rule(SchemaRule::Required(vec![
            "host".to_string(),
            "port".to_string(),
        ]));

    let mut db_props = HashMap::new();
    db_props.insert(
        "driver".to_string(),
        Schema::with_type(ValueType::String).rule(SchemaRule::Enum(vec![
            Value::String("postgresql".to_string()),
            Value::String("mysql".to_string()),
            Value::String("sqlite".to_string()),
        ])),
    );
    db_props.insert("host".to_string(), Schema::with_type(ValueType::String));
    db_props.insert(
        "port".to_string(),
        Schema::with_type(ValueType::Integer).rule(SchemaRule::Range {
            min: Some(1.0),
            max: Some(65535.0),
        }),
    );

    let db_schema = Schema::with_type(ValueType::Object)
        .rule(SchemaRule::Properties(db_props))
        .rule(SchemaRule::Required(vec![
            "driver".to_string(),
            "host".to_string(),
        ]));

    let mut service_props = HashMap::new();
    service_props.insert("name".to_string(), Schema::with_type(ValueType::String));
    service_props.insert(
        "version".to_string(),
        Schema::with_type(ValueType::String)
            .rule(SchemaRule::Pattern(Regex::new(r"^\d+\.\d+\.\d+$").unwrap())),
    );
    service_props.insert("server".to_string(), server_schema);
    service_props.insert("database".to_string(), db_schema);

    let service_schema = Schema::with_type(ValueType::Object)
        .rule(SchemaRule::Properties(service_props))
        .rule(SchemaRule::Required(vec![
            "name".to_string(),
            "version".to_string(),
        ]));

    // Test valid nested configuration
    let valid_config = r#"
name: "my-web-service"
version: "1.2.3"
server:
  host: "localhost"
  port: 8080
  ssl: true
database:
  driver: "postgresql"
  host: "db.example.com"
  port: 5432
"#;

    let result = yaml.load_str_with_schema(valid_config, &service_schema);
    assert!(
        result.is_ok(),
        "Valid nested configuration should pass validation"
    );

    // Test invalid nested configuration - invalid version format
    let invalid_version_config = r#"
name: "my-web-service"
version: "v1.2.3"
server:
  host: "localhost"
  port: 8080
database:
  driver: "postgresql"
  host: "db.example.com"
"#;

    let result = yaml.load_str_with_schema(invalid_version_config, &service_schema);
    assert!(
        result.is_err(),
        "Invalid version format should fail validation"
    );

    // Test invalid nested configuration - invalid database driver
    let invalid_driver_config = r#"
name: "my-web-service"
version: "1.2.3"
server:
  host: "localhost"
  port: 8080
database:
  driver: "mongodb"
  host: "db.example.com"
"#;

    let result = yaml.load_str_with_schema(invalid_driver_config, &service_schema);
    assert!(
        result.is_err(),
        "Invalid database driver should fail validation"
    );

    // Test invalid nested configuration - port out of range
    let invalid_port_config = r#"
name: "my-web-service"
version: "1.2.3"
server:
  host: "localhost"
  port: 99999
database:
  driver: "postgresql"
  host: "db.example.com"
"#;

    let result = yaml.load_str_with_schema(invalid_port_config, &service_schema);
    assert!(result.is_err(), "Port out of range should fail validation");
}

#[test]
fn test_array_schema_validation() {
    let yaml = Yaml::new();

    // Schema for an array of user objects
    let mut active_schema = Schema::with_type(ValueType::Boolean);
    active_schema.optional = true; // Mark as optional since it's not in required fields

    let user_item_schema = Schema::with_type(ValueType::Object)
        .rule(SchemaRule::Properties({
            let mut props = HashMap::new();
            props.insert("id".to_string(), Schema::with_type(ValueType::Integer));
            props.insert("name".to_string(), Schema::with_type(ValueType::String));
            props.insert("active".to_string(), active_schema);
            props
        }))
        .rule(SchemaRule::Required(vec![
            "id".to_string(),
            "name".to_string(),
        ]));

    let users_schema = Schema::with_type(ValueType::Object)
        .rule(SchemaRule::Properties({
            let mut props = HashMap::new();
            props.insert(
                "users".to_string(),
                Schema::with_type(ValueType::Array)
                    .rule(SchemaRule::Items(Box::new(user_item_schema)))
                    .rule(SchemaRule::Length {
                        min: Some(1),
                        max: Some(100),
                    }),
            );
            props
        }))
        .rule(SchemaRule::Required(vec!["users".to_string()]));

    // Test valid array
    let valid_users_yaml = r#"
users:
  - id: 1
    name: "Alice"
    active: true
  - id: 2
    name: "Bob"
    active: false
  - id: 3
    name: "Charlie"
"#;

    let result = yaml.load_str_with_schema(valid_users_yaml, &users_schema);
    assert!(
        result.is_ok(),
        "Valid users array should pass validation: {:?}",
        result.err()
    );

    // Test invalid array - missing required field in item
    let invalid_users_yaml = r#"
users:
  - id: 1
    name: "Alice"
  - id: 2
    # Missing name field
    active: false
"#;

    let result = yaml.load_str_with_schema(invalid_users_yaml, &users_schema);
    assert!(result.is_err(), "Invalid user item should fail validation");

    // Test empty array (violates min length)
    let empty_users_yaml = r#"
users: []
"#;

    let result = yaml.load_str_with_schema(empty_users_yaml, &users_schema);
    assert!(
        result.is_err(),
        "Empty users array should fail validation due to min length"
    );
}

#[test]
fn test_conditional_validation_logic() {
    let yaml = Yaml::new();

    // Create conditional schema: if type is "premium", then price must be > 100
    let premium_condition = Schema::with_type(ValueType::String)
        .rule(SchemaRule::Enum(vec![Value::String("premium".to_string())]));

    let high_price_schema = Schema::with_type(ValueType::Integer).rule(SchemaRule::Range {
        min: Some(100.0),
        max: None,
    });

    let mut product_props = HashMap::new();
    product_props.insert("type".to_string(), Schema::with_type(ValueType::String));
    product_props.insert("name".to_string(), Schema::with_type(ValueType::String));
    product_props.insert(
        "price".to_string(),
        Schema::with_type(ValueType::Integer).rule(SchemaRule::Conditional {
            if_schema: Box::new(premium_condition),
            then_schema: Some(Box::new(high_price_schema)),
            else_schema: None,
        }),
    );

    let product_schema = Schema::with_type(ValueType::Object)
        .rule(SchemaRule::Properties(product_props))
        .rule(SchemaRule::Required(vec![
            "type".to_string(),
            "name".to_string(),
            "price".to_string(),
        ]));

    // Test with basic product (should pass regardless of price logic implementation)
    let basic_product_yaml = r#"
type: "basic"
name: "Basic Widget"
price: 50
"#;

    let result = yaml.load_str_with_schema(basic_product_yaml, &product_schema);
    // Note: Conditional validation may not be fully implemented, so we check that it parses
    assert!(
        result.is_ok() || result.is_err(),
        "Conditional validation structure should be handled"
    );
}

#[test]
fn test_anyof_allof_oneof_validation() {
    let _yaml = Yaml::new();

    // Test AnyOf: value can be string OR integer
    let anyof_schema = Schema::new().rule(SchemaRule::AnyOf(vec![
        Schema::with_type(ValueType::String),
        Schema::with_type(ValueType::Integer),
    ]));

    let anyof_validator = SchemaValidator::new(anyof_schema);

    // Test string value
    let string_value = Value::String("hello".to_string());
    assert!(anyof_validator.validate(&string_value).is_ok());

    // Test integer value
    let int_value = Value::Int(42);
    assert!(anyof_validator.validate(&int_value).is_ok());

    // Test boolean value (should fail)
    let bool_value = Value::Bool(true);
    assert!(anyof_validator.validate(&bool_value).is_err());

    // Test AllOf: value must be string AND have specific length
    let allof_schema = Schema::new().rule(SchemaRule::AllOf(vec![
        Schema::with_type(ValueType::String),
        Schema::new().rule(SchemaRule::Length {
            min: Some(5),
            max: Some(10),
        }),
    ]));

    let allof_validator = SchemaValidator::new(allof_schema);

    // Test valid string
    let valid_string = Value::String("hello".to_string());
    assert!(allof_validator.validate(&valid_string).is_ok());

    // Test too short string
    let short_string = Value::String("hi".to_string());
    assert!(allof_validator.validate(&short_string).is_err());

    // Test OneOf: exactly one schema must match
    let oneof_schema = Schema::new().rule(SchemaRule::OneOf(vec![
        Schema::with_type(ValueType::String).rule(SchemaRule::Length {
            min: None,
            max: Some(5),
        }),
        Schema::with_type(ValueType::String).rule(SchemaRule::Length {
            min: Some(10),
            max: None,
        }),
    ]));

    let oneof_validator = SchemaValidator::new(oneof_schema);

    // Test short string (matches first schema only)
    let short_str = Value::String("hi".to_string());
    assert!(oneof_validator.validate(&short_str).is_ok());

    // Test long string (matches second schema only)
    let long_str = Value::String("this is a very long string".to_string());
    assert!(oneof_validator.validate(&long_str).is_ok());

    // Test medium string (matches neither schema)
    let medium_str = Value::String("medium".to_string());
    assert!(oneof_validator.validate(&medium_str).is_err());
}

#[test]
fn test_error_reporting_detail() {
    let yaml = Yaml::new();

    // Create schema with multiple validation rules that will fail
    let mut user_props = HashMap::new();
    user_props.insert(
        "name".to_string(),
        Schema::with_type(ValueType::String).rule(SchemaRule::Length {
            min: Some(2),
            max: Some(20),
        }),
    );
    user_props.insert(
        "email".to_string(),
        Schema::with_type(ValueType::String).rule(SchemaRule::Pattern(
            Regex::new(r"^[^@]+@[^@]+\.[^@]+$").unwrap(),
        )),
    );
    user_props.insert(
        "age".to_string(),
        Schema::with_type(ValueType::Integer).rule(SchemaRule::Range {
            min: Some(18.0),
            max: Some(100.0),
        }),
    );

    let user_schema = Schema::with_type(ValueType::Object)
        .rule(SchemaRule::Properties(user_props))
        .rule(SchemaRule::Required(vec![
            "name".to_string(),
            "email".to_string(),
            "age".to_string(),
        ]));

    // Create YAML with multiple validation errors
    let invalid_yaml = r#"
name: "A"
email: "not-an-email"
age: 150
"#;

    let result = yaml.load_str_with_schema(invalid_yaml, &user_schema);
    assert!(
        result.is_err(),
        "Should fail validation with multiple errors"
    );

    let error_message = result.unwrap_err().to_string();

    // Check that error message contains details about each validation failure
    assert!(
        error_message.contains("validation"),
        "Should mention validation failure"
    );
    // Additional checks could be added for specific error details
}

#[test]
fn test_real_world_configuration_schema() {
    let yaml = Yaml::new();

    // Real-world example: Kubernetes-style deployment configuration
    let mut container_props = HashMap::new();
    container_props.insert("name".to_string(), Schema::with_type(ValueType::String));
    container_props.insert("image".to_string(), Schema::with_type(ValueType::String));

    let mut port_schema = Schema::with_type(ValueType::Integer).rule(SchemaRule::Range {
        min: Some(1.0),
        max: Some(65535.0),
    });
    port_schema.optional = true; // Port is optional
    container_props.insert("port".to_string(), port_schema);

    let container_schema = Schema::with_type(ValueType::Object)
        .rule(SchemaRule::Properties(container_props))
        .rule(SchemaRule::Required(vec![
            "name".to_string(),
            "image".to_string(),
        ]));

    let mut spec_props = HashMap::new();
    let mut replicas_schema = Schema::with_type(ValueType::Integer).rule(SchemaRule::Range {
        min: Some(1.0),
        max: Some(100.0),
    });
    replicas_schema.optional = true; // Replicas is optional (not in required list)
    spec_props.insert("replicas".to_string(), replicas_schema);
    spec_props.insert(
        "containers".to_string(),
        Schema::with_type(ValueType::Array)
            .rule(SchemaRule::Items(Box::new(container_schema)))
            .rule(SchemaRule::Length {
                min: Some(1),
                max: Some(10),
            }),
    );

    let spec_schema = Schema::with_type(ValueType::Object)
        .rule(SchemaRule::Properties(spec_props))
        .rule(SchemaRule::Required(vec!["containers".to_string()]));

    let mut deployment_props = HashMap::new();
    deployment_props.insert(
        "apiVersion".to_string(),
        Schema::with_type(ValueType::String)
            .rule(SchemaRule::Enum(vec![Value::String("apps/v1".to_string())])),
    );
    deployment_props.insert(
        "kind".to_string(),
        Schema::with_type(ValueType::String).rule(SchemaRule::Enum(vec![Value::String(
            "Deployment".to_string(),
        )])),
    );
    deployment_props.insert("spec".to_string(), spec_schema);

    let deployment_schema = Schema::with_type(ValueType::Object)
        .rule(SchemaRule::Properties(deployment_props))
        .rule(SchemaRule::Required(vec![
            "apiVersion".to_string(),
            "kind".to_string(),
            "spec".to_string(),
        ]));

    // Test valid deployment configuration
    let valid_deployment = r#"
apiVersion: "apps/v1"
kind: "Deployment"
spec:
  replicas: 3
  containers:
    - name: "web-server"
      image: "nginx:1.20"
      port: 80
    - name: "sidecar"
      image: "busybox:latest"
"#;

    let result = yaml.load_str_with_schema(valid_deployment, &deployment_schema);
    assert!(
        result.is_ok(),
        "Valid deployment configuration should pass validation: {:?}",
        result.err()
    );

    // Test invalid deployment - wrong apiVersion
    let invalid_deployment = r#"
apiVersion: "v1"
kind: "Deployment"
spec:
  containers:
    - name: "web-server"
      image: "nginx:1.20"
"#;

    let result = yaml.load_str_with_schema(invalid_deployment, &deployment_schema);
    assert!(result.is_err(), "Invalid apiVersion should fail validation");
}