ValidationError data schema_error ai_generated true

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

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

Also available as: JSON · Markdown · 中文
90%Fix Rate
87%Confidence
1Evidence
2023-09-05First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
JSON Schema Draft 7+ active
ajv 8.12.0+ active
jsonschema 4.18.0+ (Python) active
json-schema-validator 4.0.0+ (Java) active

Root Cause

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

中文

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

Official Documentation

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

Workarounds

  1. 95% success Use a custom format validator that accepts relative URIs: `ajv.addFormat('uri-reference', { validate: (s) => /^[\w\/:.?&=#-]+$/.test(s) })`
    Use a custom format validator that accepts relative URIs: `ajv.addFormat('uri-reference', { validate: (s) => /^[\w\/:.?&=#-]+$/.test(s) })`
  2. 85% success Pre-process data to convert relative URIs to absolute by prepending a base URL before validation.
    Pre-process data to convert relative URIs to absolute by prepending a base URL before validation.
  3. 80% success Switch to a validator that uses a more lenient uri-reference implementation, like `@cfworker/json-schema`.
    Switch to a validator that uses a more lenient uri-reference implementation, like `@cfworker/json-schema`.

中文步骤

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

Dead Ends

Common approaches that don't work:

  1. 80% fail

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

  2. 50% fail

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