# Validation

Import these from `@grafloria/engine`.

## Functions

### `createConnectionGroupValidator`

Creates a connection validator that enforces connection group restrictions

Rules:
- Nodes in the same connection group can connect to each other
- Nodes with no connection group can connect to any node
- Nodes in different connection groups cannot connect

Usage:
```typescript
const validator = createConnectionGroupValidator(engine);
connectionStateManager.addValidator(validator);
```

```ts
function createConnectionGroupValidator(engine: DiagramEngine)
```

### `isConnectionAllowedByGroup`

Check if a connection between two ports is allowed based on connection groups
This is a standalone function that doesn't require a validator to be registered

```ts
function isConnectionAllowedByGroup(
  sourcePort: PortModel,
  targetPort: PortModel,
  engine: DiagramEngine
): boolean
```

## Classes

### `TypeRegistry`

```ts
class TypeRegistry
```

**Methods**

- `registerNodeType(definition: NodeTypeDefinition): void` — Register a node type
- `registerPortType(definition: PortTypeDefinition): void` — Register a port type
- `registerLinkType(definition: LinkTypeDefinition): void` — Register a link type
- `registerGroupType(definition: GroupTypeDefinition): void` — Register a group type
- `unregisterNodeType(type: string): boolean` — Unregister a node type
- `unregisterPortType(type: string): boolean` — Unregister a port type
- `unregisterLinkType(type: string): boolean` — Unregister a link type
- `unregisterGroupType(type: string): boolean` — Unregister a group type
- `getNodeType(type: string): NodeTypeDefinition | undefined` — Get node type definition
- `getPortType(type: string): PortTypeDefinition | undefined` — Get port type definition
- `getLinkType(type: string): LinkTypeDefinition | undefined` — Get link type definition
- `getGroupType(type: string): GroupTypeDefinition | undefined` — Get group type definition
- `hasNodeType(type: string): boolean` — Check if node type exists
- `hasPortType(type: string): boolean` — Check if port type exists
- `hasLinkType(type: string): boolean` — Check if link type exists
- `hasGroupType(type: string): boolean` — Check if group type exists
- `listNodeTypes(): NodeTypeDefinition[]` — List all node types
- `listPortTypes(): PortTypeDefinition[]` — List all port types
- `listLinkTypes(): LinkTypeDefinition[]` — List all link types
- `listGroupTypes(): GroupTypeDefinition[]` — List all group types
- `clear(): void` — Clear all registered types
- `getStats(): { nodeTypes: number; portTypes: number; linkTypes: number; groupTypes: number; }` — Get type statistics
- `resolveNodeType(type: string): NodeTypeDefinition` — Resolve a node type with all inherited properties
- `getNodeTypesByCategory(category: string): NodeTypeDefinition[]` — Get all node types in a category
- `getNodeTypesByFamily(family: string): NodeTypeDefinition[]` — Get all node types in a family
- `getNodeTypesByTag(tag: string): NodeTypeDefinition[]` — Get all node types with a specific tag
- `getNodeTypesByTemplate(templateId: string): NodeTypeDefinition[]` — Get all node types using a specific template
Includes types that inherit template ID from parent

### `ValidationEngine`

```ts
class ValidationEngine
```

**Methods**

- `constructor(typeRegistry: TypeRegistry, eventBus?: EventBus)`
- `enableRealTimeValidation(): void` — Enable real-time validation
- `disableRealTimeValidation(): void` — Disable real-time validation
- `isRealTimeValidationEnabled(): boolean` — Check if real-time validation is enabled
- `validateDiagram( diagram: DiagramModel, options: ValidationOptions = {} ): ValidationResult` — Validate entire diagram
- `validateNode( node: NodeModel, options: ValidationOptions = {} ): ValidationResult` — Validate a node
- `validatePort( port: PortModel, node: NodeModel, options: ValidationOptions = {} ): ValidationResult` — Validate a port
- `validateLink( link: LinkModel, diagram: DiagramModel, options: ValidationOptions = {} ): ValidationResult` — Validate a link
- `validateHierarchy( node: NodeModel, diagram: DiagramModel, options: ValidationOptions = {} ): ValidationResult` — Validate node hierarchy
- `validateGroup( group: GroupModel, diagram: DiagramModel, options: ValidationOptions = {} ): ValidationResult` — Validate group
- `validateLayout( group: GroupModel, diagram: DiagramModel, options: ValidationOptions = {} ): ValidationResult` — Validate layout configuration
- `addRule( entityType: 'diagram' | 'node' | 'port' | 'link', rule: ValidationRule ): void` — Add custom validation rule
- `clearRules(entityType: 'diagram' | 'node' | 'port' | 'link'): void` — Remove all custom rules for an entity type
- `clearAllRules(): void` — Clear all custom rules
- `getRulesCount(entityType?: 'diagram' | 'node' | 'port' | 'link'): number` — Get custom rules count

## Interfaces

### `GroupTypeDefinition`

```ts
interface GroupTypeDefinition
```

**Properties**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `type` | `string` |  |  |
| `label` | `string` |  |  |
| `description?` | `string` |  |  |
| `allowedMemberTypes?` | `string[]` |  |  |
| `allowedLinkTypes?` | `string[]` |  |  |
| `minMembers?` | `number` |  |  |
| `maxMembers?` | `number` |  |  |
| `canNest?` | `boolean` |  |  |
| `allowedLayoutTypes?` | `('flexbox' \| 'grid')[]` |  |  |
| `requireLayout?` | `boolean` |  |  |
| `validator?` | `(group: any) => ValidationResult` |  |  |

### `LinkTypeDefinition`

```ts
interface LinkTypeDefinition
```

**Properties**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `type` | `string` |  |  |
| `label` | `string` |  |  |
| `description?` | `string` |  |  |
| `allowedSourcePortTypes?` | `string[]` |  |  |
| `allowedTargetPortTypes?` | `string[]` |  |  |
| `validator?` | `(link: any) => ValidationResult` |  |  |

### `NodeTypeDefinition`

```ts
interface NodeTypeDefinition
```

**Properties**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `type` | `string` |  |  |
| `label?` | `string` |  |  |
| `description?` | `string` |  |  |
| `defaultData?` | `Record<string, any>` |  |  |
| `allowedPortTypes?` | `string[]` |  |  |
| `minPorts?` | `number` |  |  |
| `maxPorts?` | `number` |  |  |
| `validator?` | `(node: any) => ValidationResult` |  |  |
| `extends?` | `string` |  |  |
| `category?` | `string` |  |  |
| `family?` | `string` |  |  |
| `tags?` | `string[]` |  |  |
| `defaultBehavior?` | `Partial<NodeBehavior>` |  |  |
| `defaultStyle?` | `Partial<NodeStyle>` |  |  |
| `defaultSize?` | `Partial<Size>` |  |  |
| `allowedChildTypes?` | `string[]` |  |  |
| `allowedParentTypes?` | `string[]` |  |  |
| `maxChildren?` | `number` |  |  |
| `maxDepth?` | `number` |  |  |
| `canBeRoot?` | `boolean` |  |  |
| `templateId?` | `string` |  |  |
| `defaultPortRendering?` | `any` |  |  |

### `PortTypeDefinition`

```ts
interface PortTypeDefinition
```

**Properties**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `type` | `string` |  |  |
| `label` | `string` |  |  |
| `description?` | `string` |  |  |
| `direction` | `'input' \| 'output' \| 'bi'` |  |  |
| `maxConnections?` | `number` |  |  |
| `allowedLinkTypes?` | `string[]` |  |  |
| `validator?` | `(port: any) => ValidationResult` |  |  |

### `ValidationOptions`

```ts
interface ValidationOptions
```

**Properties**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `validateTypes?` | `boolean` |  |  |
| `validateConnections?` | `boolean` |  |  |
| `validatePorts?` | `boolean` |  |  |
| `strict?` | `boolean` |  |  |

## Types

### `ValidationRule`

```ts
type ValidationRule<T = any> = (entity: T) => ValidationResult;
```
