FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Class Initialisation Validation Kwarg by AndrewRiggs-Atkins · Pull Request #227 · fastapi/sqlmodel · GitHub

Repository navigation

Class Initialisation Validation Kwarg - #227

Open
AndrewRiggs-Atkins wants to merge 9 commits into
fastapi:mainfrom
AndrewRiggs-Atkins:init-validation-kwarg
Open

AndrewRiggs-Atkins wants to merge 9 commits into
fastapi:mainfrom
AndrewRiggs-Atkins:init-validation-kwarg

Conversation

AndrewRiggs-Atkins commented Jan 17, 2022 •
edited
Loading

Copy link
Copy Markdown

As discussed in issue #134, this pr enables pydantic validation to be run on object instantiation when the class is set as a table, instead of only when being commited into the database.

main.py > SQLModelMetaclass > new > line ~307

config_validate = get_config("validate")
if config_validate is True:
    # If it was passed by kwargs, ensure it's also set in config
    new_cls.__config__.validate = config_validate
    for k, v in new_cls.__fields__.items():
        col = get_column_from_field(v)
        setattr(new_cls, k, col)

main.py > SQLModel > init > line ~517

if (
    (not getattr(__pydantic_self__.__config__, "table", False)
    or getattr(__pydantic_self__.__config__, "validate", False)) # Added validate
    and validation_error
):
    raise validation_error

usage

class Hero(SQLModel, table=True, validate=True): # Added validate

    id: int = Field(primary_key=True, nullable=False)
    number: int

    @validator('number')
    def less_than_100(cls, v):
        if v > 100:
            raise ValueError('must be less than 100')
        return v

When validate is disabled, validation runs on commit as usual, with it enabled, validation runs on object initialisation. Works for pydanic @validate functions as well as others such as max_length

Copy link
Copy Markdown
Author

@tiangolo Would it be possible to get the workflows run on this pr?

Copy link
Copy Markdown
Author

@tiangolo Would it be possible to get this PR reviewed and workflows run?

This comment was marked as outdated.

This comment was marked as outdated.

andremmori mentioned this pull request Oct 3, 2022
8 tasks done

This comment was marked as outdated.

Copy link
Copy Markdown

I just ran across this issue myself, so it would be great to get this fix merged.

Looks like it is just some lint errors in the actions failures.

Any plan to get this rolled in? Thanks!

This comment was marked as outdated.

This comment was marked as outdated.

This comment was marked as outdated.

This comment was marked as outdated.

This comment was marked as outdated.

Copy link
Copy Markdown
Contributor

📝 Docs preview for commit 4499f96 at: https://63ecd2b82960b411061a9718--sqlmodel.netlify.app

Copy link
Copy Markdown
Author

I just ran across this issue myself, so it would be great to get this fix merged.

Looks like it is just some lint errors in the actions failures.

Any plan to get this rolled in? Thanks!

I've resolved the conflicts and the pr now passes the latest checks.

@tiangolo Are we able to get a go/no-go on this PR, been sitting around for over a year now so be good to get a final decision on whether it should be updated, merged, or closed.

tiangolo added the feature New feature or request label Oct 22, 2023

spott commented Jan 2, 2025

Copy link
Copy Markdown

Bump?

I'm pretty interested in this, however it is now going on 3 years old... Any updates?

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

@AndrewRiggs-Atkins, thanks for your interest and efforts!

The feature you attempted to implement in this PR is highly requested by users. And I personally think it would be great if we found a solution to it.

But to add this to the project we should test it carefully and cover by automated tests to ensure it suits user's needs and further code changes will not brake it.

Are you ready to continue working on this?

github-actions Bot added the conflicts Automatically generated when a PR has a merge conflict label Sep 5, 2025

github-actions Bot commented Sep 5, 2025

Copy link
Copy Markdown
Contributor

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

svlandeg commented Oct 7, 2025

Copy link
Copy Markdown
Member

Related PR: #1041

This branch has not been deployed

No deployments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

6 participants


Back | FazBrowse Home | New Git URL