couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tonysu...@apache.org
Subject [11/26] couchdb-mango git commit: Add text index field validator
Date Thu, 10 Sep 2015 00:34:13 GMT
Add text index field validator

Our original text field validator let any list through. The new
validator makes text fields definitions more restrictive.

BugzID: 46817


Project: http://git-wip-us.apache.org/repos/asf/couchdb-mango/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-mango/commit/a814fe78
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-mango/tree/a814fe78
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-mango/diff/a814fe78

Branch: refs/heads/master
Commit: a814fe78b76c2b84ad2f06dbb87ddb0b57fc1de3
Parents: 85f8a2b
Author: Tony Sun <tony.sun@cloudant.com>
Authored: Tue Aug 25 13:40:06 2015 -0700
Committer: Tony Sun <tony.sun@cloudant.com>
Committed: Tue Aug 25 13:40:06 2015 -0700

----------------------------------------------------------------------
 src/mango_error.erl        |  8 ++++++++
 src/mango_idx_text.erl     | 26 +++++++++++++++++++++++---
 test/01-index-crud-test.py | 21 +++++++++++++++++++++
 3 files changed, 52 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/a814fe78/src/mango_error.erl
----------------------------------------------------------------------
diff --git a/src/mango_error.erl b/src/mango_error.erl
index cf117ab..6ad76bb 100644
--- a/src/mango_error.erl
+++ b/src/mango_error.erl
@@ -141,6 +141,14 @@ info(mango_idx_view, {invalid_index_json, BadIdx}) ->
         <<"invalid_index">>,
         fmt("JSON indexes must be an object, not: ~w", [BadIdx])
     };
+info(mango_idx_text, {invalid_index_fields_definition, Def}) ->
+    {
+        400,
+        <<"invalid_index_fields_definition">>,
+        fmt("Text Index field definitions must be of the form
+            {\"name\": \"fieldname\", \"type\":
+                \"boolean,number, or string\"}. Def: ~p", [Def])
+    };
 info(mango_idx_view, {index_not_found, BadIdx}) ->
     {
         404,

http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/a814fe78/src/mango_idx_text.erl
----------------------------------------------------------------------
diff --git a/src/mango_idx_text.erl b/src/mango_idx_text.erl
index cce978e..321889a 100644
--- a/src/mango_idx_text.erl
+++ b/src/mango_idx_text.erl
@@ -15,6 +15,7 @@
 
 -export([
     validate/1,
+    validate_fields/1,
     add/2,
     remove/2,
     from_ddoc/1,
@@ -148,12 +149,31 @@ def_to_json([{Key, Value} | Rest]) ->
 
 fields_to_json([]) ->
     [];
-fields_to_json([{[{<<"name">>, Name}, {<<"type">>, Type}]} | Rest])
->
+fields_to_json([{[{<<"name">>, Name}, {<<"type">>, Type0}]} | Rest])
->
+    Type = validate_field_type(Type0),
     [{[{Name, Type}]} | fields_to_json(Rest)];
-fields_to_json([{[{<<"type">>, Type}, {<<"name">>, Name}]} | Rest])
->
+fields_to_json([{[{<<"type">>, Type0}, {<<"name">>, Name}]} | Rest])
->
+    Type = validate_field_type(Type0),
     [{[{Name, Type}]} | fields_to_json(Rest)].
 
 
+validate_field_type(<<"string">>) ->
+    <<"string">>;
+validate_field_type(<<"number">>) ->
+    <<"number">>;
+validate_field_type(<<"boolean">>) ->
+    <<"boolean">>.
+
+
+validate_fields(Fields) ->
+    try fields_to_json(Fields) of
+        _ ->
+            mango_fields:new(Fields)
+    catch error:function_clause ->
+        ?MANGO_ERROR({invalid_index_fields_definition, Fields})
+    end.
+
+
 opts() ->
     [
         {<<"default_analyzer">>, [
@@ -176,7 +196,7 @@ opts() ->
             {tag, fields},
             {optional, true},
             {default, []},
-            {validator, fun mango_opts:validate_fields/1}
+            {validator, fun ?MODULE:validate_fields/1}
         ]}
     ].
 

http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/a814fe78/test/01-index-crud-test.py
----------------------------------------------------------------------
diff --git a/test/01-index-crud-test.py b/test/01-index-crud-test.py
index 31e0b08..bd41516 100644
--- a/test/01-index-crud-test.py
+++ b/test/01-index-crud-test.py
@@ -241,6 +241,27 @@ class IndexCrudTests(mango.DbPerClass):
             return
         raise AssertionError("index not created")
 
+    def test_create_bad_text_idx(self):
+        bad_fields = [
+            True,
+            False,
+            "bing",
+            2.0,
+            ["foo", "bar"],
+            [{"name": "foo2"}],
+            [{"name": "foo3", "type": "garbage"}],
+            [{"type": "number"}],
+            [{"name": "age", "type": "number"} , {"name": "bad"}],
+            [{"name": "age", "type": "number"} , "bla"]
+        ]
+        for fields in bad_fields:
+            try:
+                self.db.create_text_index(fields=fields)
+            except Exception, e:
+                assert e.response.status_code == 400
+            else:
+                raise AssertionError("bad create text index")
+
     def test_limit_skip_index(self):
         fields = ["field1"]
         ret = self.db.create_index(fields, name="idx_01")


Mime
View raw message