Github user rnewson commented on a diff in the pull request:
https://github.com/apache/couchdb-couch-mrview/pull/28#discussion_r40000601
--- Diff: src/couch_mrview.erl ---
@@ -46,16 +46,103 @@
}).
-validate(DbName, DDoc) ->
- {Fields} = DDoc#doc.body,
- case couch_util:get_value(<<"options">>, Fields, {[]}) of
- {_} -> ok;
- _ -> throw({invalid_design_doc, <<"`options` parameter must be an
object.">>})
+
+%% Validate "views" name : value mapping.
+%%
+%% In the most common case name is the view name and
+%% value is an object that must have a "map" function,
+%% and an optional "reduce" function. "map" and "reduce"
+%% values should be strings (function contents).
+%%
+%% "lib" is a special case. The value
+%% of "lib" is an object that will contain libary code
+%% so it not validated.
+%%
+validate_view(<<"lib">>, {Libs})->
+ ok;
+validate_view(VName, {Views}) ->
+ case couch_util:get_value(<<"map">>, Views) of
+ MapVal when is_binary(MapVal) ->
+ ok;
+ undefined ->
+ ok;
+ _ ->
+ Err1 = <<"`map` in ", VName/binary, " must be a string">>,
+ throw({invalid_design_doc, Err1})
end,
- case couch_util:get_value(<<"views">>, Fields, {[]}) of
- {_} -> ok;
- _ -> throw({invalid_design_doc, <<"`views` parameter must be an
object.">>})
+ case couch_util:get_value(<<"reduce">>, Views) of
+ RedVal when is_binary(RedVal) ->
+ ok;
+ undefined ->
+ ok; % note: unlike map, reduce function is optional
+ _ ->
+ Err2 = <<"`reduce` in ", VName/binary, " must be a string">>,
+ throw({invalid_design_doc, Err2})
end,
+ ok;
+validate_view(VName, _) ->
+ throw({invalid_design_doc, <<"View ", VName/binary, " must be an
object">>}).
+
+
+%% Validate top level design document objects.
+%%
+%% Most cases (shows,lists,fitlers,...) will be
+%% function_name : function_contents(string) mappings.
+%% For example, lists can look like:
+%% {
+%% "lst1" : "function(head, req){...}",
+%% "lst2" : "function(head, req){...}"
+%% }
+%%
+%% With 2 excpetions:
+%% - views : Validated by a special function.
+%% - options : Allowed to contain non-string values.
+%%
+validate_opt_object(<<"views">>, {Views})->
+ [validate_view(VName, ViewObj) || {VName, ViewObj} <- Views],
+ ok;
+validate_opt_object(<<"options">>, {_})->
+ ok;
+validate_opt_object(Section, {Fields}) ->
+ [validate_opt_string(FName, FVal, Section) || {FName, FVal} <- Fields],
+ ok;
+validate_opt_object(_, undefined) ->
+ ok;
+validate_opt_object(FieldName, _) ->
+ throw({invalid_design_doc, <<"`", FieldName/binary, "` is not an
object">>}).
+
+
+%% If this field value is present, it must be a string
+validate_opt_string(FName, FVal)->
--- End diff --
spacing
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---