xbrl-rs 0.3.0

XBRL parser and validation
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
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
//! Schema validation: checks that facts conform to the DTS element definitions
//! and XBRL specification structural rules.

use super::{Severity, ValidationResult, value::PreparedFactValues};
use crate::{
    DeclaredAccuracy, ExpandedName, Fact, InstanceDocument, ItemFact, Period, TaxonomySet,
    TupleFact, Unit,
    taxonomy::{Concept, ElementParticle, Particle, PeriodType, XbrlType},
};
use rust_decimal::Decimal;
use std::{
    collections::{HashMap, HashSet},
    str::FromStr,
};

const NS_XBRLI: &str = "http://www.xbrl.org/2003/instance";
const NS_ISO4217: &str = "http://www.xbrl.org/2003/iso4217";
const ROLE_LINK: &str = "http://www.xbrl.org/2003/role/link";
const ROLE_FOOTNOTE: &str = "http://www.xbrl.org/2003/role/footnote";
const ROLE_LABEL: &str = "http://www.xbrl.org/2003/role/label";
const ARCROLE_FACT_FOOTNOTE: &str = "http://www.xbrl.org/2003/arcrole/fact-footnote";
const ARCROLE_ESSENCE_ALIAS: &str = "http://www.xbrl.org/2003/arcrole/essence-alias";

/// Run all schema-level validation checks.
pub(super) fn validate_schema(
    instance: &InstanceDocument,
    taxonomy: &TaxonomySet,
    _prepared: &PreparedFactValues,
    result: &mut ValidationResult,
) {
    validate_contexts(instance, taxonomy, result);
    validate_footnotes(instance, result);
    validate_instance_refs(instance, result);
    validate_essence_alias_units(instance, taxonomy, result);

    for fact in instance.facts() {
        validate_fact(fact, None, instance, taxonomy, result);
    }
}

/// Validate uniqueness of instance-level `roleRef` and `arcroleRef` declarations.
///
/// Adds an error when the same role URI or arcrole URI is declared more than
/// once in the instance document.
fn validate_instance_refs(instance: &InstanceDocument, result: &mut ValidationResult) {
    let mut role_uris = HashSet::new();
    for role_uri in instance.role_refs() {
        if !role_uris.insert(role_uri.as_str()) {
            result.add(
                Severity::Error,
                "spec.duplicate_role_ref",
                format!("Duplicate roleRef for roleURI '{}'", role_uri),
                None,
                None,
            );
        }
    }

    let mut arcrole_uris = HashSet::new();
    for arcrole_uri in instance.arcrole_refs() {
        if !arcrole_uris.insert(arcrole_uri.as_str()) {
            result.add(
                Severity::Error,
                "spec.duplicate_arcrole_ref",
                format!("Duplicate arcroleRef for arcroleURI '{}'", arcrole_uri),
                None,
                None,
            );
        }
    }
}

/// Validate essence-alias relationships by comparing units of matched facts.
///
/// For each definition arc with arcrole `essence-alias`, this checks facts in
/// the same context and reports an error when non-nil source/target facts use
/// semantically different units.
fn validate_essence_alias_units(
    instance: &InstanceDocument,
    taxonomy: &TaxonomySet,
    result: &mut ValidationResult,
) {
    if taxonomy.definitions().is_empty() {
        return;
    }

    let mut facts_by_concept_name: HashMap<ExpandedName, Vec<&ItemFact>> = HashMap::new();

    for fact in instance.item_facts() {
        if let Some(concept) = taxonomy.find_concept(fact.concept_name()) {
            facts_by_concept_name
                .entry(concept.name.clone())
                .or_default()
                .push(fact);
        }
    }

    for arcs in taxonomy.definitions().values() {
        for arc in arcs {
            if arc.arcrole.as_str() != ARCROLE_ESSENCE_ALIAS {
                continue;
            }

            let Some(essence_facts) = facts_by_concept_name.get(&arc.from) else {
                continue;
            };
            let Some(alias_facts) = facts_by_concept_name.get(&arc.to) else {
                continue;
            };

            for essence_fact in essence_facts {
                if essence_fact.is_nil() {
                    continue;
                }
                let Some(essence_unit_ref) = essence_fact.unit_ref() else {
                    continue;
                };
                let Some(essence_unit) = instance.get_unit(essence_unit_ref) else {
                    continue;
                };

                for alias_fact in alias_facts {
                    if alias_fact.is_nil() || essence_fact.context_ref() != alias_fact.context_ref()
                    {
                        continue;
                    }

                    let Some(alias_unit_ref) = alias_fact.unit_ref() else {
                        continue;
                    };
                    let Some(alias_unit) = instance.get_unit(alias_unit_ref) else {
                        continue;
                    };

                    if !units_semantically_equal(essence_unit, alias_unit) {
                        result.add(
                            Severity::Error,
                            "spec.essence_alias_unit_mismatch",
                            format!(
                                "Essence-alias facts '{}' and '{}' in context '{}' must have equal units",
                                essence_fact.concept_name(),
                                alias_fact.concept_name(),
                                essence_fact.context_ref()
                            ),
                            Some(essence_fact.concept_name().to_string()),
                            Some(essence_fact.context_ref().to_string()),
                        );
                    }
                }
            }
        }
    }
}

fn units_semantically_equal(left: &Unit, right: &Unit) -> bool {
    let mut left_num = left.numerator.clone();
    let mut right_num = right.numerator.clone();
    let mut left_den = left.denominator.clone();
    let mut right_den = right.denominator.clone();

    left_num.sort();
    right_num.sort();
    left_den.sort();
    right_den.sort();

    left_num == right_num && left_den == right_den
}

/// Validate footnote link structure and cross-reference integrity.
///
/// Enforces role constraints, locator/resource requirements, `href` target
/// resolution, and `fact-footnote` arc endpoint correctness.
fn validate_footnotes(instance: &InstanceDocument, result: &mut ValidationResult) {
    if instance.footnote_links().is_empty() {
        return;
    }

    let context_ids: HashSet<&str> = instance.contexts().keys().map(|id| id.as_str()).collect();
    let unit_ids: HashSet<&str> = instance.units().keys().map(|id| id.as_str()).collect();
    let fact_ids: HashSet<&str> = instance
        .item_facts()
        .iter()
        .filter_map(|fact| fact.id())
        .collect();

    for footnote_link in instance.footnote_links() {
        if footnote_link.role.as_deref() == Some(ROLE_FOOTNOTE) {
            result.add(
                Severity::Error,
                "spec.invalid_footnote_link_role",
                "footnoteLink cannot use standard footnote role".to_string(),
                None,
                None,
            );
        }

        let mut loc_by_label: HashMap<&str, &str> = HashMap::new();
        let mut res_by_label: HashMap<&str, &crate::instance::FootnoteResource> = HashMap::new();

        for loc in &footnote_link.locators {
            if loc.element_local_name != "loc" {
                result.add(
                    Severity::Error,
                    "spec.invalid_custom_locator",
                    "footnoteLink contains custom locator element".to_string(),
                    None,
                    None,
                );
            }

            if let (Some(label), Some(href)) = (loc.label.as_deref(), loc.href.as_deref()) {
                loc_by_label.insert(label, href);
                if let Some((_file_part, target)) = href_target_id(href) {
                    if context_ids.contains(target) || unit_ids.contains(target) {
                        result.add(
                            Severity::Error,
                            "spec.invalid_footnote_href_target",
                            format!("Footnote locator href '{}' points to context/unit", href),
                            None,
                            None,
                        );
                    }
                    if !fact_ids.contains(target) {
                        result.add(
                            Severity::Error,
                            "spec.footnote_href_not_fact",
                            format!("Footnote locator href '{}' must resolve to a fact id", href),
                            None,
                            None,
                        );
                    }
                } else {
                    result.add(
                        Severity::Error,
                        "spec.invalid_footnote_href",
                        format!("Invalid footnote locator href '{}': missing fragment", href),
                        None,
                        None,
                    );
                }
            }
        }

        for resource in &footnote_link.footnotes {
            if let Some(label) = resource.label.as_deref() {
                res_by_label.insert(label, resource);
            }

            if resource.xml_lang.is_none() && footnote_link.xml_lang.is_none() {
                result.add(
                    Severity::Error,
                    "spec.footnote_missing_lang",
                    "Footnote resource is missing xml:lang (and no link-level xml:lang provided)"
                        .to_string(),
                    None,
                    None,
                );
            }

            if let Some(role) = resource.role.as_deref()
                && (role == ROLE_LINK || role == ROLE_LABEL)
            {
                result.add(
                    Severity::Error,
                    "spec.invalid_footnote_role",
                    format!("Footnote resource uses invalid standard role '{}'", role),
                    None,
                    None,
                );
            }
        }

        for arc in &footnote_link.arcs {
            if arc.arcrole.as_deref() == Some(ARCROLE_FACT_FOOTNOTE) {
                let Some(from) = arc.from.as_deref() else {
                    result.add(
                        Severity::Error,
                        "spec.missing_footnote_arc_from",
                        "fact-footnote arc is missing xlink:from".to_string(),
                        None,
                        None,
                    );
                    continue;
                };
                let Some(to) = arc.to.as_deref() else {
                    result.add(
                        Severity::Error,
                        "spec.missing_footnote_arc_to",
                        "fact-footnote arc is missing xlink:to".to_string(),
                        None,
                        None,
                    );
                    continue;
                };

                let Some(from_href) = loc_by_label.get(from).copied() else {
                    result.add(
                        Severity::Error,
                        "spec.arc_from_out_of_scope",
                        format!(
                            "fact-footnote arc from='{}' does not resolve to a locator",
                            from
                        ),
                        None,
                        None,
                    );
                    continue;
                };

                let Some((_, from_target)) = href_target_id(from_href) else {
                    result.add(
                        Severity::Error,
                        "spec.arc_from_invalid_href",
                        format!("Locator '{}' has invalid href '{}'", from, from_href),
                        None,
                        None,
                    );
                    continue;
                };

                if !fact_ids.contains(from_target) {
                    result.add(
                        Severity::Error,
                        "spec.arc_from_not_fact",
                        format!("fact-footnote arc from='{}' does not point to a fact", from),
                        None,
                        None,
                    );
                }

                let Some(to_resource) = res_by_label.get(to).copied() else {
                    result.add(
                        Severity::Error,
                        "spec.arc_to_out_of_scope",
                        format!(
                            "fact-footnote arc to='{}' does not resolve to footnote resource",
                            to
                        ),
                        None,
                        None,
                    );
                    continue;
                };

                if let Some(role) = to_resource.role.as_deref()
                    && role != ROLE_FOOTNOTE
                {
                    result.add(
                        Severity::Error,
                        "spec.invalid_referenced_footnote_role",
                        format!(
                            "Referenced footnote resource must have role '{}' when role is provided",
                            ROLE_FOOTNOTE
                        ),
                        None,
                        None,
                    );
                }
            }
        }
    }
}

fn href_target_id(href: &str) -> Option<(Option<&str>, &str)> {
    if let Some(rest) = href.strip_prefix('#') {
        return (!rest.is_empty()).then_some((None, rest));
    }
    let (file_part, fragment) = href.split_once('#')?;
    if file_part.is_empty() || fragment.is_empty() {
        return None;
    }
    Some((Some(file_part), fragment))
}

/// Validate a fact against its concept definition recursively, checking content
/// model constraints for tuples and concept-level restrictions for items.
fn validate_fact(
    fact: &Fact,
    parent_tuple: Option<&Concept>,
    instance: &InstanceDocument,
    taxonomy: &TaxonomySet,
    result: &mut ValidationResult,
) {
    match fact {
        Fact::Item(item_fact) => {
            let concept_name = item_fact.concept_name();

            if let Some(parent_tuple) = parent_tuple
                && let Some(child_concept) = taxonomy.find_concept(concept_name)
            {
                validate_tuple_child(child_concept, parent_tuple, taxonomy, result);
            }
            validate_item_fact(item_fact, instance, taxonomy, result);
        }
        Fact::Tuple(tuple_fact) => {
            let tuple_element = validate_tuple_fact(tuple_fact, parent_tuple, taxonomy, result);

            for child in tuple_fact.children() {
                validate_fact(child, tuple_element, instance, taxonomy, result);
            }
        }
    }
}

/// Validates a tuple fact against its concept definition, checking that the
/// fact conforms to the concept's content model.
fn validate_tuple_fact<'a>(
    fact: &TupleFact,
    parent_tuple: Option<&'a Concept>,
    taxonomy: &'a TaxonomySet,
    result: &mut ValidationResult,
) -> Option<&'a Concept> {
    let concept_name = fact.concept_name();

    let Some(concept) = taxonomy.find_concept(concept_name) else {
        result.add(
            Severity::Error,
            "schema.concept_not_found",
            format!(
                "Fact '{}' concept not found in taxonomy",
                concept_name.local_name
            ),
            Some(concept_name.to_string()),
            None,
        );
        return None;
    };

    if let Some(parent_tuple) = parent_tuple
        && let Some(child_concept) = taxonomy.find_concept(concept_name)
    {
        validate_tuple_child(child_concept, parent_tuple, taxonomy, result);
    }

    if !concept.is_tuple() {
        result.add(
            Severity::Error,
            "schema.tuple_requires_tuple_concept",
            format!(
                "Tuple fact reports non-tuple concept '{}'",
                concept_name.local_name
            ),
            Some(concept_name.to_string()),
            None,
        );
        return None;
    }

    if concept.is_abstract() {
        result.add(
            Severity::Error,
            "schema.abstract_concept",
            format!(
                "Fact reports abstract concept '{}'",
                concept_name.local_name
            ),
            Some(concept_name.to_string()),
            None,
        );
    }

    validate_required_tuple_children(fact, concept, taxonomy, result);

    Some(concept)
}

/// Validates that a child concept is allowed under a parent tuple according to
/// the tuple's content model.
fn validate_tuple_child(
    child_concept: &Concept,
    parent_tuple: &Concept,
    taxonomy: &TaxonomySet,
    result: &mut ValidationResult,
) {
    let Some(model) = &parent_tuple.content_model else {
        return;
    };

    if particle_allows_concept(model, &child_concept.name, taxonomy) {
        return;
    }

    result.add(
        Severity::Error,
        "schema.tuple_child_not_allowed",
        format!(
            "Fact '{}' is not allowed as child of tuple '{}'",
            child_concept.name.local_name, parent_tuple.name.local_name
        ),
        Some(child_concept.name.to_string()),
        None,
    );
}

/// Validates that a tuple fact contains required children according to its
/// concept's content model.
fn validate_required_tuple_children(
    fact: &TupleFact,
    concept: &Concept,
    taxonomy: &TaxonomySet,
    result: &mut ValidationResult,
) {
    // A nil tuple has no content; content model constraints do not apply.
    if fact.is_nil() {
        return;
    }

    let Some(model) = &concept.content_model else {
        return;
    };

    validate_particle_children(model, fact, &concept.name.local_name, taxonomy, result);
}

/// Recursively validates occurrence constraints for element particles in a
/// sequence. Choice particles are skipped (any one branch satisfies the model).
fn validate_particle_children(
    particle: &Particle,
    fact: &TupleFact,
    tuple_name: &str,
    taxonomy: &TaxonomySet,
    result: &mut ValidationResult,
) {
    match particle {
        Particle::Sequence { children, .. } => {
            for child in children {
                validate_particle_children(child, fact, tuple_name, taxonomy, result);
            }
        }
        // For xs:choice, any one alternative satisfies; skip per-child checks.
        Particle::Choice { .. } => {}
        Particle::Element { element, occurs } => {
            let allowed_local = element.local_name();
            let count = fact
                .children()
                .iter()
                .filter(|fact| {
                    element_particle_matches_concept(element, fact.concept_name(), taxonomy)
                })
                .count() as u32;

            if count < occurs.min {
                result.add(
                    Severity::Error,
                    "schema.tuple_missing_required_child",
                    format!(
                        "Tuple '{}' requires at least {} occurrence(s) of child '{}' but found {}",
                        tuple_name, occurs.min, allowed_local, count
                    ),
                    Some(tuple_name.to_string()),
                    None,
                );
            }

            if let Some(max) = occurs.max
                && count > max
            {
                result.add(
                    Severity::Error,
                    "schema.tuple_child_not_allowed",
                    format!(
                        "Tuple '{}' allows at most {} occurrence(s) of child '{}' but found {}",
                        tuple_name, max, allowed_local, count
                    ),
                    Some(tuple_name.to_string()),
                    None,
                );
            }
        }
        Particle::Group { .. } => {}
    }
}

/// Returns `true` if any element particle in `model` matches `child_name`,
/// considering substitution groups.
fn particle_allows_concept(
    model: &Particle,
    child_name: &ExpandedName,
    taxonomy: &TaxonomySet,
) -> bool {
    model.elements().iter().any(|element_particle| {
        element_particle_matches_concept(element_particle, child_name, taxonomy)
    })
}

/// Returns `true` if `element_particle` matches the given concept name, either
/// directly or through the substitution group chain.
fn element_particle_matches_concept(
    element_particle: &ElementParticle,
    child_concept_name: &ExpandedName,
    taxonomy: &TaxonomySet,
) -> bool {
    let allowed_local = element_particle.local_name();

    if child_concept_name.local_name == allowed_local {
        return true;
    }

    // Walk substitution group chain.
    let Some(child_concept) = taxonomy.find_concept(child_concept_name) else {
        return false;
    };

    let mut current = &child_concept.substitution_group.original;
    let mut seen = HashSet::new();

    while seen.insert(current) {
        if current.local_name == allowed_local {
            return true;
        }
        let Some(parent) = taxonomy.find_concept(current) else {
            break;
        };
        current = &parent.substitution_group.original;
    }

    false
}

fn validate_item_fact(
    fact: &ItemFact,
    instance: &InstanceDocument,
    taxonomy: &TaxonomySet,
    result: &mut ValidationResult,
) {
    let concept_name = fact.concept_name();
    let ctx_ref = fact.context_ref();

    // 1. Context reference must be valid
    let context = instance.get_context(ctx_ref);
    if context.is_none() {
        result.add(
            Severity::Error,
            "spec.invalid_context_ref",
            format!("Fact '{concept_name}' references unknown context '{ctx_ref}'"),
            Some(concept_name.to_string()),
            Some(ctx_ref.to_string()),
        );
    }

    // 2. Concept must exist in the DTS
    let Some(concept) = taxonomy.find_concept(concept_name) else {
        result.add(
            Severity::Error,
            "schema.concept_not_found",
            format!(
                "Fact concept '{}' not found in taxonomy",
                concept_name.local_name
            ),
            Some(concept_name.to_string()),
            Some(ctx_ref.to_string()),
        );
        return;
    };

    // 3. Abstract elements cannot be reported
    if concept.is_abstract {
        result.add(
            Severity::Error,
            "schema.abstract_concept",
            format!(
                "Fact reports abstract concept '{}'",
                concept_name.local_name
            ),
            Some(concept_name.to_string()),
            Some(ctx_ref.to_string()),
        );
    }

    // 4. Nillable check: if fact is nil, element must be nillable
    if fact.is_nil() && !concept.nillable {
        result.add(
            Severity::Error,
            "schema.nil_not_allowed",
            format!(
                "Fact '{}' is nil but element is not nillable",
                concept_name.local_name
            ),
            Some(concept_name.to_string()),
            Some(ctx_ref.to_string()),
        );
    }

    // 5. Numeric facts: unit reference and decimals/precision constraints
    if concept.data_type.is_numeric() {
        if fact.unit_ref().is_none() {
            result.add(
                Severity::Error,
                "spec.numeric_no_unit",
                format!("Numeric fact '{}' has no unitRef", concept_name.local_name),
                Some(concept_name.to_string()),
                Some(ctx_ref.to_string()),
            );
        }

        let has_decimals = fact.decimals().is_some();
        let has_precision = fact.precision().is_some();
        let acc = DeclaredAccuracy::default();
        let has_declared_accuracy = acc.decimals.is_some() || acc.precision.is_some();

        if has_decimals && has_precision {
            result.add(
                Severity::Error,
                "spec.numeric_decimals_precision_mutual_exclusion",
                format!(
                    "Numeric fact '{}' specifies both decimals and precision",
                    concept_name.local_name
                ),
                Some(concept_name.to_string()),
                Some(ctx_ref.to_string()),
            );
        } else if !fact.is_nil() && !has_decimals && !has_precision && !has_declared_accuracy {
            result.add(
                Severity::Error,
                "spec.numeric_missing_accuracy",
                format!(
                    "Numeric fact '{}' must specify either decimals or precision",
                    concept_name.local_name
                ),
                Some(concept_name.to_string()),
                Some(ctx_ref.to_string()),
            );
        }

        if fact.is_nil() && (has_decimals || has_precision || has_declared_accuracy) {
            result.add(
                Severity::Error,
                "spec.nil_fact_has_accuracy",
                format!(
                    "Nil numeric fact '{}' must not specify decimals or precision",
                    concept_name.local_name
                ),
                Some(concept_name.to_string()),
                Some(ctx_ref.to_string()),
            );
        }

        if !fact.is_nil() && !is_valid_numeric_lexical(fact.value()) {
            result.add(
                Severity::Error,
                "schema.invalid_numeric_lexical",
                format!(
                    "Numeric fact '{}' has invalid lexical value '{}'",
                    concept_name.local_name,
                    fact.value()
                ),
                Some(concept_name.to_string()),
                Some(ctx_ref.to_string()),
            );
        }
    }

    // Non-numeric facts must not have a unitRef
    if !concept.data_type.is_numeric() && fact.unit_ref().is_some() {
        result.add(
            Severity::Error,
            "spec.non_numeric_has_unit",
            format!(
                "Non-numeric fact '{}' must not have unitRef",
                concept_name.local_name
            ),
            Some(concept_name.to_string()),
            Some(ctx_ref.to_string()),
        );
    }

    // Unit reference must resolve if present
    if let Some(unit_ref) = fact.unit_ref()
        && instance.get_unit(unit_ref).is_none()
    {
        result.add(
            Severity::Error,
            "spec.invalid_unit_ref",
            format!(
                "Fact '{}' references unknown unit '{}'",
                concept_name.local_name, unit_ref
            ),
            Some(concept_name.to_string()),
            Some(ctx_ref.to_string()),
        );
    }

    // 6. Period type check
    if let (Some(period_type), Some(context)) = (&concept.period_type, context) {
        let period_matches = matches!(
            (period_type, &context.period),
            (PeriodType::Instant, Period::Instant { .. })
                | (PeriodType::Duration, Period::Duration { .. })
        );
        if !period_matches {
            let actual = match &context.period {
                Period::Instant { .. } => "instant",
                Period::Duration { .. } => "duration",
                Period::Forever => "forever",
            };
            result.add(
                Severity::Error,
                "schema.period_type_mismatch",
                format!(
                    "Fact '{}' requires {:?} period but context '{}' has {}",
                    concept_name.local_name, period_type, ctx_ref, actual
                ),
                Some(concept_name.to_string()),
                Some(ctx_ref.to_string()),
            );
        }
    }

    // 7. Unit semantics for specific item types
    if let Some(unit_ref) = fact.unit_ref()
        && let Some(unit) = instance.get_unit(unit_ref)
    {
        validate_unit_constraints(fact, concept, unit, result);
    }
}

/// Validate context structure and period consistency.
///
/// Checks for prohibited XBRL-instance descendants in segment/scenario,
/// disallowed XBRL substitution-group elements inside context content, and
/// invalid duration period ordering (`startDate < endDate`).
fn validate_contexts(
    instance: &InstanceDocument,
    taxonomy: &TaxonomySet,
    result: &mut ValidationResult,
) {
    for (context_id, context) in instance.contexts() {
        if context.entity.scheme.trim().is_empty() {
            result.add(
                Severity::Error,
                "spec.identifier_missing_scheme",
                format!(
                    "Context '{context_id}' entity identifier is missing required @scheme attribute"
                ),
                None,
                Some(context_id.to_string()),
            );
        }

        if context.segment_has_instance_descendant {
            result.add(
                Severity::Error,
                "spec.segment_contains_xbrli",
                format!(
                    "Context '{context_id}' has a segment descendant in the XBRL instance namespace"
                ),
                None,
                Some(context_id.to_string()),
            );
        }

        if context.scenario_has_instance_descendant {
            result.add(
                Severity::Error,
                "spec.scenario_contains_xbrli",
                format!(
                    "Context '{context_id}' has a scenario descendant in the XBRL instance namespace"
                ),
                None,
                Some(context_id.to_string()),
            );
        }

        for segment_element in context
            .segment_elements
            .iter()
            .chain(context.scenario_elements.iter())
        {
            if let Some(concept) = taxonomy.find_concept(segment_element)
                && (concept.is_item() || concept.is_tuple())
            {
                result.add(
                    Severity::Error,
                    "spec.context_contains_xbrl_item",
                    format!(
                        "Context '{context_id}' contains '{}' which is in an XBRL substitution group",
                        segment_element.local_name
                    ),
                    None,
                    Some(context_id.to_string()),
                );
            }
        }

        if let Period::Duration { start, end } = &context.period
            && !period_order_is_valid(start, end)
        {
            result.add(
                Severity::Error,
                "spec.invalid_period_order",
                format!(
                    "Context '{context_id}' has start date '{start}' that is not earlier than end date '{end}'"
                ),
                None,
                Some(context_id.to_string()),
            );
        }
    }
}

fn validate_unit_constraints(
    fact: &ItemFact,
    concept: &Concept,
    unit: &crate::instance::Unit,
    result: &mut ValidationResult,
) {
    let concept_name = fact.concept_name();
    let context_ref = fact.context_ref();

    if !unit.denominator.is_empty() {
        let mut numerator_counts: HashMap<(&str, &str), usize> = HashMap::new();
        for measure in &unit.numerator {
            let key = (measure.namespace_uri.as_str(), measure.local_name.as_str());
            *numerator_counts.entry(key).or_default() += 1;
        }

        for measure in &unit.denominator {
            let key = (measure.namespace_uri.as_str(), measure.local_name.as_str());
            if let Some(count) = numerator_counts.get(&key)
                && *count > 0
            {
                result.add(
                    Severity::Error,
                    "spec.unit_not_simplest_form",
                    format!(
                        "Fact '{}' uses unit '{}' that is not in simplest form (canceling measure '{}')",
                        fact.concept_name(),
                        unit.id,
                        measure
                    ),
                    Some(concept_name.to_string()),
                    Some(context_ref.to_string()),
                );
                break;
            }
        }
    }

    for measure in unit.numerator.iter().chain(unit.denominator.iter()) {
        if measure.namespace_uri.as_str() == NS_XBRLI
            && measure.local_name != "pure"
            && measure.local_name != "shares"
        {
            result.add(
                Severity::Error,
                "spec.invalid_xbrli_measure_local_name",
                format!(
                    "Fact '{}' uses invalid measure '{}' in XBRL instance namespace",
                    fact.concept_name(),
                    measure
                ),
                Some(concept_name.to_string()),
                Some(context_ref.to_string()),
            );
            return;
        }
    }

    let is_monetary = matches!(concept.data_type, XbrlType::Monetary);
    let is_shares = matches!(concept.data_type, XbrlType::Shares);

    if is_monetary {
        if !unit.has_single_measure_no_divide() {
            result.add(
                Severity::Error,
                "spec.invalid_monetary_unit_shape",
                format!(
                    "Monetary fact '{}' must use exactly one non-divide measure",
                    fact.concept_name()
                ),
                Some(concept_name.to_string()),
                Some(context_ref.to_string()),
            );
            return;
        }

        if let Some(measure) = unit.primary_measure() {
            let is_iso = measure.namespace_uri.as_str() == NS_ISO4217;
            let is_code = measure.local_name.len() == 3
                && measure
                    .local_name
                    .chars()
                    .all(|char| char.is_ascii_uppercase());
            if !is_iso || !is_code {
                result.add(
                    Severity::Error,
                    "spec.invalid_monetary_measure",
                    format!(
                        "Monetary fact '{}' must use ISO4217 currency measure, got '{}'",
                        fact.concept_name(),
                        measure
                    ),
                    Some(concept_name.to_string()),
                    Some(context_ref.to_string()),
                );
            }
        }
    }

    if is_shares {
        if !unit.has_single_measure_no_divide() {
            result.add(
                Severity::Error,
                "spec.invalid_shares_unit_shape",
                format!(
                    "Shares fact '{}' must use exactly one non-divide measure",
                    fact.concept_name()
                ),
                Some(concept_name.to_string()),
                Some(context_ref.to_string()),
            );
            return;
        }

        if let Some(measure) = unit.primary_measure()
            && !(measure.namespace_uri.as_str() == NS_XBRLI && measure.local_name == "shares")
        {
            result.add(
                Severity::Error,
                "spec.invalid_shares_measure",
                format!(
                    "Shares fact '{}' must use xbrli:shares measure, got '{}'",
                    fact.concept_name(),
                    measure
                ),
                Some(concept_name.to_string()),
                Some(context_ref.to_string()),
            );
        }
    }
}

fn period_order_is_valid(start: &str, end: &str) -> bool {
    let start = start.trim();
    let end = end.trim();
    !start.is_empty() && !end.is_empty() && start < end
}

fn is_valid_numeric_lexical(value: &str) -> bool {
    let trimmed = value.trim();
    !trimmed.is_empty()
        && (Decimal::from_str(trimmed).is_ok() || Decimal::from_scientific(trimmed).is_ok())
}