Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions ark/schema/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -755,15 +755,22 @@ export class SchemaScope<$ extends {} = {}> extends BaseScope<$> {
const bootstrapAliasReferences = (resolution: BaseRoot | GenericRoot) => {
const aliases = resolution.references.filter(node => node.hasKind("alias"))
for (const aliasNode of aliases) {
Object.assign(aliasNode.referencesById, aliasNode.resolution.referencesById)
addReferences(aliasNode.referencesById, aliasNode.resolution.referencesById)
for (const ref of resolution.references) {
if (aliasNode.id in ref.referencesById)
Object.assign(ref.referencesById, aliasNode.referencesById)
addReferences(ref.referencesById, aliasNode.referencesById)
}
}
return resolution
}

const addReferences = (
base: BaseNode["referencesById"],
references: BaseNode["referencesById"]
) => {
for (const id in references) base[id] ??= references[id]
}

const resolutionsToJson = (resolutions: InternalResolutions): JsonStructure =>
flatMorph(resolutions, (k, v) => [
k,
Expand Down
15 changes: 15 additions & 0 deletions ark/type/__tests__/realWorld.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1531,6 +1531,21 @@ date3 must be a parsable date (was "")`)
attest(result).equals({})
})

// https://github.com/arktypeio/arktype/issues/1640
it("cyclic discriminated union with record reference", () => {
const Node = scope({
Number: { type: "'number'" },
Array: { type: "'array'", item: "Node" },
Unit: { type: "'unit'" },
Container: { things: "Record<string, Node>" },
Node: "Number | Array | Unit"
}).export().Node

const data = { type: "array", item: { type: "number" } } as const

attest(Node(data)).equals(data)
})

// https://github.com/arktypeio/arktype/issues/1284
it("cyclic discriminated union issue 4", () => {
const ruleset = scope({
Expand Down
Loading