Python arangoDB的pyarango驱动程序:验证

Python arangoDB的pyarango驱动程序:验证,python,arangodb,nosql,Python,Arangodb,Nosql,我正在为arangoDB使用pyarango驱动程序(),但我无法理解字段验证是如何工作的。我已将集合的字段设置为github示例中的字段: import pyArango.Collection as COL import pyArango.Validator as VAL from pyArango.theExceptions import ValidationError import types class String_val(VAL.Validator) : def validate

我正在为arangoDB使用pyarango驱动程序(),但我无法理解字段验证是如何工作的。我已将集合的字段设置为github示例中的字段:

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()])
      }
所以我希望当我尝试将文档添加到“Humans”集合中时,如果“name”字段不是字符串,就会出现错误。但这似乎没那么容易

以下是我向集合中添加文档的方式:

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)
因此,如果“name”不是字符串,我实际上不会得到任何错误,并上传到集合中


有人知道如何使用pyarango的内置验证来检查文档是否包含该集合的正确字段吗?

ArangoDB因为文档存储本身不强制架构,驱动程序也不强制架构。 如果您需要模式验证,可以使用Foxx服务(via)在驱动程序顶部或ArangoDB内部进行验证

一种可能的解决方案是在应用程序的驱动程序顶部使用its:

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

另一个使用JSON模式的实际示例是,它还用于记录ArangoDB REST API和ArangoDB Foxx服务

我还不知道我发布的代码出了什么问题,但现在似乎起作用了。但是,我在读取json文件时必须这样做,否则它无法识别字符串。我知道ArangoDB本身不执行方案,但我使用的是具有内置验证的方案。
对于那些对使用python对arangoDB进行内置验证感兴趣的人,请访问。

我认为您的验证器没有任何问题,只是如果您使用AQL查询插入文档,pyArango在插入之前无法知道内容

验证器仅适用于pyArango文档,前提是:

humans = db["Humans"]
doc = humans.createDocument()
doc["name"] = 101
这将触发异常,因为您已定义:

'on_set': True