Consider more specialized support for tagged union types #104
Labels
enhancement
New feature or enhancement for the Ion Schema _specification_
requires new version
Something that should be considered for next version of the Ion Schema Specification
Ion Schema generally has support for union types by using the
any_of
andone_of
constraints. Right now, a tagged union (where the tag is the distinguishing factor) can be modeled like this:When a value doesn't match any variant of the tagged union, the validation error reporting is excessive and hard to read. The violations will describe exactly how the value didn't match every single type in the union. In addition, the Ion Schema implementation must check the value against every possible type in the list, which can become expensive when there are a lot of types. The current syntax functions essentially like this (pseudocode):
With a tagged union, we could be smarter about it. If we could identify which of the variants need to be matched based on the tag, then we could test only that variant and provide violations for only that variant. In addition, we only have to check for
n
possible annotations and the validate against only 1 type, which is cheaper than validating against all of the types. Essentially it would be something like this (pseudocode):Here's a straw-man syntax proposal:
We could introduce a
tagged
modifier to the logic constraints that take a list of types. When usingtagged
, instead of a list, the constraint accepts a struct.Another alternative would be to keep the list, and add tagged sexp around the types in the list. This has the benefit of making it easy to specify what should happen for an untagged value. For example:
In this example, if the value is annotated with
pdf
, it must benull
or a value that matches thepdf_document
type. If the value is annotated withtxt
, it must be a string that is no more than 10000 bytes when encoded as utf8. If the value is annotated withmd
it must be a validmarkdown_document
. If the value is un-annotated, it must benull
.This GitHub issue is far from comprehensive. Open questions include things such as:
one_of
would reject it as invalid, but maybe that would depend on whether it also matches the tagged types? E.g. what if a value was annotated witha
andb
, but it was valid fora_type
and invalid forb_type
?)one_of
,any_of
) and product (all_of
) constraints, or should it only apply to the sum-type constraints? Or should it only apply toone_of
?The text was updated successfully, but these errors were encountered: