【发布时间】:2014-09-18 01:05:38
【问题描述】:
鉴于此架构:
{
"type": "object",
"properties": {
"account": {
"type": "object",
"required": ["id"],
"properties": {
"id": {"type": "number"}
}
},
"name": {"type": "string"},
"trigger": {
"type": "object",
"required": ["type"],
"properties": {
"type": {"type": "string"}
}
},
"content": {
"type": "object",
"properties": {
"emails": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"required": ["fromEmail","subject"],
"properties": {
"fromEmail": {"type": "string", "format": "email"},
"subject": {"type": "string"}
}
}
}
}
}
}
}
我正在尝试使用 jsonschema.Draft4Validator 来验证 POST 的 JSON 对象以检查其有效性,但在尝试从返回的错误中提出更好的人类可读消息时遇到了一些问题。
这是我验证的方式:
from jsonschema import Draft4Validator
v = Draft4Validator(self.schema)
errors = sorted(v.iter_errors(autoresponder_workflow), key=lambda e: e.path)
if errors:
print(', '.join(
'%s %s %s' % (error.path.popleft(), error.path.pop(), error.message) for error in errors
))
错误信息如下:
content emails [] is too short, trigger type None is not of type u'string'
我正在尝试创建一个看起来有点像的错误消息请在您的工作流程中添加至少一封电子邮件,“请确保您的所有电子邮件都包含主题行”,等等
【问题讨论】:
标签: python validation jsonschema