ValidationError data schema_error ai_generated true

JSON Schema 验证失败:format 'uri-reference' 拒绝有效的相对 URI

JSON Schema validation fails: format 'uri-reference' rejects valid relative URIs

ID: data/json-schema-format-uri-reference

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

版本兼容性

版本状态引入弃用备注
JSON Schema Draft 7+ active
ajv 8.12.0+ active
jsonschema 4.18.0+ (Python) active
json-schema-validator 4.0.0+ (Java) active

根因分析

JSON Schema 验证器使用不同的 'uri-reference' 格式实现;一些应用严格的 RFC 3986 解析,拒绝没有 scheme 的相对 URI。

English

JSON Schema validators use different implementations of the 'uri-reference' format; some apply strict RFC 3986 parsing that rejects relative URIs without a scheme.

generic

官方文档

https://json-schema.org/understanding-json-schema/reference/string.html#format

解决方案

  1. 使用自定义格式验证器接受相对 URI:`ajv.addFormat('uri-reference', { validate: (s) => /^[\w\/:.?&=#-]+$/.test(s) })`
  2. 在验证前预处理数据,通过添加基础 URL 将相对 URI 转换为绝对 URI。
  3. 切换到使用更宽松的 uri-reference 实现的验证器,如 `@cfworker/json-schema`。

无效尝试

常见但无效的做法:

  1. 80% 失败

    'uri' format is stricter and requires absolute URIs with scheme, rejecting relative paths.

  2. 50% 失败

    Weakens schema constraints and may allow invalid data to pass validation.