-
Notifications
You must be signed in to change notification settings - Fork 0
/
Validators.py
32 lines (28 loc) · 1.18 KB
/
Validators.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from typing import Optional
class Validators:
# Validate floating point number such as latitude, longitude
@classmethod
def valid_float_in_range(cls, proposed_value: str, min_value: float, max_value: float) -> Optional[float]:
"""Validate that a string is a floating point number in a given range"""
result: Optional[float] = None
try:
converted: float = float(proposed_value)
if (converted >= min_value) and (converted <= max_value):
result = converted
except ValueError:
# Let result go back as "none", indicating error
pass
return result
# Validate integer number
@classmethod
def valid_int_in_range(cls, proposed_value: str, min_value: int, max_value: int) -> Optional[int]:
"""Validate that a string is an integer in a given range"""
result: Optional[int] = None
try:
converted: int = int(proposed_value)
if (converted >= min_value) and (converted <= max_value):
result = converted
except ValueError:
# Let result go back as "none", indicating error
pass
return result