zorath-env 0.3.9

Fast CLI for .env validation against JSON/YAML schemas. 14 types, secret detection, watch mode, remote schemas, 7 export formats, CI templates, health diagnostics, code scanning, auto-fix. Language-agnostic single binary.
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
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::Path;

use thiserror::Error;

use crate::remote::{self, RemoteError, SecurityOptions};

#[derive(Error, Debug)]
pub enum SchemaError {
    #[error("failed to read schema file: {0}")]
    Read(String),
    #[error("invalid schema {0}: {1}")]
    Parse(String, String),
    #[error("circular inheritance detected: {0}")]
    CircularInheritance(String),
    #[error("inheritance depth exceeded (max 10)")]
    InheritanceDepthExceeded,
    #[error("failed to write schema file: {0}")]
    Write(String),
    #[error("remote schema error: {0}")]
    Remote(#[from] RemoteError),
}

/// Supported schema file formats
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum SchemaFormat {
    Json,
    Yaml,
}

impl SchemaFormat {
    /// Detect format from file path extension
    pub fn from_path(path: &str) -> Self {
        let lower = path.to_lowercase();
        if lower.ends_with(".yaml") || lower.ends_with(".yml") {
            SchemaFormat::Yaml
        } else {
            SchemaFormat::Json // Default to JSON for backwards compatibility
        }
    }

    /// Get format name for error messages
    pub fn name(&self) -> &'static str {
        match self {
            SchemaFormat::Json => "JSON",
            SchemaFormat::Yaml => "YAML",
        }
    }
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum VarType {
    #[default]
    String,
    Int,
    Float,
    Bool,
    Url,
    Enum,
    Uuid,
    Email,
    Ipv4,
    Ipv6,
    Semver,
    Port,
    Date,
    Hostname,
}

/// Custom validation rules for environment variables
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ValidationRule {
    /// Minimum value for int type
    #[serde(default)]
    pub min: Option<i64>,

    /// Maximum value for int type
    #[serde(default)]
    pub max: Option<i64>,

    /// Minimum value for float type
    #[serde(default)]
    pub min_value: Option<f64>,

    /// Maximum value for float type
    #[serde(default)]
    pub max_value: Option<f64>,

    /// Minimum length for string type
    #[serde(default)]
    pub min_length: Option<usize>,

    /// Maximum length for string type
    #[serde(default)]
    pub max_length: Option<usize>,

    /// Regex pattern for string type
    #[serde(default)]
    pub pattern: Option<String>,
}

/// Severity level for validation failures
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Severity {
    /// Validation failure causes exit code 1 (default)
    #[default]
    Error,
    /// Validation failure is reported but doesn't cause exit code 1
    Warning,
}

fn is_default_severity(severity: &Severity) -> bool {
    *severity == Severity::Error
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct VarSpec {
    #[serde(rename = "type", default)]
    pub var_type: VarType,

    #[serde(default)]
    pub required: bool,

    #[serde(default)]
    pub description: Option<String>,

    #[serde(default)]
    pub values: Option<Vec<String>>, // for enum

    #[serde(default)]
    pub default: Option<serde_json::Value>,

    /// Custom validation rules
    #[serde(default)]
    pub validate: Option<ValidationRule>,

    /// Secret detection control: false = skip secret check (known safe value)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub secret: Option<bool>,

    /// Severity level: error (default) or warning
    /// Warning-level issues don't cause exit code 1
    #[serde(default, skip_serializing_if = "is_default_severity")]
    pub severity: Severity,
}

pub type Schema = HashMap<String, VarSpec>;

/// Schema file structure that supports inheritance via "extends" field
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct SchemaFile {
    /// Path to parent schema file (relative to current schema)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub extends: Option<String>,

    /// Variable specifications
    #[serde(flatten)]
    pub vars: Schema,
}

/// Schema loading options
#[derive(Debug, Clone, Default)]
pub struct LoadOptions {
    /// Skip cache for remote schemas
    pub no_cache: bool,
    /// Expected SHA-256 hash for remote schema verification
    pub verify_hash: Option<String>,
    /// Custom CA certificate path for enterprise TLS
    pub ca_cert: Option<String>,
    /// Rate limit in seconds between remote fetches (0 to disable)
    pub rate_limit_seconds: Option<u64>,
}

impl LoadOptions {
    /// Convert LoadOptions to SecurityOptions for remote fetching
    pub fn to_security_options(&self) -> SecurityOptions {
        SecurityOptions::new()
            .with_hash(self.verify_hash.clone())
            .with_ca_cert(self.ca_cert.clone())
            .with_rate_limit(self.rate_limit_seconds.unwrap_or(remote::DEFAULT_RATE_LIMIT_SECS))
    }
}

/// Load schema with options (e.g., no_cache for remote schemas)
pub fn load_schema_with_options(path: &str, options: &LoadOptions) -> Result<Schema, SchemaError> {
    load_schema_with_chain(path, &mut Vec::new(), options)
}

/// Parse schema content based on format (JSON or YAML)
fn parse_schema_content(content: &str, format: SchemaFormat) -> Result<SchemaFile, SchemaError> {
    match format {
        SchemaFormat::Json => {
            serde_json::from_str(content)
                .map_err(|e| SchemaError::Parse(format.name().to_string(), e.to_string()))
        }
        SchemaFormat::Yaml => {
            serde_yaml::from_str(content)
                .map_err(|e| SchemaError::Parse(format.name().to_string(), e.to_string()))
        }
    }
}

/// Internal: Load schema with circular reference detection
fn load_schema_with_chain(
    path: &str,
    chain: &mut Vec<String>,
    options: &LoadOptions,
) -> Result<Schema, SchemaError> {
    // Check max depth
    if chain.len() > 10 {
        return Err(SchemaError::InheritanceDepthExceeded);
    }

    // For remote URLs, use the URL as the identifier
    // For local files, resolve to absolute path
    let abs_path = if remote::is_remote_url(path) {
        path.to_string()
    } else {
        fs::canonicalize(path)
            .map(|p| p.to_string_lossy().to_string())
            .unwrap_or_else(|_| path.to_string())
    };

    // Check for circular reference
    if chain.contains(&abs_path) {
        return Err(SchemaError::CircularInheritance(path.to_string()));
    }
    chain.push(abs_path);

    // Read schema content (from file or URL)
    let content = if remote::is_remote_url(path) {
        remote::fetch_remote_schema_secure(path, options.no_cache, &options.to_security_options())?
    } else {
        fs::read_to_string(path).map_err(|e| SchemaError::Read(e.to_string()))?
    };

    // Detect format and parse
    let format = SchemaFormat::from_path(path);
    let schema_file: SchemaFile = parse_schema_content(&content, format)?;

    // Start with parent schema if extends is specified
    let mut result = if let Some(ref parent_path) = schema_file.extends {
        // Resolve parent path relative to current schema
        let parent_full_path = if remote::is_remote_url(path) {
            // For remote schemas, resolve relative URLs
            remote::resolve_relative_url(path, parent_path)?
        } else {
            resolve_relative_path(path, parent_path)
        };
        load_schema_with_chain(&parent_full_path, chain, options)?
    } else {
        Schema::new()
    };

    // Merge current schema (child overrides parent)
    for (key, spec) in schema_file.vars {
        result.insert(key, spec);
    }

    Ok(result)
}

/// Resolve a relative path based on the parent file's directory
fn resolve_relative_path(base_path: &str, relative_path: &str) -> String {
    let base = Path::new(base_path);
    if let Some(parent_dir) = base.parent() {
        parent_dir.join(relative_path).to_string_lossy().to_string()
    } else {
        relative_path.to_string()
    }
}

/// Save schema to file (format auto-detected from path extension)
pub fn save_schema(path: &str, schema: &Schema) -> Result<(), SchemaError> {
    let format = SchemaFormat::from_path(path);
    let content = match format {
        SchemaFormat::Json => {
            serde_json::to_string_pretty(schema)
                .map_err(|e| SchemaError::Parse(format.name().to_string(), e.to_string()))?
        }
        SchemaFormat::Yaml => {
            serde_yaml::to_string(schema)
                .map_err(|e| SchemaError::Parse(format.name().to_string(), e.to_string()))?
        }
    };
    fs::write(path, content).map_err(|e| SchemaError::Write(e.to_string()))
}

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

    /// Test helper: load schema with default options
    fn load_schema(path: &str) -> Result<Schema, SchemaError> {
        load_schema_with_options(path, &LoadOptions::default())
    }

    #[test]
    fn test_parse_string_type() {
        let json = r#"{"FOO": {"type": "string", "required": true}}"#;
        let schema: Schema = serde_json::from_str(json).unwrap();
        let spec = schema.get("FOO").unwrap();
        assert!(matches!(spec.var_type, VarType::String));
        assert!(spec.required);
    }

    #[test]
    fn test_parse_int_type() {
        let json = r#"{"PORT": {"type": "int", "required": false, "default": 3000}}"#;
        let schema: Schema = serde_json::from_str(json).unwrap();
        let spec = schema.get("PORT").unwrap();
        assert!(matches!(spec.var_type, VarType::Int));
        assert!(!spec.required);
        assert_eq!(spec.default, Some(serde_json::json!(3000)));
    }

    #[test]
    fn test_parse_float_type() {
        let json = r#"{"RATE": {"type": "float"}}"#;
        let schema: Schema = serde_json::from_str(json).unwrap();
        let spec = schema.get("RATE").unwrap();
        assert!(matches!(spec.var_type, VarType::Float));
    }

    #[test]
    fn test_parse_bool_type() {
        let json = r#"{"DEBUG": {"type": "bool", "default": false}}"#;
        let schema: Schema = serde_json::from_str(json).unwrap();
        let spec = schema.get("DEBUG").unwrap();
        assert!(matches!(spec.var_type, VarType::Bool));
        assert_eq!(spec.default, Some(serde_json::json!(false)));
    }

    #[test]
    fn test_parse_url_type() {
        let json = r#"{"API_URL": {"type": "url", "required": true}}"#;
        let schema: Schema = serde_json::from_str(json).unwrap();
        let spec = schema.get("API_URL").unwrap();
        assert!(matches!(spec.var_type, VarType::Url));
    }

    #[test]
    fn test_parse_enum_type() {
        let json = r#"{"NODE_ENV": {"type": "enum", "values": ["dev", "staging", "prod"]}}"#;
        let schema: Schema = serde_json::from_str(json).unwrap();
        let spec = schema.get("NODE_ENV").unwrap();
        assert!(matches!(spec.var_type, VarType::Enum));
        assert_eq!(spec.values, Some(vec!["dev".to_string(), "staging".to_string(), "prod".to_string()]));
    }

    #[test]
    fn test_parse_description() {
        let json = r#"{"FOO": {"type": "string", "description": "A test variable"}}"#;
        let schema: Schema = serde_json::from_str(json).unwrap();
        let spec = schema.get("FOO").unwrap();
        assert_eq!(spec.description, Some("A test variable".to_string()));
    }

    #[test]
    fn test_parse_multiple_vars() {
        let json = r#"{
            "FOO": {"type": "string"},
            "BAR": {"type": "int"},
            "BAZ": {"type": "bool"}
        }"#;
        let schema: Schema = serde_json::from_str(json).unwrap();
        assert_eq!(schema.len(), 3);
    }

    #[test]
    fn test_invalid_json_error() {
        let json = r#"{"FOO": {"type": "string""#;
        let result: Result<Schema, _> = serde_json::from_str(json);
        assert!(result.is_err());
    }

    #[test]
    fn test_invalid_type_error() {
        let json = r#"{"FOO": {"type": "invalid_type"}}"#;
        let result: Result<Schema, _> = serde_json::from_str(json);
        assert!(result.is_err());
    }

    #[test]
    fn test_required_defaults_to_false() {
        let json = r#"{"FOO": {"type": "string"}}"#;
        let schema: Schema = serde_json::from_str(json).unwrap();
        let spec = schema.get("FOO").unwrap();
        assert!(!spec.required);
    }

    #[test]
    fn test_roundtrip_serialization() {
        let json = r#"{"FOO":{"type":"string","required":true,"description":"Test"}}"#;
        let schema: Schema = serde_json::from_str(json).unwrap();
        let serialized = serde_json::to_string(&schema).unwrap();
        let reparsed: Schema = serde_json::from_str(&serialized).unwrap();
        assert_eq!(schema.len(), reparsed.len());
    }

    // Schema file parsing (with optional extends field)
    #[test]
    fn test_schema_file_without_extends() {
        let json = r#"{"FOO": {"type": "string"}}"#;
        let schema_file: SchemaFile = serde_json::from_str(json).unwrap();
        assert!(schema_file.extends.is_none());
        assert!(schema_file.vars.contains_key("FOO"));
    }

    #[test]
    fn test_schema_file_with_extends() {
        let json = r#"{"extends": "base.schema.json", "FOO": {"type": "string"}}"#;
        let schema_file: SchemaFile = serde_json::from_str(json).unwrap();
        assert_eq!(schema_file.extends, Some("base.schema.json".to_string()));
        assert!(schema_file.vars.contains_key("FOO"));
    }

    #[test]
    fn test_resolve_relative_path() {
        // Test sibling file
        let result = resolve_relative_path("dir/child.json", "base.json");
        assert!(result.ends_with("dir/base.json") || result.ends_with("dir\\base.json"));

        // Test parent directory
        let result = resolve_relative_path("nested/dir/child.json", "../base.json");
        assert!(result.contains("nested") && result.contains("base.json"));
    }

    // Integration tests with actual files
    #[test]
    fn test_load_schema_without_extends() {
        use std::io::Write;
        use tempfile::NamedTempFile;

        let mut file = NamedTempFile::new().unwrap();
        writeln!(file, r#"{{"FOO": {{"type": "string"}}}}"#).unwrap();

        let schema = load_schema(file.path().to_str().unwrap()).unwrap();
        assert!(schema.contains_key("FOO"));
    }

    #[test]
    fn test_load_schema_with_extends() {
        use std::io::Write;
        use tempfile::tempdir;

        let dir = tempdir().unwrap();

        // Create base schema
        let base_path = dir.path().join("base.schema.json");
        let mut base_file = fs::File::create(&base_path).unwrap();
        writeln!(base_file, r#"{{"BASE_VAR": {{"type": "string", "required": true}}}}"#).unwrap();

        // Create child schema that extends base
        let child_path = dir.path().join("child.schema.json");
        let mut child_file = fs::File::create(&child_path).unwrap();
        writeln!(child_file, r#"{{"extends": "base.schema.json", "CHILD_VAR": {{"type": "int"}}}}"#).unwrap();

        let schema = load_schema(child_path.to_str().unwrap()).unwrap();

        // Should have both vars
        assert!(schema.contains_key("BASE_VAR"));
        assert!(schema.contains_key("CHILD_VAR"));
        assert_eq!(schema.len(), 2);
    }

    #[test]
    fn test_load_schema_child_overrides_parent() {
        use std::io::Write;
        use tempfile::tempdir;

        let dir = tempdir().unwrap();

        // Base schema with PORT as string
        let base_path = dir.path().join("base.schema.json");
        let mut base_file = fs::File::create(&base_path).unwrap();
        writeln!(base_file, r#"{{"PORT": {{"type": "string", "description": "base desc"}}}}"#).unwrap();

        // Child schema overrides PORT as int
        let child_path = dir.path().join("child.schema.json");
        let mut child_file = fs::File::create(&child_path).unwrap();
        writeln!(child_file, r#"{{"extends": "base.schema.json", "PORT": {{"type": "int", "description": "child desc"}}}}"#).unwrap();

        let schema = load_schema(child_path.to_str().unwrap()).unwrap();
        let port = schema.get("PORT").unwrap();

        // Child should override
        assert!(matches!(port.var_type, VarType::Int));
        assert_eq!(port.description, Some("child desc".to_string()));
    }

    #[test]
    fn test_load_schema_multi_level_inheritance() {
        use std::io::Write;
        use tempfile::tempdir;

        let dir = tempdir().unwrap();

        // Grandparent
        let gp_path = dir.path().join("grandparent.json");
        let mut gp_file = fs::File::create(&gp_path).unwrap();
        writeln!(gp_file, r#"{{"GP_VAR": {{"type": "string"}}}}"#).unwrap();

        // Parent extends grandparent
        let p_path = dir.path().join("parent.json");
        let mut p_file = fs::File::create(&p_path).unwrap();
        writeln!(p_file, r#"{{"extends": "grandparent.json", "P_VAR": {{"type": "string"}}}}"#).unwrap();

        // Child extends parent
        let c_path = dir.path().join("child.json");
        let mut c_file = fs::File::create(&c_path).unwrap();
        writeln!(c_file, r#"{{"extends": "parent.json", "C_VAR": {{"type": "string"}}}}"#).unwrap();

        let schema = load_schema(c_path.to_str().unwrap()).unwrap();

        // Should have all three vars
        assert!(schema.contains_key("GP_VAR"));
        assert!(schema.contains_key("P_VAR"));
        assert!(schema.contains_key("C_VAR"));
        assert_eq!(schema.len(), 3);
    }

    #[test]
    fn test_load_schema_circular_inheritance_detected() {
        use std::io::Write;
        use tempfile::tempdir;

        let dir = tempdir().unwrap();

        // A extends B
        let a_path = dir.path().join("a.json");
        let mut a_file = fs::File::create(&a_path).unwrap();
        writeln!(a_file, r#"{{"extends": "b.json", "A": {{"type": "string"}}}}"#).unwrap();

        // B extends A (circular!)
        let b_path = dir.path().join("b.json");
        let mut b_file = fs::File::create(&b_path).unwrap();
        writeln!(b_file, r#"{{"extends": "a.json", "B": {{"type": "string"}}}}"#).unwrap();

        let result = load_schema(a_path.to_str().unwrap());
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(matches!(err, SchemaError::CircularInheritance(_)));
    }

    // YAML schema format tests
    #[test]
    fn test_schema_format_detection_json() {
        assert_eq!(SchemaFormat::from_path("schema.json"), SchemaFormat::Json);
        assert_eq!(SchemaFormat::from_path("path/to/schema.JSON"), SchemaFormat::Json);
        assert_eq!(SchemaFormat::from_path("env.schema.json"), SchemaFormat::Json);
    }

    #[test]
    fn test_schema_format_detection_yaml() {
        assert_eq!(SchemaFormat::from_path("schema.yaml"), SchemaFormat::Yaml);
        assert_eq!(SchemaFormat::from_path("schema.yml"), SchemaFormat::Yaml);
        assert_eq!(SchemaFormat::from_path("path/to/schema.YAML"), SchemaFormat::Yaml);
        assert_eq!(SchemaFormat::from_path("env.schema.yml"), SchemaFormat::Yaml);
    }

    #[test]
    fn test_schema_format_detection_default() {
        // Unknown extensions default to JSON
        assert_eq!(SchemaFormat::from_path("schema"), SchemaFormat::Json);
        assert_eq!(SchemaFormat::from_path("schema.txt"), SchemaFormat::Json);
    }

    #[test]
    fn test_parse_yaml_schema() {
        let yaml = r#"
FOO:
  type: string
  required: true
  description: A test variable
BAR:
  type: int
  default: 3000
"#;
        let result = parse_schema_content(yaml, SchemaFormat::Yaml);
        assert!(result.is_ok());
        let schema_file = result.unwrap();
        assert!(schema_file.vars.contains_key("FOO"));
        assert!(schema_file.vars.contains_key("BAR"));
        let foo = schema_file.vars.get("FOO").unwrap();
        assert!(foo.required);
        assert_eq!(foo.description, Some("A test variable".to_string()));
    }

    #[test]
    fn test_parse_yaml_schema_with_extends() {
        let yaml = r#"
extends: base.schema.yaml
PORT:
  type: int
  required: true
"#;
        let result = parse_schema_content(yaml, SchemaFormat::Yaml);
        assert!(result.is_ok());
        let schema_file = result.unwrap();
        assert_eq!(schema_file.extends, Some("base.schema.yaml".to_string()));
        assert!(schema_file.vars.contains_key("PORT"));
    }

    #[test]
    fn test_parse_yaml_schema_with_enum() {
        let yaml = r#"
NODE_ENV:
  type: enum
  values:
    - development
    - staging
    - production
  required: true
"#;
        let result = parse_schema_content(yaml, SchemaFormat::Yaml);
        assert!(result.is_ok());
        let schema_file = result.unwrap();
        let env = schema_file.vars.get("NODE_ENV").unwrap();
        assert!(matches!(env.var_type, VarType::Enum));
        assert_eq!(env.values, Some(vec!["development".to_string(), "staging".to_string(), "production".to_string()]));
    }

    #[test]
    fn test_parse_yaml_invalid_syntax() {
        let yaml = r#"
FOO:
  type: string
  required: [invalid
"#;
        let result = parse_schema_content(yaml, SchemaFormat::Yaml);
        assert!(result.is_err());
    }

    #[test]
    fn test_load_yaml_schema_from_file() {
        use std::io::Write;
        use tempfile::tempdir;

        let dir = tempdir().unwrap();
        let yaml_path = dir.path().join("schema.yaml");
        let mut file = fs::File::create(&yaml_path).unwrap();
        writeln!(file, "API_KEY:\n  type: string\n  required: true").unwrap();

        let schema = load_schema(yaml_path.to_str().unwrap()).unwrap();
        assert!(schema.contains_key("API_KEY"));
        assert!(schema.get("API_KEY").unwrap().required);
    }

    #[test]
    fn test_load_yml_extension() {
        use std::io::Write;
        use tempfile::tempdir;

        let dir = tempdir().unwrap();
        let yml_path = dir.path().join("schema.yml");
        let mut file = fs::File::create(&yml_path).unwrap();
        writeln!(file, "DEBUG:\n  type: bool\n  default: false").unwrap();

        let schema = load_schema(yml_path.to_str().unwrap()).unwrap();
        assert!(schema.contains_key("DEBUG"));
    }

    #[test]
    fn test_yaml_extends_json() {
        use std::io::Write;
        use tempfile::tempdir;

        let dir = tempdir().unwrap();

        // JSON base schema
        let json_path = dir.path().join("base.schema.json");
        let mut json_file = fs::File::create(&json_path).unwrap();
        writeln!(json_file, r#"{{"BASE_VAR": {{"type": "string"}}}}"#).unwrap();

        // YAML child extends JSON
        let yaml_path = dir.path().join("child.schema.yaml");
        let mut yaml_file = fs::File::create(&yaml_path).unwrap();
        writeln!(yaml_file, "extends: base.schema.json\nCHILD_VAR:\n  type: int").unwrap();

        let schema = load_schema(yaml_path.to_str().unwrap()).unwrap();
        assert!(schema.contains_key("BASE_VAR"));
        assert!(schema.contains_key("CHILD_VAR"));
    }

    #[test]
    fn test_json_extends_yaml() {
        use std::io::Write;
        use tempfile::tempdir;

        let dir = tempdir().unwrap();

        // YAML base schema
        let yaml_path = dir.path().join("base.schema.yaml");
        let mut yaml_file = fs::File::create(&yaml_path).unwrap();
        writeln!(yaml_file, "BASE_VAR:\n  type: string").unwrap();

        // JSON child extends YAML
        let json_path = dir.path().join("child.schema.json");
        let mut json_file = fs::File::create(&json_path).unwrap();
        writeln!(json_file, r#"{{"extends": "base.schema.yaml", "CHILD_VAR": {{"type": "int"}}}}"#).unwrap();

        let schema = load_schema(json_path.to_str().unwrap()).unwrap();
        assert!(schema.contains_key("BASE_VAR"));
        assert!(schema.contains_key("CHILD_VAR"));
    }

    #[test]
    fn test_save_schema_yaml() {
        use tempfile::tempdir;

        let dir = tempdir().unwrap();
        let yaml_path = dir.path().join("output.yaml");

        let mut schema = Schema::new();
        schema.insert("TEST_VAR".to_string(), VarSpec {
            var_type: VarType::String,
            required: true,
            ..Default::default()
        });

        save_schema(yaml_path.to_str().unwrap(), &schema).unwrap();

        // Verify it can be read back
        let loaded = load_schema(yaml_path.to_str().unwrap()).unwrap();
        assert!(loaded.contains_key("TEST_VAR"));
    }

    #[test]
    fn test_yaml_with_validation_rules() {
        let yaml = r#"
PORT:
  type: int
  validate:
    min: 1024
    max: 65535
API_KEY:
  type: string
  validate:
    min_length: 32
    pattern: "^sk_"
"#;
        let result = parse_schema_content(yaml, SchemaFormat::Yaml);
        assert!(result.is_ok());
        let schema_file = result.unwrap();

        let port = schema_file.vars.get("PORT").unwrap();
        let port_validate = port.validate.as_ref().unwrap();
        assert_eq!(port_validate.min, Some(1024));
        assert_eq!(port_validate.max, Some(65535));

        let api_key = schema_file.vars.get("API_KEY").unwrap();
        let key_validate = api_key.validate.as_ref().unwrap();
        assert_eq!(key_validate.min_length, Some(32));
        assert_eq!(key_validate.pattern, Some("^sk_".to_string()));
    }

    #[test]
    fn test_load_options_default() {
        let opts = LoadOptions::default();
        assert!(!opts.no_cache);
        assert!(opts.verify_hash.is_none());
        assert!(opts.ca_cert.is_none());
        assert!(opts.rate_limit_seconds.is_none());
    }

    #[test]
    fn test_schema_error_display() {
        let read_err = SchemaError::Read("file not found".to_string());
        assert!(read_err.to_string().contains("file not found"));

        let parse_err = SchemaError::Parse("JSON".to_string(), "syntax error".to_string());
        assert!(parse_err.to_string().contains("JSON"));
        assert!(parse_err.to_string().contains("syntax error"));

        let circular_err = SchemaError::CircularInheritance("schema.json".to_string());
        assert!(circular_err.to_string().contains("circular"));

        let depth_err = SchemaError::InheritanceDepthExceeded;
        assert!(depth_err.to_string().contains("depth"));
    }
}