Amaury Brisou
Amaury Brisou

Reputation: 406

MULE ESB : Expression Filter with regex() function

I'm having as payload some Json data like :

{
    "name" : "Italy"
}

I want to filter with a regex the content of my field "name".

By now I user this filter. There's no real error but it doesn't match.

<expression-filter expression="regex('^[a-zA-Z]{3,15}$' , 'json:name')" doc:name="Expression"/>

As a proof of using this regex() function if I put '(.*)' as regex pattern, it works.

<expression-filter expression="regex('(.*)' , 'json:name')" doc:name="Expression"/>

The documentation is here.

Someone understands my mistake?

[EDIT]

Then Matthew's solution works like that :

    <set-variable variableName="PayloadBackup" value="#[payload]" doc:name="Variable"/><json:json-to-object-transformer returnClass="java.util.Map" doc:name="JSON to Object"/>
    <expression-transformer expression="#[payload['name'].matches('^[a-zA-Z]{3,15}$')]" doc:name="Expression"/>
    <expression-filter expression="#[payload]" doc:name="Expression"/>
    <set-payload value="#[PayloadBackup]" doc:name="Set Payload"/>

It's ugly though.

If the string doesn't match the regex, the engine answer :

null (java.lang.NullPointerException). Message payload is of type: Boolean

I'm not able to catch this Excetion yet.

Thanks

Upvotes: 0

Views: 5764

Answers (3)

Anirban Sen Chowdhary
Anirban Sen Chowdhary

Reputation: 8311

I am not sure if your regex expression is correct, but if it is you can always use the following code :-

<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>

 <flow name="teFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/test" doc:name="HTTP"/>
        <set-payload value="{&quot;name&quot; : &quot;Italy&quot;}" mimeType="application/json" doc:name="Set Payload"/>
         <json:json-to-object-transformer returnClass="java.lang.Object" doc:name="JSON to Object"/>
         <expression-filter expression="#[message.payload.name.matches('^[a-zA-Z]{3,15}$')]" doc:name="Expression"/>
         <logger level="INFO" message="message paased !!" doc:name="Logger"/>
        <json:object-to-json-transformer doc:name="Object to JSON"/>
    </flow>

It will work as you expect :)

Upvotes: 1

Fly Toucan
Fly Toucan

Reputation: 1

Return false if matches error:

#[?payload['name'].matches('^[a-zA-Z]{3,15}$') or false]

Upvotes: 0

nbsp
nbsp

Reputation: 340

see the section on JSON processing or take a look at this similar question

Upvotes: 1

Related Questions