Skip to content

[BUG]: No input validation on input_text in /forms/fill #477

@krrishrastogi05

Description

@krrishrastogi05

Description

POST /api/v1/forms/fill accepts any string for input_text — including empty strings and whitespace-only input. An empty submission reaches Ollama and burns LLM cycles to return nothing useful for every field.

Steps to reproduce

  1. Send POST /forms/fill with { "template_id": 1, "input_text": " " }
  2. Server processes the request fully — Ollama is called once per template field
  3. All fields return -1 (not found), a useless filled PDF is written to disk

Fix

Add a @field_validator to FormFill in (api/schemas/forms.py):

from pydantic import BaseModel, field_validator

class FormFill(BaseModel):
    template_id: int
    input_text: str

    @field_validator("input_text")
    @classmethod
    def validate_input_text(cls, v: str) -> str:
        v = v.strip()
        if not v:
            raise ValueError("input_text cannot be empty or whitespace.")
        if len(v) > 10_000:
            raise ValueError("input_text exceeds maximum length of 10,000 characters.")
        return v

No other files need changing — Pydantic will return a 422 automatically before the request reaches the controller.

Acceptance criteria

  • POST /forms/fill with empty or whitespace-only input_text returns 422
  • Input exceeding 10,000 characters returns 422
  • Valid input continues to work as before

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions