Discussion:
validate_doc_update design function
Conor Mac Aoidh
2014-08-29 13:50:08 UTC
Permalink
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
Jonas Weber
2014-08-29 13:58:32 UTC
Permalink
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
Post by Conor Mac Aoidh
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.
/
//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
Conor Mac Aoidh
2014-09-02 12:52:10 UTC
Permalink
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
Post by Jonas Weber
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
Post by Conor Mac Aoidh
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
/
//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
Stefan Klein
2014-08-29 13:58:50 UTC
Permalink
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
Post by Conor Mac Aoidh
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
/
//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
Continue reading on narkive:
Loading...