Dovi Dovi - 1 year ago 100
Python Question

pyarango driver for arangoDB: validation

I am using pyarango driver ( for arangoDB, but I cannot understand how the field validation works. I have set the fields of a collection as in the github example:

import pyArango.Collection as COL
import pyArango.Validator as VAL
from pyArango.theExceptions import ValidationError
import types

class String_val(VAL.Validator) :
def validate(self, value) :
if type(value) is not types.StringType :
raise ValidationError("Field value must be a string")
return True

class Humans(COL.Collection) :

_validation = {
'on_save' : True,
'on_set' : True,
'allow_foreign_fields' : True # allow fields that are not part of the schema

_fields = {
'name' : Field(validators = [VAL.NotNull(), String_val()]),
'anything' : Field(),
'species' : Field(validators = [VAL.NotNull(), VAL.Length(5, 15), String_val()])

So I was expecting that when I try to add a document into "Humans" collection, if 'name' field is not a string, an error would rise. But it didn't seem to work that easy.

This is how I add documents to the collection:

myjson = json.loads(open('file.json').read())
collection_name = "Humans"
bindVars = {"doc": myjson, '@collection': collection_name}
aql = "For d in @doc INSERT d INTO @@collection LET newDoc = NEW RETURN newDoc"
queryResult = db.AQLQuery(aql, bindVars = bindVars, batchSize = 100)

So if 'name' is not a string I actually don't get any error and is uploaded into the collection.

Does someone knows how can check if a document contains proper fields for that collection using the built-in validation of pyarango?


ArangoDB as document store itself doesn't enforce schemas, neither do the drivers. If you need schema validation, this can be done on top of the driver or inside of ArangoDB using a Foxx service (via the joi validation library).

One possible solution for doing this is using JSON Schema with its python implementation on top of the driver in your application:

from jsonschema import validate
schema = {
 "type" : "object",
 "properties" : {
     "name" : {"type" : "string"},
     "species" : {"type" : "string"},

Another real life example using JSON Schema is, which is also used to document the ArangoDB REST API and ArangoDB Foxx services.