def validate_packet(packet: dict[str, Any]) -> None: version = packet.get("schema_version", 1) if version not in (1, 2): raise PacketError("unsupported packet schema_version") if ("reader_link_scheme" in packet and packet["reader_link_scheme"] != CHRONOLOGICAL_LINK_SCHEME): raise PacketError("reader_link_scheme must be chronological-v1 when present") if "edition_kind" in packet and ( version != 2 or packet["edition_kind"] != "retrospective" ): raise PacketError("edition_kind must be retrospective on a v2 packet") foundation = version == 2 and packet.get("foundation") is True packet_id = _required_text(packet.get("packet_id"), "packet_id") if len(packet_id) > 120: raise PacketError("packet_id is too long") start = date.fromisoformat(_required_text(packet.get("week_start"), "week_start")) end = date.fromisoformat(_required_text(packet.get("week_end"), "week_end")) if start.weekday() != 0 or (end - start).days != 6: raise PacketError("issue week must run from Monday through Sunday") for locale in ("fr", "en"): localized = packet.get(locale) if not isinstance(localized, dict): raise PacketError(f"{locale} copy is required") _required_text(localized.get("intro_essay"), f"{locale}.intro_essay") stories = packet.get("stories") if not isinstance(stories, list) or (not foundation and not stories): raise PacketError("a news packet must contain at least one story") slugs: set[str] = set() for index, story in enumerate(stories): if not isinstance(story, dict): raise PacketError(f"stories[{index}] must be an object") slug = _required_text(story.get("slug"), f"stories[{index}].slug") if slug in slugs: raise PacketError(f"duplicate story slug: {slug}") slugs.add(slug) if foundation: for locale in ("fr", "en"): block = story.get(locale) or {} for field in ("title", "summary", "context"): _required_text(block.get(field), f"foundation.{locale}.{field}") continue context_counts: dict[str, int] = {} for locale in ("fr", "en"): copy_block = story.get(locale) if not isinstance(copy_block, dict): raise PacketError(f"{slug}.{locale} copy is required") for field in ("title", "summary", "plain_take", "context"): _required_text(copy_block.get(field), f"{slug}.{locale}.{field}") words = _word_count(copy_block.get("plain_take")) if words > PLAIN_TAKE_MAX_WORDS: raise PacketError( f"{slug}.{locale}.plain_take has {words} words; " f"maximum is {PLAIN_TAKE_MAX_WORDS}" ) context_words = _word_count(copy_block.get("context")) context_counts[locale] = context_words if context_words > CONTEXT_MAX_WORDS: raise PacketContextLengthError( f"{slug}.{locale}.context has {context_words} words; " f"maximum is {CONTEXT_MAX_WORDS}" ) movements = copy_block.get("movements") if not isinstance(movements, dict): raise PacketError(f"{slug}.{locale}.movements is required") for movement in editions.MOVEMENT_KEYS: _required_text( movements.get(movement), f"{slug}.{locale}.movements.{movement}" ) thesis = copy_block.get("thesis") if not isinstance(thesis, dict): raise PacketError(f"{slug}.{locale}.thesis is required") for field in ("text", "signpost", "breaks_if"): _required_text(thesis.get(field), f"{slug}.{locale}.thesis.{field}") red_thread = copy_block.get("red_thread") if not isinstance(red_thread, dict): raise PacketError(f"{slug}.{locale}.red_thread is required") for field in ("previous", "current", "next"): _required_text( red_thread.get(field), f"{slug}.{locale}.red_thread.{field}" ) evidence = story.get("evidence") if not isinstance(evidence, list) or not evidence: raise PacketError(f"{slug} needs inspectable evidence") for evidence_index, item in enumerate(evidence): if not isinstance(item, dict): raise PacketError(f"{slug}.evidence[{evidence_index}] must be an object") for field in ("title", "url", "source_name", "summary"): _required_text( item.get(field), f"{slug}.evidence[{evidence_index}].{field}" ) dossier = story.get("dossier") if not isinstance(dossier, dict): raise PacketError(f"{slug}.dossier is required") movement = _required_text(dossier.get("movement"), f"{slug}.dossier.movement") if movement not in ALLOWED_MOVEMENTS: raise PacketError(f"{slug}: invalid dossier movement {movement!r}") continuity = dossier.get("continuity") if not isinstance(continuity, dict): raise PacketError(f"{slug}.dossier.continuity is required") decision = _required_text( continuity.get("decision"), f"{slug}.dossier.continuity.decision" ) _required_text(continuity.get("reason"), f"{slug}.dossier.continuity.reason") if decision not in {"new", "continue"}: raise PacketError(f"{slug}: continuity decision must be new or continue") previous_story = str(continuity.get("previous_story_slug") or "").strip() if decision == "new" and ( movement != "opened" or dossier.get("existing_slug") or previous_story ): raise PacketError( f"{slug}: a new dossier must be opened without an existing dossier or previous story" ) proposed_slug = str(dossier.get("slug") or "").strip() if proposed_slug: if decision != "new": raise PacketError( f"{slug}: only a new dossier may propose a slug; use existing_slug to continue one" ) legacy_url = LEGACY_DOSSIER_SLUGS.get(slug) == proposed_slug if ( len(proposed_slug) > DOSSIER_SLUG_MAX_LENGTH and not legacy_url ) or not DOSSIER_SLUG_PATTERN.match(proposed_slug): raise PacketError( f"{slug}: dossier slug {proposed_slug!r} must be short lowercase ASCII words joined by hyphens" ) if decision == "continue" and ( movement == "opened" or not (dossier.get("existing_slug") or previous_story) ): raise PacketError( f"{slug}: a continuing story needs an existing dossier or previous story and a non-opened movement" ) minimum_context = _dossier_context_min_words(decision) for locale, context_words in context_counts.items(): if context_words < minimum_context: raise PacketContextLengthError( f"{slug}.{locale}.context has {context_words} words; " f"a {decision} dossier needs at least {minimum_context}" ) if not dossier.get("existing_slug"): for field in DOSSIER_TEXT_FIELDS: _required_text(dossier.get(field), f"{slug}.dossier.{field}") for locale in ("fr", "en"): afterword = packet.get(locale, {}).get("sucre") if foundation and not afterword: continue if not isinstance(afterword, dict): raise PacketError(f"{locale}.sucre is required") for field in ( "title", "afterword", "target", "mechanism_exposed", "closing_line", ): _required_text(afterword.get(field), f"{locale}.sucre.{field}") words = _word_count(afterword.get("afterword")) if not sucre.AFTERWORD_MIN_WORDS <= words <= sucre.AFTERWORD_MAX_WORDS: raise PacketError( f"{locale}.sucre.afterword has {words} words; expected " f"{sucre.AFTERWORD_MIN_WORDS}-{sucre.AFTERWORD_MAX_WORDS}" ) evidence_ids = afterword.get("evidence_ids") if not isinstance(evidence_ids, list) or ( not foundation and len(set(evidence_ids)) < 2 ): raise PacketError(f"{locale}.sucre needs at least two evidence receipts") voice_checks = afterword.get("voice_checks") if version == 1 and ( not isinstance(voice_checks, dict) or any(voice_checks.get(check) is not True for check in SUCRE_VOICE_CHECKS) ): raise PacketError( f"{locale}.sucre.voice_checks must confirm: " + ", ".join(SUCRE_VOICE_CHECKS) ) if version == 2: _validate_v2(packet) review_metadata = [] for locale in ("fr", "en"): reviews = (packet[locale].get("sucre") or {}).get("forecast_reviews", []) _validate_forecast_reviews( reviews, packet.get("research_as_of") or packet["week_end"] ) review_metadata.append( [ (review["status"], review["assessed_at"], review["source_url"]) for review in reviews ] ) if review_metadata[0] != review_metadata[1]: raise PacketError( "Sucre forecast reviews need matching bilingual status, date and source" )