@canard/schema-form
Version:
React-based component library that renders forms based on JSON Schema with plugin system support for validators and UI components
42 lines (41 loc) • 1.93 kB
TypeScript
import type { SchemaNode } from '../../../../../core';
/**
* Traverses the schema node tree to find a node that matches the given path segments.
*
* This function implements variant-aware traversal for oneOf scenarios where multiple nodes
* can share the same propertyKey but exist in different variants (oneOf branches).
*
* **Scope Resolution Logic:**
* - When multiple child nodes match the same propertyKey, variant filtering is applied
* - The `next` function determines if a node should be skipped based on variant mismatch
* - Priority is given to nodes where `next(source, node)` returns `false` (variant match)
* - If all matching nodes return `true` from `next`, the first found node is used as fallback
*
* **Special Path Segments:**
* - `JSONPointer.Fragment` - Navigate to root node (`#`)
* - `JSONPointer.Parent` - Navigate to parent node (`..`)
* - `JSONPointer.Current` - Reset cursor to source node (`.`)
* - Regular segments - Match against node's name
*
* **Traversal Behavior:**
* - Stops traversal at terminal nodes (leaf nodes with no subnodes)
* - Returns null if path cannot be resolved
* - Uses tentative matching with fallback for variant conflicts
*
* @param source - The starting node for traversal (used for variant comparison)
* @param segments - Array of path segments to traverse (e.g. ["user", "address", "0", "street"])
* @returns The found node or null if path is invalid or node doesn't exist
*
* @example
* ```ts
* // Navigate to nested property
* const node = traversal(rootNode, ["user", "profile", "name"]);
*
* // Navigate with array index
* const arrayItem = traversal(rootNode, ["items", "0", "title"]);
*
* // Navigate to parent then specific child
* const sibling = traversal(currentNode, [JSONPointer.Parent, "sibling"]);
* ```
*/
export declare const traversal: (source: SchemaNode, pointer: string | string[] | null) => SchemaNode | null;