v0.48.0 — SLICE-2: collection field schema + central validation (§22.4a)

Collections declare a `fields:` schema in `.collection.yaml`; entries carry
typed metadata (enum/tags/text). New central `metadata_schema` module parses
the schema leniently (INV-3) and validates entry values — advisory at read
(corpus mirror flags violations as `metadata_malformed` without hard-failing),
the enforcement point for the write boundary (edit endpoints land SLICE-4/5).

- app/metadata_schema.py: parse_fields (lenient/normalizing) + validate
- registry.parse_collection_manifest reads `fields:` into collection config
- collections.get_collection unpacks `fields`; served by the collection API
- cache._refresh_collection_corpus validates each entry advisory-only

Non-breaking, opt-in: no `fields:` → unchanged (INV-5); default `document`
collection declares none (N=1 unchanged). No DB migration — schema rides in
collections.config_json. SLICE-2 of
docs/design/2026-06-06-configurable-collection-metadata.md §7.2.

Backend suite green (601 passed).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ben Stull
2026-06-07 17:06:04 -07:00
parent 98c276a662
commit e336e31812
12 changed files with 616 additions and 3 deletions
+56
View File
@@ -6,6 +6,7 @@ Per docs/design/2026-06-06-configurable-collection-metadata.md §6.2-6.3.
from __future__ import annotations
import asyncio
import json
from fastapi.testclient import TestClient
@@ -105,6 +106,61 @@ def test_malformed_sidecar_on_migrated_entry_still_loads_flagged(app_with_fake_g
assert row["body"].strip() == "Just the body, no frontmatter."
def _set_default_fields_schema(schema):
# apply_registry leaves the default collection's config_json untouched, so a
# schema set here survives a corpus refresh (refresh_meta_repo only).
db.conn().execute(
"UPDATE collections SET config_json = ? WHERE id = 'default'",
(json.dumps({"fields": schema}),))
# ---- §22.4a SLICE-2: advisory schema validation at ingest (INV-3) ----
def test_schema_violation_flags_malformed(app_with_fake_gitea):
app, fake = app_with_fake_gitea
with TestClient(app):
_set_default_fields_schema(
{"priority": {"type": "enum", "values": ["P0", "P1", "P2"]}})
fake.files[("wiggleverse", "meta", "main", "rfcs/bad-prio.md")] = {
"content": "---\nslug: bad-prio\ntitle: Bad\nstate: active\npriority: P9\n---\n\nB.\n",
"sha": "bp1"}
fake.files[("wiggleverse", "meta", "main", "rfcs/good-prio.md")] = {
"content": "---\nslug: good-prio\ntitle: Good\nstate: active\npriority: P0\n---\n\nB.\n",
"sha": "gp1"}
_refresh()
assert _row("bad-prio")["metadata_malformed"] == 1 # INV-3: flagged
assert _row("bad-prio")["title"] == "Bad" # still loads
assert _row("good-prio")["metadata_malformed"] == 0
def test_schema_ignores_undeclared_keys(app_with_fake_gitea):
# INV-7: keys the schema doesn't declare ride along and never flag malformed.
app, fake = app_with_fake_gitea
with TestClient(app):
_set_default_fields_schema(
{"priority": {"type": "enum", "values": ["P0", "P1"]}})
fake.files[("wiggleverse", "meta", "main", "rfcs/extra-key.md")] = {
"content": "---\nslug: extra-key\ntitle: Extra\nstate: active\nowner: hasan\n---\n\nB.\n",
"sha": "ek1"}
_refresh()
assert _row("extra-key")["metadata_malformed"] == 0
def test_no_schema_never_flags(app_with_fake_gitea):
# INV-5: a collection with no field schema validates nothing, even when an
# entry carries values that would fail a schema if one existed.
app, fake = app_with_fake_gitea
with TestClient(app):
fake.files[("wiggleverse", "meta", "main", "rfcs/anything.md")] = {
"content": "---\nslug: anything\ntitle: Any\nstate: active\npriority: whatever\n---\n\nB.\n",
"sha": "an1"}
_refresh()
assert _row("anything")["metadata_malformed"] == 0
def test_legacy_collection_without_sidecars_unchanged(app_with_fake_gitea):
app, fake = app_with_fake_gitea
with TestClient(app):