(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 } }export function 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(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 $defs: unknown$defs, ...const schema: {
[x: string]: unknown
}
schema } = js: JsonSchemajs
return {
Document<"draft-2020-12">.dialect: "draft-2020-12"dialect: "draft-2020-12",
Document<D extends Dialect>.schema: JsonSchemaschema,
Document<D extends Dialect>.definitions: Definitionsdefinitions: import PredicatePredicate.function isObject(
input: unknown
): input is {
[x: PropertyKey]: unknown
}
Checks whether a value is a non-null object value that is not an array.
When to use
Use to narrow unknown input to a non-null, non-array object with a
Predicate guard.
Details
This is a structural runtime check using typeof input === "object", so it
also accepts object instances such as Date, Map, class instances, and
typed arrays. It excludes null and arrays.
Example (Guarding objects)
import { Predicate } from "effect"
console.log(Predicate.isObject({ a: 1 }))
console.log(Predicate.isObject([1, 2]))
isObject(const $defs: unknown$defs) ? (const $defs: {
[x: string]: unknown
[x: number]: unknown
[x: symbol]: unknown
}
$defs as Definitions) : {}
}
}