Talen
Talen

Reputation: 15

Mule custom validation with property or parameter

We are trying migrating our existing code to Mule, in order to complete all the validation, we need custom validator to be more powerful to support the following:

<RULE>
    <ID crosssite="Y" stoponerr="Y">CheckAmount</ID>
    <MSG>Err_CheckAmount</MSG>
    <PARAM name="isIndex"></PARAM>
    <PARAM name="ccyIdField"></PARAM>
    <PARAM name="ccyListField"></PARAM>
    <PARAM name="ccyField">HKD</PARAM>
</RULE>

where CheckAmount is a validator class, and take a list of parameter, so it can validate everything based the parameter.

i noticed this issue has already been raised many years ago in the comment of below. https://www.mulesoft.org/jira/browse/MULE-6087

so does mule has anything like the this?

<validation:all doc:name="Validate min">
        <validation:validations>
            <validation:custom-validator class="test.CheckVal" message="Err_CheckValue">
                **<validation:param name="CITY">HK</validation:param>
                <validation:param name="MIN">20</validation:param>
                <validation:param name="MAX">50</validation:param>**
            </validation:custom-validator>
            <validation:is-false expression=""/>
        </validation:validations>
</validation:all>

So when i implement Validator interface, i can get do the validation based on the parameters.

Thanks in advance

Upvotes: 1

Views: 2335

Answers (1)

Roger Butenuth
Roger Butenuth

Reputation: 546

You have to use a Spring bean as validator. All you configurable properties must be accessible by public get/set methods (in this example the properties min and max).

Example Java class:

package de.codecentric;

import org.mule.api.MuleEvent;
import org.mule.extension.validation.api.ValidationResult;
import org.mule.extension.validation.api.Validator;
import org.mule.extension.validation.internal.ImmutableValidationResult;

public class CustomValidator implements Validator {
    private int min;
    private int max;

    public int getMin() {
        return min;
    }

    public void setMin(int min) {
        this.min = min;
    }

    public int getMax() {
        return max;
    }

    public void setMax(int max) {
        this.max = max;
    }

    @Override
    public ValidationResult validate(MuleEvent event) {
        Object payload = event.getMessage().getPayload();
        if (!(payload instanceof Number)) {
            return ImmutableValidationResult.error("Not a number: " +     payload);
        }
        Number number = (Number) payload;
        if (number.intValue() > max) {
            return ImmutableValidationResult.error("Too big: " + number);
        }
        if (number.intValue() < min) {
            return ImmutableValidationResult.error("Too small: " + number);
        }
        return ImmutableValidationResult.ok();
    }
}

Then have to create an instance as Spring bean from this:

<spring:beans>
    <spring:bean id="MyValidator" class="de.codecentric.CustomValidator">
        <spring:property name="min" value="10" />
        <spring:property name="max" value="20" />
    </spring:bean>
</spring:beans>

And use it in your Mule flow like this:

<validation:custom-validator doc:name="Validation" ref="MyValidator"/>

Upvotes: 3

Related Questions