From: Carl Worth Date: Tue, 20 Oct 2020 19:06:29 +0000 (-0700) Subject: Add validation of the "Hunt ID" (slug) field X-Git-Url: https://git.cworth.org/git?a=commitdiff_plain;h=d37deb7e89158c1aa6b6d003dc2e9e7565c20f8b;p=turbot Add validation of the "Hunt ID" (slug) field This gives the user a clean message stating the requirements of the field and preventing the submission until this field is valid. --- diff --git a/turbot/actions.py b/turbot/actions.py index 5bc815e..6763e34 100644 --- a/turbot/actions.py +++ b/turbot/actions.py @@ -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)