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 new file mode 100644 index 0000000000000..e30a1c0c04771 --- /dev/null +++ b/posthog/migrations/0535_alter_hogfunction_type.py @@ -0,0 +1,17 @@ +# Generated by Django 4.2.15 on 2024-12-19 14:58 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("posthog", "0534_team_cookieless_server_hash_mode"), + ] + + operations = [ + migrations.AlterField( + model_name="hogfunction", + name="type", + field=models.CharField(blank=True, max_length=24, null=True), + ), + ] diff --git a/posthog/migrations/max_migration.txt b/posthog/migrations/max_migration.txt index 43f1f6ffa127f..4270a16eb7703 100644 --- a/posthog/migrations/max_migration.txt +++ b/posthog/migrations/max_migration.txt @@ -1 +1 @@ -0534_team_cookieless_server_hash_mode +0535_alter_hogfunction_type diff --git a/posthog/models/hog_functions/hog_function.py b/posthog/models/hog_functions/hog_function.py index 3ddd0212f21d5..8328973bc0a2e 100644 --- a/posthog/models/hog_functions/hog_function.py +++ b/posthog/models/hog_functions/hog_function.py @@ -66,7 +66,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)