File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ from pyvalueobjects .abstract .valueobject import ValueObject
2+ from pyvalueobjects .errors .ValueObjectError import ValueObjectError
3+
4+
5+ class String (ValueObject ):
6+
7+ _ALLOWED_INPUT_TYPES = {str , int , float , bool }
8+
9+ def __init__ (self , value : str ):
10+ super ().__init__ (value )
11+ self ._validate (value )
12+
13+ def _validate (self , value ):
14+ input_type = type (value )
15+ if input_type != str :
16+ raise ValueObjectError (f'Input type should be: str.' )
17+
18+ @classmethod
19+ def from_float (cls , value : float ):
20+ if type (value ) != float :
21+ raise ValueObjectError ('Input type should be float.' )
22+
23+ try :
24+ return cls (str (value ))
25+ except Exception as _ :
26+ raise ValueObjectError ('Input type should be valid float.' )
27+
28+ @classmethod
29+ def from_int (cls , value : int ):
30+ if type (value ) != int :
31+ raise ValueObjectError ('Input type should be int.' )
32+
33+ try :
34+ return cls (str (value ))
35+ except Exception as _ :
36+ raise ValueObjectError ('Input type should be valid int.' )
You can’t perform that action at this time.
0 commit comments