diff --git a/test_cerberus.py b/test_cerberus.py index e81e7e6..6921f7f 100644 --- a/test_cerberus.py +++ b/test_cerberus.py @@ -23,7 +23,14 @@ def iso_date(field, value, error): datetime.datetime.strptime(value, "%Y-%m-%d") return True except ValueError: - error(field, 'not a valid IDO 8601 date') + 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') document = { 'finanzdaten': @@ -41,7 +48,7 @@ document = { 'mitgliedsbeitrag': '30', 'scan-antrag': '', 'schliessberechtigung': 'Ja', - 'spendenbeitrag': '', + 'spendenbeitrag': '0', 'status': 'V', 'von': '2012-01-01' }, @@ -64,13 +71,47 @@ document = { 'timestamp': '2020-03-25T23:58:11', 'id': '6af68' } - -schema_fin={'bic': {'type': 'string', 'check_with': valid_bic}, - 'iban': {'type': 'string', 'check_with': valid_iban}, - 'issuance': {'type': 'string', 'check_with': iso_date}, + +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}, 'reference': {'type': 'string'}, 'scan-sepa-mandate': {'type': 'string'}, 'holder': {'type': 'string'}} +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}} + v = cerberus.Validator() -v.validate(document['finanzdaten'], schema_fin) \ No newline at end of file +print(v.validate(document['finanzdaten'], schema_fin)) +print(v.errors) +print(v.validate(document['mitgliederdaten'], schema_member)) +print(v.errors) \ No newline at end of file