(schema: JsonSchema): Document<"draft-2020-12">Parses a raw OpenAPI 3.0 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.0 specification.
Details
This handles OpenAPI 3.0 extensions, including nullable, singular
example, and boolean exclusiveMinimum or exclusiveMaximum. It
normalizes the schema to Draft-07 first, then converts to Draft-2020-12 via
fromSchemaDraft07.
Example (Parsing an OpenAPI 3.0 nullable schema)
import { JsonSchema } from "effect"
const raw: JsonSchema.JsonSchema = {
type: "string",
nullable: true
}
const doc = JsonSchema.fromSchemaOpenApi3_0(raw)
// nullable is expanded into a type array
console.log(doc.schema.type) // ["string", "null"]export function function fromSchemaOpenApi3_0(
schema: JsonSchema
): Document<"draft-2020-12">
Parses a raw OpenAPI 3.0 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.0
specification.
Details
This handles OpenAPI 3.0 extensions, including nullable, singular
example, and boolean exclusiveMinimum or exclusiveMaximum. It
normalizes the schema to Draft-07 first, then converts to Draft-2020-12 via
Example (Parsing an OpenAPI 3.0 nullable schema)
import { JsonSchema } from "effect"
const raw: JsonSchema.JsonSchema = {
type: "string",
nullable: true
}
const doc = JsonSchema.fromSchemaOpenApi3_0(raw)
// nullable is expanded into a type array
console.log(doc.schema.type) // ["string", "null"]
fromSchemaOpenApi3_0(schema: JsonSchemaschema: 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 normalized: unknownnormalized = function normalize_OpenApi3_0_to_Draft07(
node: unknown
): unknown
normalize_OpenApi3_0_to_Draft07(schema: JsonSchemaschema)
return function fromSchemaDraft07(
js: JsonSchema
): Document<"draft-2020-12">
Parses a raw Draft-07 JSON Schema into a Document<"draft-2020-12">.
When to use
Use when you have a raw JSON Schema object that follows Draft-07 conventions
and need the canonical Draft-2020-12 document representation.
Details
This converts Draft-07 tuple syntax (items as array plus
additionalItems) to Draft-2020-12 form (prefixItems plus items),
rewrites #/definitions/... refs to #/$defs/..., and extracts root-level
definitions into the definitions field.
Gotchas
Unsupported keywords, such as if/then/else and $id, are dropped.
Example (Parsing a Draft-07 schema)
import { JsonSchema } from "effect"
const raw: JsonSchema.JsonSchema = {
type: "object",
properties: {
tags: {
type: "array",
items: { type: "string" }
}
}
}
const doc = JsonSchema.fromSchemaDraft07(raw)
console.log(doc.dialect) // "draft-2020-12"
console.log(doc.schema.properties) // { tags: { type: "array", items: { type: "string" } } }
fromSchemaDraft07(const normalized: unknownnormalized as JsonSchema)
}