Nodejs functional programming and schema validation lightning talk

974 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
974
On SlideShare
0
From Embeds
0
Number of Embeds
79
Actions
Shares
0
Downloads
3
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Nodejs functional programming and schema validation lightning talk

  1. 1. #1 : SCHEMA VALIDATION“Good code is its own best documentation.”(Steve McConnell)
  2. 2. Mobile Server – JSON Processor
  3. 3. Mobile Server with Schema ValidationMobile ClientValidation layerMobile Server
  4. 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. 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. 6. How can I use it?• Avro is open-source, available in Apache.Schema details:http://avro.apache.org/docs/current/spec.html• Node module for helping withAvro:https://npmjs.org/package/avro-schema
  7. 7. #2: FUNCTIONAL PROGRAMMING"It is better to have 100 functions operate on one datastructure than 10 functions on 10 data structures."—Alan Perlis
  8. 8. Pure vs Impure Functionsfunction 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. 9. Why?• Easy to use & understand• Simple to test. No before clauses required tosetup big objects.• No coupling
  10. 10. Higher Order Functions• What?– Functions that accept functions as parameters orreturn functions– Yes… functions are just like any other data• Why?– Code Reuse– Terse, beautiful code
  11. 11. Functions that take a function as inputvar 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. 12. Example without Higher Levelfunctionsfunction 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. 13. Example with higher order functionfunction 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);}});}
  14. 14. Functional libraries & resources• Underscore.js (http://underscorejs.org )• lodash.js (http://lodash.com )• Wu.js (http://fitzgen.github.com/wu.js )

×