-
Notifications
You must be signed in to change notification settings - Fork 14
fix schemas validators passthrough #45
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7de4d37
create tests for validators (TDD)
mahenzon 878d864
added another way to extract validators (WIP)
CosmoV 361152a
updated field validators extraction logic
CosmoV 0d928e7
updated extracting of root validators, added tests
CosmoV cc17623
fix
CosmoV 0d81acd
added changelog and refactor
CosmoV 77cf5b0
update changelog
mahenzon 1df4d27
update requirements for docs
mahenzon eeddf39
rename test
mahenzon ff9c9c4
update changelog
mahenzon a581763
upgrade tests
mahenzon d9f5a0a
minor refactor test
mahenzon 6a6ef5a
validator tests moved to own test module
CosmoV 1e0f9c8
added validation_utils test
CosmoV File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
fastapi<0.100.0 | ||
pydantic<2 | ||
simplejson>=3.17.6 | ||
sphinx | ||
sphinx_rtd_theme | ||
sqlalchemy<2 | ||
tortoise-orm>=0.19.3 |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
from copy import deepcopy | ||
from typing import ( | ||
Callable, | ||
Dict, | ||
Set, | ||
Type, | ||
) | ||
|
||
from pydantic import ( | ||
class_validators, | ||
root_validator, | ||
validator, | ||
) | ||
from pydantic.fields import Validator | ||
from pydantic.utils import unique_list | ||
|
||
from fastapi_jsonapi.schema_base import BaseModel | ||
|
||
|
||
def extract_root_validators(model: Type[BaseModel]) -> Dict[str, Callable]: | ||
pre_rv_new, post_rv_new = class_validators.extract_root_validators(model.__dict__) | ||
pre_root_validators = unique_list( | ||
model.__pre_root_validators__ + pre_rv_new, | ||
name_factory=lambda v: v.__name__, | ||
) | ||
post_root_validators = unique_list( | ||
model.__post_root_validators__ + post_rv_new, | ||
name_factory=lambda skip_on_failure_and_v: skip_on_failure_and_v[1].__name__, | ||
) | ||
|
||
result_validators = {} | ||
|
||
for validator_func in pre_root_validators: | ||
result_validators[validator_func.__name__] = root_validator( | ||
pre=True, | ||
allow_reuse=True, | ||
)(validator_func) | ||
|
||
for skip_on_failure, validator_func in post_root_validators: | ||
result_validators[validator_func.__name__] = root_validator( | ||
allow_reuse=True, | ||
skip_on_failure=skip_on_failure, | ||
)(validator_func) | ||
|
||
return result_validators | ||
|
||
|
||
def _deduplicate_field_validators(validators: Dict) -> Dict: | ||
result_validators = {} | ||
|
||
for field_name, field_validators in validators.items(): | ||
result_validators[field_name] = list( | ||
{ | ||
# override in definition order | ||
field_validator.func.__name__: field_validator | ||
for field_validator in field_validators | ||
}.values(), | ||
) | ||
|
||
return result_validators | ||
|
||
|
||
def extract_field_validators( | ||
model: Type[BaseModel], | ||
*, | ||
include_for_field_names: Set[str] = None, | ||
exclude_for_field_names: Set[str] = None, | ||
): | ||
validators = class_validators.inherit_validators( | ||
class_validators.extract_validators(model.__dict__), | ||
deepcopy(model.__validators__), | ||
) | ||
validators = _deduplicate_field_validators(validators) | ||
validator_origin_param_keys = ( | ||
"pre", | ||
"each_item", | ||
"always", | ||
"check_fields", | ||
) | ||
|
||
exclude_for_field_names = exclude_for_field_names or set() | ||
|
||
if include_for_field_names and exclude_for_field_names: | ||
include_for_field_names = include_for_field_names.difference( | ||
exclude_for_field_names, | ||
) | ||
|
||
result_validators = {} | ||
for field_name, field_validators in validators.items(): | ||
if field_name in exclude_for_field_names: | ||
continue | ||
|
||
if include_for_field_names and field_name not in include_for_field_names: | ||
continue | ||
|
||
field_validator: Validator | ||
for field_validator in field_validators: | ||
validator_name = f"{field_name}_{field_validator.func.__name__}_validator" | ||
validator_params = { | ||
# copy validator params | ||
param_key: getattr(field_validator, param_key) | ||
for param_key in validator_origin_param_keys | ||
} | ||
result_validators[validator_name] = validator( | ||
field_name, | ||
**validator_params, | ||
allow_reuse=True, | ||
)(field_validator.func) | ||
|
||
return result_validators | ||
|
||
|
||
def extract_validators( | ||
model: Type[BaseModel], | ||
exclude_for_field_names: Set[str] = None, | ||
) -> Dict[str, Callable]: | ||
return { | ||
**extract_field_validators( | ||
model, | ||
exclude_for_field_names=exclude_for_field_names, | ||
), | ||
**extract_root_validators(model), | ||
} |
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.