josdejong/svelte-jsoneditor

Resolve JSON Schema `$ref` uri's

开放

#547 创建于 2025年5月20日

 (0 条评论) (1 个反应) (0 位负责人)TypeScript (147 个派生)github user discovery
enhancementhelp wanted

仓库指标

星标
 (1,260 个星标)
PR 合并指标
 (平均合并 9小时 26分钟) (30 天内合并 5 个 PR)

描述

When a JSON Schema document contains a $ref referring to a separate JSON Schema document, the editor should load these documents into the AJV validator so the validation works.

I think this requires iterating over the JSON schema document to find all $ref occurrences, and then asynchronously loading the documents via a uri fetch, and then loading all schemas into the Ajv instance.

See https://ajv.js.org/guide/combining-schemas.html#combining-schemas-with-ref

const schema = {
  $id: "http://example.com/schemas/schema.json",
  type: "object",
  properties: {
    foo: {$ref: "defs.json#/definitions/int"},
    bar: {$ref: "defs.json#/definitions/str"},
  },
}

const defsSchema = {
  $id: "http://example.com/schemas/defs.json",
  definitions: {
    int: {type: "integer"},
    str: {type: "string"},
  },
}

const ajv = new Ajv({schemas: [schema, defsSchema]})
const validate = ajv.getSchema(schema.$id)

贡献者指南