[docs]defverify_legal_reference(reference:LegalReference,*,source_root:Path|None=None,)->None:"""Verify one already parsed legal reference is filing-grade. Type-system invariants enforced by the Pydantic schema (and thus NOT re-checked here): - ``review_status`` is ``Literal["reviewed"]`` — the type makes any other value unrepresentable, so a prior runtime check ``if reference.review_status != "reviewed"`` was structurally unreachable dead code. - ``evidence_tier`` on ``LegalReference`` is ``Literal["legal_authority"]`` — same dead-branch reasoning. - ``corpus_ref`` matches the ``path#anchor`` shape — the ``LegalReference`` model validator rejects malformed refs at parse time, so the defensive ``corpus_ref.split("#", 1)[0]`` in :func:`_legal_corpus_text` will always find a non-empty path. This function therefore only checks runtime invariants that the type system cannot express: known-bad citation patterns and required-text presence against the local corpus. """ifreference.kind=="manual":path_text=reference.corpus_ref.split("#",1)[0]ifsource_rootisnotNone:path=(source_root/path_text).resolve()ifpath.is_file():try:from...manualsimportSectionSection.model_validate_json(path.read_text(encoding="utf-8"))exceptExceptionasexc:raiseRegistryValidationError(f"legal reference {reference.id!r} manual section JSON validation failed: {exc}",)fromexcifreference.required_textandsource_rootisnotNone:corpus_text=_legal_corpus_text(source_root,reference)forrequiredinreference.required_text:ifnormalise_corpus_text(required)notincorpus_text:raiseRegistryValidationError(f"legal reference {reference.id!r} corpus text missing required text {required!r}",)ifreference.articleisNone:returnsource=_SOURCE_BY_KIND.get(reference.kind)ifsourceisNone:returnrole_text=" ".join(partforpartin(reference.section,reference.notes)ifpart)ifrole_textand(known_bad:=find_known_bad(source,reference.article,role_text)):raiseRegistryValidationError(f"legal reference {reference.id!r} matches known-bad citation: {known_bad.reason}",)
[docs]defverify_legal_catalogue(legal:Mapping[str,LegalReference],*,source_root:Path|None=None,)->None:"""Verify every legal reference in a shared legal catalogue."""failures:list[str]=[]forref_id,referenceinlegal.items():ifref_id!=reference.id:failures.append(f"legal catalogue key {ref_id!r} does not match reference id {reference.id!r}")try:verify_legal_reference(reference,source_root=source_root)exceptRegistryValidationErrorasexc:failures.append(str(exc))iffailures:raiseRegistryValidationError("legal catalogue validation failed:\n"+"\n".join(f" - {f}"forfinfailures))