Back to Examples

RabbitMQ service - Constraint validation

The Ballerina constraint module allows you to add additional constraints to the message content. The constraints can be added to a given data type using different annotations. When a message with a constraint is received from the RabbitMQ server, it is validated internally. This validation happens soon after the successful data-binding of the message content before executing the onMessage remote method. If the validation fails, the onError remote method is invoked with the error type nats:PayloadValidationError. Use this to validate the message content as the application receives it, which allows you to guard against unnecessary remote method processing and malicious content.

import ballerina/constraint;
import ballerina/log;
import ballerinax/rabbitmq;

public type Order record {
    int orderId;
    // Add a constraint to allow only string values of length between 1 and 30.
    @constraint:String {maxLength: 30, minLength: 1}
    string productName;
    decimal price;
    boolean isValid;
};

// The consumer service listens to the "OrderQueue" queue.
service "OrderQueue" on new rabbitmq:Listener(rabbitmq:DEFAULT_HOST, rabbitmq:DEFAULT_PORT) {

    remote function onMessage(Order 'order) returns error? {
        if 'order.isValid {
            log:printInfo(string `Received valid order for ${'order.productName}`);
        }
    }

    // When an error occurs, `onError` gets invoked.
    remote function onError(rabbitmq:AnydataMessage message, rabbitmq:Error err) {
        if err is rabbitmq:PayloadValidationError {
            log:printError("Payload validation failed", err);
        }
    }
}

Prerequisites

Run the service by executing the following command.

$ bal run rabbitmq_service_constraint_validation.baltime = 2022-12-05T08:08:36.426+05:30 level = INFO module = "" message = "Received valid order for Sport shoe"time = 2022-12-05T08:08:57.313+05:30 level = ERROR module = "" message = "Payload validation failed" error = "Validation failed for \'$.productName:maxLength\' constraint(s)."time = 2022-12-05T08:09:19.634+05:30 level = INFO module = "" message = "Received valid order for Sport shoe"

Tip: You can invoke the above service via the RabbitMQ client example with a valid product name (0 < length <= 30), then with an invalid product name and again with a valid product name.

Related links

PreviousTransactional consumer
NextDeclare a queue