Hyperlinkv0.8.0-beta.28

JsonSchema

JsonSchema.resolveTopLevel$reffunctioneffect/JsonSchema.ts:940
(document: Document<"draft-2020-12">): Document<"draft-2020-12">

Resolves a document whose root schema is a top-level $ref.

When to use

Use when you need to dereference a top-level $ref before inspecting the root JSON Schema object's properties directly.

Details

This returns the same object if no change is needed, or a shallow copy with the resolved schema.

Example (Resolving a top-level $ref)

import { JsonSchema } from "effect"

const doc: JsonSchema.Document<"draft-2020-12"> = {
  dialect: "draft-2020-12",
  schema: { $ref: "#/$defs/User" },
  definitions: {
    User: { type: "object", properties: { name: { type: "string" } } }
  }
}

const resolved = JsonSchema.resolveTopLevel$ref(doc)
console.log(resolved.schema) // { type: "object", properties: { name: { type: "string" } } }
export function resolveTopLevel$ref(document: Document<"draft-2020-12">): Document<"draft-2020-12"> {
  if (typeof document.schema.$ref === "string") {
    const schema = resolve$ref(document.schema.$ref, document.definitions)
    if (schema !== undefined) {
      return { ...document, schema }
    }
  }
  return document
}