feat: let an entity declare another entity as a field - #14
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
Enables entity aggregates to declare other entities’ .instance schemas as fields (including in arrays), so nested entities remain real instances with behaviour, computed fields, and _tag, while keeping validation paths and JSON round-tripping intact.
Changes:
- Relax
shape()’s type-levelOnlyNominalgate to accept entity instances (identified structurally), allowingCustomer.instanceto be used as a field type. - Add
_tagto the staticinstance/~standardTypeScript surface to match runtime behaviour and support typed tag-based matching. - Add a dedicated
nesting.spec.tsruntime test suite plus type-level assertions; update READMEs and publish a minor changeset.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| README.md | Updates docs to describe aggregates as entities and show nested entity behaviour. |
| packages/entity/src/types.ts | Extends instance / ~standard inferred types to include _tag. |
| packages/entity/src/shape.ts | Updates OnlyNominal to treat entity instances as acceptable domain fields and improves rejection message readability. |
| packages/entity/src/shape.test-d.ts | Adds type-level checks covering nested entity fields and _tag presence/read-only behaviour. |
| packages/entity/src/nesting.spec.ts | Adds runtime tests pinning nesting, invariants across entities, issue paths, JSON round-trips, and freeze behaviour. |
| packages/entity/README.md | Updates package docs to reflect instance as a valid field for aggregates. |
| .changeset/nest-entities.md | Declares a minor release and summarizes the new nesting capability and error-message improvement. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The README's fourth pillar — "something that nests inside other entities" —
did not work.
OnlyNominalrejectedCustomer.instance, so an aggregate hadto be a plain
z.object(...)and lostmake,update, invariants,immutability and
_tag. What "nested" was a schema, not an entity.What works, all pinned by tests
_tagand
equals["customer", "name"]JSON.stringifywalks the tree to plain data, and the result feeds backthrough
makeHow an entity is recognised
Structurally, on the three members every entity instance has (
toJSON,equals,update) — not againstBaseInstanceitself. That interface isgeneric in the entity's own shape and there is no argument matching every
entity:
neveris too narrow to match any (measured — it rejected the fieldoutright), and the field map has no way to name the specific one.
instancenow carries_tagIts type was
BaseInstance & DeepReadonly<Output>, omitting the_tagtheruntime has always set. Nested pattern matching with
P.tag(...)did nottypecheck. Fixed, and pinned in
shape.test-d.ts.The error message
Rejecting a genuinely unbranded field used to produce a tuple TypeScript
truncated to
& [...], hiding the advice behind a wall ofZodType<BaseInstance<…>>:The name is the message now, so it survives truncation. (Written as a
typerather than an
interfaceforconsistent-type-definitions; verified thealias still prints by name rather than being expanded.)
Gate
format --check,lint,typecheck(three passes),test(103, 10 files),knip,build— all green.