josdejong/svelte-jsoneditor

Resolve JSON Schema `$ref` uri's

Offen

#547 geöffnet am 20.05.2025

 (0 Kommentare) (1 Reaktion) (0 zugewiesene Personen)TypeScript (147 Forks)github user discovery
enhancementhelp wanted

Repository-Metriken

Stars
 (1.260 Sterne)
PR-Merge-Metriken
 (Durchschn. Merge 9h 26m) (5 gemergte PRs in 30 T)

Beschreibung

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)

Contributor Guide