Can I do data validation via RegularExpression Policy

Can I validate my Json Payload for special characters using regular expression policy. below is my code snippet. Also, if I want to check the data type like customer_id cannot be in alphabets. Is this possible ?. If yes, how can I add the regular expression. 

<JSONPayload escapeSlashCharacter="true">
<JSONPath>
<Expression>$.Customer_ID</Expression>
<Pattern><![CDATA[[$&+,:;=?@#|'<>.-^*()%!]]]></Pattern>
</JSONPath>

3 3 83
3 REPLIES 3

Hi @hiramannan 

Yes you can do JSON validation in ReqularExpressionProtection policy, Please find the below pattern which will allow only if the Customer_ID is in digits.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<RegularExpressionProtection async="false" continueOnError="false" enabled="true" name="REP-Test">
    <DisplayName>REP-Test</DisplayName>
    <Properties/>
    <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
    <JSONPayload>
        <JSONPath>
            <Pattern><![CDATA[[^0-9]]]></Pattern>
            <Expression>$.Customer_ID</Expression>
        </JSONPath>
    </JSONPayload>
    <Source>request</Source>
</RegularExpressionProtection>

 Good luck 🙂 

This didn't work out in my case @chrismca73 as the values in payload are in string format. 

{
    "Customer_ID": "9000",
    "Document_Date": "20231121",
    "Invoice_Ref": "FFM-invoice1",
    "Tax_Code": "B3"
}

@hiramannan  That's strange. Can you please post the complete curl request & response for this particular issue? I tried with same JSON body, It's working for me.

curl -X POST 'https://xxx.yyy.zzz/v1/abcd' \
-H 'Content-Type: application/json' \
-d '{
"Customer_ID": "9000",
"Document_Date": "20231121",
"Invoice_Ref": "FFM-invoice1",
"Tax_Code": "B3"
}'

It validated through the ReqularExpressionProtection policy & sent response as 200 OK.