(
multiDocument: MultiDocument<"draft-2020-12">
): MultiDocument<"openapi-3.1">Converts a MultiDocument<"draft-2020-12"> to a
MultiDocument<"openapi-3.1">.
When to use
Use when you need to emit an OpenAPI 3.1 multi-document from canonical JSON Schema documents.
Details
This rewrites #/$defs/... refs to #/components/schemas/..., sanitizes
definition keys to match the OpenAPI component key pattern
(^[a-zA-Z0-9.\-_]+$) by replacing invalid characters with _, updates all
$ref pointers to use the sanitized keys, and converts all schemas and
definitions in the multi-document.
Example (Converting to OpenAPI 3.1)
import { JsonSchema } from "effect"
const multi: JsonSchema.MultiDocument<"draft-2020-12"> = {
dialect: "draft-2020-12",
schemas: [{ $ref: "#/$defs/User" }],
definitions: {
User: { type: "object", properties: { name: { type: "string" } } }
}
}
const openapi = JsonSchema.toMultiDocumentOpenApi3_1(multi)
console.log(openapi.dialect) // "openapi-3.1"
console.log(openapi.schemas[0]) // { $ref: "#/components/schemas/User" }export function function toMultiDocumentOpenApi3_1(
multiDocument: MultiDocument<"draft-2020-12">
): MultiDocument<"openapi-3.1">
Converts a MultiDocument<"draft-2020-12"> to a
MultiDocument<"openapi-3.1">.
When to use
Use when you need to emit an OpenAPI 3.1 multi-document from canonical JSON
Schema documents.
Details
This rewrites #/$defs/... refs to #/components/schemas/..., sanitizes
definition keys to match the OpenAPI component key pattern
(^[a-zA-Z0-9.\-_]+$) by replacing invalid characters with _, updates all
$ref pointers to use the sanitized keys, and converts all schemas and
definitions in the multi-document.
Example (Converting to OpenAPI 3.1)
import { JsonSchema } from "effect"
const multi: JsonSchema.MultiDocument<"draft-2020-12"> = {
dialect: "draft-2020-12",
schemas: [{ $ref: "#/$defs/User" }],
definitions: {
User: { type: "object", properties: { name: { type: "string" } } }
}
}
const openapi = JsonSchema.toMultiDocumentOpenApi3_1(multi)
console.log(openapi.dialect) // "openapi-3.1"
console.log(openapi.schemas[0]) // { $ref: "#/components/schemas/User" }
toMultiDocumentOpenApi3_1(multiDocument: MultiDocument<"draft-2020-12">(parameter) multiDocument: {
dialect: D;
schemas: readonly [JsonSchema, ...Array<JsonSchema>];
definitions: Definitions;
}
multiDocument: interface MultiDocument<D extends Dialect>Like
Document
, but carries multiple root schemas that share a
single definitions pool.
When to use
Use when generating several schemas, such as a request body
and a response body, that reference the same set of definitions.
Details
The schemas tuple is non-empty and contains at least one element.
MultiDocument<"draft-2020-12">): interface MultiDocument<D extends Dialect>Like
Document
, but carries multiple root schemas that share a
single definitions pool.
When to use
Use when generating several schemas, such as a request body
and a response body, that reference the same set of definitions.
Details
The schemas tuple is non-empty and contains at least one element.
MultiDocument<"openapi-3.1"> {
const const keyMap: Map<string, string>keyMap = new var Map: MapConstructor
new <string, string>(iterable?: Iterable<readonly [string, string]> | null | undefined) => Map<string, string> (+3 overloads)
Map<string, string>()
for (const const key: stringkey of var Object: ObjectConstructorProvides functionality common to all JavaScript objects.
Object.ObjectConstructor.keys(o: {}): string[] (+1 overload)Returns the names of the enumerable string properties and methods of an object.
keys(multiDocument: MultiDocument<"draft-2020-12">(parameter) multiDocument: {
dialect: D;
schemas: readonly [JsonSchema, ...Array<JsonSchema>];
definitions: Definitions;
}
multiDocument.MultiDocument<D extends Dialect>.definitions: Definitionsdefinitions)) {
const const sanitized: stringsanitized = function sanitizeOpenApiComponentsSchemasKey(
s: string
): string
Returns a sanitized key for an OpenAPI component schema.
Should match the ^[a-zA-Z0-9.\-_]+$ regular expression.
sanitizeOpenApiComponentsSchemasKey(const key: stringkey)
if (const sanitized: stringsanitized !== const key: stringkey) {
const keyMap: Map<string, string>keyMap.Map<string, string>.set(key: string, value: string): Map<string, string>Adds a new element with a specified key and value to the Map. If an element with the same key already exists, the element will be updated.
set(const key: stringkey, const sanitized: stringsanitized)
}
}
function function (local function) rewrite(schema: JsonSchema): JsonSchemarewrite(schema: JsonSchemaschema: JsonSchema): JsonSchema {
return function rewrite_refs(
node: unknown,
f: ($ref: string) => string
): unknown
rewrite_refs(schema: JsonSchemaschema, ($ref: string$ref) => {
const const tokens: string[]tokens = $ref: string$ref.String.split(separator: string | RegExp, limit?: number): string[] (+1 overload)Split a string into substrings using the specified separator and return them as an array.
split("/")
if (const tokens: string[]tokens.Array<string>.length: numberGets or sets the length of the array. This is a number one higher than the highest index in the array.
length > 0) {
const const identifier: anyidentifier = import unescapeTokenunescapeToken(const tokens: string[]tokens[const tokens: string[]tokens.Array<string>.length: numberGets or sets the length of the array. This is a number one higher than the highest index in the array.
length - 1])
const const sanitized: string | undefinedsanitized = const keyMap: Map<string, string>keyMap.Map<string, string>.get(key: string): string | undefinedReturns a specified element from the Map object. If the value that is associated to the provided key is an object, then you will get a reference to that object and any change made to that object will effectively modify it inside the Map.
get(const identifier: anyidentifier)
if (const sanitized: string | undefinedsanitized !== var undefinedundefined) {
$ref: string$ref = const tokens: string[]tokens.Array<string>.slice(start?: number, end?: number): string[]Returns a copy of a section of an array.
For both start and end, a negative index can be used to indicate an offset from the end of the array.
For example, -2 refers to the second to last element of the array.
slice(0, -1).Array<string>.join(separator?: string): stringAdds all the elements of an array into a string, separated by the specified separator string.
join("/") + "/" + const sanitized: stringsanitized
}
}
return $ref: string$ref.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_DEFS: RegExpRE_DEFS, "#/components/schemas")
}) as JsonSchema
}
return {
MultiDocument<"openapi-3.1">.dialect: "openapi-3.1"dialect: "openapi-3.1",
MultiDocument<D extends Dialect>.schemas: readonly [JsonSchema, ...JsonSchema[]](property) MultiDocument<D extends Dialect>.schemas: {
0: JsonSchema;
length: number;
toString: () => string;
toLocaleString: { (): string; (locales: string | string[], options?: Intl.NumberFormatOptions & Intl.DateTimeFormatOptions): string };
pop: () => JsonSchema | undefined;
push: (...items: Array<JsonSchema>) => number;
concat: { (...items: Array<ConcatArray<JsonSchema>>): Array<JsonSchema>; (...items: Array<JsonSchema | ConcatArray<JsonSchema>>): Array<JsonSchema> };
join: (separator?: string) => string;
reverse: () => Array<JsonSchema>;
shift: () => JsonSchema | undefined;
slice: (start?: number, end?: number) => Array<JsonSchema>;
sort: (compareFn?: ((a: JsonSchema, b: JsonSchema) => number) | undefined) => [JsonSchema, ...JsonSchema[]];
splice: { (start: number, deleteCount?: number): Array<JsonSchema>; (start: number, deleteCount: number, ...items: Array<JsonSchema>): Array<JsonSchema> };
unshift: (...items: Array<JsonSchema>) => number;
indexOf: (searchElement: JsonSchema, fromIndex?: number) => number;
lastIndexOf: (searchElement: JsonSchema, fromIndex?: number) => number;
every: { (predicate: (value: JsonSchema, index: number, array: Array<JsonSchema>) => value is S, thisArg?: any): this is S[]; (predicate: (value: JsonSchema, index: number, array: Array<JsonSchema>) => unknown, thisArg?: any): boolean };
some: (predicate: (value: JsonSchema, index: number, array: Array<JsonSchema>) => unknown, thisArg?: any) => boolean;
forEach: (callbackfn: (value: JsonSchema, index: number, array: Array<JsonSchema>) => void, thisArg?: any) => void;
map: (callbackfn: (value: JsonSchema, index: number, array: Array<JsonSchema>) => U, thisArg?: any) => Array<U>;
filter: { (predicate: (value: JsonSchema, index: number, array: Array<JsonSchema>) => value is S, thisArg?: any): Array<S>; (predicate: (value: JsonSchema, index: number, array: Array<JsonSchema>) => unknown, thisArg?: any): Array<JsonSchema> };
reduce: { (callbackfn: (previousValue: JsonSchema, currentValue: JsonSchema, currentIndex: number, array: Array<JsonSchema>) => JsonSchema): JsonSchema; (callbackfn: (previousValue: JsonSchema, currentValue: JsonSchema, currentIndex: number, array…;
reduceRight: { (callbackfn: (previousValue: JsonSchema, currentValue: JsonSchema, currentIndex: number, array: Array<JsonSchema>) => JsonSchema): JsonSchema; (callbackfn: (previousValue: JsonSchema, currentValue: JsonSchema, currentIndex: number, array…;
find: { (predicate: (value: JsonSchema, index: number, obj: Array<JsonSchema>) => value is S, thisArg?: any): S | undefined; (predicate: (value: JsonSchema, index: number, obj: Array<JsonSchema>) => unknown, thisArg?: any): JsonSchema | undefine…;
findIndex: (predicate: (value: JsonSchema, index: number, obj: Array<JsonSchema>) => unknown, thisArg?: any) => number;
fill: (value: JsonSchema, start?: number, end?: number) => [JsonSchema, ...JsonSchema[]];
copyWithin: (target: number, start: number, end?: number) => [JsonSchema, ...JsonSchema[]];
entries: () => ArrayIterator<[number, JsonSchema]>;
keys: () => ArrayIterator<number>;
values: () => ArrayIterator<JsonSchema>;
includes: (searchElement: JsonSchema, fromIndex?: number) => boolean;
flatMap: (callback: (this: This, value: JsonSchema, index: number, array: Array<JsonSchema>) => U | ReadonlyArray<U>, thisArg?: This | undefined) => Array<U>;
flat: (this: A, depth?: D | undefined) => Array<FlatArray<A, D>>;
at: (index: number) => JsonSchema | undefined;
findLast: { (predicate: (value: JsonSchema, index: number, array: Array<JsonSchema>) => value is S, thisArg?: any): S | undefined; (predicate: (value: JsonSchema, index: number, array: Array<JsonSchema>) => unknown, thisArg?: any): JsonSchema | unde…;
findLastIndex: (predicate: (value: JsonSchema, index: number, array: Array<JsonSchema>) => unknown, thisArg?: any) => number;
toReversed: () => Array<JsonSchema>;
toSorted: (compareFn?: ((a: JsonSchema, b: JsonSchema) => number) | undefined) => Array<JsonSchema>;
toSpliced: { (start: number, deleteCount: number, ...items: Array<JsonSchema>): Array<JsonSchema>; (start: number, deleteCount?: number): Array<JsonSchema> };
with: (index: number, value: JsonSchema) => Array<JsonSchema>;
}
schemas: import ArrArr.map(multiDocument: MultiDocument<"draft-2020-12">(parameter) multiDocument: {
dialect: D;
schemas: readonly [JsonSchema, ...Array<JsonSchema>];
definitions: Definitions;
}
multiDocument.MultiDocument<D extends Dialect>.schemas: readonly [JsonSchema, ...Array<JsonSchema>](property) MultiDocument<D extends Dialect>.schemas: {
0: JsonSchema;
length: number;
toString: () => string;
toLocaleString: { (): string; (locales: string | string[], options?: Intl.NumberFormatOptions & Intl.DateTimeFormatOptions): string };
concat: { (...items: Array<ConcatArray<JsonSchema>>): Array<JsonSchema>; (...items: Array<JsonSchema | ConcatArray<JsonSchema>>): Array<JsonSchema> };
join: (separator?: string) => string;
slice: (start?: number, end?: number) => Array<JsonSchema>;
indexOf: (searchElement: JsonSchema, fromIndex?: number) => number;
lastIndexOf: (searchElement: JsonSchema, fromIndex?: number) => number;
every: { (predicate: (value: JsonSchema, index: number, array: ReadonlyArray<JsonSchema>) => value is S, thisArg?: any): this is readonly S[]; (predicate: (value: JsonSchema, index: number, array: ReadonlyArray<JsonSchema>) => unknown, thisArg?: …;
some: (predicate: (value: JsonSchema, index: number, array: ReadonlyArray<JsonSchema>) => unknown, thisArg?: any) => boolean;
forEach: (callbackfn: (value: JsonSchema, index: number, array: ReadonlyArray<JsonSchema>) => void, thisArg?: any) => void;
map: (callbackfn: (value: JsonSchema, index: number, array: ReadonlyArray<JsonSchema>) => U, thisArg?: any) => Array<U>;
filter: { (predicate: (value: JsonSchema, index: number, array: ReadonlyArray<JsonSchema>) => value is S, thisArg?: any): Array<S>; (predicate: (value: JsonSchema, index: number, array: ReadonlyArray<JsonSchema>) => unknown, thisArg?: any): Array<…;
reduce: { (callbackfn: (previousValue: JsonSchema, currentValue: JsonSchema, currentIndex: number, array: ReadonlyArray<JsonSchema>) => JsonSchema): JsonSchema; (callbackfn: (previousValue: JsonSchema, currentValue: JsonSchema, currentIndex: numbe…;
reduceRight: { (callbackfn: (previousValue: JsonSchema, currentValue: JsonSchema, currentIndex: number, array: ReadonlyArray<JsonSchema>) => JsonSchema): JsonSchema; (callbackfn: (previousValue: JsonSchema, currentValue: JsonSchema, currentIndex: numbe…;
find: { (predicate: (value: JsonSchema, index: number, obj: ReadonlyArray<JsonSchema>) => value is S, thisArg?: any): S | undefined; (predicate: (value: JsonSchema, index: number, obj: ReadonlyArray<JsonSchema>) => unknown, thisArg?: any): JsonS…;
findIndex: (predicate: (value: JsonSchema, index: number, obj: ReadonlyArray<JsonSchema>) => unknown, thisArg?: any) => number;
entries: () => ArrayIterator<[number, JsonSchema]>;
keys: () => ArrayIterator<number>;
values: () => ArrayIterator<JsonSchema>;
includes: (searchElement: JsonSchema, fromIndex?: number) => boolean;
flatMap: (callback: (this: This, value: JsonSchema, index: number, array: Array<JsonSchema>) => U | ReadonlyArray<U>, thisArg?: This | undefined) => Array<U>;
flat: (this: A, depth?: D | undefined) => Array<FlatArray<A, D>>;
at: (index: number) => JsonSchema | undefined;
findLast: { (predicate: (value: JsonSchema, index: number, array: ReadonlyArray<JsonSchema>) => value is S, thisArg?: any): S | undefined; (predicate: (value: JsonSchema, index: number, array: ReadonlyArray<JsonSchema>) => unknown, thisArg?: any): J…;
findLastIndex: (predicate: (value: JsonSchema, index: number, array: ReadonlyArray<JsonSchema>) => unknown, thisArg?: any) => number;
toReversed: () => Array<JsonSchema>;
toSorted: (compareFn?: ((a: JsonSchema, b: JsonSchema) => number) | undefined) => Array<JsonSchema>;
toSpliced: { (start: number, deleteCount: number, ...items: Array<JsonSchema>): Array<JsonSchema>; (start: number, deleteCount?: number): Array<JsonSchema> };
with: (index: number, value: JsonSchema) => Array<JsonSchema>;
}
schemas, function (local function) rewrite(schema: JsonSchema): JsonSchemarewrite),
MultiDocument<D extends Dialect>.definitions: Record<string, JsonSchema>definitions: import RecRec.const mapEntries: {
<K extends string, A, K2 extends string, B>(
f: (a: A, key: K) => readonly [K2, B]
): (self: ReadonlyRecord<K, A>) => Record<K2, B>
<K extends string, A, K2 extends string, B>(
self: ReadonlyRecord<K, A>,
f: (a: A, key: K) => [K2, B]
): Record<K2, B>
}
mapEntries(
multiDocument: MultiDocument<"draft-2020-12">(parameter) multiDocument: {
dialect: D;
schemas: readonly [JsonSchema, ...Array<JsonSchema>];
definitions: Definitions;
}
multiDocument.MultiDocument<D extends Dialect>.definitions: Definitionsdefinitions,
(definition: JsonSchemadefinition, key: stringkey) => [const keyMap: Map<string, string>keyMap.Map<string, string>.get(key: string): string | undefinedReturns a specified element from the Map object. If the value that is associated to the provided key is an object, then you will get a reference to that object and any change made to that object will effectively modify it inside the Map.
get(key: stringkey) ?? key: stringkey, function (local function) rewrite(schema: JsonSchema): JsonSchemarewrite(definition: JsonSchemadefinition)]
)
}
}