Skip to content

Support for Ordered Unions and/or Matchers as types #1607

@HamishWHC

Description

@HamishWHC

Request a feature

🤷 Motivation

In a lot of cases, I find myself writing schemas that cause ParseErrors: An unordered union of a type including a morph and a type with overlapping input is indeterminate

I know the input overlaps, but I also have the context of my application and know which schema I was to prioritise. Currently, I do this to shoehorn in an ordered union:

const Thing = type({
	myProp: type.unknown.pipe((v) =>
		match
			.case(TypeA, (v) => v)
			.case(TypeB, (v) => v)
			.default("reject")(v)
	)
})

Playground Link w/ Example

But this has the downside of cutting off errors at the wrong "level" (see playground for comparison of errors this gets, vs what I'd expect).

💡 Solution

I would really like some of kind ordered union option, either by allowing us to drop matchers (i.e. match.case().case()) into a type, or by introducing something like type.firstMatch to make clear the first match behaviour.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    To do

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions