-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(component): add parameters validate
Signed-off-by: weiwee <wbwmat@gmail.com>
- Loading branch information
Showing
5 changed files
with
83 additions
and
10 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
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,59 @@ | ||
import pydantic | ||
|
||
|
||
class Parameter: | ||
def dict(self): | ||
raise NotImplementedError() | ||
|
||
|
||
class ConInt(Parameter): | ||
def __init__(self, gt: int = None, ge: int = None, lt: int = None, le: int = None) -> None: | ||
self.gt = gt | ||
self.ge = ge | ||
self.lt = lt | ||
self.le = le | ||
|
||
def parse(self, obj): | ||
return pydantic.parse_obj_as(pydantic.conint(gt=self.gt, ge=self.ge, lt=self.lt, le=self.le), obj) | ||
|
||
def dict(self): | ||
meta = {} | ||
if self.gt is not None: | ||
meta["gt"] = self.gt | ||
if self.ge is not None: | ||
meta["ge"] = self.ge | ||
if self.lt is not None: | ||
meta["lt"] = self.lt | ||
if self.le is not None: | ||
meta["le"] = self.le | ||
return meta | ||
|
||
|
||
class ConFloat(Parameter): | ||
def __init__(self, gt: float = None, ge: float = None, lt: float = None, le: float = None) -> None: | ||
self.gt = gt | ||
self.ge = ge | ||
self.lt = lt | ||
self.le = le | ||
|
||
def parse(self, obj): | ||
return pydantic.parse_obj_as(pydantic.confloat(gt=self.gt, ge=self.ge, lt=self.lt, le=self.le), obj) | ||
|
||
def dict(self): | ||
meta = {} | ||
if self.gt is not None: | ||
meta["gt"] = self.gt | ||
if self.ge is not None: | ||
meta["ge"] = self.ge | ||
if self.lt is not None: | ||
meta["lt"] = self.lt | ||
if self.le is not None: | ||
meta["le"] = self.le | ||
return meta | ||
|
||
|
||
def parse(parameter_type, obj): | ||
if isinstance(parameter_type, Parameter) and hasattr(parameter_type, "parse"): | ||
return getattr(parameter_type, "parse")(obj) | ||
else: | ||
return pydantic.parse_obj_as(parameter_type, obj) |
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