You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Heike Bernhard <he...@gmx.de> on 2010/01/09 00:11:31 UTC

Validate_doc_update required fields - can't delete documents

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\");
  }
}"

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



Re: Validate_doc_update required fields - can't delete documents

Posted by Heike Bernhard <he...@gmx.de>.
Chris Anderson wrote:
> On Fri, Jan 8, 2010 at 3:25 PM, Heike Bernhard <he...@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
> 

Hello Chris,

thank you for your fast response. This helped me to understand how this
works.

I've also cloned sofa and had the same problem with deleting documents.
But now it's clear. I wouldn't blame the book. Nevertheless more code
samples are always a good thing for newbies like me :)

Heike


>>> 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
>>
> 
> 
> 


Re: Validate_doc_update required fields - can't delete documents

Posted by Chris Anderson <jc...@apache.org>.
On Fri, Jan 8, 2010 at 3:25 PM, Heike Bernhard <he...@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

Re: Validate_doc_update required fields - can't delete documents

Posted by Heike Bernhard <he...@gmx.de>.
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\");
>   }
> }"
> 
> 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