1

I'm new to Couchdb just a few week back I git clone the couchdb app called sofa [what and app] . Over the week It was going great but suddenly today I stumble upon something.

Here what I meant when I browse the Sofa app and try to create a Post without the title it prompt with and alert box "The document could not be saved: The database could not be created, the file already exists." which was weird as looking at the source I found that the require (in validate_doc_update.js return its custom json error) something like in this format {"forbidden" : message }) with forbidden as key

  v.forbidden = function(message) {
      throw({forbidden : message})
  };

   v.require = function() {
        for (var i=0; i < arguments.length; i++) {
          var field = arguments[i];
          message = "The '"+field+"' field is required.";
          if (typeof newDoc[field] == "undefined") v.forbidden(message);
        };
      }; 

in validate_doc_update.js

if (newDoc.type == 'post') {
    if (!v.isAuthor()) {
      v.unauthorized("Only authors may edit posts.");
    }
    v.require("created_at", "author", "body", "format", "title");

inspecting the response state that the json returned was found to be different from the json had it would have been return by the above require function in validate_doc_update.js here is the json {"error":"file_exists","reason":"The database could not be created, the file already exists."}

This make be believe that the validation in validation_doc_update.js only execute during updating of document

to Prove this point I try to update a document without the title, expecting that it would return the error but surprisingly the document just got saved

so Here are my Question regarding all the Point I mention above

Does validate_doc_update.js "validate" work only during updation of document

if YES 
   then 
      how can I manage to succeed in updating a post without the error [Weird bypassing the Validation Completely] . +  How can execute validation on create of a document
if NO
   then 
     What is  the Error {"error":"file_exists","reason":"The database could not be created, the file already exists."} that is prevent a document to be saved  

Can anyone please share light on all the questions listed here

Viren
  • 5,812
  • 6
  • 45
  • 98

1 Answers1

2

Yes, the validate_doc_update functions are run only when updating documents (include creation and deletion).

The function you show here will allow a document without a title as long as its type is not "post". If you could include the actual request you attempted, I could confirm it.

Finally, the ""The database could not be created" is because you are attempting to create the database (by doing PUT /dbname/ instead of PUT /dbname/docid, I would guess) when it already exists. Again, if you would include the actual request, I could confirm that too.

Robert Newson
  • 4,631
  • 20
  • 18
  • point accepted @Robert but there are thing that are still unclear [1.] The above error {"error":"file_exists", "reason":"The database could not be created,the file already exists"} is generated while the time I'm creating a new "post" record in "sofa" app and not while creating a new database as I'm aware of the above error appearing if someone tries to create a database that already exist but geneartion of error at the time of creating a record is weird,so can you please share some more light on it – Viren Sep 28 '11 at 04:27
  • [2.] If validate_doc_update.js also validate the record during creation then how come the validation applied in sofa app to ensure that no post is created without the title doesnt generate the error for the same if title is not supplied while creating the post inshort the validation is bypass during creation of post. – Viren Sep 28 '11 at 04:43