Unions

The UnionType paired with SchemaType::Union can be used to represent a list of heterogeneous schema types (variants), in which a value must match one or more of the types.

#![allow(unused)]
fn main() {
use schematic::{Schematic, Schema, SchemaBuilder, SchemaType, schema::{UnionType, UnionOperator}};

impl Schematic for T {
	fn build_schema(mut schema: SchemaBuilder) -> Schema {
		schema.union(UnionType {
			operator: UnionOperator::AnyOf,
			variants_types: vec![
				Box::new(schema.infer::<String>()),
				Box::new(schema.infer::<bool>()),
				Box::new(schema.nest().integer(IntegerType::new_kind(IntegerKind::U32))),
			],
			..UnionType::default()
		})
	}
}
}

If you’re only defining the variants_types field, you can use the shorthand UnionType::new_any() (any of) or UnionType::new_one() (one of) methods. When using this approach, the Boxs are automatically inserted for you.

#![allow(unused)]
fn main() {
// Any of
schema.union(UnionType::new_any([
	schema.infer::<String>(),
	schema.infer::<bool>(),
	schema.nest().integer(IntegerType::new_kind(IntegerKind::U32)),
]));

// One of
schema.union(UnionType::new_one([
	// ...
]));
}

Operators

Unions support 2 kinds of operators, any of and one of, both of which can be defined with the operator field.

  • Any of requires the value to match any of the variants.
  • One of requires the value to match only one of the variants.
#![allow(unused)]
fn main() {
UnionType {
	// ...
	operator: UnionOperator::OneOf,
}
}