refactor validation functions
This commit is contained in:
parent
7671739300
commit
e3557f5d52
2 changed files with 31 additions and 30 deletions
|
@ -1,36 +1,7 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
from cerberus import Validator
|
||||
from schwifty import IBAN, BIC
|
||||
import datetime
|
||||
|
||||
def valid_iban(field, value, error):
|
||||
try:
|
||||
IBAN(value)
|
||||
return True
|
||||
except ValueError:
|
||||
error(field, 'not a valid IBAN')
|
||||
|
||||
def valid_bic(field, value, error):
|
||||
try:
|
||||
BIC(value)
|
||||
return True
|
||||
except ValueError:
|
||||
error(field, 'not a valid BIC')
|
||||
|
||||
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')
|
||||
|
||||
def valid_money_amount(field, value, error):
|
||||
try:
|
||||
float(value)
|
||||
return True
|
||||
except (ValueError, TypeError):
|
||||
error(field, 'not a valid money value')
|
||||
from validators import *
|
||||
|
||||
document = {
|
||||
'finanzdaten':
|
||||
|
|
30
validators.py
Normal file
30
validators.py
Normal file
|
@ -0,0 +1,30 @@
|
|||
from schwifty import IBAN, BIC
|
||||
import datetime
|
||||
|
||||
def valid_iban(field, value, error):
|
||||
try:
|
||||
IBAN(value)
|
||||
return True
|
||||
except ValueError:
|
||||
error(field, 'not a valid IBAN')
|
||||
|
||||
def valid_bic(field, value, error):
|
||||
try:
|
||||
BIC(value)
|
||||
return True
|
||||
except ValueError:
|
||||
error(field, 'not a valid BIC')
|
||||
|
||||
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')
|
||||
|
||||
def valid_money_amount(field, value, error):
|
||||
try:
|
||||
float(value)
|
||||
return True
|
||||
except (ValueError, TypeError):
|
||||
error(field, 'not a valid money value')
|
Loading…
Reference in a new issue