Document validator

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

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:)

1 Like

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

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

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?

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)
}
}
}

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

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

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

is it possible , that I run couchbase queries?

You can fire N1QL queries and do bucket operations.

Can you tell me how ?

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

1 Like