@canard/schema-form
Version:
React-based component library that renders forms based on JSON Schema with plugin system support for validators and UI components
43 lines (42 loc) • 1.6 kB
TypeScript
import type { AbstractNode } from '../../../../../core/nodes/AbstractNode';
/**
* Generates a JSON Schema path segment for a node based on its scope and context.
*
* This function handles the complex path generation required for different JSON Schema
* constructs like oneOf/anyOf/allOf unions, object properties, array items, and custom scopes.
*
* @param name - The field name or identifier for the node
* @param scope - The JSON Schema scope type ('oneOf', 'anyOf', 'allOf', 'properties', 'items', or custom)
* @param parentType - The parent node's type, affects path structure for union types
* @param variant - Optional variant index for union types and array items
*
* @returns The constructed path segment starting with JSONPointer separator
*
* @example
* ```typescript
* // Object property in oneOf
* getScopedSegment('username', 'oneOf', 'object', 0)
* // Returns: '/oneOf/0/properties/username'
*
* // Array item in anyOf
* getScopedSegment('item', 'anyOf', 'array', 1)
* // Returns: '/anyOf/1/items'
*
* // Simple property
* getScopedSegment('email', 'properties')
* // Returns: '/properties/email'
*
* // Array items with index
* getScopedSegment('element', 'items', 'array', 2)
* // Returns: '/items/2'
*
* // Empty scope fallback
* getScopedSegment('field', '')
* // Returns: '/field'
*
* // Custom scope with variant
* getScopedSegment('custom', 'myScope', 'object', 1)
* // Returns: '/myScope/1/custom'
* ```
*/
export declare const getScopedSegment: (name: string, scope: string, parentType?: AbstractNode["type"], variant?: number) => string;