Skip to content

Commit

Permalink
file validation in serailizer with number of file (#1099)
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammad-ali-e authored Jan 29, 2025
1 parent 35e3038 commit 10f48e0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
6 changes: 2 additions & 4 deletions backend/api_v2/api_deployment_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from api_v2.constants import ApiExecution
from api_v2.deployment_helper import DeploymentHelper
from api_v2.exceptions import InvalidAPIRequest, NoActiveAPIKeyError
from api_v2.exceptions import NoActiveAPIKeyError
from api_v2.models import APIDeployment
from api_v2.postman_collection.dto import PostmanCollection
from api_v2.serializers import (
Expand Down Expand Up @@ -47,16 +47,14 @@ def initialize_request(
def post(
self, request: Request, org_name: str, api_name: str, api: APIDeployment
) -> Response:
file_objs = request.FILES.getlist(ApiExecution.FILES_FORM_DATA)
serializer = ExecutionRequestSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
file_objs = serializer.validated_data.get(ApiExecution.FILES_FORM_DATA)
timeout = serializer.validated_data.get(ApiExecution.TIMEOUT_FORM_DATA)
include_metadata = serializer.validated_data.get(ApiExecution.INCLUDE_METADATA)
include_metrics = serializer.validated_data.get(ApiExecution.INCLUDE_METRICS)
use_file_history = serializer.validated_data.get(ApiExecution.USE_FILE_HISTORY)
tag_names = serializer.validated_data.get(ApiExecution.TAGS)
if not file_objs or len(file_objs) == 0:
raise InvalidAPIRequest("File shouldn't be empty")
response = DeploymentHelper.execute_workflow(
organization_name=org_name,
api=api,
Expand Down
21 changes: 21 additions & 0 deletions backend/api_v2/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
from rest_framework.serializers import (
BooleanField,
CharField,
FileField,
IntegerField,
JSONField,
ListField,
ModelSerializer,
Serializer,
ValidationError,
Expand Down Expand Up @@ -115,12 +117,31 @@ class ExecutionRequestSerializer(TagParamsSerializer):
e.g:'tag1,tag2-name,tag3_name'
"""

MAX_FILES_ALLOWED = 32

timeout = IntegerField(
min_value=-1, max_value=ApiExecution.MAXIMUM_TIMEOUT_IN_SEC, default=-1
)
include_metadata = BooleanField(default=False)
include_metrics = BooleanField(default=False)
use_file_history = BooleanField(default=False)
files = ListField(
child=FileField(),
required=True,
allow_empty=False,
error_messages={
"required": "At least one file must be provided.",
"empty": "The file list cannot be empty.",
},
)

def validate_files(self, value):
"""Validate the files field."""
if len(value) == 0:
raise ValidationError("The file list cannot be empty.")
if len(value) > self.MAX_FILES_ALLOWED:
raise ValidationError(f"Maximum '{self.MAX_FILES_ALLOWED}' files allowed.")
return value


class ExecutionQuerySerializer(Serializer):
Expand Down

0 comments on commit 10f48e0

Please sign in to comment.