You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by John Le Brasseur <jo...@gmail.com> on 2017/03/02 10:05:48 UTC

Re: New Document Validation

I have found an answer to this question [here][1].(In the
Adding Validation section about 2/3rds of the way down the article.(By
Robert Kowalski  August 28, 2014.))

The adapted code is: (hope I have copied it over correctly)

    {
    "_id": "_design/verifier",
    "_rev": "5-33f3ff3d650bf1594a9357af58153232",
    "validate_doc_update": "function (newDoc, oldDoc, userCtx, secObj) {\n
 if     (newDoc.type == \"xyz\"){\n    if (newDoc._deleted === true) {\n
   return;\n    }\n    if (!newDoc.abc) {\n      throw({forbidden:
\"Document must have an abc.\"});\n    }\n    if (!newDoc.def) {\n
 throw({forbidden: \"Document must have a def.\"});\n    }\n  if
(!newDoc.ghi) {\n      throw({forbidden: \"Document must have a ghi.\"});\n
   }\n  if (!newDoc.jkl.mno) {\n      throw({forbidden: \"Document must
have a jkl mno.\"});\n }\n  if (!newDoc.pqr.stu) {\n      throw({forbidden:
\"Document must have a pqr stu.\"});\n }\n if (!newDoc.vwx.yyy) {\n
 throw({forbidden: \"Document must have a vwx yyy.\"});\n   }\n   }\n  }"
    }

It works just as I wanted it to with the nested json objects.

Many thanks sir for that post.


  [1]: https://www.sitepoint.com/tracking-expenses-couchdb-angular/

On 15 February 2017 at 11:53, John Le Brasseur <jo...@gmail.com>
wrote:

> Hi.
> Could someone give me a pointer as to why this works:
>
> {
>   "_id": "_design/must_have_field_verifier",
>   "_rev": "14-7aa573a99ec8cb007edfe6c8dd72ba15",
>   "validate_doc_update": "function(newDoc, oldDoc, userCtx) {
>  function require(field, message) {
>  message = message || \"Document must have a \" + field;
>   if (!newDoc[field]) throw({forbidden : message});
> };
> {
>  require(\"abc\");
>  }
> }"
> }
>
>
> but 'abc.def' is not recognised in the code below, although the code is
> read and applied:
>
>
>
> {
>   "_id": "_design/must_have_field_verifier",
>   "_rev": "14-7aa573a99ec8cb007edfe6c8dd72ba15",
>   "validate_doc_update": "function(newDoc, oldDoc, userCtx) {
>  function require(field, message) {
>  message = message || \"Document must have a \" + field;
>   if (!newDoc[field]) throw({forbidden : message});
> };
> {
>  require(\"abc.def\");
>  }
> }"
> }
>
>
>
> The reply is "Save failed: Document must have a abc.def"
>
>
>
>
> I have tried including : &&(||) typeof  (!newDoc[field]) === 'object'
> inside the if statement but the code is not accepted as valid.
>
> Sorry. Wish there was a beginners section here.
>
> If I wanted an easy route I would use Mongo. But Couch is a much more
> eloquent system!!
>
>
>
> John
>
>
>
>
>

John.