n8n
Version:
n8n Workflow Automation Tool
108 lines • 4.97 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.WorkflowValidationService = void 0;
const db_1 = require("@n8n/db");
const di_1 = require("@n8n/di");
const n8n_workflow_1 = require("n8n-workflow");
const constants_1 = require("../constants");
let WorkflowValidationService = class WorkflowValidationService {
constructor(workflowRepository) {
this.workflowRepository = workflowRepository;
}
validateForActivation(nodes, nodeTypes) {
const triggerValidation = (0, n8n_workflow_1.validateWorkflowHasTriggerLikeNode)(nodes, nodeTypes, constants_1.STARTING_NODES);
if (!triggerValidation.isValid) {
return {
isValid: false,
error: triggerValidation.error ??
'Workflow cannot be activated because it has no trigger node. At least one trigger, webhook, or polling node is required.',
};
}
return { isValid: true };
}
async validateSubWorkflowReferences(workflowId, nodes) {
const executeWorkflowNodes = nodes.filter((node) => node.type === 'n8n-nodes-base.executeWorkflow' && !node.disabled);
if (executeWorkflowNodes.length === 0) {
return { isValid: true };
}
const invalidReferences = [];
for (const node of executeWorkflowNodes) {
const subWorkflowId = this.extractWorkflowId(node);
const source = typeof node.parameters?.source === 'string' ? node.parameters.source : undefined;
if (this.shouldSkipSubWorkflowValidation(subWorkflowId, source)) {
continue;
}
const status = await this.getWorkflowStatus(workflowId, subWorkflowId);
if (!status.exists || !status.isPublished) {
invalidReferences.push({
nodeName: node.name,
workflowId: subWorkflowId,
workflowName: status.exists ? status.name : undefined,
});
}
}
if (invalidReferences.length > 0) {
const errorMessages = invalidReferences.map((ref) => {
const workflowName = ref.workflowName ? ` ("${ref.workflowName}")` : '';
return `Node "${ref.nodeName}" references workflow ${ref.workflowId}${workflowName} which is not published`;
});
return {
isValid: false,
error: `Cannot publish workflow: ${errorMessages.join('; ')}. Please publish all referenced sub-workflows first.`,
invalidReferences,
};
}
return { isValid: true };
}
async getWorkflowStatus(parentWorkflowId, subWorkflowId) {
if (subWorkflowId === parentWorkflowId) {
return { exists: true, isPublished: true };
}
const subWorkflow = await this.workflowRepository.get({ id: subWorkflowId }, { relations: [] });
if (!subWorkflow) {
return { exists: false, isPublished: false };
}
return {
exists: true,
isPublished: subWorkflow.activeVersionId !== null,
name: subWorkflow.name,
};
}
hasValueProperty(obj) {
return typeof obj === 'object' && obj !== null && 'value' in obj;
}
extractWorkflowId(node) {
const workflowIdParam = node.parameters?.workflowId;
if (this.hasValueProperty(workflowIdParam)) {
return workflowIdParam.value;
}
if (typeof workflowIdParam === 'string') {
return workflowIdParam;
}
return undefined;
}
shouldSkipSubWorkflowValidation(workflowId, source) {
if (!workflowId)
return true;
if (workflowId.startsWith('='))
return true;
if (source && source !== 'database')
return true;
return false;
}
};
exports.WorkflowValidationService = WorkflowValidationService;
exports.WorkflowValidationService = WorkflowValidationService = __decorate([
(0, di_1.Service)(),
__metadata("design:paramtypes", [db_1.WorkflowRepository])
], WorkflowValidationService);
//# sourceMappingURL=workflow-validation.service.js.map