Return-Path: Delivered-To: apmail-couchdb-user-archive@www.apache.org Received: (qmail 58686 invoked from network); 9 Jan 2010 17:35:53 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 9 Jan 2010 17:35:53 -0000 Received: (qmail 48820 invoked by uid 500); 9 Jan 2010 17:35:52 -0000 Delivered-To: apmail-couchdb-user-archive@couchdb.apache.org Received: (qmail 48734 invoked by uid 500); 9 Jan 2010 17:35:52 -0000 Mailing-List: contact user-help@couchdb.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: user@couchdb.apache.org Delivered-To: mailing list user@couchdb.apache.org Received: (qmail 48724 invoked by uid 99); 9 Jan 2010 17:35:51 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 09 Jan 2010 17:35:51 +0000 X-ASF-Spam-Status: No, hits=1.5 required=10.0 tests=SPF_PASS,WEIRD_PORT X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of heikebernhard@gmx.de designates 213.165.64.20 as permitted sender) Received: from [213.165.64.20] (HELO mail.gmx.net) (213.165.64.20) by apache.org (qpsmtpd/0.29) with SMTP; Sat, 09 Jan 2010 17:35:44 +0000 Received: (qmail invoked by alias); 09 Jan 2010 17:35:22 -0000 Received: from unknown (EHLO [192.168.2.106]) [91.12.209.58] by mail.gmx.net (mp072) with SMTP; 09 Jan 2010 18:35:22 +0100 X-Authenticated: #4713228 X-Provags-ID: V01U2FsdGVkX18uo0A5gdUuP4vaEHrYHOq4ODYIJDoV/EAonmYSBQ M96q3hbBECDZDf Message-ID: <4B48BE52.1030700@gmx.de> Date: Sat, 09 Jan 2010 18:35:14 +0100 From: Heike Bernhard User-Agent: Thunderbird 2.0.0.23 (X11/20090817) MIME-Version: 1.0 To: user@couchdb.apache.org Subject: Re: Validate_doc_update required fields - can't delete documents References: <4B47BBA3.4030700@gmx.de> <4B47BEE2.4050409@gmx.de> In-Reply-To: X-Enigmail-Version: 0.95.7 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Y-GMX-Trusted: 0 X-FuHaFi: 0.47 Chris Anderson wrote: > On Fri, Jan 8, 2010 at 3:25 PM, Heike Bernhard 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 >> > > >