entities_validation_svc/entity_validator.py

108 lines
2.9 KiB
Python
Raw Normal View History

2020-12-04 21:18:19 +01:00
#!/usr/bin/env python3
from cerberus import Validator
from validation_functions import *
2021-04-20 22:39:12 +02:00
2020-12-04 21:18:19 +01:00
def validate(entity):
2021-04-20 22:39:12 +02:00
schema_fin = {
2020-12-04 21:18:19 +01:00
'bic': {
2021-04-20 22:39:12 +02:00
'type': 'string',
2020-12-04 21:18:19 +01:00
'required': True,
'check_with': valid_bic},
'iban': {
2021-04-20 22:39:12 +02:00
'type': 'string',
2020-12-04 21:18:19 +01:00
'required': True,
'check_with': valid_iban},
'issuance': {
2021-04-20 22:39:12 +02:00
'type': 'string',
2020-12-04 21:18:19 +01:00
'required': True,
'check_with': iso_date},
'reference': {'type': 'string'},
'scan-sepa-mandate': {'type': 'string'},
'holder': {'type': 'string'}}
2021-04-20 22:39:12 +02:00
schema_membership = {
2020-12-04 21:18:19 +01:00
'bis': {
'type': 'string',
2021-04-20 22:39:12 +02:00
'oneof': [{'check_with': iso_date}, {'empty': True}]},
2020-12-04 21:18:19 +01:00
'mitgliedsbeitrag': {
2021-04-20 22:39:12 +02:00
'type': 'string',
2020-12-04 21:18:19 +01:00
'check_with': valid_money_amount},
2021-04-20 22:39:12 +02:00
'scan-antrag': {'type': 'string'},
2020-12-04 21:18:19 +01:00
'schliessberechtigung': {
'type': 'string',
2021-04-20 22:39:12 +02:00
'allowed': ['Ja', 'Nein', 'J', 'N', 'j', 'n', 'y', 'Y']},
2020-12-04 21:18:19 +01:00
'spendenbeitrag': {
'type': 'string',
2021-04-20 22:39:12 +02:00
'check_with': valid_money_amount},
2020-12-04 21:18:19 +01:00
'status': {
'type': 'string',
'required': True,
2021-04-20 22:39:12 +02:00
'allowed': ['V', 'E', 'F']},
2020-12-04 21:18:19 +01:00
'von': {
'type': 'string',
'required': True,
'check_with': iso_date}}
2021-04-20 22:39:12 +02:00
schema_base = {
2020-12-04 21:18:19 +01:00
'address_code': {
2021-04-20 22:39:12 +02:00
'type': 'string'},
2020-12-04 21:18:19 +01:00
'address_country': {
2021-04-20 22:39:12 +02:00
'type': 'string'},
2020-12-04 21:18:19 +01:00
'address_label': {
2021-04-20 22:39:12 +02:00
'type': 'string'},
2020-12-04 21:18:19 +01:00
'address_locality': {
2021-04-20 22:39:12 +02:00
'type': 'string'},
2020-12-04 21:18:19 +01:00
'address_region': {
2021-04-20 22:39:12 +02:00
'type': 'string'},
2020-12-04 21:18:19 +01:00
'address_street': {
'type': 'string'},
'birth_date': {
'type': 'string',
'required': True,
2021-04-20 22:39:12 +02:00
'check_with': iso_date},
2020-12-04 21:18:19 +01:00
'birth_location': {
'type': 'string'},
'email': {
'type': 'string',
'required': True,
2021-04-20 22:39:12 +02:00
'check_with': valid_email},
2020-12-04 21:18:19 +01:00
'fullname': {
'type': 'string',
'required': True},
'nickname': {
2021-04-20 22:39:12 +02:00
'type': 'string'},
2020-12-04 21:18:19 +01:00
'pgp-key': {
2021-04-20 22:39:12 +02:00
'type': 'string'},
2020-12-04 21:18:19 +01:00
'ssh-key': {
'type': 'string'}}
schema = {
'finanzdaten': {
2020-12-04 21:23:07 +01:00
'type': 'dict',
'required': True,
2020-12-04 21:18:19 +01:00
'schema': schema_fin},
'mitgliederdaten': {
2020-12-04 21:23:07 +01:00
'type': 'dict',
'required': True,
2020-12-04 21:18:19 +01:00
'schema': schema_membership},
'stammdaten': {
2020-12-04 21:23:07 +01:00
'type': 'dict',
'required': True,
2020-12-04 21:18:19 +01:00
'schema': schema_base},
'id': {
'type': 'string',
'required': True,
'regex': '^[a-f0-9]{5}$'},
'timestamp': {
'type': 'string',
'required': True}
}
v = Validator()
result = {
'valid': v.validate(entity, schema),
2021-04-20 22:39:12 +02:00
'errors': v.errors}
return result