You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Stephen Bartell <sn...@gmail.com> on 2013/06/01 03:13:30 UTC

commonjs in validation docs

Hi all!

Can I use cjs modules in validate_doc_update functions?

I had a look through the docs at http://couchdb.readthedocs.org/en/latest/query-servers.html#commonjs-modules and it seems to suggest that I can have a validate_doc_update function inside a view field.  I tried this and the validation doesn't seem to run.  Is this supposed to work?

Thanks!

Re: commonjs in validation docs

Posted by Alexander Shorin <kx...@gmail.com>.
Hi Travis,

These docs are part of CouchDB project, so they are maintained as like
as whole project. Feel free to contribute is you have or found
something (;
There is special guide how to quick start:
http://docs.couchdb.org/en/latest/contributing.html
--
,,,^..^,,,


On Mon, Jun 3, 2013 at 4:49 PM, Travis Paul <Tr...@vispaul.me> wrote:
> Ok, I feel better now. I saw Stephen's question last night and was about to
> respond but was really confused after seeing those that docs.
> Are those docs maintained by Apache or the community? Just wondering, I
> haven't seen them before.
>
> Thanks
>
>
> On Mon, Jun 3, 2013 at 4:12 AM, Alexander Shorin <kx...@gmail.com> wrote:
>
>> On Mon, Jun 3, 2013 at 11:50 AM, Stephen Bartell <sn...@gmail.com>
>> wrote:
>> > So then am I out of luck when it comes to requires in my validation docs?
>>
>> Why so? I'd a bit modified the example to remove views confusion:
>>
>> {
>>    "_id": "_design/test",
>>    "lib": {
>>        "security": "function user(userctx, secobj){\n  return {\n
>> is_admin: function(){ return userctx.roles.indexOf('_admin') !== -1 }
>>   \n  }  \n}\nexports.user = user;"
>>    },
>>    "validate_doc_update": "function(newdoc, olddoc, userctx, secobj)
>> {\n  user = require('lib/security').user(userctx, secobj);\n  if
>> (!user.is_admin()) { throw({forbidden: '_admin role required'}) };\n}"
>> }
>>
>> I believe the docs example is needed to be completely rewritted.
>>
>>
>> --
>> ,,,^..^,,,
>>

Re: commonjs in validation docs

Posted by Travis Paul <Tr...@visPaul.me>.
Ok, I feel better now. I saw Stephen's question last night and was about to
respond but was really confused after seeing those that docs.
Are those docs maintained by Apache or the community? Just wondering, I
haven't seen them before.

Thanks


On Mon, Jun 3, 2013 at 4:12 AM, Alexander Shorin <kx...@gmail.com> wrote:

> On Mon, Jun 3, 2013 at 11:50 AM, Stephen Bartell <sn...@gmail.com>
> wrote:
> > So then am I out of luck when it comes to requires in my validation docs?
>
> Why so? I'd a bit modified the example to remove views confusion:
>
> {
>    "_id": "_design/test",
>    "lib": {
>        "security": "function user(userctx, secobj){\n  return {\n
> is_admin: function(){ return userctx.roles.indexOf('_admin') !== -1 }
>   \n  }  \n}\nexports.user = user;"
>    },
>    "validate_doc_update": "function(newdoc, olddoc, userctx, secobj)
> {\n  user = require('lib/security').user(userctx, secobj);\n  if
> (!user.is_admin()) { throw({forbidden: '_admin role required'}) };\n}"
> }
>
> I believe the docs example is needed to be completely rewritted.
>
>
> --
> ,,,^..^,,,
>

Re: commonjs in validation docs

Posted by Alexander Shorin <kx...@gmail.com>.
On Mon, Jun 3, 2013 at 11:50 AM, Stephen Bartell <sn...@gmail.com> wrote:
> So then am I out of luck when it comes to requires in my validation docs?

Why so? I'd a bit modified the example to remove views confusion:

{
   "_id": "_design/test",
   "lib": {
       "security": "function user(userctx, secobj){\n  return {\n
is_admin: function(){ return userctx.roles.indexOf('_admin') !== -1 }
  \n  }  \n}\nexports.user = user;"
   },
   "validate_doc_update": "function(newdoc, olddoc, userctx, secobj)
{\n  user = require('lib/security').user(userctx, secobj);\n  if
(!user.is_admin()) { throw({forbidden: '_admin role required'}) };\n}"
}

I believe the docs example is needed to be completely rewritted.


--
,,,^..^,,,

Re: commonjs in validation docs

Posted by Stephen Bartell <sn...@gmail.com>.
No worries!  I can make a pull request with something that makes more sense.

So then am I out of luck when it comes to requires in my validation docs?  I don't see how else to get them in there.


On Jun 3, 2013, at 12:37 AM, Alexander Shorin <kx...@gmail.com> wrote:

> Nice found Stephen! Indeed, this is a "bug" and my bad (:
> "validate_doc_update" field should be on the same level as "views"
> one.
> --
> ,,,^..^,,,
> 
> 
> On Mon, Jun 3, 2013 at 11:30 AM, Stephen Bartell <sn...@gmail.com> wrote:
>> I thought it was strange too,  but thats from the docs (http://couchdb.readthedocs.org/en/latest/query-servers.html#commonjs-modules). The way I interpret the example is "its ok to put validation docs inside the views field so that you can have access to commonjs exports.".  This must just be a shitty example.
>> 
>> 
>> On Jun 3, 2013, at 12:21 AM, Aurélien Bénel <au...@utt.fr> wrote:
>> 
>>> Hi Stephen,
>>> 
>>>> So I can't seem to get a validate_update_doc nested in a view field to work.
>>> 
>>> Why would you do that? Validations and views are completely different concepts.
>>> 
>>>> "views": {
>>>>     "validate_doc_update": "function(newdoc, olddoc, ctx, sec) {throw {forbidden: 'You shall not pass.'}}" // this one doesn't work
>>> 
>>> If you write this, `validate_doc_update` will be just a view. I'm pretty sure this is not what you want.
>>> As a view, it should have a map and could have a reduce function, but would have nothing to do with validation.
>>> 
>>> 
>>> Regards,
>>> 
>>> Aurélien
>>> 
>> 


Re: commonjs in validation docs

Posted by Alexander Shorin <kx...@gmail.com>.
Nice found Stephen! Indeed, this is a "bug" and my bad (:
"validate_doc_update" field should be on the same level as "views"
one.
--
,,,^..^,,,


On Mon, Jun 3, 2013 at 11:30 AM, Stephen Bartell <sn...@gmail.com> wrote:
> I thought it was strange too,  but thats from the docs (http://couchdb.readthedocs.org/en/latest/query-servers.html#commonjs-modules). The way I interpret the example is "its ok to put validation docs inside the views field so that you can have access to commonjs exports.".  This must just be a shitty example.
>
>
> On Jun 3, 2013, at 12:21 AM, Aurélien Bénel <au...@utt.fr> wrote:
>
>> Hi Stephen,
>>
>>> So I can't seem to get a validate_update_doc nested in a view field to work.
>>
>> Why would you do that? Validations and views are completely different concepts.
>>
>>>  "views": {
>>>      "validate_doc_update": "function(newdoc, olddoc, ctx, sec) {throw {forbidden: 'You shall not pass.'}}" // this one doesn't work
>>
>> If you write this, `validate_doc_update` will be just a view. I'm pretty sure this is not what you want.
>> As a view, it should have a map and could have a reduce function, but would have nothing to do with validation.
>>
>>
>> Regards,
>>
>> Aurélien
>>
>

Re: commonjs in validation docs

Posted by Stephen Bartell <sn...@gmail.com>.
I thought it was strange too,  but thats from the docs (http://couchdb.readthedocs.org/en/latest/query-servers.html#commonjs-modules). The way I interpret the example is "its ok to put validation docs inside the views field so that you can have access to commonjs exports.".  This must just be a shitty example.


On Jun 3, 2013, at 12:21 AM, Aurélien Bénel <au...@utt.fr> wrote:

> Hi Stephen,
> 
>> So I can't seem to get a validate_update_doc nested in a view field to work.  
> 
> Why would you do that? Validations and views are completely different concepts.
> 
>>  "views": {
>>      "validate_doc_update": "function(newdoc, olddoc, ctx, sec) {throw {forbidden: 'You shall not pass.'}}" // this one doesn't work
> 
> If you write this, `validate_doc_update` will be just a view. I'm pretty sure this is not what you want.
> As a view, it should have a map and could have a reduce function, but would have nothing to do with validation.
> 
> 
> Regards,
> 
> Aurélien
> 


Re: commonjs in validation docs

Posted by Aurélien Bénel <au...@utt.fr>.
Hi Stephen,

> So I can't seem to get a validate_update_doc nested in a view field to work.  

Why would you do that? Validations and views are completely different concepts.

>   "views": {
>       "validate_doc_update": "function(newdoc, olddoc, ctx, sec) {throw {forbidden: 'You shall not pass.'}}" // this one doesn't work

If you write this, `validate_doc_update` will be just a view. I'm pretty sure this is not what you want.
As a view, it should have a map and could have a reduce function, but would have nothing to do with validation.
 

Regards,

Aurélien


Re: commonjs in validation docs

Posted by Stephen Bartell <sn...@gmail.com>.
So I can't seem to get a validate_update_doc nested in a view field to work.  Heres a test ddoc straight from my database.

This one won't work
```
{
   "_id": "_design/val",
   "language": "javascript",
   "views": {
       "validate_doc_update": "function(newdoc, olddoc, ctx, sec) {throw {forbidden: 'You shall not pass.'}}" // this one doesn't work
   }
}
```

This one works fine.
```
{
   "_id": "_design/val",
   "language": "javascript",
   "validate_doc_update": "function(newdoc, olddoc, ctx, sec) {throw {forbidden: 'You shall not pass.'}}"  // this one works fine.
}
```

Are you able to spot what I am doing wrong here? If I can get this to work, then I can use cjs, which would be awesome.

Best
sb

On Jun 1, 2013, at 2:19 AM, Johannes Jörg Schmidt <sc...@netzmerk.com> wrote:

> Yes, you can use CommonJS modules inside validate_doc_update functions.
> The docs are a bit misleading in this case.
> 
> {
>  "lib": {
>    "validation": "exports.presence = ..."
>  },
>  "validate_doc_update": "function(doc) {
> require('lib/validation').presence(doc, 'title') }"
> }
> 
> g jo
> Am 01.06.2013 03:14 schrieb "Stephen Bartell" <sn...@gmail.com>:
> 
>> Hi all!
>> 
>> Can I use cjs modules in validate_doc_update functions?
>> 
>> I had a look through the docs at
>> http://couchdb.readthedocs.org/en/latest/query-servers.html#commonjs-modulesand it seems to suggest that I can have a validate_doc_update function
>> inside a view field.  I tried this and the validation doesn't seem to run.
>> Is this supposed to work?
>> 
>> Thanks!


Re: commonjs in validation docs

Posted by Johannes Jörg Schmidt <sc...@netzmerk.com>.
Yes, you can use CommonJS modules inside validate_doc_update functions.
The docs are a bit misleading in this case.

{
  "lib": {
    "validation": "exports.presence = ..."
  },
  "validate_doc_update": "function(doc) {
require('lib/validation').presence(doc, 'title') }"
}

g jo
Am 01.06.2013 03:14 schrieb "Stephen Bartell" <sn...@gmail.com>:

> Hi all!
>
> Can I use cjs modules in validate_doc_update functions?
>
> I had a look through the docs at
> http://couchdb.readthedocs.org/en/latest/query-servers.html#commonjs-modulesand it seems to suggest that I can have a validate_doc_update function
> inside a view field.  I tried this and the validation doesn't seem to run.
>  Is this supposed to work?
>
> Thanks!