We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
To systematically validate input and address issue like one originally described in the following issue, type of all parameter should be validated.
A possible approach would be to add validator like:
Naming was adapted from this discussion1 related to pydantic2
pydantic
from abc import ABC, abstractmethod class Validator(ABC): def __set_name__(self, owner, name): self.private_name = '_' + name def __get__(self, obj, objtype=None): return getattr(obj, self.private_name) def __set__(self, obj, value): self.validate(value) setattr(obj, self.private_name, value) @abstractmethod def validate(self, value): pass class Number(Validator): def __init__(self, minvalue=None, maxvalue=None, types=None): self.minvalue = minvalue self.maxvalue = maxvalue self.types = (int, float) if types is None else types def validate(self, value): if not isinstance(value, self.types): raise TypeError(f'Expected {value!r} to be {",",join(self.types)}') if self.minvalue is not None and value < self.minvalue: raise ValueError( f'Expected {value!r} to be at least {self.minvalue!r}' ) if self.maxvalue is not None and value > self.maxvalue: raise ValueError( f'Expected {value!r} to be no more than {self.maxvalue!r}' ) class Float(Number): def __init__(self, minvalue=None, maxvalue=None) super.__init__(minvalue=minvalue, maxvalue=maxvalue, types=(float,) class Integer(Number): def __init__(self, minvalue=None, maxvalue=None) super.__init__(minvalue=minvalue, maxvalue=maxvalue, types=(int,)
References:
Other informative resources:
https://github.com/tiangolo/fastapi/discussions/8024 ↩
https://github.com/pydantic/pydantic/issues/578 ↩
The text was updated successfully, but these errors were encountered:
NicerNewerCar
Successfully merging a pull request may close this issue.
To systematically validate input and address issue like one originally described in the following issue, type of all parameter should be validated.
A possible approach would be to add validator like:
Naming was adapted from this discussion1 related to
pydantic
2References:
Other informative resources:
Footnotes
https://github.com/tiangolo/fastapi/discussions/8024 ↩
https://github.com/pydantic/pydantic/issues/578 ↩
The text was updated successfully, but these errors were encountered: