-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: ajust typing, exceptions classes, get_pydantic_version, add…
… __version__
- Loading branch information
Showing
9 changed files
with
1,612 additions
and
1,551 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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,74 @@ | ||
from enum import Enum | ||
|
||
from .get_versions import get_pydantic_version | ||
|
||
__all__ = [ | ||
"FieldTypeError", | ||
"FieldMaskError", | ||
"FieldDigitError", | ||
"FieldInvalidError", | ||
"raise_field", | ||
] | ||
|
||
|
||
pydantic_version = get_pydantic_version() | ||
|
||
|
||
class FieldTypes(Enum): | ||
type = "type" | ||
mask = "mask" | ||
digit = "digit" | ||
invalid = "invalid" | ||
|
||
|
||
class FieldTypeError: | ||
code = "not_str" | ||
msg_template = "Input should be a valid string" | ||
message_template = msg_template | ||
|
||
|
||
class FieldMaskError: | ||
code = "invalid_mask" | ||
msg_template = "invalid mask format" | ||
message_template = msg_template | ||
|
||
|
||
class FieldDigitError: | ||
code = "not_digits" | ||
msg_template = "field only accept digits as string" | ||
message_template = msg_template | ||
|
||
|
||
class FieldInvalidError: | ||
code = "invalid_data" | ||
msg_template = "invalid data" | ||
message_template = msg_template | ||
|
||
|
||
def raise_error(code: str, msg_template: str): # type: ignore | ||
if pydantic_version.value == 1: | ||
from pydantic import PydanticTypeError | ||
|
||
PydanticTypeError.code = code | ||
PydanticTypeError.msg_template = msg_template | ||
PydanticTypeError.message_template = msg_template | ||
raise PydanticTypeError() | ||
|
||
if pydantic_version.value == 2: | ||
from pydantic_core import PydanticCustomError | ||
|
||
PydanticCustomError.code = code | ||
PydanticCustomError.msg_template = msg_template | ||
PydanticCustomError.message_template = msg_template | ||
raise PydanticCustomError(code, msg_template) | ||
|
||
|
||
def raise_field(context: FieldTypes): | ||
field_types = { | ||
FieldTypes.type: (FieldTypeError.code, FieldTypeError.msg_template), | ||
FieldTypes.mask: (FieldMaskError.code, FieldMaskError.msg_template), | ||
FieldTypes.digit: (FieldDigitError.code, FieldDigitError.msg_template), | ||
FieldTypes.invalid: (FieldInvalidError.code, FieldInvalidError.msg_template), | ||
} | ||
|
||
raise_error(*field_types.get(context)) |
This file was deleted.
Oops, something went wrong.
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,68 +1,87 @@ | ||
from typing import Any, Dict | ||
from __future__ import annotations | ||
|
||
from typing import Any, Dict, Mapping, TypeAlias | ||
|
||
from ..get_versions import get_pydantic_version | ||
|
||
pydantic_version = get_pydantic_version() | ||
|
||
|
||
if pydantic_version.value == 1: | ||
|
||
class core_schema: # noqa | ||
def CoreSchema(self): | ||
... | ||
|
||
def general_after_validator_function(self): | ||
... | ||
if pydantic_version.value == 2: | ||
try: | ||
from pydantic_core import core_schema # type: ignore | ||
except ModuleNotFoundError: | ||
raise ModuleNotFoundError("Are you sure you installed pydantic_core") | ||
|
||
def str_schema(self): | ||
... | ||
CoreSchema: TypeAlias = Mapping[str, Any] | ||
JsonSchemaValue = Dict[str, Any] | ||
|
||
def ValidationInfo(self): | ||
... | ||
|
||
class BasePydanticV2: | ||
format = "generic" | ||
|
||
if pydantic_version.value == 2: | ||
from pydantic_core import core_schema # noqa | ||
@classmethod | ||
def validate_type(cls, value: Any): | ||
... | ||
|
||
@classmethod | ||
def validate(cls, value: str): | ||
... | ||
|
||
JsonSchemaValue = Dict[str, Any] | ||
@classmethod | ||
def validate_mask(cls, value: str): | ||
... | ||
|
||
@classmethod | ||
def validate_numbers(cls, value: str): | ||
... | ||
|
||
class BasePydanticV2: | ||
@classmethod | ||
def __get_pydantic_core_schema__( | ||
cls, | ||
source, | ||
) -> core_schema.CoreSchema: | ||
return core_schema.general_after_validator_function( | ||
cls._validate, core_schema.str_schema() | ||
) | ||
*args, | ||
**kwargs, | ||
) -> CoreSchema: | ||
try: | ||
schema = core_schema.with_info_after_validator_function( | ||
cls._validate, core_schema.str_schema() | ||
) | ||
except Exception: | ||
schema = core_schema.general_after_validator_function( | ||
cls._validate, core_schema.str_schema() | ||
) | ||
return schema | ||
|
||
@classmethod | ||
def __get_pydantic_json_schema__( | ||
cls, core_schema: core_schema.CoreSchema, handler | ||
cls, | ||
core_schema: CoreSchema, | ||
handler, | ||
*args, | ||
**kwargs, | ||
) -> JsonSchemaValue: | ||
field_schema = handler(core_schema) | ||
field_schema.update(type="string", format=cls.format) | ||
return field_schema | ||
|
||
@classmethod | ||
def _validate(cls, __input_value: str, _: core_schema.ValidationInfo) -> str: | ||
def _validate(cls, __input_value: str, *args, **kwargs) -> str: | ||
cls.validate_type(__input_value) | ||
return cls.validate(__input_value) | ||
|
||
|
||
class BaseMaskV2(BasePydanticV2): | ||
@classmethod | ||
def _validate(cls, __input_value: str, _: core_schema.ValidationInfo) -> str: | ||
def _validate(cls, __input_value: str, *args, **kwargs) -> str: | ||
cls.validate_type(__input_value) | ||
cls.validate_mask(__input_value) | ||
return cls.validate(__input_value) | ||
|
||
|
||
class BaseDigitsV2(BasePydanticV2): | ||
@classmethod | ||
def _validate(cls, __input_value: str, _: core_schema.ValidationInfo) -> str: | ||
def _validate(cls, __input_value: str, *args, **kwargs) -> str: | ||
cls.validate_type(__input_value) | ||
cls.validate_numbers(__input_value) | ||
return cls.validate(__input_value) |
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.