Skip to content

Refactor: move input_text validation to schema level using Pydantic validators#379

Open
Lochit-Vinay wants to merge 3 commits intofireform-core:mainfrom
Lochit-Vinay:input-text-validation
Open

Refactor: move input_text validation to schema level using Pydantic validators#379
Lochit-Vinay wants to merge 3 commits intofireform-core:mainfrom
Lochit-Vinay:input-text-validation

Conversation

@Lochit-Vinay
Copy link
Copy Markdown

Closes #378

Summary

Moved input validation for input_text from the endpoint to the schema level using Pydantic validators.

Problem

Validation was previously handled inside the route, leading to scattered and non-reusable logic.

Before

Validation was implemented inside the endpoint:

if not form.input_text.strip():
    raise AppError("Input text cannot be empty", status_code=400)

After

Validation is now centralized in the schema:

@field_validator("input_text")
def validate_input_text(cls, value):
    if not value.strip():
        raise ValueError("Input text cannot be empty")
    return value

Solution

Added a field validator in the FormFill schema and removed manual validation from the route.

Benefits

  • Cleaner endpoint code
  • Centralized validation
  • Improved maintainability
  • Aligns with FastAPI best practices

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Move input validation to schema level for FormFill

1 participant