Skip to main content

transformValue

function

Combinator that normalizes the value another SerializationSchema parsed, for serialized properties whose accepted domain is wider than the stored one — the motivating case is a legacy shorthand that older documents carry (`format: 'bold'`) being folded into the stored numeric form. `inner` still owns the domain: it validates the untrusted input (falling back to its default as usual), and `transform` then maps every value it can produce into the target domain, so the node's setter only ever sees normalized values. `transform` must be pure and total over `inner`'s outputs: it runs once when the schema is built to derive the SerializationSchema.defaultValue (the transform of `inner`'s default) and once per parsed value. The meta is a `transform` kind holding `inner`, so introspection still reaches the accepted input domain — tooling that generates example JSON keeps generating the legacy forms, which is exactly what a parser test wants to exercise. Like every combinator, this takes a schema that names no accessor; see withAccessors. `inner`'s isEqual is *not* inherited: it compares values of `inner`'s domain, and the transformed domain may be a different type entirely. Pass `{isEqual}` when the output domain is reference-typed, or a transformed array/object property can never compact away and, used as a `createState` parse, dirties its node on every write of an equal value. A comparator passed here is used wherever this schema is used directly, but is *not* consulted when the schema is a unionValue member: a union selects by what a member accepts, and this accepts `inner`'s domain while producing another, so it cannot tell which member made a value and compares structurally instead. The effect is a stricter answer than yours — a value you would call the default is written out rather than compacted away — never a looser one. Only then: an equality is for a domain `===` cannot compare, so declaring one over a primitive output is an error. `===` already answers there, and a comparator can only widen it — call two distinct serialized values equal — after which the compact form omits whichever is not the default and parsing restores the default in its place. A rotation compared modulo 360 serializes `360` as nothing and reads back as `0`. Normalize in the `transform` instead, where the value that reaches storage is the one that round-trips.

function transformValue<Inner, Out, In = Inner>(inner: SerializationSchema<Inner, never, In>, transform: (value: Inner) => Out, options?: { readonly isEqual?: (a: Out, b: Out) => boolean; }): SerializationSchema<Out, never, In>

Import

import { transformValue } from '@modular/sdk/editor';

Notes

example
```ts const parseFormat = transformValue( unionValue( [numberValue(), enumValue(['bold', 'italic', 'underline'])], 0, ), value => (typeof value === 'string' ? TEXT_TYPE_TO_FORMAT[value] : value), ); // ^? SerializationSchema<number> parseFormat(1); // 1 parseFormat('bold'); // IS_BOLD parseFormat('junk'); // 0 (inner falls back to its default) ```
__NO_SIDE_EFFECTS__