You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by rn...@apache.org on 2015/08/12 17:54:07 UTC
[07/13] couch commit: updated refs/heads/master to 311ba94
Add couch_db_plugin:validate_docid/1
Project: http://git-wip-us.apache.org/repos/asf/couchdb-couch/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-couch/commit/f28bd7ca
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch/tree/f28bd7ca
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch/diff/f28bd7ca
Branch: refs/heads/master
Commit: f28bd7ca26f01648bc314a511bba9e1ded491d4e
Parents: 4f6db4a
Author: ILYA Khlopotov <ii...@ca.ibm.com>
Authored: Fri Jun 19 10:50:58 2015 -0700
Committer: ILYA Khlopotov <ii...@ca.ibm.com>
Committed: Tue Aug 11 12:24:59 2015 -0700
----------------------------------------------------------------------
src/couch_db_plugin.erl | 8 +++++++-
src/couch_doc.erl | 9 ++++++++-
test/couch_db_plugin_tests.erl | 29 +++++++++++++++++++++++++++--
3 files changed, 42 insertions(+), 4 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/f28bd7ca/src/couch_db_plugin.erl
----------------------------------------------------------------------
diff --git a/src/couch_db_plugin.erl b/src/couch_db_plugin.erl
index 88e37c3..e9aabff 100644
--- a/src/couch_db_plugin.erl
+++ b/src/couch_db_plugin.erl
@@ -15,7 +15,8 @@
-export([
validate_dbname/2,
before_doc_update/2,
- after_doc_read/2
+ after_doc_read/2,
+ validate_docid/1
]).
-define(SERVICE_ID, couch_db).
@@ -44,6 +45,11 @@ after_doc_read(#db{after_doc_read = Fun} = Db, Doc0) ->
[Doc1, _Db] -> Doc1
end.
+validate_docid(Id) ->
+ Handle = couch_epi:get_handle(?SERVICE_ID),
+ %% callbacks return true only if it specifically allow the given Id
+ couch_epi:any(Handle, ?SERVICE_ID, validate_docid, [Id], [ignore_providers]).
+
%% ------------------------------------------------------------------
%% Internal Function Definitions
%% ------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/f28bd7ca/src/couch_doc.erl
----------------------------------------------------------------------
diff --git a/src/couch_doc.erl b/src/couch_doc.erl
index d152a2a..cf34094 100644
--- a/src/couch_doc.erl
+++ b/src/couch_doc.erl
@@ -169,7 +169,14 @@ validate_docid(Id) when is_binary(Id) ->
<<"_design/", _/binary>> -> ok;
<<"_local/", _/binary>> -> ok;
<<"_", _/binary>> ->
- throw({bad_request, <<"Only reserved document ids may start with underscore.">>});
+ case couch_db_plugin:validate_docid(Id) of
+ true ->
+ ok;
+ false ->
+ throw(
+ {bad_request,
+ <<"Only reserved document ids may start with underscore.">>})
+ end;
_Else -> ok
end;
validate_docid(Id) ->
http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/f28bd7ca/test/couch_db_plugin_tests.erl
----------------------------------------------------------------------
diff --git a/test/couch_db_plugin_tests.erl b/test/couch_db_plugin_tests.erl
index 06a2360..bb41474 100644
--- a/test/couch_db_plugin_tests.erl
+++ b/test/couch_db_plugin_tests.erl
@@ -15,7 +15,8 @@
-export([
validate_dbname/2,
before_doc_update/2,
- after_doc_read/2
+ after_doc_read/2,
+ validate_docid/1
]).
-include_lib("couch/include/couch_eunit.hrl").
@@ -50,6 +51,10 @@ after_doc_read({fail, _Doc}, _Db) -> throw(after_doc_read);
after_doc_read({true, Doc}, Db) -> [{true, [after_doc_read|Doc]}, Db];
after_doc_read({false, Doc}, Db) -> [{false, Doc}, Db].
+validate_docid({true, _Id}) -> true;
+validate_docid({false, _Id}) -> false;
+validate_docid({fail, _Id}) -> throw(validate_docid).
+
callback_test_() ->
{
"callback tests",
@@ -66,7 +71,11 @@ callback_test_() ->
fun after_doc_read_match/0,
fun after_doc_read_no_match/0,
- fun after_doc_read_throw/0
+ fun after_doc_read_throw/0,
+
+ fun validate_docid_match/0,
+ fun validate_docid_no_match/0,
+ fun validate_docid_throw/0
]
}
}.
@@ -117,3 +126,19 @@ after_doc_read_throw() ->
?_assertThrow(
after_doc_read,
couch_db_plugin:after_doc_read(#db{}, {fail, [doc]})).
+
+
+validate_docid_match() ->
+ ?_assertMatch(
+ {true, [validate_docid, doc]},
+ couch_db_plugin:validate_docid({true, [doc]})).
+
+validate_docid_no_match() ->
+ ?_assertMatch(
+ {false, [doc]},
+ couch_db_plugin:validate_docid({false, [doc]})).
+
+validate_docid_throw() ->
+ ?_assertThrow(
+ validate_docid,
+ couch_db_plugin:validate_docid({fail, [doc]})).