(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 function resolveTopLevel$ref(
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" } } }
resolveTopLevel$ref(document: Document<"draft-2020-12">(parameter) document: {
dialect: D;
schema: JsonSchema;
definitions: Definitions;
}
document: interface Document<D extends Dialect>A structured container for a single JSON Schema and its associated
definitions.
When to use
Use when you need to carry a root schema together with its shared
definitions, or when converting between dialects with the from* and to*
functions.
Details
The schema field holds the root schema without the definitions
collection. Root definitions are stored separately in definitions and
referenced via #/$defs/<name> for Draft-2020-12, #/definitions/<name>
for Draft-07, and #/components/schemas/<name> for OpenAPI 3.1 and
OpenAPI 3.0.
Example (Inspecting a parsed document)
import { JsonSchema } from "effect"
const raw: JsonSchema.JsonSchema = {
type: "string",
$defs: { Trimmed: { type: "string", minLength: 1 } }
}
const doc = JsonSchema.fromSchemaDraft2020_12(raw)
console.log(doc.dialect) // "draft-2020-12"
console.log(doc.schema) // { type: "string" }
console.log(doc.definitions) // { Trimmed: { type: "string", minLength: 1 } }
Document<"draft-2020-12">): interface Document<D extends Dialect>A structured container for a single JSON Schema and its associated
definitions.
When to use
Use when you need to carry a root schema together with its shared
definitions, or when converting between dialects with the from* and to*
functions.
Details
The schema field holds the root schema without the definitions
collection. Root definitions are stored separately in definitions and
referenced via #/$defs/<name> for Draft-2020-12, #/definitions/<name>
for Draft-07, and #/components/schemas/<name> for OpenAPI 3.1 and
OpenAPI 3.0.
Example (Inspecting a parsed document)
import { JsonSchema } from "effect"
const raw: JsonSchema.JsonSchema = {
type: "string",
$defs: { Trimmed: { type: "string", minLength: 1 } }
}
const doc = JsonSchema.fromSchemaDraft2020_12(raw)
console.log(doc.dialect) // "draft-2020-12"
console.log(doc.schema) // { type: "string" }
console.log(doc.definitions) // { Trimmed: { type: "string", minLength: 1 } }
Document<"draft-2020-12"> {
if (typeof document: Document<"draft-2020-12">(parameter) document: {
dialect: D;
schema: JsonSchema;
definitions: Definitions;
}
document.Document<D extends Dialect>.schema: JsonSchemaschema.JsonSchema[string]: unknown$ref === "string") {
const const schema: JsonSchema | undefinedschema = function resolve$ref(
$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
resolve$ref(document: Document<"draft-2020-12">(parameter) document: {
dialect: D;
schema: JsonSchema;
definitions: Definitions;
}
document.Document<D extends Dialect>.schema: JsonSchemaschema.JsonSchema[string]: string$ref, document: Document<"draft-2020-12">(parameter) document: {
dialect: D;
schema: JsonSchema;
definitions: Definitions;
}
document.Document<D extends Dialect>.definitions: Definitionsdefinitions)
if (const schema: JsonSchema | undefinedschema !== var undefinedundefined) {
return { ...document: Document<"draft-2020-12">(parameter) document: {
dialect: D;
schema: JsonSchema;
definitions: Definitions;
}
document, Document<D extends Dialect>.schema: JsonSchemaschema }
}
}
return document: Document<"draft-2020-12">(parameter) document: {
dialect: D;
schema: JsonSchema;
definitions: Definitions;
}
document
}