Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
dphuang2 committed Oct 3, 2024
1 parent 09b677e commit c4c3140
Show file tree
Hide file tree
Showing 3 changed files with 632 additions and 1,061 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,40 @@ class StrBase(ValidatorBase):
return super()._validate_oapg(arg, validation_metadata=validation_metadata)


class IntBase:
@property
def as_int_oapg(self) -> int:
try:
return self._as_int
except AttributeError:
self._as_int = int(self)
return self._as_int

@classmethod
def __validate_format(cls, arg: typing.Optional[decimal.Decimal], validation_metadata: ValidationMetadata):
if isinstance(arg, decimal.Decimal):

denominator = arg.as_integer_ratio()[-1]
if denominator != 1:
raise ApiValueError(
"Invalid value '{}' for type integer at {}".format(arg, validation_metadata.path_to_item)
)

@classmethod
def _validate_oapg(
cls,
arg,
validation_metadata: ValidationMetadata,
):
"""
IntBase _validate_oapg
TODO what about types = (int, number) -> IntBase, NumberBase? We could drop int and keep number only
"""
if cls._types and int not in cls._types: cls._types.add(int)
cls.__validate_format(arg, validation_metadata=validation_metadata)
return super()._validate_oapg(arg, validation_metadata=validation_metadata)


class UUIDBase:
@property
@functools.lru_cache()
Expand Down Expand Up @@ -2011,6 +2045,7 @@ class ComposedSchema(
ComposedBase,
DictBase,
ListBase,
IntBase,
NumberBase,
StrBase,
BoolBase,
Expand Down Expand Up @@ -2073,40 +2108,6 @@ class NumberSchema(
return super().__new__(cls, arg, **kwargs)


class IntBase:
@property
def as_int_oapg(self) -> int:
try:
return self._as_int
except AttributeError:
self._as_int = int(self)
return self._as_int

@classmethod
def __validate_format(cls, arg: typing.Optional[decimal.Decimal], validation_metadata: ValidationMetadata):
if isinstance(arg, decimal.Decimal):

denominator = arg.as_integer_ratio()[-1]
if denominator != 1:
raise ApiValueError(
"Invalid value '{}' for type integer at {}".format(arg, validation_metadata.path_to_item)
)

@classmethod
def _validate_oapg(
cls,
arg,
validation_metadata: ValidationMetadata,
):
"""
IntBase _validate_oapg
TODO what about types = (int, number) -> IntBase, NumberBase? We could drop int and keep number only
"""
if cls._types and int not in cls._types: cls._types.add(int)
cls.__validate_format(arg, validation_metadata=validation_metadata)
return super()._validate_oapg(arg, validation_metadata=validation_metadata)


class IntSchema(IntBase, NumberBase, Schema, IntMixin):

@classmethod
Expand Down
Loading

0 comments on commit c4c3140

Please sign in to comment.