Skip to content

Reject nested collections with a clear error - #245

Merged
endel merged 1 commit into
colyseus:masterfrom
vtxe02023274:nested-collection-error
Oct 20, 2026
Merged

endel merged 1 commit into
colyseus:masterfrom
vtxe02023274:nested-collection-error

Conversation

@vtxe02023274

Copy link
Copy Markdown
Contributor

Closes #212

A collection of collections ({ map: { map: "number" } }, { map: { array: "string" } }, [["string"]]) used to be accepted by @type() and then fail later with an unrelated error. Map of maps threw Right-hand side of 'instanceof' is not callable on the first set(), and array of arrays did not throw at all until encoding.

getNormalizedType() now rejects a collection whose child is another collection, and @type() prefixes the class and field, so the error points at the definition:

State.scores: @colyseus/schema: nested collections are not supported ("map" of "map"). Collections can only hold primitive types or Schema classes, wrap the inner map in a Schema class.

t.* builders already rejected builders as element types, so this covers the decorator path. Collections of Schema classes are unchanged.

Added tests in Definition.test.ts. npm run test:mocha (1054 passing) and npm run test:types pass locally.

@endel
endel merged commit 72797bd into colyseus:master Oct 20, 2026
endel added a commit that referenced this pull request Oct 20, 2026
#245 prefixed the field by wrapping @type()'s normalization in a
try/catch, so Metadata.setFields() and schema() threw without one, and
unrelated errors (bad quantize options) got re-wrapped. getNormalizedType()
takes the label instead, and checks once on the normalized result: a
nested collection is always the outermost collection's direct child.

Assisted-by: Claude Opus 5.5
@endel

endel commented Oct 20, 2026

Copy link
Copy Markdown
Member

Thank you so much for the PR @vtxe02023274 - I've applied a few fixes on top of it and released @colyseus/schema version 5.0.42 🎉

endel added a commit that referenced this pull request Nov 10, 2026
…t of 5.0.42)

Port of master b0367f9, c9bb58f (#245) and 2b1e8b2. addField() keeps
normalizing on v6: Reflection hands it raw collection descriptors, and
normalization is what tags them with $collectionCtor and validates the map
key type.

Assisted-by: Claude Opus 5.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: better error message when trying to use nested maps of maps, maps of arrays, etc

2 participants