entities_validation_svc/test_cerberus.py

117 lines
3.7 KiB
Python
Raw Normal View History

2020-12-02 22:47:40 +01:00
#!/usr/bin/python3
import cerberus
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:
2020-12-02 23:30:31 +01:00
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')
2020-12-02 22:47:40 +01:00
document = {
'finanzdaten':
{
'bic': 'PBNKDEFFXXX',
'holder': '',
'iban': 'DE89370400440532013000',
'issuance': '2012-01-01',
'reference': '0042',
'scan-sepa-mandate': ''
},
'mitgliederdaten':
{
'bis': '',
'mitgliedsbeitrag': '30',
'scan-antrag': '',
'schliessberechtigung': 'Ja',
2020-12-02 23:30:31 +01:00
'spendenbeitrag': '0',
2020-12-02 22:47:40 +01:00
'status': 'V',
'von': '2012-01-01'
},
'stammdaten':
{
'address_code': '39104',
'address_country': 'DE',
'address_label': 'Max Hackerberg\nLeibnizstr. 32\n39104 Magdeburg',
'address_locality': 'Magdeburg',
'address_region': '',
'address_street': 'Leibnizstr. 32',
'birth_date': '1970-01-01',
'birth_location': 'Hackstadt',
'email': 'max.hackerberg@netz39.de',
'fullname': 'Max Hackerberg',
'nickname': 'maxH',
'pgp-key': '',
'ssh-key': ''
},
'timestamp': '2020-03-25T23:58:11',
'id': '6af68'
}
2020-12-02 23:30:31 +01:00
schema_fin={
'bic': {
'type': 'string',
'required': True,
'check_with': valid_bic},
'iban': {
'type': 'string',
'required': True,
'check_with': valid_iban},
'issuance': {
'type': 'string',
'required': True,
'check_with': iso_date},
2020-12-02 22:47:40 +01:00
'reference': {'type': 'string'},
'scan-sepa-mandate': {'type': 'string'},
'holder': {'type': 'string'}}
2020-12-02 23:30:31 +01:00
schema_member={'bis': {
'type': 'string',
'oneof': [{'check_with': iso_date},{'empty': True}]},
'mitgliedsbeitrag': {
'type': 'string',
'check_with': valid_money_amount},
'scan-antrag': {'type': 'string'},
'schliessberechtigung': {
'type': 'string',
'allowed': ['Ja', 'Nein', 'J', 'N', 'j', 'n', 'y', 'Y']},
'spendenbeitrag': {
'type': 'string',
'check_with': valid_money_amount},
'status': {
'type': 'string',
'allowed': ['V', 'E', 'F']},
'von': {
'type': 'string',
'required': True,
'check_with': iso_date}}
2020-12-02 22:47:40 +01:00
v = cerberus.Validator()
2020-12-02 23:30:31 +01:00
print(v.validate(document['finanzdaten'], schema_fin))
print(v.errors)
print(v.validate(document['mitgliederdaten'], schema_member))
print(v.errors)