diff --git a/posthog/api/hog_function.py b/posthog/api/hog_function.py index 338f7b8500a6c..b112919b5870c 100644 --- a/posthog/api/hog_function.py +++ b/posthog/api/hog_function.py @@ -32,6 +32,7 @@ TYPES_WITH_COMPILED_FILTERS, TYPES_WITH_TRANSPILED_FILTERS, TYPES_WITH_JAVASCRIPT_SOURCE, + HogFunctionType, ) from posthog.models.plugin import TranspilerError from posthog.plugins.plugin_server_api import create_hog_invocation_test @@ -88,6 +89,8 @@ class HogFunctionSerializer(HogFunctionMinimalSerializer): template = HogFunctionTemplateSerializer(read_only=True) masking = HogFunctionMaskingSerializer(required=False, allow_null=True) + type = serializers.ChoiceField(choices=HogFunctionType.choices, required=False, allow_null=True) + class Meta: model = HogFunction fields = [ diff --git a/posthog/api/test/test_hog_function.py b/posthog/api/test/test_hog_function.py index 169b37e796765..bd68c2ce66506 100644 --- a/posthog/api/test/test_hog_function.py +++ b/posthog/api/test/test_hog_function.py @@ -409,6 +409,25 @@ def test_inputs_required(self, *args): "attr": "inputs__url", } + def test_validation_error_on_invalid_type(self, *args): + payload = { + "name": "Fetch URL", + "hog": "fetch(inputs.url);", + "inputs_schema": [ + {"key": "url", "type": "string", "label": "Webhook URL", "required": True}, + ], + "type": "invalid_type", + } + # Check required + res = self.client.post(f"/api/projects/{self.team.id}/hog_functions/", data={**payload}) + assert res.status_code == status.HTTP_400_BAD_REQUEST, res.json() + assert res.json() == { + "type": "validation_error", + "code": "invalid_choice", + "detail": '"invalid_type" is not a valid choice.', + "attr": "type", + } + def test_inputs_mismatch_type(self, *args): payload = { "name": "Fetch URL", diff --git a/posthog/migrations/0535_alter_hogfunction_type.py b/posthog/migrations/0535_alter_hogfunction_type.py index cb7ea3d65a237..e30a1c0c04771 100644 --- a/posthog/migrations/0535_alter_hogfunction_type.py +++ b/posthog/migrations/0535_alter_hogfunction_type.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.15 on 2024-12-19 11:01 +# Generated by Django 4.2.15 on 2024-12-19 14:58 from django.db import migrations, models @@ -12,23 +12,6 @@ class Migration(migrations.Migration): migrations.AlterField( model_name="hogfunction", name="type", - field=models.CharField( - blank=True, - choices=[ - ("destination", "Destination"), - ("site_destination", "Site Destination"), - ("internal_destination", "Internal Destination"), - ("site_app", "Site App"), - ("transformation", "Transformation"), - ("email", "Email"), - ("sms", "Sms"), - ("push", "Push"), - ("activity", "Activity"), - ("alert", "Alert"), - ("broadcast", "Broadcast"), - ], - max_length=24, - null=True, - ), + field=models.CharField(blank=True, max_length=24, null=True), ), ] diff --git a/posthog/models/hog_functions/hog_function.py b/posthog/models/hog_functions/hog_function.py index a85a961abb277..a715f10b86b7b 100644 --- a/posthog/models/hog_functions/hog_function.py +++ b/posthog/models/hog_functions/hog_function.py @@ -72,7 +72,7 @@ class Meta: deleted = models.BooleanField(default=False) updated_at = models.DateTimeField(auto_now=True) enabled = models.BooleanField(default=False) - type = models.CharField(max_length=24, choices=HogFunctionType.choices, null=True, blank=True) + type = models.CharField(max_length=24, null=True, blank=True) icon_url = models.TextField(null=True, blank=True)