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 <[email protected]> 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