(rec: dict, schema: dict[str, type])
| 64 | |
| 65 | # --- 3. explicit schema validation: never write a malformed record to the store |
| 66 | def validate_record(rec: dict, schema: dict[str, type]) -> list[str]: |
| 67 | errs = [] |
| 68 | for field, typ in schema.items(): |
| 69 | if field not in rec: |
| 70 | errs.append(f"missing '{field}'") |
| 71 | elif not isinstance(rec[field], typ): |
| 72 | errs.append(f"'{field}' is {type(rec[field]).__name__}, want {typ.__name__}") |
| 73 | return errs # empty list == valid |
| 74 | |
| 75 | |
| 76 | # --- 4. dedup / entity key |