简体   繁体   中英

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?

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

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 swagger.io , which is also used to document the ArangoDB REST API and ArangoDB Foxx services.

I don't know yet what was wrong with the code I posted but now seems to work. However I had to convert unicode to utf-8 when reading the json file otherwise it was not able to identify strings. I know ArangoDB as itself does not enforce schemes but I am using that has a built-in validation. For those interested in a built-in validation of arangoDB using python visit pyarango github .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM