Spaces:
Sleeping
Sleeping
File size: 6,239 Bytes
da2e594 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | /**
* Type Structure Definitions
*
* Defines the structure and validation rules for n8n node property types.
* These structures help validate node configurations and provide better
* AI assistance by clearly defining what each property type expects.
*
* @module types/type-structures
* @since 2.23.0
*/
import type { NodePropertyTypes } from 'n8n-workflow';
/**
* Structure definition for a node property type
*
* Describes the expected data structure, JavaScript type,
* example values, and validation rules for each property type.
*
* @interface TypeStructure
*
* @example
* ```typescript
* const stringStructure: TypeStructure = {
* type: 'primitive',
* jsType: 'string',
* description: 'A text value',
* example: 'Hello World',
* validation: {
* allowEmpty: true,
* allowExpressions: true
* }
* };
* ```
*/
export interface TypeStructure {
/**
* Category of the type
* - primitive: Basic JavaScript types (string, number, boolean)
* - object: Complex object structures
* - array: Array types
* - collection: n8n collection types (nested properties)
* - special: Special n8n types with custom behavior
*/
type: 'primitive' | 'object' | 'array' | 'collection' | 'special';
/**
* Underlying JavaScript type
*/
jsType: 'string' | 'number' | 'boolean' | 'object' | 'array' | 'any';
/**
* Human-readable description of the type
*/
description: string;
/**
* Detailed structure definition for complex types
* Describes the expected shape of the data
*/
structure?: {
/**
* For objects: map of property names to their types
*/
properties?: Record<string, TypePropertyDefinition>;
/**
* For arrays: type of array items
*/
items?: TypePropertyDefinition;
/**
* Whether the structure is flexible (allows additional properties)
*/
flexible?: boolean;
/**
* Required properties (for objects)
*/
required?: string[];
};
/**
* Example value demonstrating correct usage
*/
example: any;
/**
* Additional example values for complex types
*/
examples?: any[];
/**
* Validation rules specific to this type
*/
validation?: {
/**
* Whether empty values are allowed
*/
allowEmpty?: boolean;
/**
* Whether n8n expressions ({{ ... }}) are allowed
*/
allowExpressions?: boolean;
/**
* Minimum value (for numbers)
*/
min?: number;
/**
* Maximum value (for numbers)
*/
max?: number;
/**
* Pattern to match (for strings)
*/
pattern?: string;
/**
* Custom validation function name
*/
customValidator?: string;
};
/**
* Version when this type was introduced
*/
introducedIn?: string;
/**
* Version when this type was deprecated (if applicable)
*/
deprecatedIn?: string;
/**
* Type that replaces this one (if deprecated)
*/
replacedBy?: NodePropertyTypes;
/**
* Additional notes or warnings
*/
notes?: string[];
}
/**
* Property definition within a structure
*/
export interface TypePropertyDefinition {
/**
* Type of this property
*/
type: 'string' | 'number' | 'boolean' | 'object' | 'array' | 'any';
/**
* Description of this property
*/
description?: string;
/**
* Whether this property is required
*/
required?: boolean;
/**
* Nested properties (for object types)
*/
properties?: Record<string, TypePropertyDefinition>;
/**
* Type of array items (for array types)
*/
items?: TypePropertyDefinition;
/**
* Example value
*/
example?: any;
/**
* Allowed values (enum)
*/
enum?: Array<string | number | boolean>;
/**
* Whether this structure allows additional properties beyond those defined
*/
flexible?: boolean;
}
/**
* Complex property types that have nested structures
*
* These types require special handling and validation
* beyond simple type checking.
*/
export type ComplexPropertyType =
| 'collection'
| 'fixedCollection'
| 'resourceLocator'
| 'resourceMapper'
| 'filter'
| 'assignmentCollection';
/**
* Primitive property types (simple values)
*
* These types map directly to JavaScript primitives
* and don't require complex validation.
*/
export type PrimitivePropertyType =
| 'string'
| 'number'
| 'boolean'
| 'dateTime'
| 'color'
| 'json';
/**
* Type guard to check if a property type is complex
*
* Complex types have nested structures and require
* special validation logic.
*
* @param type - The property type to check
* @returns True if the type is complex
*
* @example
* ```typescript
* if (isComplexType('collection')) {
* // Handle complex type
* }
* ```
*/
export function isComplexType(type: NodePropertyTypes): type is ComplexPropertyType {
return (
type === 'collection' ||
type === 'fixedCollection' ||
type === 'resourceLocator' ||
type === 'resourceMapper' ||
type === 'filter' ||
type === 'assignmentCollection'
);
}
/**
* Type guard to check if a property type is primitive
*
* Primitive types map to simple JavaScript values
* and only need basic type validation.
*
* @param type - The property type to check
* @returns True if the type is primitive
*
* @example
* ```typescript
* if (isPrimitiveType('string')) {
* // Handle as primitive
* }
* ```
*/
export function isPrimitiveType(type: NodePropertyTypes): type is PrimitivePropertyType {
return (
type === 'string' ||
type === 'number' ||
type === 'boolean' ||
type === 'dateTime' ||
type === 'color' ||
type === 'json'
);
}
/**
* Type guard to check if a value is a valid TypeStructure
*
* @param value - The value to check
* @returns True if the value conforms to TypeStructure interface
*
* @example
* ```typescript
* const maybeStructure = getStructureFromSomewhere();
* if (isTypeStructure(maybeStructure)) {
* console.log(maybeStructure.example);
* }
* ```
*/
export function isTypeStructure(value: any): value is TypeStructure {
return (
value !== null &&
typeof value === 'object' &&
'type' in value &&
'jsType' in value &&
'description' in value &&
'example' in value &&
['primitive', 'object', 'array', 'collection', 'special'].includes(value.type) &&
['string', 'number', 'boolean', 'object', 'array', 'any'].includes(value.jsType)
);
}
|