cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Help us improve the PTC Community by taking this short Community Survey! X

Validate service response using validator

DeepakParihar
10-Marble

Validate service response using validator

HI, I am starting with Thingworx. I have a query as I am getting service response as jsonarray and i want to validate one parameter of each object in Jsonarray. how to do that using validator. i tried to create one parameter in validator and using expression i am validating it but validator is called for first object of array and it validates the first object only but how to validate the remaining items in Array? Appriciate help in advance. 

Thanks.

5 REPLIES 5

its works for first object of json array. but how to get all response of service and validation each object using validator. i have seen loops are not supported in validator expressions.

So If i get it correctly, you want to validate the JSON array inside the validator? You might use a loop and test if either all of the objects are valid or one of them is invalid and so on.

 

If you want to validate individual object and return valid/invalid for each object in runtime, that might not be possible, as the length might be dynamic, unless you have a static array of objects.

Loops are not allowed in validator expression.

DanZ
15-Moonstone
(To:DeepakParihar)

If you pass the JSON in the Validator you can loop through it.

 

I am using a simple hardcoded Array in this example, but I think a JSON object should work the same way.

 

let arr = [1, null, 3];
let hasInvalidElements = (testArr) => testArr.some((element) => element === null);

Output = hasInvalidElements(arr);

 

In this case I am using the "some" Method to check if there are "null" values inside the array. This method will stop iterating as soon as the provided function (element === null) returns true for an element. So no further unnecessary iteration.

Announcements


Top Tags