无法在 jsonschema 中使用日期验证
unable to use date validation in jsonschema
我无法使用 "date" 在 jsonschema 中进行类型验证
myschema = {
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"self": {
"primary_key": ["email"]
},
"properties": {
"email": {
"pattern": "[^@]+@[^@]+\.[^@]+"
},
"dob": {
"description": "Date of Birth YYYY-MM-DD",
"type": "date"
}
}
}
当我使用上面的模式执行下面的代码时
from jsonschema import validate
validate({ "dob": "2001-02-30"}, myschema)
得到以下错误轨迹
Unhandled Exception: 'date' is not valid under any of the given schemas
Failed validating 'anyOf' in schema['properties']['properties']['additionalProperties']['properties']['type']:
{'anyOf': [{'$ref': '#/definitions/simpleTypes'},
{'items': {'$ref': '#/definitions/simpleTypes'},
'minItems': 1,
'type': 'array',
'uniqueItems': True}]}
On instance['properties']['dob']['type']:
'date'
更新:日期似乎是一种格式而不是类型,但它仍然让我输入无效日期。我可以在 jsonschema 代码中清楚地看到它尝试使用 datetime 解析它,但我无法在那里设置断点。
date
应该用作 "format",而不是 "type":
"dob": {
"description": "Date of Birth YYYY-MM-DD",
"type": "string",
"format": "date"
}
然后,要检查格式,请使用:
from jsonschema import validate, FormatChecker
validate({"dob": "2001-02-30"}, myschema, format_checker=FormatChecker())
我无法使用 "date" 在 jsonschema 中进行类型验证
myschema = {
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"self": {
"primary_key": ["email"]
},
"properties": {
"email": {
"pattern": "[^@]+@[^@]+\.[^@]+"
},
"dob": {
"description": "Date of Birth YYYY-MM-DD",
"type": "date"
}
}
}
当我使用上面的模式执行下面的代码时
from jsonschema import validate
validate({ "dob": "2001-02-30"}, myschema)
得到以下错误轨迹
Unhandled Exception: 'date' is not valid under any of the given schemas
Failed validating 'anyOf' in schema['properties']['properties']['additionalProperties']['properties']['type']:
{'anyOf': [{'$ref': '#/definitions/simpleTypes'},
{'items': {'$ref': '#/definitions/simpleTypes'},
'minItems': 1,
'type': 'array',
'uniqueItems': True}]}
On instance['properties']['dob']['type']:
'date'
更新:日期似乎是一种格式而不是类型,但它仍然让我输入无效日期。我可以在 jsonschema 代码中清楚地看到它尝试使用 datetime 解析它,但我无法在那里设置断点。
date
应该用作 "format",而不是 "type":
"dob": {
"description": "Date of Birth YYYY-MM-DD",
"type": "string",
"format": "date"
}
然后,要检查格式,请使用:
from jsonschema import validate, FormatChecker
validate({"dob": "2001-02-30"}, myschema, format_checker=FormatChecker())