3. Mobile Server with Schema Validation
Mobile Client
Validation layer
Mobile Server
4. • Every request / response goes through validation with schema.
• In case of response validation failure, the response is a 500
• In case of request validation failure, the response is a 400.
How?
5. Avro Schema
{
"type" : "record",
"name" : ”Person",
"namespace": "com.linkedin.mobileserver.templates",
"doc" : ”Represents all the data fields for a person.",
"fields" : [
{
"name" : "firstName",
"type" : "string",
"doc" : "First name of the person"
},
{
"name" : "lastName",
"type" : "string",
"doc" : "Last name of the person"
}],
"version" : 1
}
{
firstName: “Deepank”,
lastName: “Gupta”
}
6. How can I use it?
• Avro is open-source, available in Apache.
Schema details:
http://avro.apache.org/docs/current/spec.ht
ml
• Node module for helping with
Avro:https://npmjs.org/package/avro-schema
7. #2: FUNCTIONAL PROGRAMMING
"It is better to have 100 functions operate on one data
structure than 10 functions on 10 data structures."
—Alan Perlis
8. Pure vs Impure Functions
function add(a, b) {
return a + b;
}
function getFirst(name) {
return name.slice(0, 1)[0];
}
var x;
function add() {
x = this.a + this.b;
}
function getFirst(name) {
return name.splice(0, 1)[0];
}
9. Why?
• Easy to use & understand
• Simple to test. No before clauses required to
setup big objects.
• No coupling
10. Higher Order Functions
• What?
– Functions that accept functions as parameters or
return functions
– Yes… functions are just like any other data
• Why?
– Code Reuse
– Terse, beautiful code
11. Functions that take a function as input
var res = [];
var l = arr.length;
for (var i = 0, i < l; ++i) {
res.push(arr[i]);
}
var res = _.map(arr, function(e){
return e;
});
12. Example without Higher Level
functionsfunction getProfile(req, res) {
ProfileHelper.get(req, function(err, result) {
if (err) {
var error = getError(500, {msg: err});
Util.sendError(req, res, error);
} else {
Util.sendJsonResponse(req, res, result);
}
});
}
function getUpdates(req, res) {
UpdatesHelper.get(req, function(err, result) {
if (err) {
var error = getError(500, {msg: err});
Util.sendError(req, res, error);
} else {
Util.sendJsonResponse(req, res, result);
}
});
}
13. Example with higher order function
function getProfile(req, res) {
ProfileHelper.get(req, jsonResponder(req, res, 500));
}
function getUpdates(req, res) {
UpdatesHelper.get(req, jsonResponder(req, res, 400));
}
function jsonResponder(req, res, errorKey) {
return function(err, result) {
if (err) {
var error = getError(errorKey, {msg: err});
Util.sendError(req, res, error);
} else {
Util.sendJsonResponse(req, res, result);
}
});
}