Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IntField: add MinMaxValidator #1868

Closed
wants to merge 0 commits into from

Conversation

markus-96
Copy link
Contributor

Description

Before sending anything to the DB, IntField values are checked beforehand now. As far as I could test, the added overhead is not much, lets see what Codspeed says.

Will add documentation later.

Motivation and Context

Using sqlite, you could send values greater than defined in IntField.constraints.get('le') to the DB and nothing was raised. If you for example want to migrate to another DBMS, you have a problem.

Also, have a look at the Issue I raised: #1853

How Has This Been Tested?

Existent tests and new tests

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added the changelog accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

Copy link

codspeed-hq bot commented Jan 30, 2025

CodSpeed Performance Report

Merging #1868 will not alter performance

Comparing markus-96:intfield-validator (8ebc81f) with develop (5aae984)

Summary

✅ 16 untouched benchmarks

@coveralls
Copy link

coveralls commented Jan 30, 2025

Pull Request Test Coverage Report for Build 13369845719

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • 15 unchanged lines in 1 file lost coverage.
  • Overall coverage increased (+0.01%) to 89.435%

Files with Coverage Reduction New Missed Lines %
tortoise/fields/data.py 15 93.7%
Totals Coverage Status
Change from base Build 13368119422: 0.01%
Covered Lines: 6535
Relevant Lines: 7131

💛 - Coveralls

def __init__(
self,
primary_key: Optional[bool] = None,
ge: Optional[int] = None,
Copy link
Contributor

Choose a reason for hiding this comment

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

Extending the field configuration with ge and le introduces the second way of setting up validation. I think we should have a single way of doing it to keep things simple - through validators. If some one needs specific validation, they can add a validator. So I suggest to remove these options from the constructor.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ge and le are currently baked in in the constraints property. Instead of that I wanted them to be customizable, a bit like with max_char in CharField where this is reflected during validation and also by the constraints property.

Copy link
Contributor

Choose a reason for hiding this comment

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

ge and le are currently baked in in the constraints property. Instead of that I wanted them to be customizable, a bit like with max_char

The int field constraints are related to the nature of the data type. I think it is different from business logic validation and we already have a way of doing it. We shouldn't have two ways of doing the same thing.

@waketzheng
Copy link
Contributor

@markus-96 I push another solution: #1872

@markus-96
Copy link
Contributor Author

@markus-96 I push another solution: #1872

Sorry if I ask, but where is this different from mine? I put much effort into optimizing my solution, could you tell what is wrong about that? I think we all have an interest in tortoise-orm being the fastest orm for python, so something that crucial like an int field has to be as fast as possible as it can be, so I think hard-coding default validators is a completly valid solution.

def to_db_value(self, value: Any, instance: "Union[Type[Model], Model]") -> Any:
if value is not None:
if not isinstance(value, self.field_type):
value = self.field_type(value) # pylint: disable=E1102
Copy link
Contributor

Choose a reason for hiding this comment

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

In which cases is it required?

@henadzit
Copy link
Contributor

henadzit commented Feb 5, 2025

I think we should be able to merge it if we remove the ability to customize constraints.

@waketzheng
Copy link
Contributor

Sorry if I ask, but where is this different from mine? I put much effort into optimizing my solution, could you tell what is wrong about that? I think we all have an interest in tortoise-orm being the fastest orm for python, so something that crucial like an int field has to be as fast as possible as it can be, so I think hard-coding default validators is a completly valid solution.

@markus-96 Your issue #1853 request a feature to check constraints for int fields before saving to db. There are several ways to do that. We can compare the two PRs and choose one or merge both. Code speed is important, and compatibility also counts. I use Django for a long time before migrating to tortoise-orm, and do not want it to be too much different from Django.

CHANGELOG.rst Outdated
@@ -17,6 +17,8 @@ Fixed
Changed
^^^^^^^
- add benchmarks for `get_for_dialect` (#1862)
- optimize `get_for_dialect` (#1863)
- add validator to `IntField` (#1866)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you please extend the note so it is easier to understand for people reading the changelog? I think something like "Added default range validation to IntField, BigIntField, SmallIntField" would be better.

Could you please rebase on develop? I cleaned up the changelog to remove internal notes from the changelog.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i am so bad at rebasing, i create a new pr...

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.

4 participants