Document validator


#1

Is there any server side validator for documents?
Something like https://docs.mongodb.com/manual/reference/operator/query/jsonSchema/


#2

Reply please …
How can I ensure document consistency when I don’t use ODM tools? When I use only native driver and n1ql queries?
It is very likely that with a mistake I loose consistency for instance a simple typo (mail instead of email :persevere:)


#3

Hi @socketman2016, probably not the answer you want to hear, but no Couchbase does not have such a validator.


#4

You could always do the check on the client side though, there’s a list of JSON schema validator libs here.


#5

The is a dark side , If I do this client side
I must always fetch whole doc and update it , that means I have many network overhead and may be operation overrides

for instance , How can I update an email field that is string and required and enforce schema validation without fetching whole document?


#6

You can implement a server-side schema checker using Eventing. For eg. the following would check the schema for documents of the type “airline” in the travel-sample example:

function OnUpdate(doc, meta) {
log(‘document’, doc);
//only for Documents of the Type - airline
if (doc.type ==“airline”)
{
var valid_keys = [ “callsign”, “country”, “iata”, “icao”, “id”, “name”, “type”];
try
{
var keys = Object.keys(doc);
for(let thiskey of keys)
{
if (valid_keys.indexOf(thiskey) == -1)
{
log(‘Txn[’+String(meta.id)+’]*****Invalid Field:’+ thiskey);
doc[“comments”] = “Invalid Field:”+ thiskey;
doc[“reason_code”] = “X-FIELD”;
flagged[meta.id] = doc;
}
}
}
catch (e)
{
log("Exception: ",e)
}
}
}


#7

Can I import ajv ? https://www.npmjs.com/package/ajv


#8

You cannot import external js libraries into Eventing, for now.

https://docs.couchbase.com/server/6.0/eventing/eventing-overview.html


#9

is it possible , that I run couchbase queries?


#10

You can fire N1QL queries and do bucket operations.


#11

Can you tell me how ?


#12

Do have a look at our Documentation or our sample examples : https://docs.couchbase.com/server/6.0/eventing/eventing-examples.html