(document: Document<"draft-2020-12">): Document<"draft-07">Converts a Document<"draft-2020-12"> to a Document<"draft-07">.
When to use
Use when you need to output a canonical JSON Schema document in Draft-07 format.
Details
This rewrites #/$defs/... refs to #/definitions/..., converts
Draft-2020-12 tuple syntax (prefixItems plus items) to Draft-07 form
(items as array plus additionalItems), and converts both the root schema
and all definitions.
Gotchas
Unsupported Draft-2020-12 keywords are dropped.
Example (Converting to Draft-07)
import { JsonSchema } from "effect"
const doc = JsonSchema.fromSchemaDraft2020_12({
type: "array",
prefixItems: [{ type: "string" }, { type: "number" }],
items: { type: "boolean" }
})
const draft07 = JsonSchema.toDocumentDraft07(doc)
console.log(draft07.dialect) // "draft-07"
console.log(draft07.schema.items) // [{ type: "string" }, { type: "number" }]
console.log(draft07.schema.additionalItems) // { type: "boolean" }export function function toDocumentDraft07(
document: Document<"draft-2020-12">
): Document<"draft-07">
Converts a Document<"draft-2020-12"> to a Document<"draft-07">.
When to use
Use when you need to output a canonical JSON Schema document in Draft-07
format.
Details
This rewrites #/$defs/... refs to #/definitions/..., converts
Draft-2020-12 tuple syntax (prefixItems plus items) to Draft-07 form
(items as array plus additionalItems), and converts both the root schema
and all definitions.
Gotchas
Unsupported Draft-2020-12 keywords are dropped.
Example (Converting to Draft-07)
import { JsonSchema } from "effect"
const doc = JsonSchema.fromSchemaDraft2020_12({
type: "array",
prefixItems: [{ type: "string" }, { type: "number" }],
items: { type: "boolean" }
})
const draft07 = JsonSchema.toDocumentDraft07(doc)
console.log(draft07.dialect) // "draft-07"
console.log(draft07.schema.items) // [{ type: "string" }, { type: "number" }]
console.log(draft07.schema.additionalItems) // { type: "boolean" }
toDocumentDraft07(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-07"> {
return {
Document<"draft-07">.dialect: "draft-07"dialect: "draft-07",
Document<D extends Dialect>.schema: JsonSchemaschema: function toSchemaDraft07(
schema: JsonSchema
): JsonSchema
toSchemaDraft07(document: Document<"draft-2020-12">(parameter) document: {
dialect: D;
schema: JsonSchema;
definitions: Definitions;
}
document.Document<D extends Dialect>.schema: JsonSchemaschema),
Document<D extends Dialect>.definitions: Record<string, JsonSchema>definitions: import RecRec.const map: {
<K extends string, A, B>(
f: (a: A, key: NoInfer<K>) => B
): (self: ReadonlyRecord<K, A>) => Record<K, B>
<K extends string, A, B>(
self: ReadonlyRecord<K, A>,
f: (a: A, key: NoInfer<K>) => B
): Record<K, B>
}
map(document: Document<"draft-2020-12">(parameter) document: {
dialect: D;
schema: JsonSchema;
definitions: Definitions;
}
document.Document<D extends Dialect>.definitions: Definitionsdefinitions, function toSchemaDraft07(
schema: JsonSchema
): JsonSchema
toSchemaDraft07)
}
}