-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from midezz/pydantic-support
Pydantic support
- Loading branch information
Showing
23 changed files
with
182 additions
and
89 deletions.
There are no files selected for viewing
This file contains 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,5 @@ | ||
[flake8] | ||
max-line-length = 88 | ||
max-line-length = 120 | ||
exclude = | ||
venv, | ||
__pycache__, | ||
|
This file contains 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 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 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,5 @@ | ||
[tool.black] | ||
line-length = 88 | ||
line-length = 120 | ||
target-version = ['py36'] | ||
include = '\.pyi?$' | ||
exclude = ''' | ||
|
This file contains 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 |
---|---|---|
|
@@ -7,4 +7,5 @@ requests==2.25.1 | |
flake8==3.8.4 | ||
pytest==6.2.2 | ||
isort==5.7.0 | ||
black==20.8b1 | ||
black==20.8b1 | ||
pydantic-sqlalchemy==0.0.9 |
This file contains 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 |
---|---|---|
|
@@ -6,11 +6,12 @@ | |
requirements = [ | ||
'SQLAlchemy>1.3.0,<=1.3.23', | ||
'starlette>0.13.0,<=0.14.2', | ||
'pydantic>1.7,<=1.8', | ||
] | ||
|
||
setuptools.setup( | ||
name='simplerestapi', | ||
version='1.0.1', | ||
version='1.0.2', | ||
author='Andrey Nikulin', | ||
author_email='[email protected]', | ||
description='SimpleRestAPI is the library for launch REST API based on your SQLAlchemy models', | ||
|
@@ -37,7 +38,6 @@ | |
|
||
], | ||
install_requires=requirements, | ||
package_dir={'': 'src'}, | ||
packages=setuptools.find_packages(where='src'), | ||
packages=['simplerestapi'], | ||
python_requires='>3.6', | ||
) |
File renamed without changes.
This file contains 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 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 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,5 @@ | ||
class ConfigEndpointError(Exception): | ||
def __init__(self, message, errors): | ||
message_errors = message + '\n' + '\n'.join(errors) | ||
super().__init__(message_errors) | ||
self.errors = errors |
This file contains 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 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,43 @@ | ||
from pydantic import BaseModel, validator | ||
|
||
|
||
class BaseConfigEndpoint(BaseModel): | ||
pagination: int = 100 | ||
denied_methods: list = [] | ||
path: str = None | ||
exclude_fields: list = [] | ||
|
||
@validator('denied_methods') | ||
def denied_methods_validate(cls, v): | ||
correct_methods = {'get', 'post', 'delete', 'put', 'patch'} | ||
if len(v) > 0 and set(v) - correct_methods: | ||
raise ValueError('methods must be from list (\'get\', \'post\', \'delete\', \'put\', \'patch\')') | ||
|
||
@validator('pagination') | ||
def pagination_validate(cls, v): | ||
if v < 0: | ||
raise ValueError('value must be above or equal 0') | ||
|
||
|
||
class ModelValidator: | ||
def __init__(self, model): | ||
self.model = model | ||
self.errors = [] | ||
self.validate_config_endpoint(model) | ||
|
||
def validate_config_endpoint(self, model): | ||
base_config_properties = set(BaseConfigEndpoint.schema()['properties']) | ||
model_config_properties = {key for key in model.ConfigEndpoint.__dict__ if not key.startswith('__')} | ||
config_properties = {key: getattr(model.ConfigEndpoint, key) for key in base_config_properties} | ||
if len(model_config_properties - base_config_properties) > 0: | ||
msg = '\'{0}\': not support parameters' | ||
self.add_error(msg.format(', '.join(model_config_properties - base_config_properties))) | ||
try: | ||
_ = BaseConfigEndpoint(**config_properties) | ||
except Exception as e: | ||
for error in e.errors(): | ||
error_msg = f'\'{" ".join(error["loc"])}\': {error["msg"]}' | ||
self.add_error(error_msg) | ||
|
||
def add_error(self, error_msg): | ||
self.errors.append(f'Model \'{self.model.__name__}\' has incorrect ConfigEndpoint parameter {error_msg}') |
File renamed without changes.
This file contains 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 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 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 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.