0
# Type Validation
1
2
Comprehensive type validation system with runtime type checking, schema validation, and type guard functions for ensuring data integrity and type safety throughout workflow execution.
3
4
## Capabilities
5
6
### Field Type Validation
7
8
Core validation functions for validating field types and values.
9
10
```typescript { .api }
11
/**
12
* Validate field type and value
13
* @param field - Field name for error reporting
14
* @param value - Value to validate
15
* @param type - Expected field type
16
* @returns Validation result with success status and errors
17
*/
18
function validateFieldType(
19
field: string,
20
value: any,
21
type: FieldType
22
): ValidationResult;
23
24
/**
25
* Validate multiple parameters against schema
26
* @param parameters - Parameters object to validate
27
* @param parameterTypes - Schema defining expected types
28
* @returns Validation result with detailed errors
29
*/
30
function validateParameters(
31
parameters: INodeParameters,
32
parameterTypes: IParameterTypeSchema
33
): ValidationResult;
34
35
interface ValidationResult {
36
isValid: boolean;
37
errors: ValidationError[];
38
}
39
40
interface ValidationError {
41
field: string;
42
message: string;
43
receivedType: string;
44
expectedType: string;
45
value?: any;
46
}
47
48
type FieldType =
49
| 'string'
50
| 'number'
51
| 'integer'
52
| 'boolean'
53
| 'array'
54
| 'object'
55
| 'null'
56
| 'undefined'
57
| 'dateTime'
58
| 'email'
59
| 'url'
60
| 'json'
61
| 'binary';
62
```
63
64
### Type Guard Functions
65
66
Comprehensive type guards for checking interface and object types.
67
68
```typescript { .api }
69
/**
70
* Check if value is valid node properties
71
* @param value - Value to check
72
* @returns Type guard for INodeProperties
73
*/
74
function isINodeProperties(value: any): value is INodeProperties;
75
76
/**
77
* Check if value is valid node property options
78
* @param value - Value to check
79
* @returns Type guard for INodePropertyOptions
80
*/
81
function isINodePropertyOptions(value: any): value is INodePropertyOptions;
82
83
/**
84
* Check if value is valid node property collection
85
* @param value - Value to check
86
* @returns Type guard for INodePropertyCollection
87
*/
88
function isINodePropertyCollection(value: any): value is INodePropertyCollection;
89
90
/**
91
* Check if value is valid node properties list
92
* @param value - Value to check
93
* @returns Type guard for INodePropertiesList
94
*/
95
function isINodePropertiesList(value: any): value is INodePropertiesList;
96
97
/**
98
* Check if value is valid collection list
99
* @param value - Value to check
100
* @returns Type guard for INodePropertyCollectionList
101
*/
102
function isINodePropertyCollectionList(value: any): value is INodePropertyCollectionList;
103
104
/**
105
* Check if value is valid options list
106
* @param value - Value to check
107
* @returns Type guard for INodePropertyOptionsList
108
*/
109
function isINodePropertyOptionsList(value: any): value is INodePropertyOptionsList;
110
111
/**
112
* Check if value is resource mapper value
113
* @param value - Value to check
114
* @returns Type guard for IResourceMapperValue
115
*/
116
function isResourceMapperValue(value: any): value is IResourceMapperValue;
117
118
/**
119
* Check if value is resource locator value
120
* @param value - Value to check
121
* @returns Type guard for IResourceLocatorValue
122
*/
123
function isResourceLocatorValue(value: any): value is IResourceLocatorValue;
124
125
/**
126
* Check if value is filter value
127
* @param value - Value to check
128
* @returns Type guard for IFilterValue
129
*/
130
function isFilterValue(value: any): value is IFilterValue;
131
```
132
133
### Schema Validation
134
135
Schema-based validation for complex data structures and nested objects.
136
137
```typescript { .api }
138
/**
139
* Validate object against JSON schema
140
* @param data - Object to validate
141
* @param schema - JSON schema definition
142
* @returns Validation result with detailed errors
143
*/
144
function validateSchema(
145
data: any,
146
schema: JSONSchema
147
): SchemaValidationResult;
148
149
/**
150
* Validate node parameter schema
151
* @param parameters - Node parameters to validate
152
* @param nodeType - Node type definition
153
* @returns Parameter validation result
154
*/
155
function validateNodeParameters(
156
parameters: INodeParameters,
157
nodeType: INodeTypeDescription
158
): ParameterValidationResult;
159
160
interface SchemaValidationResult {
161
isValid: boolean;
162
errors: SchemaValidationError[];
163
data?: any;
164
}
165
166
interface SchemaValidationError {
167
path: string;
168
message: string;
169
keyword: string;
170
params?: any;
171
schemaPath: string;
172
}
173
174
interface ParameterValidationResult {
175
isValid: boolean;
176
errors: ParameterValidationError[];
177
warnings: ParameterValidationWarning[];
178
}
179
180
interface ParameterValidationError {
181
parameter: string;
182
message: string;
183
type: 'missing' | 'invalid' | 'constraint';
184
value?: any;
185
}
186
```
187
188
### Runtime Type Checking
189
190
Runtime type checking utilities for dynamic type validation.
191
192
```typescript { .api }
193
/**
194
* Get runtime type of value
195
* @param value - Value to check type of
196
* @returns Runtime type string
197
*/
198
function getRuntimeType(value: any): string;
199
200
/**
201
* Check if value matches expected type
202
* @param value - Value to check
203
* @param expectedType - Expected type string or constructor
204
* @returns Boolean indicating type match
205
*/
206
function isType(value: any, expectedType: string | Function): boolean;
207
208
/**
209
* Ensure value is of expected type or throw error
210
* @param value - Value to check
211
* @param expectedType - Expected type
212
* @param fieldName - Field name for error reporting
213
* @throws TypeError if type doesn't match
214
*/
215
function ensureType(
216
value: any,
217
expectedType: string | Function,
218
fieldName?: string
219
): void;
220
221
/**
222
* Convert value to specified type with validation
223
* @param value - Value to convert
224
* @param targetType - Target type for conversion
225
* @param strict - Strict conversion mode
226
* @returns Converted value
227
* @throws TypeError if conversion fails
228
*/
229
function convertToType<T>(
230
value: any,
231
targetType: string | Function,
232
strict?: boolean
233
): T;
234
```
235
236
### Parameter Type Validation
237
238
Specialized validation for node parameters and workflow configuration.
239
240
```typescript { .api }
241
/**
242
* Validate parameter value against parameter definition
243
* @param parameterName - Parameter name
244
* @param value - Parameter value
245
* @param parameterDefinition - Parameter definition
246
* @returns Parameter validation result
247
*/
248
function validateParameterValue(
249
parameterName: string,
250
value: NodeParameterValue,
251
parameterDefinition: INodeProperties
252
): ParameterValueValidationResult;
253
254
/**
255
* Check parameter dependencies and display conditions
256
* @param parameters - All node parameters
257
* @param parameterDefinition - Parameter definition to check
258
* @returns Boolean indicating if parameter should be displayed
259
*/
260
function checkParameterDisplayConditions(
261
parameters: INodeParameters,
262
parameterDefinition: INodeProperties
263
): boolean;
264
265
interface ParameterValueValidationResult {
266
isValid: boolean;
267
error?: string;
268
warning?: string;
269
coercedValue?: any;
270
}
271
272
/**
273
* Validate collection parameter structure
274
* @param collectionValue - Collection parameter value
275
* @param collectionDefinition - Collection definition
276
* @returns Collection validation result
277
*/
278
function validateCollectionParameter(
279
collectionValue: INodeParameters[],
280
collectionDefinition: INodePropertyCollection
281
): CollectionValidationResult;
282
283
interface CollectionValidationResult {
284
isValid: boolean;
285
errors: CollectionValidationError[];
286
itemErrors: { [index: number]: ParameterValidationError[] };
287
}
288
```
289
290
### Constraint Validation
291
292
Advanced constraint validation for parameter values and data integrity.
293
294
```typescript { .api }
295
/**
296
* Validate value constraints (min, max, pattern, etc.)
297
* @param value - Value to validate
298
* @param constraints - Constraint definition
299
* @returns Constraint validation result
300
*/
301
function validateConstraints(
302
value: any,
303
constraints: IParameterConstraints
304
): ConstraintValidationResult;
305
306
interface IParameterConstraints {
307
min?: number;
308
max?: number;
309
minLength?: number;
310
maxLength?: number;
311
pattern?: string | RegExp;
312
enum?: any[];
313
required?: boolean;
314
unique?: boolean;
315
format?: string;
316
}
317
318
interface ConstraintValidationResult {
319
isValid: boolean;
320
violations: ConstraintViolation[];
321
}
322
323
interface ConstraintViolation {
324
constraint: string;
325
message: string;
326
actualValue: any;
327
expectedValue?: any;
328
}
329
330
/**
331
* Validate email format
332
* @param email - Email string to validate
333
* @returns Boolean indicating valid email format
334
*/
335
function isValidEmail(email: string): boolean;
336
337
/**
338
* Validate URL format
339
* @param url - URL string to validate
340
* @returns Boolean indicating valid URL format
341
*/
342
function isValidUrl(url: string): boolean;
343
344
/**
345
* Validate JSON format
346
* @param jsonString - JSON string to validate
347
* @returns Boolean indicating valid JSON format
348
*/
349
function isValidJson(jsonString: string): boolean;
350
```
351
352
**Usage Examples:**
353
354
```typescript
355
import {
356
validateFieldType,
357
validateParameters,
358
isINodeProperties,
359
validateSchema,
360
ensureType,
361
validateParameterValue
362
} from "n8n-workflow";
363
364
// Basic field type validation
365
const stringValidation = validateFieldType('username', 'john_doe', 'string');
366
console.log('String validation:', stringValidation.isValid); // true
367
368
const numberValidation = validateFieldType('age', '25', 'number');
369
console.log('Number validation errors:', numberValidation.errors);
370
371
// Parameter validation
372
const parameterValidation = validateParameters(
373
{
374
apiUrl: 'https://api.example.com',
375
timeout: 5000,
376
retries: 3
377
},
378
{
379
apiUrl: { type: 'string', required: true, format: 'url' },
380
timeout: { type: 'number', min: 1000, max: 60000 },
381
retries: { type: 'integer', min: 0, max: 10 }
382
}
383
);
384
385
if (!parameterValidation.isValid) {
386
console.log('Parameter validation errors:', parameterValidation.errors);
387
}
388
389
// Type guard usage
390
const nodeProperty = {
391
displayName: 'API URL',
392
name: 'apiUrl',
393
type: 'string',
394
required: true
395
};
396
397
if (isINodeProperties(nodeProperty)) {
398
console.log('Valid node property:', nodeProperty.displayName);
399
} else {
400
console.log('Invalid node property structure');
401
}
402
403
// Schema validation
404
const schema = {
405
type: 'object',
406
properties: {
407
name: { type: 'string', minLength: 1 },
408
age: { type: 'number', minimum: 0 },
409
email: { type: 'string', format: 'email' }
410
},
411
required: ['name', 'email']
412
};
413
414
const userData = {
415
name: 'John Doe',
416
age: 30,
417
email: 'john@example.com'
418
};
419
420
const schemaValidation = validateSchema(userData, schema);
421
if (schemaValidation.isValid) {
422
console.log('User data is valid');
423
} else {
424
console.log('Schema validation errors:', schemaValidation.errors);
425
}
426
427
// Runtime type checking
428
try {
429
ensureType('hello', 'string', 'message');
430
console.log('Type check passed');
431
432
ensureType(123, 'string', 'invalidField'); // Throws TypeError
433
} catch (error) {
434
console.log('Type check failed:', error.message);
435
}
436
437
// Parameter value validation
438
const parameterDefinition = {
439
displayName: 'Timeout',
440
name: 'timeout',
441
type: 'number',
442
default: 30000,
443
typeOptions: {
444
minValue: 1000,
445
maxValue: 300000
446
}
447
};
448
449
const parameterResult = validateParameterValue('timeout', 45000, parameterDefinition);
450
if (parameterResult.isValid) {
451
console.log('Parameter value is valid');
452
} else {
453
console.log('Parameter validation error:', parameterResult.error);
454
}
455
456
// Constraint validation with complex rules
457
const constraints = {
458
min: 1,
459
max: 100,
460
pattern: /^[a-zA-Z0-9_]+$/,
461
required: true
462
};
463
464
const constraintResult = validateConstraints('valid_name_123', constraints);
465
if (!constraintResult.isValid) {
466
constraintResult.violations.forEach(violation => {
467
console.log(`Constraint violation: ${violation.constraint} - ${violation.message}`);
468
});
469
}
470
```