Skip to content

feat: Add length validation for superuser password#2192

Open
siyavash wants to merge 1 commit intofastapi:masterfrom
siyavash:fix/copier-password-validation
Open

feat: Add length validation for superuser password#2192
siyavash wants to merge 1 commit intofastapi:masterfrom
siyavash:fix/copier-password-validation

Conversation

@siyavash
Copy link

This PR improves the copier template by adding a validation step for the first_superuser_password. It ensures that users cannot generate a project with a password shorter than 8 characters, which is a requirement for the backend.

The Issue

Currently, when creating a project with this template, copier allows setting a short password (e.g., "salam"). However, the backend's UserCreate Pydantic model strictly validates that passwords must be at least 8 characters long.

This caused the prestart service to crash immediately after deployment with a ValidationError, requiring manual intervention to fix the .env file and restart services.

Logs

prestart-1  | INFO:__main__:Creating initial data
prestart-1  | Traceback (most recent call last):
prestart-1  |   File "/app/backend/app/initial_data.py", line 23, in <module>
prestart-1  |     main()
prestart-1  |   File "/app/backend/app/initial_data.py", line 18, in main
prestart-1  |     init()
prestart-1  |   File "/app/backend/app/initial_data.py", line 13, in init
prestart-1  |     init_db(session)
prestart-1  |   File "/app/backend/app/core/db.py", line 28, in init_db
prestart-1  |     user_in = UserCreate(
prestart-1  |   File "/app/.venv/lib/python3.10/site-packages/sqlmodel/main.py", line 793, in __init__
prestart-1  |     sqlmodel_init(self=__pydantic_self__, data=data)
prestart-1  |   File "/app/.venv/lib/python3.10/site-packages/sqlmodel/_compat.py", line 331, in sqlmodel_init
prestart-1  |     self.__pydantic_validator__.validate_python(
prestart-1  | pydantic_core._pydantic_core.ValidationError: 1 validation error for UserCreate
prestart-1  | password
prestart-1  |   String should have at least 8 characters [type=string_too_short, input_value='salam', input_type=str]
prestart-1  |     For further information visit [https://errors.pydantic.dev/2.12/v/string_too_short](https://errors.pydantic.dev/2.12/v/string_too_short)

The Fix

I updated copier.yml to:

  1. Add a Validator: Use a Jinja2 check {% if first_superuser_password|length < 8 %} to reject passwords shorter than 8 characters during generation.
  2. Update Help Text: Explicitly state the "at least 8 characters" requirement in the prompt to guide the user.
    This ensures all new projects start with a valid configuration.

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.

1 participant

Comments