Structure & Data

Polymorphic Record

Represent a family of related entities in a single core schema with type-specific extensions.

Problem

Two naive choices both go wrong. One schema per sub-type duplicates the common fields and forces every client to know about every sub-type; when a new sub-type appears, old clients break or have to be updated in lockstep. A single flat schema that contains every possible field for every sub-type is bloated, hard to validate, and silently allows nonsensical combinations such as a fabric record carrying a yarn weight. The team needs a representation that keeps the common parts common, isolates the per-sub-type fields, and lets old clients survive the addition of a new sub-type.

Solution

Define a core schema with the common fields and a discriminator (e.g. `material_type`). Sub-type fields live in a namespaced extension block (e.g. `yarn: {...}` for yarn-specific). Clients that do not understand a sub-type still read the core fields and round-trip the rest without data loss.

When to use

  • A family of related entities shares a core schema with type-specific extensions.
  • Clients should round-trip unknown sub-types without losing data.
  • A discriminator field can flag the sub-type cleanly.

Open the full interactive page

Diagram, neighbourhood map, code examples, related patterns and full provenance.

Related