arktype icon indicating copy to clipboard operation
arktype copied to clipboard

Ensure cyclic aliases are serialized with a scope reference

Open ssalbdivad opened this issue 9 months ago • 0 comments

Currently, because JSON for a cyclic type references only its alias like $a, if two scopes had differing definitions for the same cyclic alias, caching could break.

Really this should use the id that is precomputed when parsing a node, but I want to ensure that is done in a way that doesn't arbitrarily introduce unnecessary barriers to serialization.

Maybe scopes should should have a globally unique id, then an alias could be ${scodeId}$${alias} or similar.

Failing test case:

				it("intersect cyclic reference with repeat name", () => {
					const types = scope({
						arf: {
							a: "bork"
						},
						bork: {
							b: "arf&bork"
						}
					}).export()

					const resolveRef: string = (
						types.bork.raw.firstReferenceOfKindOrThrow("alias").json as any
					).resolve

					attest(types.bork.json).snap({
						required: [
							{ key: "b", value: { resolve: resolveRef, alias: "$arf&bork" } }
						],
						domain: "object"
					})

					attest(types.arf.json).snap({
						required: [
							{
								key: "a",
								value: types.bork.json
							}
						],
						domain: "object"
					})

					attest(types.arf({ a: { b: {} } }).toString())
						.snap(`a.b.a must be { b: arf&bork } (was missing)
		a.b.b must be arf&bork (was missing)`)
				})

ssalbdivad avatar Apr 27 '24 00:04 ssalbdivad