remove now unused {% load url from future %}
[mygpo.git] / couchdb / _design / categories / validate_doc_update.js
blob124ca90e52a56fef6d25ad7bad2007ea5ae7e9ed
1 function(newDoc, oldDoc, userCtx)
3     String.prototype.trim = function()
4     {
5         return this.replace(/^\s+|\s+$/g,"");
6     }
8     function require(field, message)
9     {
10         message = message || "Document must have a " + field;
11         if (newDoc[field] == void 0 || newDoc[field] == null)
12         {
13             throw({forbidden: message});
14         }
15     }
17     function check(cond, message)
18     {
19         message = message || "Condition check failed";
20         if(!cond)
21         {
22             throw({forbidden: message});
23         }
24     }
26     function checkTrimmed(field)
27     {
28         if (newDoc[field] != newDoc[field].trim())
29         {
30             throw({forbidden: field + " must be trimmed"});
31         }
32     }
34     function checkUnique(arr, field)
35     {
36         for(n in arr)
37         {
38             function f(x) {return x==arr[n];};
39             if(arr.filter(f).length > 1)
40             {
41                 throw({forbidden: field + " must be unique"});
42             }
43         }
44     }
47     if(newDoc.doc_type == "Category")
48     {
49         require("label");
50         check(newDoc.label != "", "label must not be empty");
51         checkTrimmed("label");
53         require("updated");
55         if (newDoc.spellings)
56         {
57             check(newDoc.spellings.indexOf(newDoc.label) < 0,
58                     "The label should not be contained in the alternative spellings");
59             checkUnique(newDoc.spellings, "spellings");
60         }
61     }