incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Chris Anderson <jch...@apache.org>
Subject Re: Validate_doc_update required fields - can't delete documents
Date Sat, 09 Jan 2010 05:15:19 GMT
On Fri, Jan 8, 2010 at 3:25 PM, Heike Bernhard <heikebernhard@gmx.de> wrote:
> Heike Bernhard wrote:
>> Hello,
>>
>> I'm new to CouchDB and I've just wrote my first validate function
>> (thanks to the book "CouchDB: The definitive guide"). But now I have a
>> problem, when I want to delete documents.
>>
>> For each document there are two fields required (firstName and lastName)
>> and my function looks like this:
>>
>> "validate_doc_update": "function(newDoc, oldDoc, userCtx)
>> {
>>   function require(field, message)
>>   {
>>     message = message || \"Document requires the field \" + field;
>>     if (!newDoc[field]) throw({forbidden : message});
>>   };
>>   if (newDoc)
>>   {
>>     require(\"lastName\");require(\"firstName\");
>>   }
>> }"
>>

You can check for the _deleted field and short-circut validation
before it requires the other fields.

if (newDoc._deleted && oldDoc.author == userCtx.name) return;

I'm also checking to see that the user is allowed to delete, in this case.

You're right, the book could be more clear on this. I'm not sure if
there's time to add to the code samples before printing, but maybe
there is.

Chris

>> So if I want to delete a document, I get an error, because of the
>> required fields:
>>
>> user@computer:~$ curl -X DELETE
>> http://username:password@localhost:5984/testdb/foo?rev=6-5f2e9784c354bdf95e2d851f7f87d045
>> {"error":"forbidden","reason":"Document requires the field lastName"}
>>
>> What am I missing?
>> Thanks for any hints.
>>
>> Heike
>>
>>
> Sorry, forgot to mention:
> I'm using CouchDB 0.10.0 on Ubuntu 9.04
>



-- 
Chris Anderson
http://jchrisa.net
http://couch.io

Mime
View raw message