ocpp
ocpp copied to clipboard
format date-time validation does not fails for invalid string date-time
trafficstars
Problem: Given a string with a non date-time format does not fails validation
Example: CP:
async def send_status_notification(self):
request = call.StatusNotificationPayload(
connector_id=1, error_code='NoError', status='Available', timestamp='AnyStringHere100')
response = await self.call(request)
CS:
@on(Action.StatusNotification)
async def on_status_notification(self, connector_id: int,
error_code: str, status: str, timestamp: str) -> Dict:
logging.info("status: %s, error_code: %s, optional: %s", status, error_code, timestamp)
return call_result.StatusNotificationPayload()
Result: No validation fails. Data is parsed as a string even if it does not result in a date-time format.
✦ ➜ python3 charge_point.py
INFO:ocpp:CP_1: send [2,"ef44e9d9-7e6b-4cd7-9e3b-2fc04c4c1d0f","BootNotification",{"chargePointModel":"Optimus","chargePointVendor":"The Mobility House"}]
INFO:ocpp:CP_1: receive message [3,"ef44e9d9-7e6b-4cd7-9e3b-2fc04c4c1d0f",{"currentTime":"2021-06-13T12:16:34.207573","interval":10,"status":"Accepted"}]
Connected to central system.
INFO:ocpp:CP_1: send [2,"8bf5c25e-107d-404e-90b7-f4c408a3085a","StatusNotification",{"connectorId":1,"errorCode":"NoError","status":"Available","timestamp":"AnyStringHere100"}]
INFO:ocpp:CP_1: receive message [3,"8bf5c25e-107d-404e-90b7-f4c408a3085a",{}]
✦ ➜ python3 central_system.py
INFO:root:Server Started listening to new connections...
INFO:root:Protocols Matched: ocpp1.6
INFO:ocpp:CP_1: receive message [2,"ef44e9d9-7e6b-4cd7-9e3b-2fc04c4c1d0f","BootNotification",{"chargePointModel":"Optimus","chargePointVendor":"The Mobility House"}]
INFO:ocpp:CP_1: send [3,"ef44e9d9-7e6b-4cd7-9e3b-2fc04c4c1d0f",{"currentTime":"2021-06-13T12:16:34.207573","interval":10,"status":"Accepted"}]
INFO:ocpp:CP_1: receive message [2,"8bf5c25e-107d-404e-90b7-f4c408a3085a","StatusNotification",{"connectorId":1,"errorCode":"NoError","status":"Available","timestamp":"AnyStringHere100"}]
INFO:root:status: Available, error_code: NoError, optional: AnyStringHere100
Expected: Same with the isolated example.
Isolated example:
import jsonschema
import json
data = {"connectorId": 1, "errorCode": "NoError", "status": "Available", "timestamp": "AnyString100"}
schema = """{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "StatusNotificationRequest",
"type": "object",
"properties": {
"connectorId": {
"type": "integer"
},
"errorCode": {
"type": "string",
"additionalProperties": false,
"enum": [
"NoError"
]
},
"info": {
"type": "string",
"maxLength": 50
},
"status": {
"type": "string",
"additionalProperties": false,
"enum": [
"Available"
]
},
"timestamp": {
"type": "string",
"format": "date-time"
},
"vendorId": {
"type": "string",
"maxLength": 255
},
"vendorErrorCode": {
"type": "string",
"maxLength": 50
}
},
"additionalProperties": false,
"required": ["connectorId", "errorCode", "status"]
}
"""
jsonschema.validate(data, json.loads(schema), format_checker=jsonschema.FormatChecker())
Result:
✦ ➜ python3 examply.py
Traceback (most recent call last):
File "examply.py", line 73, in <module>
jsonschema.validate(data, json.loads(schema), format_checker=jsonschema.FormatChecker())
File "/home/tmhdev/.pyenv/versions/new-env/lib/python3.7/site-packages/jsonschema/validators.py", line 934, in validate
raise error
jsonschema.exceptions.ValidationError: 'AnyString100' is not a 'date-time'
Failed validating 'format' in schema['properties']['timestamp']:
{'format': 'date-time', 'type': 'string'}
On instance['timestamp']:
'AnyString100'
Question: how is it supposed to be with the schemas and the validation?
I have observed the issue on #213
The following ticket has been created as a path forward for this https://github.com/mobilityhouse/ocpp/issues/499 - therefore, this one will be closed