When your app receives data from an API, you can check the values returned with a request, and tailor your functionality to the results using request Events.
This tutorial will focus on validating values returned with a success status code such as 220: OK – see Respond to API Errors for a guide to handling error status code responses.
With a request added to a page in your app:
- Open the request in the API tab on the right of the editor and select Events. Open the Event associated with the data you want to validate, for example 220: OK.
- Use the plus button to add an Action, choosing If Else so that you can tailor your app behavior to the results of the validation. Edit the Action.
- In the first input, use the Event Data container to select the response field (and property) you want to validate, for example using the Count property of an array field to check how many entries it contains.
- In the center input, select a comparison operator, for example > to test for a value greater than the value you enter next.
- In the final input, select or enter a value to compare to the API response field, for example 0 to test if the array has at least one entry in it.
Save the Action.
- In the nested True and False Events, add Actions to respond to the results of the test, for example to output a message using Set Value on a text Element, or navigating to another page if the data is invalid.
If your API response contains an array and you need to validate the contents of individual entries, you can either use the Iterate Through Array Action in a request status code Event as above, or you can use Element load Events if the array is bound to a dynamic Element in your page.