SlideShare a Scribd company logo
1 of 14
#1 : SCHEMA VALIDATION
“Good code is its own best documentation.”
(Steve McConnell)
Mobile Server – JSON Processor
Mobile Server with Schema Validation
Mobile Client
Validation layer
Mobile Server
• 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?
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”
}
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
#2: FUNCTIONAL PROGRAMMING
"It is better to have 100 functions operate on one data
structure than 10 functions on 10 data structures."
—Alan Perlis
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];
}
Why?
• Easy to use & understand
• Simple to test. No before clauses required to
setup big objects.
• No coupling
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
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;
});
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);
}
});
}
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);
}
});
}
Functional libraries & resources
• Underscore.js (http://underscorejs.org )
• lodash.js (http://lodash.com )
• Wu.js (http://fitzgen.github.com/wu.js )

More Related Content

What's hot

Unit testing JavaScript using Mocha and Node
Unit testing JavaScript using Mocha and NodeUnit testing JavaScript using Mocha and Node
Unit testing JavaScript using Mocha and Node
Josh Mock
 
Zero to Testing in JavaScript
Zero to Testing in JavaScriptZero to Testing in JavaScript
Zero to Testing in JavaScript
pamselle
 

What's hot (19)

Intro to Ruby on Rails
Intro to Ruby on RailsIntro to Ruby on Rails
Intro to Ruby on Rails
 
Unit testing JavaScript using Mocha and Node
Unit testing JavaScript using Mocha and NodeUnit testing JavaScript using Mocha and Node
Unit testing JavaScript using Mocha and Node
 
Elixir and Phoenix for Rubyists
Elixir and Phoenix for RubyistsElixir and Phoenix for Rubyists
Elixir and Phoenix for Rubyists
 
Apache Flink - A Sneek Preview on Language Integrated Queries
Apache Flink - A Sneek Preview on Language Integrated QueriesApache Flink - A Sneek Preview on Language Integrated Queries
Apache Flink - A Sneek Preview on Language Integrated Queries
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
 
Zero to Testing in JavaScript
Zero to Testing in JavaScriptZero to Testing in JavaScript
Zero to Testing in JavaScript
 
Moose: Perl Objects
Moose: Perl ObjectsMoose: Perl Objects
Moose: Perl Objects
 
Elm dev front-end
Elm   dev front-endElm   dev front-end
Elm dev front-end
 
Play framework training by Neelkanth Sachdeva @ Scala traits event , New Delh...
Play framework training by Neelkanth Sachdeva @ Scala traits event , New Delh...Play framework training by Neelkanth Sachdeva @ Scala traits event , New Delh...
Play framework training by Neelkanth Sachdeva @ Scala traits event , New Delh...
 
Underscore
UnderscoreUnderscore
Underscore
 
Why and how Pricing Assistant migrated from Celery to RQ - Paris.py #2
Why and how Pricing Assistant migrated from Celery to RQ - Paris.py #2Why and how Pricing Assistant migrated from Celery to RQ - Paris.py #2
Why and how Pricing Assistant migrated from Celery to RQ - Paris.py #2
 
MidwestJS Zero to Testing
MidwestJS Zero to TestingMidwestJS Zero to Testing
MidwestJS Zero to Testing
 
RSpec 2 Best practices
RSpec 2 Best practicesRSpec 2 Best practices
RSpec 2 Best practices
 
Apache Velocity
Apache Velocity Apache Velocity
Apache Velocity
 
Introduction to Jquery
Introduction to Jquery Introduction to Jquery
Introduction to Jquery
 
Phoenix demysitify, with fun
Phoenix demysitify, with funPhoenix demysitify, with fun
Phoenix demysitify, with fun
 
Underscore.js
Underscore.jsUnderscore.js
Underscore.js
 
Crafting [Better] API Clients
Crafting [Better] API ClientsCrafting [Better] API Clients
Crafting [Better] API Clients
 
TDD with PhpSpec
TDD with PhpSpecTDD with PhpSpec
TDD with PhpSpec
 

Similar to Nodejs functional programming and schema validation lightning talk

Build web application by express
Build web application by expressBuild web application by express
Build web application by express
Shawn Meng
 
node.js practical guide to serverside javascript
node.js practical guide to serverside javascriptnode.js practical guide to serverside javascript
node.js practical guide to serverside javascript
Eldar Djafarov
 
Intro To JavaScript Unit Testing - Ran Mizrahi
Intro To JavaScript Unit Testing - Ran MizrahiIntro To JavaScript Unit Testing - Ran Mizrahi
Intro To JavaScript Unit Testing - Ran Mizrahi
Ran Mizrahi
 
Rapid Prototyping with Solr
Rapid Prototyping with SolrRapid Prototyping with Solr
Rapid Prototyping with Solr
Erik Hatcher
 
540slidesofnodejsbackendhopeitworkforu.pdf
540slidesofnodejsbackendhopeitworkforu.pdf540slidesofnodejsbackendhopeitworkforu.pdf
540slidesofnodejsbackendhopeitworkforu.pdf
hamzadamani7
 
Ruby on Rails Oracle adaptera izstrāde
Ruby on Rails Oracle adaptera izstrādeRuby on Rails Oracle adaptera izstrāde
Ruby on Rails Oracle adaptera izstrāde
Raimonds Simanovskis
 
json.ppt download for free for college project
json.ppt download for free for college projectjson.ppt download for free for college project
json.ppt download for free for college project
AmitSharma397241
 

Similar to Nodejs functional programming and schema validation lightning talk (20)

Scalable web application architecture
Scalable web application architectureScalable web application architecture
Scalable web application architecture
 
Writing RESTful web services using Node.js
Writing RESTful web services using Node.jsWriting RESTful web services using Node.js
Writing RESTful web services using Node.js
 
Take Data Validation Seriously - Paul Milham, WildWorks
Take Data Validation Seriously - Paul Milham, WildWorksTake Data Validation Seriously - Paul Milham, WildWorks
Take Data Validation Seriously - Paul Milham, WildWorks
 
Build web application by express
Build web application by expressBuild web application by express
Build web application by express
 
Scala in Places API
Scala in Places APIScala in Places API
Scala in Places API
 
Python RESTful webservices with Python: Flask and Django solutions
Python RESTful webservices with Python: Flask and Django solutionsPython RESTful webservices with Python: Flask and Django solutions
Python RESTful webservices with Python: Flask and Django solutions
 
Make BDD great again
Make BDD great againMake BDD great again
Make BDD great again
 
Programming in java basics
Programming in java  basicsProgramming in java  basics
Programming in java basics
 
CoffeeScript - A Rubyist's Love Affair
CoffeeScript - A Rubyist's Love AffairCoffeeScript - A Rubyist's Love Affair
CoffeeScript - A Rubyist's Love Affair
 
Django in the Office: Get Your Admin for Nothing and Your SQL for Free
Django in the Office: Get Your Admin for Nothing and Your SQL for FreeDjango in the Office: Get Your Admin for Nothing and Your SQL for Free
Django in the Office: Get Your Admin for Nothing and Your SQL for Free
 
Introduction to AWS Step Functions:
Introduction to AWS Step Functions: Introduction to AWS Step Functions:
Introduction to AWS Step Functions:
 
node.js practical guide to serverside javascript
node.js practical guide to serverside javascriptnode.js practical guide to serverside javascript
node.js practical guide to serverside javascript
 
Intro To JavaScript Unit Testing - Ran Mizrahi
Intro To JavaScript Unit Testing - Ran MizrahiIntro To JavaScript Unit Testing - Ran Mizrahi
Intro To JavaScript Unit Testing - Ran Mizrahi
 
Rapid Prototyping with Solr
Rapid Prototyping with SolrRapid Prototyping with Solr
Rapid Prototyping with Solr
 
IaaS with ARM templates for Azure
IaaS with ARM templates for AzureIaaS with ARM templates for Azure
IaaS with ARM templates for Azure
 
540slidesofnodejsbackendhopeitworkforu.pdf
540slidesofnodejsbackendhopeitworkforu.pdf540slidesofnodejsbackendhopeitworkforu.pdf
540slidesofnodejsbackendhopeitworkforu.pdf
 
Don't Be Afraid of Abstract Syntax Trees
Don't Be Afraid of Abstract Syntax TreesDon't Be Afraid of Abstract Syntax Trees
Don't Be Afraid of Abstract Syntax Trees
 
Json the-x-in-ajax1588
Json the-x-in-ajax1588Json the-x-in-ajax1588
Json the-x-in-ajax1588
 
Ruby on Rails Oracle adaptera izstrāde
Ruby on Rails Oracle adaptera izstrādeRuby on Rails Oracle adaptera izstrāde
Ruby on Rails Oracle adaptera izstrāde
 
json.ppt download for free for college project
json.ppt download for free for college projectjson.ppt download for free for college project
json.ppt download for free for college project
 

Recently uploaded

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 

Nodejs functional programming and schema validation lightning talk

  • 1. #1 : SCHEMA VALIDATION “Good code is its own best documentation.” (Steve McConnell)
  • 2. Mobile Server – JSON Processor
  • 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); } }); }
  • 14. Functional libraries & resources • Underscore.js (http://underscorejs.org ) • lodash.js (http://lodash.com ) • Wu.js (http://fitzgen.github.com/wu.js )