Validate and modify data in Mobile Services by using server scripts
This topic shows you how to leverage server scripts in Windows Azure Mobile Services. Server scripts are registered in a mobile service and can be used to perform a wide range of operations on data being inserted and updated, including validation and data modification. In this tutorial, you will define and register server scripts that validate and modify data. Because the behavior of server side scripts often affects the client, you will also update your iOS app to take advantage of these new behaviors.
This tutorial walks you through these basic steps:
- Add string length validation
- Update the client to support validation
This tutorial builds on the steps and the sample app from the previous tutorial Get started with data. Before you begin this tutorial, you must first complete Get started with data.
Add validation
It is always a good practice to validate the length of data that is submitted by users. First, you register a script that validates the length of string data sent to the mobile service and rejects strings that are too long, in this case longer than 10 characters.
-
Log into the Windows Azure Management Portal, click Mobile Services, and then click your app.

-
Click the Data tab, then click the TodoItem table.

-
Click Script, then select the Insert operation.

-
Replace the existing script with the following function, and then click Save.
function insert(item, user, request) {
if (item.text.length > 10) {
request.respond(statusCodes.BAD_REQUEST, 'Text length must be 10 characters or less.');
} else {
request.execute();
}
} This script checks the length of the text property and sends an error response when the length exceeds 10 characters. Otherwise, the execute method is called to complete the insert.
Note
You can remove a registered script on the Script tab by clicking Clear and then Save.
Update the client
Now that the mobile service is validating data and sending error responses, you need to update your app to be able to handle error responses from validation.
-
In Xcode, open the project that you modified when you completed the tutorial Get started with data.
-
Press the Run button (Command + R) to build the project and start the app, then type text longer than 10 characters in the textbox and click the plus (+) icon.
Notice that the app raises an unhandled error as a result of the 400 response (Bad Request) returned by the mobile service.
-
In the QSTodoService.m file, locate the following line of code in the addItem method:
[self logErrorIfNotNil:error];
After this line of code, replace the remainder of the completion block with the following code:
BOOL goodRequest = !((error) && (error.code == MSErrorMessageErrorCode));
// detect text validation error from service.
if (goodRequest) // The service responded appropriately
{
NSUInteger index = [items count];
[(NSMutableArray *)items insertObject:result atIndex:index];
// Let the caller know that we finished
completion(index);
}
else{
// if there's an error that came from the service
// log it, and popup up the returned string.
if (error && error.code == MSErrorMessageErrorCode) {
NSLog(@"ERROR %@", error);
UIAlertView *av =
[[UIAlertView alloc]
initWithTitle:@"Request Failed"
message:error.localizedDescription
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil
];
[av show];
}
} This logs the error to the output window and displays it to the user.
-
Rebuild and start the app.

Notice that error is handled and the error messaged is displayed to the user.
Next steps
Now that you have completed this tutorial, consider continuing on with the final tutorial in the data series: Refine queries with paging.
Server scripts are also used when authorizing users and for sending push notifications. For more information see the following tutorials: