From 00ae02a67fa093ba242f6c004438e59f6675ce76 Mon Sep 17 00:00:00 2001 From: Markus Grunwald <97972764+tmh-grunwald-markus@users.noreply.github.com> Date: Thu, 3 Mar 2022 09:46:07 +0100 Subject: [PATCH] Raise maxLength violation as ocpp error (#312) According to ocpp-j-1.6-specification , section "4.2.3 CallError", a unique identifier that is too long is a reason for a call error of type TypeConstraintViolationError --- ocpp/messages.py | 3 +++ tests/test_messages.py | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/ocpp/messages.py b/ocpp/messages.py index 45d97ad22..d0a8a33e8 100644 --- a/ocpp/messages.py +++ b/ocpp/messages.py @@ -197,6 +197,9 @@ def validate_payload(message, ocpp_version): raise FormatViolationError(details={"cause": e.message}) elif (e.validator == SchemaValidators.required.__name__): raise ProtocolError(details={"cause": e.message}) + elif e.validator == "maxLength": + raise TypeConstraintViolationError( + details={"cause": e.message}) from e else: raise ValidationError(f"Payload '{message.payload} for action " f"'{message.action}' is not valid: {e}") diff --git a/tests/test_messages.py b/tests/test_messages.py index 693e0644a..90a7407e7 100644 --- a/tests/test_messages.py +++ b/tests/test_messages.py @@ -328,3 +328,21 @@ def test_validate_meter_values_hertz(): ) validate_payload(message, ocpp_version="1.6") + + +def test_validate_set_maxlength_violation_payload(): + """ + Test if payloads that violate maxLength raise a + TypeConstraintViolationError + """ + message = Call( + unique_id="1234", + action="StartTransaction", + payload={ + "idTag": "012345678901234567890", + "connectorId": 1, + }, + ) + + with pytest.raises(TypeConstraintViolationError): + validate_payload(message, ocpp_version="1.6")