{
  "id": "9loQL9vFR9lofvwj",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Contact Form Workflow with Data Validation and MongoDB Integration",
  "tags": [],
  "nodes": [
    {
      "id": "63569954-4739-4730-81eb-a004f3f24fd8",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -288,
        -384
      ],
      "parameters": {
        "width": 1536,
        "height": 624,
        "content": "# n8n Workflow: Contact Form Workflow with Data Validation and MongoDB Integration\nAuthor: [Samuel Heredia - LinkedIn](https://www.linkedin.com/in/samuel-heredia-2b5b7a98/)\n## **Description**\nThis workflow in n8n is designed to handle contact form submissions securely and efficiently, ensuring that all collected data is validated, formatted, and stored in a MongoDB Atlas database.\n\n## **Workflow Steps**"
      },
      "typeVersion": 1
    },
    {
      "id": "5af1ce8f-100b-48e4-bb0a-81e7bbb78861",
      "name": "Validate Pattern",
      "type": "n8n-nodes-base.code",
      "position": [
        208,
        80
      ],
      "parameters": {
        "jsCode": "const item = $input.first();\n\n// Validate Phone Number\nconst phone = item.json[\"Phone Number\"];\nconst phoneRegex = /^(\\+?\\d{1,4}[\\s-]?)?(\\(?\\d{1,4}\\)?[\\s-]?)?[\\d\\s-]{5,}$/;\nif (!phoneRegex.test(phone)) {\n  item.json[\"Phone Number\"] = \"Is not Valid\";\n}\n\n// Validate other fields for dangerous patterns\nconst dangerousPattern = /('|;|--|\\/\\*|\\*\\/|xp_|exec|drop|select|insert|delete|update|union|script|<|>)/i;\nfor (const field of [\"Name\", \"Last Name\", \"Email\"]) {\n  const value = item.json[field];\n  if (dangerousPattern.test(value)) {\n    item.json[field] = \"Is not Valid\";\n  }\n}\n\nreturn item.json;"
      },
      "typeVersion": 2
    },
    {
      "id": "afac30d0-547d-412b-b42d-16948ac42ae0",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -816,
        -48
      ],
      "parameters": {
        "color": 5,
        "width": 688,
        "height": 192,
        "content": "### 1. On Form Submission\na. Acts as the entry point of the workflow.\nb. Captures user inputs from the contact form with the following fields:\n - Name\n - Last Name\n - Email\n - Phone Number\n"
      },
      "typeVersion": 1
    },
    {
      "id": "1142f3b4-caf6-43fa-a200-ecb9da3acf86",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -816,
        176
      ],
      "parameters": {
        "color": 5,
        "width": 688,
        "height": 208,
        "content": "### 2. Code Node (Validation Layer)\n\n- Implements input validation to ensure data integrity and security.\n\n- Checks for and removes potential SQL injection attempts or malicious characters.\n\n- Validates that the Phone Number field follows a proper phone number format.\n\n- If any field fails validation, the record is tagged as \u201cIs not Valid\u201d to prevent unsafe data storage."
      },
      "typeVersion": 1
    },
    {
      "id": "158c7af4-ecb2-4af6-b487-9e73513c5eab",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -816,
        432
      ],
      "parameters": {
        "color": 5,
        "width": 688,
        "height": 176,
        "content": "### 3. Edit Fields Node (Data Formatting)\n\n- Transforms the validated form fields into **snake_case** format.\n\n- Ensures consistent field naming convention for MongoDB storage (e.g., first_name, last_name, phone_number)"
      },
      "typeVersion": 1
    },
    {
      "id": "8412f473-fb8d-42e0-bd13-d317196ad36d",
      "name": "Sticky Note8",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -64,
        0
      ],
      "parameters": {
        "color": 4,
        "width": 198,
        "height": 240,
        "content": "## 1"
      },
      "typeVersion": 1
    },
    {
      "id": "112fa7b6-f7f5-4a2c-afe9-bc7778d50e97",
      "name": "Sticky Note9",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        160,
        0
      ],
      "parameters": {
        "color": 4,
        "width": 198,
        "height": 240,
        "content": "## 2"
      },
      "typeVersion": 1
    },
    {
      "id": "f52b7c18-5126-4d1d-962a-c696097c7fe6",
      "name": "Sticky Note10",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        608,
        0
      ],
      "parameters": {
        "color": 4,
        "width": 198,
        "height": 240,
        "content": "## 4"
      },
      "typeVersion": 1
    },
    {
      "id": "7a5783da-8617-4b60-b29c-b427c1c16289",
      "name": "On form submission",
      "type": "n8n-nodes-base.formTrigger",
      "position": [
        -16,
        80
      ],
      "parameters": {
        "options": {
          "ignoreBots": true,
          "appendAttribution": false
        },
        "formTitle": "\ud83d\udce9 Get in Touch with Us",
        "formFields": {
          "values": [
            {
              "fieldLabel": "Name",
              "placeholder": "Name",
              "requiredField": true
            },
            {
              "fieldLabel": "Last Name",
              "placeholder": "Last Name",
              "requiredField": true
            },
            {
              "fieldType": "email",
              "fieldLabel": "Email",
              "placeholder": "Email",
              "requiredField": true
            },
            {
              "fieldLabel": "Phone Number",
              "placeholder": "(+XX ) XXXXXXXX",
              "requiredField": true
            }
          ]
        },
        "formDescription": "We\u2019d love to hear from you! Whether you have a question, feedback, or simply want to connect, fill out the form below and our team will get back to you as soon as possible"
      },
      "typeVersion": 2.3
    },
    {
      "id": "299f1e43-a848-4c9d-8c4c-1759a5ca7aca",
      "name": "Insert documents",
      "type": "n8n-nodes-base.mongoDb",
      "position": [
        656,
        80
      ],
      "parameters": {
        "fields": "name, last_name, email, phone, submitted_at",
        "options": {
          "dateFields": "={{ $json.name }},{{ $json.last_name }},{{ $json.email }},{{ $json.phone }},{{ $json.submitted_at }}",
          "useDotNotation": true
        },
        "operation": "insert",
        "collection": "contact"
      },
      "credentials": {
        "mongoDb": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "b78fa066-00f8-48c5-979e-d8da9d6c1593",
      "name": "Edit Fields",
      "type": "n8n-nodes-base.set",
      "position": [
        432,
        80
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "4569533b-44c5-4c3b-a0af-0634e2ffba4c",
              "name": "name",
              "type": "string",
              "value": "={{ $json.Name }}"
            },
            {
              "id": "f828360a-e1f6-46ed-856a-0768efdb073e",
              "name": "last_name",
              "type": "string",
              "value": "={{ $json[\"Last Name\"] }}"
            },
            {
              "id": "46322cb3-5080-4902-b5dc-110e1047054c",
              "name": "email",
              "type": "string",
              "value": "={{ $json.Email }}"
            },
            {
              "id": "a5648b31-50f9-4022-b5bb-0a9d43027341",
              "name": "phone",
              "type": "string",
              "value": "={{ $json[\"Phone Number\"] }}"
            },
            {
              "id": "3cc35bbb-3ce1-4bab-bb1c-a3983a1ed2d1",
              "name": "submitted_at",
              "type": "string",
              "value": "={{ $json.submittedAt }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "e1312e0c-f824-4d5b-bcc4-86ffbe82c0ea",
      "name": "Sticky Note11",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        384,
        0
      ],
      "parameters": {
        "color": 4,
        "width": 198,
        "height": 240,
        "content": "## 3"
      },
      "typeVersion": 1
    },
    {
      "id": "dca3c2c8-5ed0-48ef-a1ee-692fdd66e08b",
      "name": "Sticky Note12",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        832,
        0
      ],
      "parameters": {
        "color": 4,
        "width": 198,
        "height": 240,
        "content": "## 5"
      },
      "typeVersion": 1
    },
    {
      "id": "8ba91a2e-f86a-49c9-8ea4-c0117669b0a3",
      "name": "Form Ending",
      "type": "n8n-nodes-base.form",
      "position": [
        880,
        80
      ],
      "parameters": {
        "options": {},
        "operation": "completion",
        "completionTitle": "\u2705 Message Sent Successfully",
        "completionMessage": "Thank you for reaching out! Your message has been received, and our team will get back to you shortly. We appreciate your interest and look forward to connecting with you."
      },
      "typeVersion": 2.3
    },
    {
      "id": "2b610b7f-5477-47e2-adc8-db94db46ed0f",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -816,
        656
      ],
      "parameters": {
        "color": 5,
        "width": 688,
        "height": 144,
        "content": "### 4. MongoDB Node (Insert Documents)\n- Handles database persistence by inserting the validated and formatted data into MongoDB.\n\n- Uses MongoDB Atlas as the data store, with connection established via a secure connection URL."
      },
      "typeVersion": 1
    },
    {
      "id": "8021689f-b978-434f-bd21-480518b89868",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -816,
        848
      ],
      "parameters": {
        "color": 5,
        "width": 688,
        "height": 144,
        "content": "### 5. Form Ending Node\n- Provides a clean conclusion to the workflow.\n\n- Returns a confirmation message to the user indicating successful submission and storage of their contact details."
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "5dbb6364-7f28-46f5-b95e-46e7cd558a69",
  "connections": {
    "Edit Fields": {
      "main": [
        [
          {
            "node": "Insert documents",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Insert documents": {
      "main": [
        [
          {
            "node": "Form Ending",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Validate Pattern": {
      "main": [
        [
          {
            "node": "Edit Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "On form submission": {
      "main": [
        [
          {
            "node": "Validate Pattern",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}