ValidationError data network_error ai_generated true

JSON Schema验证在解析远程$ref URI时超时失败

JSON Schema validation fails with timeout when resolving remote $ref URIs

ID: data/json-schema-uri-resolution-timeout

其他格式: JSON · Markdown 中文 · English
92%修复率
86%置信度
1证据数
2024-03-12首次发现

版本兼容性

版本状态引入弃用备注
ajv 8.12.0 active
jsonschema 4.20.0 active
Python 3.12 active
Node.js 21 active

根因分析

使用外部$ref URI(例如https://json-schema.org/draft/2020-12/schema)的JSON Schema定义,在远程服务器不可达或响应缓慢时会导致验证挂起或超时,特别是在离线或受限网络环境中。

English

JSON Schema definitions that use external $ref URIs (e.g., https://json-schema.org/draft/2020-12/schema) cause validation to hang or timeout when the remote server is unreachable or slow, especially in offline or restricted network environments.

generic

官方文档

https://json-schema.org/understanding-json-schema/structuring.html

解决方案

  1. Download the remote schema locally and use a local file reference: `curl -o draft-2020-12.json https://json-schema.org/draft/2020-12/schema; then use $ref: './draft-2020-12.json'`
  2. Use a schema resolver with caching: `from jsonschema import RefResolver; resolver = RefResolver.from_schema(schema, store={'https://json-schema.org/draft/2020-12/schema': cached_schema})`
  3. Set a short timeout for HTTP requests in the validator: `import urllib.request; urllib.request.urlopen(url, timeout=5)`

无效尝试

常见但无效的做法:

  1. 60% 失败

    This only delays the failure; if the remote server is permanently unreachable, the validation will still fail after the increased timeout.

  2. 50% 失败

    Most validators have the same behavior; the issue is the network dependency, not the library implementation.

  3. 70% 失败

    Retries cannot fix a fundamentally unreachable remote resource; they only add latency and complexity.