Repository navigation
Fix crash when validated data has keys not defined in the schema - #46
Conversation
The recursive traversal in `_validate_instance` looked up every key in the instance with `SchemaView.induced_slot`, guarding only with `if not slot: continue`. But `induced_slot` raises `ValueError` for unknown slots rather than returning a falsy value, so any data key that is not a defined slot on its class (a stray `id`, a metadata field, a typo) crashed the entire validation — including through the full `Validator` pipeline used by the CLI. Skip keys that are not in the class's slots before calling `induced_slot`. Add tests covering an unknown top-level key, an unknown nested key, and the full Validator pipeline. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
Claude finished @cmungall's task —— View job Code Review Complete ✅Todo List:
🎯 SummaryThis is an excellent fix that addresses a critical crash issue with minimal, surgical changes. The implementation follows best practices and includes comprehensive tests. ✅ Code Quality & Best PracticesStrengths:
Implementation Quality: # Only slots actually defined on the class can be induced; data may
# carry extra keys (a stray ``id``, metadata, a typo) that are not
# slots. induced_slot() raises ValueError for those, so skip them.
class_slots = set(self.schema_view.class_slots(class_name))
for slot_name, value in instance.items():
if value is None or slot_name not in class_slots:
continueThe fix is elegant - filtering keys before the problematic 🐛 Bug AnalysisProblem correctly identified: Solution is robust: The pre-filtering approach ensures unknown keys are skipped entirely, preventing crashes while preserving all intended validation behavior. No edge cases identified: The fix handles all scenarios correctly:
🚀 Performance ConsiderationsImprovement over original:
Efficiency: Converting to No performance concerns: The overhead of 🔒 Security AssessmentNo security issues identified:
🧪 Test Coverage ExcellenceComprehensive test suite in
Test quality highlights:
📝 Minor Suggestions
🏆 VerdictStrongly recommend approval. This PR:
The fix demonstrates excellent software engineering practices: identifying root cause, choosing the most appropriate solution approach, implementing minimally, and testing thoroughly. |
Problem
The recursive traversal in
ReferenceValidationPlugin._validate_instancewalks every key of an instance and resolves it withSchemaView.induced_slot, guarding only with:But
induced_slotraisesValueErrorfor unknown slots — it never returns a falsy value. So any data key that isn't a defined slot on its class (a strayid, a metadata field, a typo) crashes the entire validation, including through the fullValidatorpipeline used byvalidate dataon the CLI.Reproduction (a bare
idfield is enough):Fix
Filter keys against the class's declared slots before calling
induced_slot:This avoids a try/except (per the repo's style guidance) and is the minimal change.
Tests
Added
tests/test_unknown_slot_traversal.py(test-first; all three failed with theValueErrorbefore the fix):Validatorpipeline tolerates an unknownidkeyFull non-integration suite: 484 passed.
mypyandruffclean.🤖 Generated with Claude Code