[Groonga-commit] groonga/express-kotoumi [master] Implement validation for numeric/integer parameter

Back to archive index

YUKI Hiroshi null+****@clear*****
Wed Jan 16 15:20:18 JST 2013


YUKI Hiroshi	2013-01-16 15:20:18 +0900 (Wed, 16 Jan 2013)

  New Revision: 25da0863585fc96bb3f9ea8abd107365c3c6a5e5
  https://github.com/groonga/express-kotoumi/commit/25da0863585fc96bb3f9ea8abd107365c3c6a5e5

  Log:
    Implement validation for numeric/integer parameter

  Modified files:
    lib/builders/rest-request.js

  Modified: lib/builders/rest-request.js (+21 -3)
===================================================================
--- lib/builders/rest-request.js    2013-01-16 13:03:53 +0900 (f99734f)
+++ lib/builders/rest-request.js    2013-01-16 15:20:18 +0900 (221abeb)
@@ -1,5 +1,23 @@
+var NUMERIC = /^-?(?:[0-9]+(?:\.[0-9]+)?|\.[0-9]+)$/;
+function getNumericValue(params, key) {
+  var value = params[key];
+  if (!NUMERIC.test(value))
+    throw new Error(key + ': ' +value + ' is invalid number');
+  return parseFloat(value);
+}
+
+var INTEGER = /^-?[0-9]+$/;
+function getIntegerValue(params, key) {
+  var value = params[key];
+  if (!INTEGER.test(value))
+    throw new Error(key + ': ' +value + ' is invalid integer');
+  return parseInt(value);
+}
+
 function searchRequestBuilder(request) {
   var params = request.params;
+  if (!params.tableName)
+    throw new Error('no source is specified');
 
   var queries = {};
   queries.result = {
@@ -16,13 +34,13 @@ function searchRequestBuilder(request) {
   if (params.attributes)
     queries.result.attributes = params.attributes.split(',');
   if (params.limit)
-    queries.result.limit = parseInt(params.limit);
+    queries.result.limit = getIntegerValue(params, 'limit');
   if (params.match_escalation_threshold)
-    queries.result.matchEscalationThreshold = parseInt(params.match_escalation_threshold);
+    queries.result.matchEscalationThreshold = getIntegerValue(params, 'match_escalation_threshold');
   if (params.match_to)
     queries.result.matchTo = params.match_to.split(',');
   if (params.offset)
-    queries.result.offset = parseInt(params.offset);
+    queries.result.offset = getIntegerValue(params, 'offset');
   if (params.query)
     queries.result.query = params.query;
   if (params.sort_by)
-------------- next part --------------
HTML����������������������������...
Télécharger 



More information about the Groonga-commit mailing list
Back to archive index