return values instead of raising them
[mygpo.git] / couchdb / _design / podcasts / validate_doc_update.js
blob1c33dbdcfdcd07389e8eced508221135d07fb30b
1 function(newDoc, oldDoc, userCtx)
3     function require(doc, field, message)
4     {
5         message = message || "Document must have a " + field;
6         if (doc[field] == void 0 || doc[field] == null || doc[field].length == 0)
7         {
8             throw({forbidden: message});
9         }
10     }
12     function check(cond, message)
13     {
14         message = message || "Condition check failed";
15         if(!cond)
16         {
17             throw({forbidden: message});
18         }
19     }
21     function checkPodcast(podcast)
22     {
23         last_timestamp = null;
24         if(podcast.subscribers)
25         {
26             for(var i=0, len=podcast.subscribers.length; sub=podcast.subscribers[i], i<len; i++)
27             {
28                 check((last_timestamp == null) || (last_timestamp < sub.timestamp), "Subscriber Data must be sorted");
29                 last_timestamp = sub.timestamp;
30             }
31         }
32     }
34     if(newDoc.doc_type == "PodcastGroup")
35     {
36         for(var n=0, len=newDoc.podcasts.length; podcast=newDoc.podcasts[n], n<len; n++)
37         {
38             if(oldDoc)
39             {
40                 oldpodcast = oldDoc.podcasts[n];
41             }
42             else
43             {
44                 oldpodcast = null;
45             }
47             require(podcast, "id");
48             require(podcast, "group");
49             check(podcast.group == newDoc._id);
50             checkPodcast(podcast);
51         }
52     }
53     else if(newDoc.doc_type == "Podcast")
54     {
55         checkPodcast(newDoc);
56     }