Skip to content

Commit

Permalink
Raise maxLength violation as ocpp error (#312)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tmh-grunwald-markus authored Mar 3, 2022
1 parent 3bd72ba commit 00ae02a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ocpp/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
18 changes: 18 additions & 0 deletions tests/test_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit 00ae02a

Please sign in to comment.