couchdb

validate_on_update prevents deletion of a specified document


I have a simple validate_on_update function:

if (!newDoc.type) {
    throw({forbidden: "All documents must have a type specified"});
}

If I do

curl -X DELETE $HOST/$DB/$DOC?rev=$REV

I get back

{"error":"forbidden","reason":"All documents must have a type specified"}

This happens even if I do

rev=$REV&type=type

Or if I do

-d'{"type":"type"}'

with curl

How can I bypass validation for deletion of documents?


Solution

  • CouchDB internals only know reads and updates. An update can be the creation of the doc, an edit of a doc, or the deletion of the doc. Update funs can’t be circumvented for any type. To solve this, use

    if(!newDoc.type && !newDoc._deleted) {
        throw({forbidden: "All documents must have a type specified"});
    }