Do not allow empty field name

Currently, the indexer crashes when a field name is empty. Even though
it's valid json, we should disallow empty field names to coincide
with selector syntax that requires a non-empty field name for queries.

COUCHDB-3202
diff --git a/src/mango_error.erl b/src/mango_error.erl
index 76a8362..7d77b5e 100644
--- a/src/mango_error.erl
+++ b/src/mango_error.erl
@@ -152,7 +152,7 @@
         400,
         <<"invalid_index_fields_definition">>,
         fmt("Text Index field definitions must be of the form
-            {\"name\": \"fieldname\", \"type\":
+            {\"name\": \"non-empty fieldname\", \"type\":
                 \"boolean,number, or string\"}. Def: ~p", [Def])
     };
 info(mango_idx_view, {index_not_found, BadIdx}) ->
diff --git a/src/mango_idx_text.erl b/src/mango_idx_text.erl
index 4cfda5a..ad9d2e8 100644
--- a/src/mango_idx_text.erl
+++ b/src/mango_idx_text.erl
@@ -159,13 +159,25 @@
 fields_to_json([]) ->
     [];
 fields_to_json([{[{<<"name">>, Name}, {<<"type">>, Type0}]} | Rest]) ->
+    ok = validate_field_name(Name),
     Type = validate_field_type(Type0),
     [{[{Name, Type}]} | fields_to_json(Rest)];
 fields_to_json([{[{<<"type">>, Type0}, {<<"name">>, Name}]} | Rest]) ->
+    ok = validate_field_name(Name),
     Type = validate_field_type(Type0),
     [{[{Name, Type}]} | fields_to_json(Rest)].
 
 
+%% In the future, we can possibly add more restrictive validation.
+%% For now, let's make sure the field name is not blank.
+validate_field_name(<<"">>) ->
+    throw(invalid_field_name);
+validate_field_name(Else) when is_binary(Else)->
+    ok;
+validate_field_name(_) ->
+    throw(invalid_field_name).
+
+
 validate_field_type(<<"string">>) ->
     <<"string">>;
 validate_field_type(<<"number">>) ->
@@ -181,6 +193,8 @@
         _ ->
             mango_fields:new(Fields)
     catch error:function_clause ->
+        ?MANGO_ERROR({invalid_index_fields_definition, Fields});
+    throw:invalid_field_name ->
         ?MANGO_ERROR({invalid_index_fields_definition, Fields})
     end.
 
diff --git a/src/mango_sort.erl b/src/mango_sort.erl
index 796ed33..17249c2 100644
--- a/src/mango_sort.erl
+++ b/src/mango_sort.erl
@@ -47,6 +47,8 @@
     [Dir || {_Name, Dir} <- Props].
 
 
+sort_field(<<"">>) ->
+    ?MANGO_ERROR({invalid_sort_field, <<"">>});
 sort_field(Field) when is_binary(Field) ->
     {Field, <<"asc">>};
 sort_field({[{Name, <<"asc">>}]}) when is_binary(Name) ->
diff --git a/test/01-index-crud-test.py b/test/01-index-crud-test.py
index a44e149..342c94f 100644
--- a/test/01-index-crud-test.py
+++ b/test/01-index-crud-test.py
@@ -26,7 +26,8 @@
             {"foo": "bar"},
             [{"foo": 2}],
             [{"foo": "asc", "bar": "desc"}],
-            [{"foo": "asc"}, {"bar": "desc"}]
+            [{"foo": "asc"}, {"bar": "desc"}],
+            [""]
         ]
         for fields in bad_fields:
             try:
@@ -254,7 +255,8 @@
             [{"name": "foo3", "type": "garbage"}],
             [{"type": "number"}],
             [{"name": "age", "type": "number"} , {"name": "bad"}],
-            [{"name": "age", "type": "number"} , "bla"]
+            [{"name": "age", "type": "number"} , "bla"],
+            [{"name": "", "type": "number"} , "bla"]
         ]
         for fields in bad_fields:
             try: