]> git.cworth.org Git - turbot/commitdiff
Add validation of the "Hunt ID" (slug) field
authorCarl Worth <cworth@cworth.org>
Tue, 20 Oct 2020 19:06:29 +0000 (12:06 -0700)
committerCarl Worth <cworth@cworth.org>
Tue, 20 Oct 2020 19:38:04 +0000 (12:38 -0700)
This gives the user a clean message stating the requirements of the field
and preventing the submission until this field is valid.

turbot/actions.py

index 5bc815e34ac07a3f92b9f75824598d4be9ec8d82..6763e34f3d453e14a2af6c6035050bfcb8ac199b 100644 (file)
@@ -1,5 +1,7 @@
 from turbot.blocks import input_block
 import turbot.sheets
+import json
+import re
 
 def new_hunt(turb, payload):
     """Handler for the action of user pressing the new_hunt button"""
@@ -11,7 +13,7 @@ def new_hunt(turb, payload):
         "submit": { "type": "plain_text", "text": "Create" },
         "blocks": [
             input_block("Hunt name", "name", "Name of the hunt"),
-            input_block("Hunt ID", "slug", "Short hunt prefix (no spaces)"),
+            input_block("Hunt ID", "slug", "Short hunt ID (no spaces)"),
             input_block("Hunt URL", "url", "External URL of hunt",
                         optional=True)
         ],
@@ -38,6 +40,23 @@ def new_hunt_submission(turb, payload):
     slug = state['slug']['slug']['value']
     url = state['url']['url']['value']
 
+    # Validate that the slug contains no invalid characters
+    if not re.match(r'[-_a-zA-Z0-9]+$', slug):
+        print("Slug field is invalid. Attmpting to return a clean error.")
+        return {
+            'statusCode': 200,
+            'headers': {
+                "Content-Type": "application/json"
+            },
+            'body': json.dumps({
+                "response_action": "errors",
+                "errors": {
+                    "slug": "Hunt ID can only contain letters, "
+                    + "numbers, hyphens and underscores"
+                }
+            })
+        }
+
     # Create a channel for the hunt
     response = turb.slack_client.conversations_create(name=slug)