babbel_yaml 0.1.2

Fast, modular YAML 1.2 parser and emitter with anchors, aliases, and tags
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
//! YAML Validation Example
//!
//! Demonstrates JSON Schema-style validation for YAML documents.

use std::collections::BTreeMap;
use babbel_yaml::validation::error::ValidationError;
use babbel_yaml::*;

/// Helper to parse YAML from a string and get the first document
fn parse_yaml(yaml: &str) -> Node {
    let mut source = BufferSource::new(yaml.as_bytes());
    let parsed = parse(&mut source).unwrap();

    // Extract first document from Documents wrapper
    match parsed {
        Node::Documents(docs) => {
            if let Some(Node::Document(doc)) = docs.get(0) {
                if !doc.is_empty() {
                    doc[0].clone()
                } else {
                    Node::None
                }
            } else {
                Node::None
            }
        }
        _ => parsed,
    }
}

fn main() {
    println!("========================================");
    println!("YAML Validation Examples");
    println!("========================================\n");

    example_1_simple_type_validation();
    example_2_object_with_required_fields();
    example_3_array_validation();
    example_4_nested_object_validation();
    example_5_range_and_length_constraints();
    example_6_pattern_and_enum_validation();
    example_7_custom_validators();
    example_8_real_world_config_validation();
}

/// Example 1: Simple type validation
fn example_1_simple_type_validation() {
    println!("Example 1: Simple Type Validation");
    println!("-----------------------------------");

    let schema = Schema::string();
    let validator = SchemaValidator::new(schema);

    // Valid string
    let message_node = Node::from("Hello, World!");
    match validator.validate(&message_node) {
        Ok(_) => println!("✓ Valid string accepted"),
        Err(e) => println!("✗ Unexpected error: {:?}", e),
    }

    // Invalid: number instead of string
    let count_node = Node::from(42);
    match validator.validate(&count_node) {
        Ok(_) => println!("✗ Should have rejected number"),
        Err(e) => println!("✓ Correctly rejected number: {}", e),
    }

    println!();
}

/// Example 2: Object with required fields
fn example_2_object_with_required_fields() {
    println!("Example 2: Object with Required Fields");
    println!("---------------------------------------");

    // Define schema: { name: string (required), age: integer (optional) }
    let mut properties = BTreeMap::new();
    properties.insert(
        "name".to_string(),
        PropertySchema::new(SchemaType::String)
            .required()
            .with_min_length(2)
            .with_max_length(50),
    );
    properties.insert(
        "age".to_string(),
        PropertySchema::new(SchemaType::Integer)
            .with_minimum(0.0)
            .with_maximum(150.0),
    );

    let schema = Schema::object(properties);
    let validator = SchemaValidator::new(schema);

    // Valid: has required name
    let valid_yaml = r#"
name: Alice
age: 30
"#;
    let valid_doc = parse_yaml(valid_yaml);
    match validator.validate(&valid_doc) {
        Ok(_) => println!("✓ Valid user object accepted"),
        Err(e) => println!("✗ Unexpected error: {:?}", e),
    }

    // Invalid: missing required name
    let invalid_yaml = r#"
age: 25
"#;
    let invalid_doc = parse_yaml(invalid_yaml);
    match validator.validate(&invalid_doc) {
        Ok(_) => println!("✗ Should have rejected missing required field"),
        Err(e) => println!("✓ Correctly rejected: {}", e),
    }

    println!();
}

/// Example 3: Array validation
fn example_3_array_validation() {
    println!("Example 3: Array Validation");
    println!("----------------------------");

    // Array of integers
    let schema = Schema::array(PropertySchema::new(SchemaType::Integer));
    let validator = SchemaValidator::new(schema);

    // Valid array
    let valid_yaml = "numbers: [1, 2, 3, 4, 5]";
    let valid_doc = parse_yaml(valid_yaml);
    match validator.validate(&valid_doc["numbers"]) {
        Ok(_) => println!("✓ Valid integer array accepted"),
        Err(e) => println!("✗ Unexpected error: {:?}", e),
    }

    // Invalid: mixed types
    let invalid_yaml = "mixed: [1, two, 3]";
    let invalid_doc = parse_yaml(invalid_yaml);
    match validator.validate(&invalid_doc["mixed"]) {
        Ok(_) => println!("✗ Should have rejected mixed types"),
        Err(e) => println!("✓ Correctly rejected mixed types: {}", e),
    }

    println!();
}

/// Example 4: Nested object validation
fn example_4_nested_object_validation() {
    println!("Example 4: Nested Object Validation");
    println!("------------------------------------");

    // Define nested user schema
    let mut address_props = BTreeMap::new();
    address_props.insert(
        "street".to_string(),
        PropertySchema::new(SchemaType::String).required(),
    );
    address_props.insert(
        "city".to_string(),
        PropertySchema::new(SchemaType::String).required(),
    );
    address_props.insert(
        "zipcode".to_string(),
        PropertySchema::new(SchemaType::String)
            .with_pattern("-")
            .with_min_length(5),
    );

    let mut user_props = BTreeMap::new();
    user_props.insert(
        "name".to_string(),
        PropertySchema::new(SchemaType::String).required(),
    );
    user_props.insert(
        "address".to_string(),
        PropertySchema::new(SchemaType::Object)
            .with_properties(address_props)
            .required(),
    );

    let schema = Schema::object(user_props);
    let validator = SchemaValidator::new(schema);

    // Valid nested structure
    let valid_yaml = r#"
name: Bob
address:
  street: 123 Main St
  city: Springfield
  zipcode: 12345-6789
"#;
    let valid_doc = parse_yaml(valid_yaml);
    match validator.validate(&valid_doc) {
        Ok(_) => println!("✓ Valid nested object accepted"),
        Err(e) => println!("✗ Unexpected error: {:?}", e),
    }

    // Invalid: missing nested required field
    let invalid_yaml = r#"
name: Charlie
address:
  city: Springfield
"#;
    let invalid_doc = parse_yaml(invalid_yaml);
    match validator.validate(&invalid_doc) {
        Ok(_) => println!("✗ Should have rejected missing nested field"),
        Err(e) => {
            println!("✓ Correctly rejected: {}", e);
        }
    }

    println!();
}

/// Example 5: Range and length constraints
fn example_5_range_and_length_constraints() {
    println!("Example 5: Range and Length Constraints");
    println!("----------------------------------------");

    // Password schema: 8-20 characters
    let mut props = BTreeMap::new();
    props.insert(
        "username".to_string(),
        PropertySchema::new(SchemaType::String)
            .required()
            .with_min_length(3)
            .with_max_length(20),
    );
    props.insert(
        "password".to_string(),
        PropertySchema::new(SchemaType::String)
            .required()
            .with_min_length(8)
            .with_max_length(50),
    );
    props.insert(
        "age".to_string(),
        PropertySchema::new(SchemaType::Integer)
            .with_minimum(18.0)
            .with_maximum(100.0),
    );

    let schema = Schema::object(props);
    let validator = SchemaValidator::new(schema);

    // Valid credentials
    let valid_yaml = r#"
username: alice123
password: supersecret123
age: 25
"#;
    let valid_doc = parse_yaml(valid_yaml);
    match validator.validate(&valid_doc) {
        Ok(_) => println!("✓ Valid credentials accepted"),
        Err(e) => println!("✗ Unexpected error: {:?}", e),
    }

    // Invalid: password too short
    let invalid_yaml = r#"
username: bob
password: short
age: 30
"#;
    let invalid_doc = parse_yaml(invalid_yaml);
    match validator.validate(&invalid_doc) {
        Ok(_) => println!("✗ Should have rejected short password"),
        Err(e) => println!("✓ Correctly rejected: {}", e),
    }

    // Invalid: age out of range
    let invalid_age_yaml = r#"
username: charlie
password: validpassword123
age: 15
"#;
    let invalid_age_doc = parse_yaml(invalid_age_yaml);
    match validator.validate(&invalid_age_doc) {
        Ok(_) => println!("✗ Should have rejected underage"),
        Err(e) => println!("✓ Correctly rejected age: {}", e),
    }

    println!();
}

/// Example 6: Pattern and enum validation
fn example_6_pattern_and_enum_validation() {
    println!("Example 6: Pattern and Enum Validation");
    println!("---------------------------------------");

    let mut props = BTreeMap::new();
    props.insert(
        "email".to_string(),
        PropertySchema::new(SchemaType::String)
            .required()
            .with_pattern("@"), // Simple email check
    );
    props.insert(
        "role".to_string(),
        PropertySchema::new(SchemaType::String)
            .required()
            .with_enum(vec![
                "admin".to_string(),
                "user".to_string(),
                "guest".to_string(),
            ]),
    );

    let schema = Schema::object(props);
    let validator = SchemaValidator::new(schema);

    // Valid
    let valid_yaml = r#"
email: user@example.com
role: admin
"#;
    let valid_doc = parse_yaml(valid_yaml);
    match validator.validate(&valid_doc) {
        Ok(_) => println!("✓ Valid email and role accepted"),
        Err(e) => println!("✗ Unexpected error: {:?}", e),
    }

    // Invalid: bad email
    let invalid_email_yaml = r#"
email: notanemail
role: user
"#;
    let invalid_email_doc = parse_yaml(invalid_email_yaml);
    match validator.validate(&invalid_email_doc) {
        Ok(_) => println!("✗ Should have rejected invalid email"),
        Err(e) => println!("✓ Correctly rejected email: {}", e),
    }

    // Invalid: bad role
    let invalid_role_yaml = r#"
email: test@example.com
role: superadmin
"#;
    let invalid_role_doc = parse_yaml(invalid_role_yaml);
    match validator.validate(&invalid_role_doc) {
        Ok(_) => println!("✗ Should have rejected invalid role"),
        Err(e) => println!("✓ Correctly rejected role: {}", e),
    }

    println!();
}

/// Example 7: Custom validators
fn example_7_custom_validators() {
    println!("Example 7: Custom Validators");
    println!("-----------------------------");

    // Custom validator: port number must be > 1024
    let custom_validator =
        CustomValidator::new("Port must be greater than 1024", |node| match node {
            Node::Number(Numeric::Integer(port)) if *port > 1024 => Ok(()),
            Node::Number(Numeric::Integer(port)) => Err(ValidationError::Custom(format!(
                "Port {} is reserved (must be > 1024)",
                port
            ))
            .into()),
            _ => Err(ValidationError::Custom("Port must be an integer".to_string()).into()),
        });

    // Test valid port
    let valid_port = Node::Number(Numeric::Integer(8080));
    match custom_validator.validate(&valid_port) {
        Ok(_) => println!("✓ Valid port 8080 accepted"),
        Err(e) => println!("✗ Unexpected error: {}", e),
    }

    // Test invalid port
    let invalid_port = Node::Number(Numeric::Integer(80));
    match custom_validator.validate(&invalid_port) {
        Ok(_) => println!("✗ Should have rejected port 80"),
        Err(e) => println!("✓ Correctly rejected: {}", e),
    }

    println!();
}

/// Example 8: Real-world configuration validation
fn example_8_real_world_config_validation() {
    println!("Example 8: Real-World Configuration Validation");
    println!("-----------------------------------------------");

    // Define server config schema
    let mut server_props = BTreeMap::new();
    server_props.insert(
        "host".to_string(),
        PropertySchema::new(SchemaType::String)
            .required()
            .with_description("Server hostname"),
    );
    server_props.insert(
        "port".to_string(),
        PropertySchema::new(SchemaType::Integer)
            .required()
            .with_minimum(1024.0)
            .with_maximum(65535.0),
    );
    server_props.insert(
        "timeout".to_string(),
        PropertySchema::new(SchemaType::Integer)
            .with_minimum(0.0)
            .with_maximum(300.0),
    );

    let mut db_props = BTreeMap::new();
    db_props.insert(
        "driver".to_string(),
        PropertySchema::new(SchemaType::String)
            .required()
            .with_enum(vec![
                "postgres".to_string(),
                "mysql".to_string(),
                "sqlite".to_string(),
            ]),
    );
    db_props.insert(
        "host".to_string(),
        PropertySchema::new(SchemaType::String).required(),
    );
    db_props.insert(
        "port".to_string(),
        PropertySchema::new(SchemaType::Integer).required(),
    );
    db_props.insert(
        "database".to_string(),
        PropertySchema::new(SchemaType::String).required(),
    );

    let mut config_props = BTreeMap::new();
    config_props.insert(
        "server".to_string(),
        PropertySchema::new(SchemaType::Object)
            .with_properties(server_props)
            .required(),
    );
    config_props.insert(
        "database".to_string(),
        PropertySchema::new(SchemaType::Object)
            .with_properties(db_props)
            .required(),
    );
    config_props.insert(
        "features".to_string(),
        PropertySchema::new(SchemaType::Array).with_items(PropertySchema::new(SchemaType::String)),
    );

    let schema = Schema::new(PropertySchema::new(SchemaType::Object).with_properties(config_props))
        .with_title("Application Configuration")
        .with_description("Configuration schema for the application");

    let validator = SchemaValidator::new(schema);

    // Valid configuration
    let valid_config = r#"
server:
  host: localhost
  port: 8080
  timeout: 30
database:
  driver: postgres
  host: db.example.com
  port: 5432
  database: myapp
features:
  - auth
  - logging
  - metrics
"#;
    let valid_doc = parse_yaml(valid_config);
    match validator.validate(&valid_doc) {
        Ok(_) => println!("✓ Valid configuration accepted"),
        Err(e) => println!("✗ Unexpected error: {:?}", e),
    }

    // Invalid configuration: missing required database.driver
    let invalid_config = r#"
server:
  host: localhost
  port: 8080
database:
  host: db.example.com
  port: 5432
  database: myapp
"#;
    let invalid_doc = parse_yaml(invalid_config);
    match validator.validate(&invalid_doc) {
        Ok(_) => println!("✗ Should have rejected incomplete config"),
        Err(e) => {
            println!("✓ Validation error: {}", e);
        }
    }

    // Invalid configuration: bad port number
    let invalid_port_config = r#"
server:
  host: localhost
  port: 99999
database:
  driver: postgres
  host: db.example.com
  port: 5432
  database: myapp
"#;
    let invalid_port_doc = parse_yaml(invalid_port_config);
    match validator.validate(&invalid_port_doc) {
        Ok(_) => println!("✗ Should have rejected invalid port"),
        Err(e) => {
            println!("✓ Correctly rejected invalid port: {}", e);
        }
    }

    println!();
    println!("========================================");
    println!("All validation examples completed!");
    println!("========================================");
}