entities_validation_svc/validation_functions.py

45 lines
1,018 B
Python
Raw Normal View History

2020-12-04 21:02:55 +01:00
from schwifty import IBAN, BIC
import datetime
from validator_collection import validators, errors
2020-12-04 21:02:55 +01:00
2021-01-04 22:14:36 +01:00
2020-12-04 21:02:55 +01:00
def valid_iban(field, value, error):
try:
IBAN(value)
return True
except ValueError:
error(field, 'not a valid IBAN')
2021-01-04 22:14:36 +01:00
2020-12-04 21:02:55 +01:00
def valid_bic(field, value, error):
try:
BIC(value)
return True
except ValueError:
error(field, 'not a valid BIC')
2021-01-04 22:14:36 +01:00
2020-12-04 21:02:55 +01:00
def iso_date(field, value, error):
try:
datetime.datetime.strptime(value, "%Y-%m-%d")
return True
except ValueError:
error(field, 'not a valid ISO 8601 date')
2021-01-04 22:14:36 +01:00
2020-12-04 21:02:55 +01:00
def valid_money_amount(field, value, error):
try:
# value is string, check formatting by parsing as float
2020-12-04 21:02:55 +01:00
float(value)
return True
except (ValueError, TypeError):
error(field, 'not a valid money value')
2021-01-04 22:14:36 +01:00
def valid_email(field, value, error):
try:
validators.email(value)
return True
except errors.InvalidEmailError:
2021-01-04 22:14:36 +01:00
error(field, 'not a valid email')