Skip to content

✨ Add length validation for superuser password - #2192

Closed
siyavash wants to merge 3 commits into
fastapi:masterfrom
siyavash:fix/copier-password-validation
Closed

✨ Add length validation for superuser password#2192
siyavash wants to merge 3 commits into
fastapi:masterfrom
siyavash:fix/copier-password-validation

Conversation

@siyavash

Copy link
Copy Markdown

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.

@siyavash

Copy link
Copy Markdown
Author

Hi @tiangolo , I don't have access to add labels. Would you please resolve it so we can merge this?

@YuriiMotov YuriiMotov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Image

We now have this constrain defined in 2 different places (copier config and models.py) - it's not perfect, but probably fine

@YuriiMotov YuriiMotov changed the title feat: Add length validation for superuser password ✨ Add length validation for superuser password May 21, 2026
@YuriiMotov YuriiMotov added the feature New feature or request label May 21, 2026
@github-actions

Copy link
Copy Markdown
Contributor

This pull request has a merge conflict that needs to be resolved.

@github-actions github-actions Bot added the conflicts Automatically generated when a PR has a merge conflict label Aug 10, 2026
@tiangolo

Copy link
Copy Markdown
Member

Thanks for the interest! I decided to stop suggesting copier as it requires extra work and maintenance, instead, this repo is now a GitHub template, so people can generate a new repo from it and update values as needed without saving them locally.

I'll now close this one, thanks! ☕

@tiangolo tiangolo closed this Aug 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

conflicts Automatically generated when a PR has a merge conflict feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants