Hyperlinkv0.8.0-beta.28

JsonSchema

JsonSchema.resolve$reffunctioneffect/JsonSchema.ts:894
($ref: string, definitions: Definitions): JsonSchema | undefined

Resolves a $ref string by looking up the last path segment in a definitions map.

When to use

Use when you need to dereference a $ref pointer to get the JSON Schema object it points to.

Details

This only resolves the final segment of the ref path, such as "User" from "#/$defs/User". It returns undefined if the definition is not found.

Gotchas

This function does not follow arbitrary JSON Pointer paths.

Example (Resolving a $ref)

import { JsonSchema } from "effect"

const definitions: JsonSchema.Definitions = {
  User: { type: "object", properties: { name: { type: "string" } } }
}

const result = JsonSchema.resolve$ref("#/$defs/User", definitions)
console.log(result) // { type: "object", properties: { name: { type: "string" } } }

const missing = JsonSchema.resolve$ref("#/$defs/Unknown", definitions)
console.log(missing) // undefined
export function resolve$ref($ref: string, definitions: Definitions): JsonSchema | undefined {
  const tokens = $ref.split("/")
  if (tokens.length > 0) {
    const identifier = unescapeToken(tokens[tokens.length - 1])
    const definition = definitions[identifier]
    if (definition !== undefined) {
      return definition
    }
  }
}
Referenced by 1 symbols