Exemplo n.º 1
0
def validate_payload(payload, action, message_type_id):
    if message_type_id not in [MessageType.Call, MessageType.CallResult]:
        raise ValidationError("Payload can't be validated because message "
                              f"type id isn't valid. It's '{message_type_id}',"
                              f" but it should be either '{MessageType.Call}' "
                              f" or'{MessageType.CallResult}'.")

    schema_name = action
    if message_type_id == MessageType.CallResult:
        schema_name += 'Response'

    try:
        schema = get_schema(schema_name)
    except (OSError, json.JSONDecodeError) as e:
        raise ValidationError("Failed to load validation schema for action "
                              f"'{action}': {e}")

    if action in ['SetChargingProfile', 'RemoteStartTransaction']:
        # todo: special actions
        pass

    try:
        validate(payload, schema)
    except SchemaValidationError as e:
        raise ValidationError(f"Payload '{payload} for action '{action}' is "
                              f"not valid: {e}")
Exemplo n.º 2
0
def validate_payload(message, ocpp_version):
    """ Validate the payload of the message using JSON schemas. """
    if type(message) not in [Call, CallResult]:
        raise ValidationError("Payload can't be validated because message "
                              f"type. It's '{type(message)}', but it should "
                              "be either 'Call'  or 'CallResult'.")

    try:
        # 3 OCPP 1.6 schedules have fields of type floats. The JSON schema
        # defines a certain precision for these fields of 1 decimal. A value of
        # 21.4 is valid, whereas a value if 4.11 is not.
        #
        # The problem is that Python's internal representation of 21.4 might
        # have more than 1 decimal. It might be 21.399999999999995. This would
        # make the validation fail, although the payload is correct. This is a
        # known issue with jsonschemas, see:
        # https://github.com/Julian/jsonschema/issues/247
        #
        # This issue can be fixed by using a different parser for floats than
        # the default one that is used.
        #
        # Both the schema and the payload must be parsed using the different
        # parser for floats.
        if ocpp_version == '1.6' and (
            (type(message) == Call and message.action
             in ['SetChargingProfile', 'RemoteStartTransaction'])  # noqa
                or (type(message) == CallResult
                    and message.action == ['GetCompositeSchedule'])):
            schema = get_schema(message.message_type_id,
                                message.action,
                                ocpp_version,
                                parse_float=decimal.Decimal)

            message.payload = json.loads(json.dumps(message.payload),
                                         parse_float=decimal.Decimal)
        else:
            schema = get_schema(message.message_type_id, message.action,
                                ocpp_version)
    except (OSError, json.JSONDecodeError) as e:
        raise ValidationError("Failed to load validation schema for action "
                              f"'{message.action}': {e}")

    try:
        validate(message.payload, schema)
    except SchemaValidationError as e:
        raise ValidationError(f"Payload '{message.payload} for action "
                              f"'{message.action}' is not valid: {e}")