Lab 02 — Schema Compatibility & Data Contracts in Scala
Phase: 03 — Serialization, Schema & Data Contracts | Difficulty: ⭐⭐⭐⭐☆ | Time: 4–5 hours
The Scala twin of Lab 01's Python registry — the schema-governance logic a JVM platform team ships as a library. Compatibility checking (backward/forward/full + Protobuf field-number rules) plus Cats
Validatedrecord validation that accumulates every violation in one pass. Verified with ScalaTest viasbt test.
What you build
Schema/Field— a schema model with Protobuf field numbers and thehasDefaultrule that makes add/remove safety mechanicalCompatibility—backwardViolations,forwardViolations,compatibilityViolations(none/backward/forward/full), andprotobufViolations(reused tag + changed type = silent byte corruption)Validation.validateRecord— CatsValidatedNelvalidation accumulating missing-required, type-mismatch, and semantic-rule failures (a producer fixes them all at once)
Run
sbt test
First run downloads Scala 2.13 + Cats + ScalaTest, then compiles and runs. Expected:
11 tests, all green across CompatibilitySpec and ValidationSpec.
Key concepts (Scala expression)
| Concept | Scala |
|---|---|
| Backward compat | new reader, old data → added field needs a default |
| Forward compat | old reader, new data → removed field needed a default |
| Protobuf tags | field NUMBER is the wire identity; reuse+retype corrupts bytes |
| Accumulating validation | Validated mapN/fold collects all errors (vs Either fail-fast) |
Success criteria
sbt test→ all 11 specs pass.- You can explain why a renamed field with the same number+type is Protobuf-safe but a retyped reused number is catastrophic.
- You can explain why
validateRecordusesValidated(accumulate) notEither(fail-fast).
Extensions
- Add enum evolution rules (adding a symbol is backward-safe; removing isn't).
- Add a
CI gatefunction:assertCompatible(old, new, mode)that throws with all violations — the pre-merge check (P03 WARMUP Ch. 7). - Replace
typ: Stringwith a sealedFieldTypeADT for exhaustive, typo-proof matching (P12's make-illegal-states-unrepresentable).