Skip to main content

nodeSchema

function

A node's serialization schema, checked against the node it is for. The same shape objectValue takes, with one type argument naming the node — which is what lets every `field`, accessor `method` and `when` predicate be verified to exist. A name the node does not have is a compile error at the property that declares it, with the correction suggested: ```ts const codeNodeSchema = nodeSchema<CodeNode>()({ language: withField(optional(nullable(stringValue())), { field: '__langauge', }), }); // ~~~~~~~~~~~~ // Type '"field:__langauge"' is not assignable to type '... | TaggedNamesOf<CodeNode> | ObligationsOf<CodeNode>'. // Did you mean '"field:__language"'? ``` Where the schema is written does not change what is checked: a module-scope `const` above the class — a class's *type* is in scope before its definition, and this is what every built-in node does — or inline in `$config()`, as `TabNode` spells it. Checking a declaration means resolving the class's members, and an unannotated `$config()` has a return type inferred from this very schema; the members whose types come from it are skipped (`ScannableKeys`), and what a setter returns is compared against the brand every node carries rather than all of `LexicalNode` (`SetterReturn`), so that neither position asks the check for its own answer. The result reports no outstanding names, which is what `$config`'s `json` requires — so a schema that names anything has to come through here, and the check cannot be skipped by declaring the properties some other way.

function nodeSchema<N extends Checkable<N>>(): <const F extends { readonly [key: string]: SerializationSchema<unknown, MemberOf<N>, unknown>; }>(fields: F & Conventional<N, F>) => NodeSerializationSchema<N, { readonly [K in keyof F]?: SchemaInput<F[K]>; }>

Import

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

Notes

__NO_SIDE_EFFECTS__