cloudformation-validate-schema-validator 1.11.0

AWS CloudFormation Validate
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
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::collections::HashMap;

/// Definitions are stored separately and referenced by name to avoid exponential blowup.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct CompiledSchema {
    #[serde(default)]
    pub type_name: String,
    #[serde(default)]
    pub properties: HashMap<String, PropSchema>,
    #[serde(default)]
    pub definitions: HashMap<String, PropSchema>,
    #[serde(default)]
    pub required: Vec<String>,
    /// Whether root-level `required` was explicitly stated in the source that
    /// produced this schema. When `true`, merging replaces the base's root
    /// required list (even if the list is empty, which clears it); when `false`,
    /// merging preserves the base. Not serialized - existing committed artifacts
    /// deserialize with the default (`false`), which is correct: bundled schemas
    /// are never overlay sources.
    #[serde(default, skip_serializing, skip_deserializing)]
    pub required_present: bool,
    #[serde(default)]
    pub additional_properties: Option<bool>,
    #[serde(default)]
    pub read_only_properties: Vec<String>,
    #[serde(default)]
    pub write_only_properties: Vec<String>,
    #[serde(default)]
    pub create_only_properties: Vec<String>,
    #[serde(default)]
    pub deprecated_properties: Vec<String>,
    #[serde(default)]
    pub conditional_create_only_properties: Vec<String>,
    #[serde(default)]
    pub primary_identifier: Vec<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub replacement_strategy: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub documentation_url: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub source_url: Option<String>,
    #[serde(default)]
    pub all_of: Vec<SubSchema>,
    #[serde(default)]
    pub any_of: Vec<SubSchema>,
    #[serde(default)]
    pub one_of: Vec<SubSchema>,
    #[serde(default)]
    pub if_then_else: Vec<IfThenElse>,
    #[serde(default)]
    pub dependent_required: HashMap<String, Vec<String>>,
    #[serde(default)]
    pub dependent_excluded: HashMap<String, Vec<String>>,
    #[serde(default)]
    pub required_or: Vec<String>,
    #[serde(default)]
    pub required_xor: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct IfThenElse {
    pub condition: ConditionSchema,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub then_schema: Option<SubSchema>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub else_schema: Option<SubSchema>,
    /// Whether the selected branch is enforced in full (required,
    /// additionalProperties, value constraints) rather than dependencies-only.
    ///
    /// Set for conditionals an overlay supplies: the overlay author states the
    /// conditional deliberately and no dedicated rule covers it. Bundled
    /// conditionals stay dependencies-only, because their richer semantics are
    /// owned by dedicated resource-specific rules (with their own IDs and
    /// severities) and enforcing them generically would double-report. Never
    /// serialized - the committed artifact stays unchanged and deserializes to
    /// dependencies-only.
    #[serde(skip)]
    pub enforce_full_branch: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ConditionSchema {
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub properties: HashMap<String, PropSchema>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub required: Vec<String>,
    /// Properties that must be absent for the condition to hold (a `false`
    /// property schema in the source).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub absent: Vec<String>,
    /// The instance type the condition requires (`if: {"type": ...}`). A
    /// condition stating a type only matches an instance of that type; resource
    /// roots are always objects, so `"object"` is a no-op there while any other
    /// type makes the condition unsatisfiable at the root.
    #[serde(default, rename = "type", skip_serializing_if = "Option::is_none")]
    pub prop_type: Option<PropType>,
    /// When set, the condition matches if ANY of these sub-conditions match.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub any_of: Vec<ConditionSchema>,
    /// When non-empty, exactly one of these sub-conditions must match.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub one_of: Vec<ConditionSchema>,
    /// When set, the condition matches only if this sub-condition does not.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub not: Option<Box<ConditionSchema>>,
}

impl ConditionSchema {
    /// Whether the condition constrains nothing at all. Such a condition can only
    /// come from a source `if` the compiler could not represent (older artifacts
    /// compiled `not`/`oneOf` conditions this way), and evaluating it as
    /// always-true would apply its branch to every resource of the type.
    pub fn states_nothing(&self) -> bool {
        let ConditionSchema { properties, required, absent, prop_type, any_of, one_of, not } = self;
        properties.is_empty()
            && required.is_empty()
            && absent.is_empty()
            && prop_type.is_none()
            && any_of.is_empty()
            && one_of.is_empty()
            && not.is_none()
    }
}

/// `$ref` is stored as `ref_name` and resolved at validation time against the parent schema's `definitions`.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct PropSchema {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ref_name: Option<String>,
    #[serde(default, rename = "type", skip_serializing_if = "Option::is_none")]
    pub prop_type: Option<PropType>,
    #[serde(default, rename = "enum", skip_serializing_if = "Vec::is_empty")]
    pub enum_values: Vec<serde_json::Value>,
    /// Allowed values compared case-insensitively - used for properties whose
    /// service accepts any casing of the documented value. A schema carries
    /// either this or `enum_values` for a given property, never both.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub enum_case_insensitive: Vec<serde_json::Value>,
    /// JSON Schema `not: { enum: [...] }` - value must NOT match any of these.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub not_enum: Vec<serde_json::Value>,
    #[serde(default, rename = "const", skip_serializing_if = "Option::is_none")]
    pub const_value: Option<serde_json::Value>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub pattern: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub minimum: Option<f64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub maximum: Option<f64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub exclusive_minimum: Option<f64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub exclusive_maximum: Option<f64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub multiple_of: Option<f64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub min_length: Option<u64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub max_length: Option<u64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub min_items: Option<u64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub max_items: Option<u64>,
    /// `None` when the schema omits `uniqueItems`; `Some(false)` when it is
    /// explicitly relaxed. Keeping the distinction lets an overlay clear a
    /// bundled `true`.
    #[serde(default, skip_serializing_if = "skip_unless_true")]
    pub unique_items: Option<bool>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub min_properties: Option<u64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub max_properties: Option<u64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub format: Option<String>,
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub properties: HashMap<String, PropSchema>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub required: Vec<String>,
    /// Whether `required` was explicitly stated at this property, definition, or
    /// item schema level. When `true`, merging replaces the corresponding nested
    /// required list (even if the list is empty, which clears it); when `false`,
    /// merging preserves the base. Not serialized - existing artifacts
    /// deserialize unchanged.
    #[serde(default, skip_serializing, skip_deserializing)]
    pub required_present: bool,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub additional_properties: Option<bool>,
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub pattern_properties: HashMap<String, PropSchema>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub items: Option<Box<PropSchema>>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub all_of: Vec<SubSchema>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub any_of: Vec<SubSchema>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub one_of: Vec<SubSchema>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub if_then_else: Vec<IfThenElse>,
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub dependent_required: HashMap<String, Vec<String>>,
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub dependent_excluded: HashMap<String, Vec<String>>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub required_or: Vec<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub required_xor: Vec<String>,
}

fn skip_unless_true(value: &Option<bool>) -> bool {
    *value != Some(true)
}

/// A composition branch is now a full property schema - every constraint that
/// can appear on a property is available in a branch and evaluated at runtime.
/// This alias preserves naming clarity at usage sites.
pub type SubSchema = PropSchema;

/// Upper bound on the length of a `$ref` chain followed by
/// `PropSchema::resolve`. Real provider schemas chain at most a handful of
/// hops; the bound exists so a malformed definition graph cannot make resolution
/// unbounded.
pub(crate) const MAX_REF_CHAIN: usize = 64;

impl PropSchema {
    /// The schema that actually applies to this property: the terminal target of
    /// its `$ref` chain, with any fields stated alongside the reference merged on
    /// top.
    ///
    /// A property may carry both a `$ref` and its own constraints - that is what
    /// an overlay extending a referenced property produces. Resolving them here,
    /// rather than folding the referenced definition into the property when the
    /// overlay is applied, keeps the reference live: a later overlay that changes
    /// the definition still reaches every property pointing at it, and the result
    /// does not depend on the order definitions happened to be merged in.
    ///
    /// Resolution is iterative and cycle-safe: a definition graph that loops back
    /// on itself, or a chain longer than [`MAX_REF_CHAIN`], stops at the last
    /// schema reached instead of recursing forever. Cyclic graphs are rejected
    /// when an overlay is applied, so this is the second line of defence - a
    /// caller-supplied schema must never be able to exhaust the stack and abort
    /// the process.
    pub fn resolve<'a>(&'a self, defs: &'a HashMap<String, PropSchema>) -> Cow<'a, PropSchema> {
        if self.ref_name.is_none() {
            return Cow::Borrowed(self);
        }
        // Every hop may state constraints of its own beside its reference, so the
        // whole chain is collected rather than just its ends.
        let mut chain: Vec<&PropSchema> = vec![self];
        let mut seen: Vec<&str> = Vec::new();
        for _ in 0..MAX_REF_CHAIN {
            let Some(name) = chain.last().and_then(|hop| hop.ref_name.as_deref()) else {
                break;
            };
            if seen.contains(&name) {
                break;
            }
            seen.push(name);
            match defs.get(name) {
                Some(next) => chain.push(next),
                None => break,
            }
        }
        let (terminal, referrers) = match chain.split_last() {
            Some(pair) => pair,
            // `chain` is initialized with `self`, so it is never empty. This
            // branch is structurally unreachable but avoids a panic path.
            None => return Cow::Borrowed(self),
        };
        if !referrers.iter().any(|hop| hop.has_own_constraints()) {
            return Cow::Borrowed(terminal);
        }
        let mut effective = (*terminal).clone();
        effective.ref_name = None;
        // Nearest wins: apply the innermost referrer first and this schema last.
        for hop in referrers.iter().rev() {
            if !hop.has_own_constraints() {
                continue;
            }
            let mut own = (*hop).clone();
            own.ref_name = None;
            crate::overlay::merge_prop(&mut effective, own);
        }
        Cow::Owned(effective)
    }

    /// Whether this property states anything of its own beside a `$ref`.
    ///
    /// Destructured exhaustively so a new field cannot be forgotten here and make
    /// a property's own constraints silently disappear at resolution time.
    /// Whether this schema states anything `schema_value_matches` could fail a
    /// value against. Destructured exhaustively so a newly added constraint
    /// field cannot be omitted and silently skip branch value matching.
    ///
    /// A `ref_name` counts because a dangling reference makes matching fail.
    pub(crate) fn constrains_value(&self) -> bool {
        let PropSchema {
            ref_name,
            prop_type,
            enum_values,
            enum_case_insensitive,
            not_enum,
            const_value,
            pattern,
            minimum,
            maximum,
            exclusive_minimum,
            exclusive_maximum,
            multiple_of,
            min_length,
            max_length,
            min_items,
            max_items,
            unique_items,
            min_properties,
            max_properties,
            format,
            properties,
            required,
            required_present: _,
            additional_properties,
            pattern_properties,
            items,
            all_of,
            any_of,
            one_of,
            if_then_else,
            dependent_required,
            dependent_excluded,
            required_or,
            required_xor,
        } = self;
        ref_name.is_some()
            || prop_type.is_some()
            || !enum_values.is_empty()
            || !enum_case_insensitive.is_empty()
            || !not_enum.is_empty()
            || const_value.is_some()
            || pattern.is_some()
            || minimum.is_some()
            || maximum.is_some()
            || exclusive_minimum.is_some()
            || exclusive_maximum.is_some()
            || multiple_of.is_some()
            || min_length.is_some()
            || max_length.is_some()
            || min_items.is_some()
            || max_items.is_some()
            || unique_items == &Some(true)
            || min_properties.is_some()
            || max_properties.is_some()
            || format.is_some()
            || !properties.is_empty()
            || !required.is_empty()
            || additional_properties.is_some()
            || !pattern_properties.is_empty()
            || items.is_some()
            || !all_of.is_empty()
            || !any_of.is_empty()
            || !one_of.is_empty()
            || !if_then_else.is_empty()
            || !dependent_required.is_empty()
            || !dependent_excluded.is_empty()
            || !required_or.is_empty()
            || !required_xor.is_empty()
    }

    fn has_own_constraints(&self) -> bool {
        let PropSchema {
            ref_name: _,
            prop_type,
            enum_values,
            enum_case_insensitive,
            not_enum,
            const_value,
            pattern,
            minimum,
            maximum,
            exclusive_minimum,
            exclusive_maximum,
            multiple_of,
            min_length,
            max_length,
            min_items,
            max_items,
            unique_items,
            min_properties,
            max_properties,
            format,
            properties,
            required,
            required_present: _,
            additional_properties,
            pattern_properties,
            items,
            all_of,
            any_of,
            one_of,
            if_then_else,
            dependent_required,
            dependent_excluded,
            required_or,
            required_xor,
        } = self;
        prop_type.is_some()
            || !enum_values.is_empty()
            || !enum_case_insensitive.is_empty()
            || !not_enum.is_empty()
            || const_value.is_some()
            || pattern.is_some()
            || minimum.is_some()
            || maximum.is_some()
            || exclusive_minimum.is_some()
            || exclusive_maximum.is_some()
            || multiple_of.is_some()
            || min_length.is_some()
            || max_length.is_some()
            || min_items.is_some()
            || max_items.is_some()
            || unique_items.is_some()
            || min_properties.is_some()
            || max_properties.is_some()
            || format.is_some()
            || !properties.is_empty()
            || !required.is_empty()
            || additional_properties.is_some()
            || !pattern_properties.is_empty()
            || items.is_some()
            || !all_of.is_empty()
            || !any_of.is_empty()
            || !one_of.is_empty()
            || !if_then_else.is_empty()
            || !dependent_required.is_empty()
            || !dependent_excluded.is_empty()
            || !required_or.is_empty()
            || !required_xor.is_empty()
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum PropType {
    Single(String),
    Multi(Vec<String>),
}

impl PropType {
    pub fn primary(&self) -> Option<&str> {
        match self {
            PropType::Single(s) => Some(s),
            PropType::Multi(v) => v.iter().find(|s| s.as_str() != "null").map(|s| s.as_str()),
        }
    }

    /// Every type name this `type` keyword admits.
    pub fn names(&self) -> impl Iterator<Item = &str> {
        match self {
            PropType::Single(s) => std::slice::from_ref(s).iter(),
            PropType::Multi(v) => v.iter(),
        }
        .map(String::as_str)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;
    use std::ptr;

    #[test]
    fn primary_single_returns_the_type() {
        let pt = PropType::Single("string".into());
        assert_eq!(pt.primary(), Some("string"));
    }

    #[test]
    fn primary_multi_skips_null() {
        let pt = PropType::Multi(vec!["null".into(), "integer".into()]);
        assert_eq!(pt.primary(), Some("integer"));
    }

    #[test]
    fn primary_multi_all_null_returns_none() {
        let pt = PropType::Multi(vec!["null".into()]);
        assert_eq!(pt.primary(), None);
    }

    #[test]
    fn primary_multi_empty_returns_none() {
        let pt = PropType::Multi(vec![]);
        assert_eq!(pt.primary(), None);
    }

    #[test]
    fn primary_multi_first_non_null() {
        let pt = PropType::Multi(vec!["string".into(), "null".into(), "integer".into()]);
        assert_eq!(pt.primary(), Some("string"));
    }

    #[test]
    fn resolve_no_ref_returns_self() {
        let schema = PropSchema { prop_type: Some(PropType::Single("string".into())), ..Default::default() };
        let defs = HashMap::new();
        let resolved = schema.resolve(&defs);
        assert!(matches!(resolved, Cow::Borrowed(borrowed) if ptr::eq(borrowed, &schema)));
    }

    #[test]
    fn resolve_follows_ref_to_definition() {
        let target = PropSchema { prop_type: Some(PropType::Single("integer".into())), ..Default::default() };
        let mut defs = HashMap::new();
        defs.insert("MyDef".into(), target);

        let schema = PropSchema { ref_name: Some("MyDef".into()), ..Default::default() };
        let resolved = schema.resolve(&defs);
        assert_eq!(resolved.prop_type.as_ref().unwrap().primary(), Some("integer"));
    }

    #[test]
    fn resolve_keeps_required_groups_stated_beside_ref() {
        let mut defs = HashMap::new();
        defs.insert(
            "Config".into(),
            PropSchema { prop_type: Some(PropType::Single("object".into())), ..Default::default() },
        );
        let schema = PropSchema {
            ref_name: Some("Config".into()),
            required_or: vec!["A".into(), "B".into()],
            required_xor: vec!["C".into(), "D".into()],
            ..Default::default()
        };
        let resolved = schema.resolve(&defs);
        assert_eq!(resolved.required_or, vec!["A".to_string(), "B".to_string()]);
        assert_eq!(resolved.required_xor, vec!["C".to_string(), "D".to_string()]);
    }

    #[test]
    fn resolve_chained_refs() {
        let final_target = PropSchema { prop_type: Some(PropType::Single("boolean".into())), ..Default::default() };
        let intermediate = PropSchema { ref_name: Some("Final".into()), ..Default::default() };
        let mut defs = HashMap::new();
        defs.insert("Final".into(), final_target);
        defs.insert("Intermediate".into(), intermediate);

        let schema = PropSchema { ref_name: Some("Intermediate".into()), ..Default::default() };
        let resolved = schema.resolve(&defs);
        assert_eq!(resolved.prop_type.as_ref().unwrap().primary(), Some("boolean"));
    }

    #[test]
    fn resolve_missing_ref_returns_self() {
        let schema = PropSchema { ref_name: Some("NonExistent".into()), ..Default::default() };
        let defs = HashMap::new();
        let resolved = schema.resolve(&defs);
        assert!(matches!(resolved, Cow::Borrowed(borrowed) if ptr::eq(borrowed, &schema)));
    }

    /// Cyclic graphs are rejected when an overlay is applied, so these cover the
    /// second line of defence: resolution must terminate on its own rather than
    /// recursing until the stack is gone, because a stack overflow aborts the
    /// host process instead of surfacing as a catchable error.
    #[test]
    fn resolve_terminates_on_a_self_referential_definition() {
        let mut defs = HashMap::new();
        defs.insert("Loop".to_string(), PropSchema { ref_name: Some("Loop".into()), ..Default::default() });

        let schema = PropSchema { ref_name: Some("Loop".into()), ..Default::default() };
        let resolved = schema.resolve(&defs);

        assert_eq!(
            resolved.ref_name.as_deref(),
            Some("Loop"),
            "resolution stops at the definition it has already visited"
        );
    }

    #[test]
    fn resolve_terminates_on_a_multi_node_definition_cycle() {
        let mut defs = HashMap::new();
        defs.insert("First".to_string(), PropSchema { ref_name: Some("Second".into()), ..Default::default() });
        defs.insert("Second".to_string(), PropSchema { ref_name: Some("First".into()), ..Default::default() });

        let schema = PropSchema { ref_name: Some("First".into()), ..Default::default() };
        let resolved = schema.resolve(&defs);

        assert_eq!(resolved.ref_name.as_deref(), Some("First"), "resolution stops at the hop that closes the cycle");
    }

    #[test]
    fn resolve_stops_at_the_chain_limit_and_keeps_the_property_own_constraints() {
        let unreachable_hop = MAX_REF_CHAIN + 5;
        let mut defs = HashMap::new();
        for hop in 0..unreachable_hop {
            defs.insert(
                format!("Hop{hop}"),
                PropSchema { ref_name: Some(format!("Hop{}", hop + 1)), ..Default::default() },
            );
        }
        defs.insert(
            format!("Hop{unreachable_hop}"),
            PropSchema { pattern: Some("^unreachable$".into()), ..Default::default() },
        );

        let schema = PropSchema { ref_name: Some("Hop0".into()), max_length: Some(10), ..Default::default() };
        let resolved = schema.resolve(&defs);

        assert_eq!(
            resolved.max_length,
            Some(10),
            "a chain too long to follow must not discard what the property itself states"
        );
        assert_eq!(
            resolved.pattern, None,
            "the constraint beyond the resolution limit is not reachable, which is why such a chain is rejected on input"
        );
    }

    #[test]
    fn prop_schema_roundtrip_json() {
        let schema = PropSchema {
            prop_type: Some(PropType::Single("string".into())),
            enum_values: vec![json!("a"), json!("b")],
            pattern: Some("^[a-z]+$".into()),
            minimum: Some(0.0),
            maximum: Some(100.0),
            min_length: Some(1),
            max_length: Some(256),
            unique_items: Some(true),
            ..Default::default()
        };
        let json_str = serde_json::to_string(&schema).unwrap();
        let deserialized: PropSchema = serde_json::from_str(&json_str).unwrap();
        assert_eq!(deserialized.prop_type.as_ref().unwrap().primary(), Some("string"));
        assert_eq!(deserialized.enum_values.len(), 2);
        assert_eq!(deserialized.pattern.as_deref(), Some("^[a-z]+$"));
        assert_eq!(deserialized.minimum, Some(0.0));
        assert_eq!(deserialized.maximum, Some(100.0));
        assert_eq!(deserialized.min_length, Some(1));
        assert_eq!(deserialized.max_length, Some(256));
        assert_eq!(deserialized.unique_items, Some(true), "unique_items should round trip as true");
    }

    #[test]
    fn prop_type_single_deserializes_from_string() {
        let pt: PropType = serde_json::from_str(r#""string""#).unwrap();
        assert_eq!(pt.primary(), Some("string"));
    }

    #[test]
    fn prop_type_multi_deserializes_from_array() {
        let pt: PropType = serde_json::from_str(r#"["string", "null"]"#).unwrap();
        assert_eq!(pt.primary(), Some("string"));
    }

    #[test]
    fn compiled_schema_defaults_are_empty() {
        let json_str = r#"{"type_name": "AWS::Test::Resource"}"#;
        let schema: CompiledSchema = serde_json::from_str(json_str).unwrap();
        assert_eq!(schema.type_name, "AWS::Test::Resource");
        assert_eq!(schema.properties.len(), 0, "properties should be empty");
        assert_eq!(schema.definitions.len(), 0, "definitions should be empty");
        assert_eq!(schema.required.len(), 0, "required should be empty");
        assert_eq!(schema.additional_properties, None, "additional_properties should be None");
        assert_eq!(schema.read_only_properties.len(), 0, "read_only_properties should be empty");
        assert_eq!(schema.if_then_else.len(), 0, "if_then_else should be empty");
    }

    #[test]
    fn if_then_else_roundtrip() {
        let ite = IfThenElse {
            enforce_full_branch: false,
            condition: ConditionSchema {
                properties: {
                    let mut m = HashMap::new();
                    m.insert("Engine".into(), PropSchema { enum_values: vec![json!("aurora")], ..Default::default() });
                    m
                },
                ..Default::default()
            },
            then_schema: Some(SubSchema { required: vec!["Port".into()], ..Default::default() }),
            else_schema: None,
        };
        let json_str = serde_json::to_string(&ite).unwrap();
        let deserialized: IfThenElse = serde_json::from_str(&json_str).unwrap();
        assert_eq!(deserialized.condition.properties.len(), 1);
        assert!(deserialized.then_schema.is_some(), "then_schema should be present");
        assert!(deserialized.else_schema.is_none(), "else_schema should be None");
    }

    #[test]
    fn prop_schema_skip_serializing_defaults() {
        let schema = PropSchema::default();
        let json_str = serde_json::to_string(&schema).unwrap();
        let val: serde_json::Value = serde_json::from_str(&json_str).unwrap();
        let obj = val.as_object().unwrap();
        assert!(obj.is_empty(), "expected empty JSON for default PropSchema, got: {}", json_str);
    }

    #[test]
    fn sub_schema_with_dependent_required() {
        let sub = SubSchema {
            dependent_required: {
                let mut m = HashMap::new();
                m.insert("A".into(), vec!["B".into(), "C".into()]);
                m
            },
            ..Default::default()
        };
        let json_str = serde_json::to_string(&sub).unwrap();
        let deserialized: SubSchema = serde_json::from_str(&json_str).unwrap();
        assert_eq!(deserialized.dependent_required.get("A").unwrap(), &vec!["B".to_string(), "C".to_string()]);
    }

    #[test]
    fn required_or_xor_serialization_roundtrip_and_default_omission() {
        let empty = PropSchema::default();
        let json_str = serde_json::to_string(&empty).unwrap();
        assert!(!json_str.contains("required_or"), "empty required_or must be omitted from serialized output");
        assert!(!json_str.contains("required_xor"), "empty required_xor must be omitted from serialized output");

        let schema = PropSchema {
            required_or: vec!["A".into(), "B".into()],
            required_xor: vec!["C".into(), "D".into()],
            ..Default::default()
        };
        let json_str = serde_json::to_string(&schema).unwrap();
        assert!(json_str.contains("required_or"), "populated required_or must be serialized, got: {}", json_str);
        assert!(json_str.contains("required_xor"), "populated required_xor must be serialized, got: {}", json_str);

        let deserialized: PropSchema = serde_json::from_str(&json_str).unwrap();
        assert_eq!(deserialized.required_or, vec!["A".to_string(), "B".to_string()]);
        assert_eq!(deserialized.required_xor, vec!["C".to_string(), "D".to_string()]);

        let minimal: PropSchema = serde_json::from_str("{}").unwrap();
        assert!(minimal.required_or.is_empty(), "missing required_or deserializes to empty");
        assert!(minimal.required_xor.is_empty(), "missing required_xor deserializes to empty");
    }
}