You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Conor Mac Aoidh <co...@gmail.com> on 2014/08/29 15:50:08 UTC

validate_doc_update design function

Hi All,

I'm writing a validate_doc_update function at the moment. I want to 
validate that document inserts comply with a strict schema. I have been 
thinking of how to do this, without making the validate function too 
inefficient.

Since there is no way to pass parameters to the validate_doc_update 
function, I was thinking of fetching the schema (contained in a local 
JSON file) asynchronously. This could be a terrible idea. However, I've 
found that I can request the schema once and then store it. So, there 
would be one initial performance hit in fetching the file, and from then 
on it would be saved. See the example function:
/
//function validate(new_doc, old_doc, userCtx){//
//   if(typeof this.schema == 'undefined')//{//
//        // get the schema//
//   }//
//   // make sure new_doc conforms to the this.schema[new_doc.type]//
//}/

I'm just wondering, is there a better way to do this? Are there any 
compelling reasons not to do this?

Also, I have considered just including the schema statically in the 
function but the solution above is preferable as the schema changes 
often and I don't want to have to update the design functions.

Thanks

Conor

Re: validate_doc_update design function

Posted by Conor Mac Aoidh <co...@gmail.com>.
Hi Jonas,

Thanks for the response. I hadn't actually tested the asynchronous part 
- just that data can be retained between calls of the update function 
using 'this'.

The solution of adding the schema to the design document sounds like it 
should suffice. Though, I will still have to manage updating the schema 
and replicating the updates over a large number of databases. But, this 
is doable.

Thanks

Conor

On 29/08/14 14:58, Jonas Weber wrote:
> Hi Conor,
>
> unless I'm terribly mistaken there is no way to either act 
> asynchronously in validate_doc_update functions or to reliably cache 
> some data for other runs. It sounds like you are using a glitch for 
> your cache, but you should never rely on that.
>
> If you don't want to update the function itself there is an easy way 
> to include the schema directly into the design document as part of 
> design document JSON, and this can be require()d during 
> validate_doc_update.
>
> So yes, there are two compelling reasons: This caching behavior is not 
> intended at all!  And even worse: Validate_doc_update responds sooner 
> (synchronously) than the schema is fetched, so validation always 
> succeeds for the first run!
>
> All the best,
> Jonas
>
> On August 29, 2014, Conor Mac Aoidh <co...@gmail.com> wrote:
>
>> Hi All,
>>
>> I'm writing a validate_doc_update function at the moment. I want to 
>> validate that document inserts comply with a strict schema. I have 
>> been thinking of how to do this, without making the validate function 
>> too inefficient.
>>
>> Since there is no way to pass parameters to the validate_doc_update 
>> function, I was thinking of fetching the schema (contained in a local 
>> JSON file) asynchronously. This could be a terrible idea. However, 
>> I've found that I can request the schema once and then store it. So, 
>> there would be one initial performance hit in fetching the file, and 
>> from then on it would be saved. See the example function:
>> /
>> //function validate(new_doc, old_doc, userCtx){//
>> // if(typeof this.schema == 'undefined')//{//
>> // // get the schema//
>> // }//
>> // // make sure new_doc conforms to the this.schema[new_doc.type]//
>> //}/
>>
>> I'm just wondering, is there a better way to do this? Are there any 
>> compelling reasons not to do this?
>>
>> Also, I have considered just including the schema statically in the 
>> function but the solution above is preferable as the schema changes 
>> often and I don't want to have to update the design functions.
>>
>> Thanks
>>
>> Conor


Re: validate_doc_update design function

Posted by Jonas Weber <ma...@jonasw.de>.
Hi Conor,

unless I'm terribly mistaken there is no way to either act asynchronously in validate_doc_update functions or to reliably cache some data for other runs. It sounds like you are using a glitch for your cache, but you should never rely on that.

If you don't want to update the function itself there is an easy way to include the schema directly into the design document as part of design document JSON, and this can be require()d during validate_doc_update.
So yes, there are two compelling reasons: This caching behavior is not intended at all! And even worse: Validate_doc_update responds sooner (synchronously) than the schema is fetched, so validation always succeeds for the first run!

All the best,Jonas


On August 29, 2014, Conor Mac Aoidh <co...@gmail.com> wrote:
> Hi All,
> 
> I'm writing a validate_doc_update function at the moment. I want to validate that document inserts comply with a strict schema. I have been thinking of how to do this, without making the validate function too inefficient.
> 
> Since there is no way to pass parameters to the validate_doc_update function, I was thinking of fetching the schema (contained in a local JSON file) asynchronously. This could be a terrible idea. However, I've found that I can request the schema once and then store it. So, there would be one initial performance hit in fetching the file, and from then on it would be saved. See the example function:
> /
> //function validate(new_doc, old_doc, userCtx){//
> // if(typeof this.schema == 'undefined')//{//
> // // get the schema//
> // }//
> // // make sure new_doc conforms to the this.schema[new_doc.type]//
> //}/
> 
> I'm just wondering, is there a better way to do this? Are there any compelling reasons not to do this?
> 
> Also, I have considered just including the schema statically in the function but the solution above is preferable as the schema changes often and I don't want to have to update the design functions.
> 
> Thanks
> 
> Conor
> 

Re: validate_doc_update design function

Posted by Stefan Klein <st...@gmail.com>.
Hi,

as far as i know it's not possible to fetch other documents in any of the
design functions. (is this changed?)


What you could do is to have the schema in your design document, like it is
done here:
https://github.com/TravisPaul/couchapp-schema

The design functions are executed in the scope of the design document
(litle unsure if there are exeptions), so "this" is the design document. So
the couchapp-schema can use this.schema[newDoc.schema] to get the schema.

You would still have to update the design document, but you don't have to
touch the validation function.

regards,
Stefan





2014-08-29 15:50 GMT+02:00 Conor Mac Aoidh <co...@gmail.com>:

> Hi All,
>
> I'm writing a validate_doc_update function at the moment. I want to
> validate that document inserts comply with a strict schema. I have been
> thinking of how to do this, without making the validate function too
> inefficient.
>
> Since there is no way to pass parameters to the validate_doc_update
> function, I was thinking of fetching the schema (contained in a local JSON
> file) asynchronously. This could be a terrible idea. However, I've found
> that I can request the schema once and then store it. So, there would be
> one initial performance hit in fetching the file, and from then on it would
> be saved. See the example function:
> /
> //function validate(new_doc, old_doc, userCtx){//
> //   if(typeof this.schema == 'undefined')//{//
> //        // get the schema//
> //   }//
> //   // make sure new_doc conforms to the this.schema[new_doc.type]//
> //}/
>
> I'm just wondering, is there a better way to do this? Are there any
> compelling reasons not to do this?
>
> Also, I have considered just including the schema statically in the
> function but the solution above is preferable as the schema changes often
> and I don't want to have to update the design functions.
>
> Thanks
>
> Conor
>