arangoDB 的 pyarango 驱动程序:验证
pyarango driver for arangoDB: validation
我正在为 arangoDB 使用 pyarango 驱动程序 (https://github.com/tariqdaouda/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 服务在 ArangoDB 内部完成(通过 the joi validation library)。
一个可能的解决方案是在您的应用程序的驱动程序之上使用 JSON Schema with its python implementation:
from jsonschema import validate
schema = {
"type" : "object",
"properties" : {
"name" : {"type" : "string"},
"species" : {"type" : "string"},
},
}
另一个使用 JSON 模式的真实示例是 swagger.io,它也用于记录 ArangoDB REST API 和 ArangoDB Foxx 服务。
我还不知道我发布的代码有什么问题,但现在似乎可以工作了。但是我在读取 json 文件时必须 convert unicode to utf-8 否则它无法识别字符串。我知道 ArangoDB 本身不强制执行方案,但我正在使用它具有内置验证。
对于那些对使用 python 的 arangoDB 内置验证感兴趣的人,请访问 pyarango github.
我没有发现您的验证器有任何问题,只是如果您使用 AQL 查询来插入文档,pyArango 无法在插入之前知道内容。
如果您这样做,验证器仅适用于 pyArango 文档:
humans = db["Humans"]
doc = humans.createDocument()
doc["name"] = 101
这应该会触发异常,因为您已经定义了:
'on_set': True
我正在为 arangoDB 使用 pyarango 驱动程序 (https://github.com/tariqdaouda/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 服务在 ArangoDB 内部完成(通过 the joi validation library)。
一个可能的解决方案是在您的应用程序的驱动程序之上使用 JSON Schema with its python implementation:
from jsonschema import validate
schema = {
"type" : "object",
"properties" : {
"name" : {"type" : "string"},
"species" : {"type" : "string"},
},
}
另一个使用 JSON 模式的真实示例是 swagger.io,它也用于记录 ArangoDB REST API 和 ArangoDB Foxx 服务。
我还不知道我发布的代码有什么问题,但现在似乎可以工作了。但是我在读取 json 文件时必须 convert unicode to utf-8 否则它无法识别字符串。我知道 ArangoDB 本身不强制执行方案,但我正在使用它具有内置验证。 对于那些对使用 python 的 arangoDB 内置验证感兴趣的人,请访问 pyarango github.
我没有发现您的验证器有任何问题,只是如果您使用 AQL 查询来插入文档,pyArango 无法在插入之前知道内容。
如果您这样做,验证器仅适用于 pyArango 文档:
humans = db["Humans"]
doc = humans.createDocument()
doc["name"] = 101
这应该会触发异常,因为您已经定义了:
'on_set': True