Search code examples
pythonarangodbnosql

pyarango driver for arangoDB: validation


I am using pyarango driver (https://github.com/tariqdaouda/pyArango) 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?


Solution

  • I don't see anything wrong with your validator, its just that if you're using AQL queries to insert your documents, pyArango has no way of knowing the contents prior to insertion.

    Validators only work on pyArango documents if you do:

    humans = db["Humans"]
    doc = humans.createDocument()
    doc["name"] = 101
    

    That should trigger the exception because you've defined:

    'on_set': True