I want to validate the field value as integer and make the test value as integer. In below code if the field value is string then I am getting error. Here I need to configure like if the value is not integer then make the test value is "" .
%dw 1.0
%output application/json
%var field="2312321a"
%function isEmpty(value) (value!=null and value!="")
---
{
test: field as :number as :string {format: "###"} as :number when isEmpty(field) otherwise ""
}
expected: eg: 123.44--> 123, 1234-> 1234, 123ab-> "", "" -> ""
You can use a regex to verify if a string contains only numeric values. This is a regex you can use for this purpose. It will handle negative values too.
/[+-]?([0-9]*[.]?[0-9]+)/
And use matches
to check if the string is numeric or not.
%dw 1.0
%output application/json
%var field="2312321a"
%function isNumeric(string) string matches /[+-]?([0-9]*[.]?[0-9]+)/
---
{
test: field as :number as :string{format: "###"} as :number when isNumeric(field) otherwise ""
}