ValidationError: Unknown format 'date-time' data validation_error ai_generated true

JSON Schema 验证因未知格式 'date-time' 拒绝有效日期字符串

JSON Schema validation rejects valid date strings due to unknown format 'date-time'

ID: data/json-schema-unknown-format

其他格式: JSON · Markdown 中文 · English
90%修复率
88%置信度
1证据数
2023-08-10首次发现

版本兼容性

版本状态引入弃用备注
JSON Schema draft-04 active
JSON Schema draft-06 active
ajv 8.12.0 active
jsonschema 4.18.0 active

根因分析

JSON Schema draft-04 使用 'date-time' 格式,但更新版本(draft-06+)拆分为 'date' 和 'time',部分验证器无法识别组合格式。

English

JSON Schema draft-04 uses 'date-time' format, but newer drafts (draft-06+) split into 'date' and 'time', and some validators fail to recognize the combined format.

generic

官方文档

https://json-schema.org/understanding-json-schema/reference/string.html#dates-and-times

解决方案

  1. Change 'format': 'date-time' to 'format': 'date' and 'format': 'time' as separate properties in draft-06+.
  2. Use a validator that supports draft-04, e.g., jsonschema with Draft4Validator: from jsonschema import Draft4Validator
  3. Upgrade the JSON Schema library to a version that auto-detects the draft version from $schema.

无效尝试

常见但无效的做法:

  1. Adding 'format': 'date-time' to schema definitions 90% 失败

    This is the problematic format itself; validators that don't support it will still reject it.

  2. Using 'pattern' instead of 'format' with a regex for datetime 40% 失败

    While this works for validation, it does not fix the schema compatibility issue and may be overly restrictive or permissive.