UNPKG

n8n-nodes-content-validation

Version:
112 lines 5.92 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ContentValidationV1 = void 0; const n8n_workflow_1 = require("n8n-workflow"); const versionDescription_1 = require("./actions/versionDescription"); const html_validate_1 = require("html-validate"); const htmlvalidate = new html_validate_1.HtmlValidate(); const languagedetect_1 = __importDefault(require("languagedetect")); const spokenLanguageDetector = new languagedetect_1.default(); class ContentValidationV1 { constructor(baseDescription) { this.description = { ...baseDescription, ...versionDescription_1.versionDescription }; } async execute() { var _a, _b, _c; const items = this.getInputData(); const returnData = []; const resource = this.getNodeParameter('resource', 0); const operation = this.getNodeParameter('operation', 0); for (let i = 0; i < items.length; i++) { let itemData = {}; switch (resource) { case 'validator': switch (operation) { case 'Validate HTML': { const html = this.getNodeParameter('html', i); if (html === undefined || html === null) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'HTML content is required for validation!', { itemIndex: i, }); } const additionalFields = this.getNodeParameter('additionalFields', i, {}); const rejectEmptyString = (_a = additionalFields.rejectEmptyString) !== null && _a !== void 0 ? _a : false; const requireAtLeastOneTag = (_b = additionalFields.requireAtLeastOneTag) !== null && _b !== void 0 ? _b : false; if (rejectEmptyString && html.trim() === '') { itemData = { ok: false, error: 'HTML content cannot be an empty string or consist only of spaces!', content: html }; } else if (requireAtLeastOneTag && !/<[^>]+>/.test(html)) { itemData = { ok: false, error: 'HTML content must contain at least one tag!', content: html }; } else { const validationReport = await htmlvalidate.validateString(html); itemData = { ok: validationReport.valid === true, rejectEmptyString, report: validationReport, content: html }; } break; } case 'Validate spoken language': { const text = this.getNodeParameter('Probe Text', i); const targetLanguage = this.getNodeParameter('Target Language', i); const additionalFields = this.getNodeParameter('additionalFields', i, {}); const allowEmpty = (_c = additionalFields.allowEmpty) !== null && _c !== void 0 ? _c : true; if (text === undefined || text === null) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Text content is required for spoken language validation!', { itemIndex: i, }); } if (!allowEmpty && text.trim() === '') { itemData = { ok: false, error: 'Text content cannot be an empty string or consist only of spaces!', content: text }; } else { const result = spokenLanguageDetector.detect(text); const isOk = Array.isArray(result) && result[0] && Array.isArray(result[0]) ? result[0][0] === targetLanguage : false; itemData = { ok: isOk, result, content: text }; } break; } default: { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Operation "${operation}" is not supported for resource "${resource}"!`); } } break; default: { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Resource "${resource}" is not supported!`); } } returnData.push({ json: { itemIndex: i, ...itemData } }); } return [this.helpers.returnJsonArray(returnData)]; } } exports.ContentValidationV1 = ContentValidationV1; //# sourceMappingURL=ContentValidationV1.node.js.map