File tree Expand file tree Collapse file tree 1 file changed +13
-6
lines changed Expand file tree Collapse file tree 1 file changed +13
-6
lines changed Original file line number Diff line number Diff line change @@ -93,6 +93,16 @@ def __format__(self, format_spec):
93
93
# some substitutions - e.g. replacing () with some default value.
94
94
95
95
96
+ def _converter (value , classinfo ):
97
+ """Convert value (str) to number, otherwise return None if is not possible"""
98
+ for one_info in classinfo :
99
+ if issubclass (one_info , numbers .Number ):
100
+ try :
101
+ return one_info (value )
102
+ except ValueError :
103
+ pass
104
+
105
+
96
106
def of_type (* classinfo , ** kwargs ):
97
107
"""Returns a validator for a JSON property that requires it to have a value of
98
108
the specified type. If optional=True, () is also allowed.
@@ -108,12 +118,9 @@ def validate(value):
108
118
if (optional and value == ()) or isinstance (value , classinfo ):
109
119
return value
110
120
else :
111
- for one_info in classinfo :
112
- if issubclass (one_info , numbers .Number ):
113
- try :
114
- return one_info (value )
115
- except ValueError :
116
- pass
121
+ converted_value = _converter (value , classinfo )
122
+ if converted_value :
123
+ return converted_value
117
124
118
125
if not optional and value == ():
119
126
raise ValueError ("must be specified" )
You can’t perform that action at this time.
0 commit comments