unionValue
functionCombinator for a value whose domain is the union of several schemas, such as a dimension that is either a number or the literal `'inherit'`. The domain is inferred as the union of the members' value types; annotate the result when you want to assert a narrower intended domain instead. A SerializationSchema is total — it always returns a value, falling back to its own default rather than reporting a rejection — so a member is considered to accept `value` when parsing it lands anywhere *other* than that member's default, or when the value is itself that default (the one case a total schema cannot distinguish from a fallback). Selection is in two passes. The first asks every member whether it accepts the value *entirely* — every element of an array, every declared field of an object — and the first such member wins. Only if none does are the members asked again for a partial match, where the first accepting one wins and the union yields what it parsed. So a member that normalizes its input (numberValue reading a stringified number) composes here the same way it behaves alone, and a value that belongs entirely to a later member is not taken by an earlier one that would only partly coerce it — declaration order decides between members that fit equally well, not between a complete fit and a partial one. If no member accepts at all, the result is `defaultValue` when given, otherwise the first member's default. The inference above is only the fallback. A member that declares its own domain — which every combinator here does — is asked directly, and that is the only way to recognize a value it normalizes *into* its own default (`numberValue()` reading `'0'`). A member whose `defaultValue` lies outside its own constrained domain (`numberValue(0, {min: 1})`) is therefore declined for that value rather than accepting it, and the union falls through to the next member. The result is itself a member of the union in both respects: it declares an `accepts` that asks each member in turn, so a union nested in another union (or reached through a wrapper) keeps its domain, and an `isEqual`, so a union over a reference-typed member still compares by content. That equality is a structural comparison, not a member's own: a union picks a member by what each *accepts*, and transformValue accepts one domain and produces another, so which member produced a value is not something a union can recover. arrayValue and objectValue compare element-wise and field-wise, which is what this does, so a union over either is unaffected. A **custom `isEqual` passed to `transformValue` is not consulted through a union** — two values it would call equal are reported as different, so a property holding one is written out instead of compacted away, `optional({omitDefault})` around the union keeps it instead of dropping it, and as a `createState` parse its `NodeState.toJSON()` writes the value rather than omitting it, `$getStateChange` reports a change, and an updater-form `$setState` performs the write. (A plain-value `$setState` compares nothing either way.) Never the reverse, which would discard the difference. Outside a union the comparator is used as declared.
Import
import { unionValue } from '@modular/sdk/editor';Notes
- example
- ```ts const parseDimension = unionValue([numberValue(), enumValue(['inherit'])], 'inherit'); // ^? SerializationSchema<number | 'inherit'> parseDimension(640); // 640 parseDimension('640'); // 640 (numberValue reads a stringified number) parseDimension('inherit'); // 'inherit' parseDimension('banana'); // 'inherit' (no member accepts it) ```
- __NO_SIDE_EFFECTS__