(js: JsonSchema): Document<"draft-2020-12">Parses a raw OpenAPI 3.1 JSON Schema into a Document<"draft-2020-12">.
When to use
Use when you need to consume raw JSON Schema objects from an OpenAPI 3.1 specification.
Details
This rewrites #/components/schemas/... refs to #/$defs/..., then delegates
to fromSchemaDraft2020_12.
Example (Parsing an OpenAPI 3.1 schema)
import { JsonSchema } from "effect"
const raw: JsonSchema.JsonSchema = {
type: "object",
properties: {
user: { $ref: "#/components/schemas/User" }
}
}
const doc = JsonSchema.fromSchemaOpenApi3_1(raw)
// $ref is rewritten to Draft-2020-12 form
console.log(doc.schema.properties) // { user: { $ref: "#/$defs/User" } }export function function fromSchemaOpenApi3_1(
js: JsonSchema
): Document<"draft-2020-12">
Parses a raw OpenAPI 3.1 JSON Schema into a Document<"draft-2020-12">.
When to use
Use when you need to consume raw JSON Schema objects from an OpenAPI 3.1
specification.
Details
This rewrites #/components/schemas/... refs to #/$defs/..., then delegates
to
fromSchemaDraft2020_12
.
Example (Parsing an OpenAPI 3.1 schema)
import { JsonSchema } from "effect"
const raw: JsonSchema.JsonSchema = {
type: "object",
properties: {
user: { $ref: "#/components/schemas/User" }
}
}
const doc = JsonSchema.fromSchemaOpenApi3_1(raw)
// $ref is rewritten to Draft-2020-12 form
console.log(doc.schema.properties) // { user: { $ref: "#/$defs/User" } }
fromSchemaOpenApi3_1(js: JsonSchemajs: JsonSchema): 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"> {
const const schema: JsonSchemaschema = function rewrite_refs(
node: unknown,
f: ($ref: string) => string
): unknown
rewrite_refs(js: JsonSchemajs, (ref: stringref) => ref: stringref.String.replace(searchValue: {
[Symbol.replace](string: string, replaceValue: string): string;
}, replaceValue: string): string (+3 overloads)
Passes a string and
{@linkcode
replaceValue
}
to the [Symbol.replace] method on
{@linkcode
searchValue
}
. This method is expected to implement its own replacement algorithm.
replace(const RE_COMPONENTS_SCHEMAS: RegExpRE_COMPONENTS_SCHEMAS, "#/$defs")) as JsonSchema
return function fromSchemaDraft2020_12(
js: JsonSchema
): Document<"draft-2020-12">
Parses a raw Draft-2020-12 JSON Schema into a Document<"draft-2020-12">.
When to use
Use when you already have a raw JSON Schema object in Draft-2020-12 format.
Details
This separates $defs from the root schema into the definitions field.
Unlike
fromSchemaDraft07
, this performs no keyword rewriting.
Example (Parsing a Draft-2020-12 schema)
import { JsonSchema } from "effect"
const raw: JsonSchema.JsonSchema = {
type: "number",
minimum: 0,
$defs: { PositiveInt: { type: "integer", minimum: 1 } }
}
const doc = JsonSchema.fromSchemaDraft2020_12(raw)
console.log(doc.schema) // { type: "number", minimum: 0 }
console.log(doc.definitions) // { PositiveInt: { type: "integer", minimum: 1 } }
fromSchemaDraft2020_12(const schema: JsonSchemaschema)
}