Skip to content

[bug] inconsistent errors for unions with cyclesΒ #1630

Description

@michaelboyles

Adding a field to a union variant which refers to the union affects errors reported for other variants.

πŸ”Ž Search Terms

union, path, key

🧩 Context

  • ArkType version: 2.2.1 (latest)
  • TypeScript version (5.1+): 7

πŸ§‘β€πŸ’» Repro

Playground Link

import { scope, type } from "arktype"

const api = scope({
	Field: {
		type: "'field'",
		value: "string >= 1"
	},
	Group: {
		type: "'group'",
		// comment this out, errors will change
		parts: "Part[]"
	},
	Part: "Field | Group"
}).export()

const Thing = type({
	parts: api.Part.array()
})

const out = Thing({
	parts: [{ type: "field", value: "" }]
})

In this example, I am not validating any Groups. I'm validating a single Field whose value is too short. I expect the issue.code to be minLength. This works as expected when Group does not have the parts: "Part[]" field.

When Group does have the parts: "Part[]" field, the error changes to issue.code="union". This seems wrong. I don't think it should change the behaviour.

I understand that technically because of the invalid constraint, this value is not a match for either Field or Group, and that's why it's reporting the potential choice. But given the discriminant is correct and the structure is correct, the likelihood of this being an invalid Group seems pretty much zero.

The practical impract of this is that I was developing a form with Tanstack Form which contains some unions and the validation errors don't get correct associated with the fields because of this issue.code="union" issue.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

Status
To do

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions