schemas() function
undefined
export namespace json {
export function schemas<
Schemas extends unknown[],
Version extends "3.0" | "3.1" = "3.1",
>(): IJsonSchemaCollection<Version>;
}undefined
file not found: src/schemas/json/IJsonSchemaCollection.tsundefined
import { IJsonSchemaAttribute } from "../schema/IJsonSchemaAttribute";
import * as tags from "../tags";
/**
* Emended OpenAPI v3.1 specification.
*
* `OpenApi` is a refined OpenAPI v3.1 specification that normalizes ambiguous
* and redundant expressions from various OpenAPI versions (Swagger 2.0, OpenAPI
* 3.0, 3.1). This unified format simplifies schema processing for `typia` and
* `@nestia/sdk`.
*
* Key simplifications:
*
* - Schema `$ref` references are unified to `#/components/schemas/{name}` format
* - Non-schema references (parameters, responses) are resolved inline
* - `nullable` is converted to `{ oneOf: [schema, { type: "null\" }] }`
* - `allOf` compositions are merged into single schemas
* - Schema attributes are normalized across all versions
*
* Use `HttpLlm.application()` from `@typia/utils` to convert
* `OpenApi.IDocument` into {@link IHttpLlmApplication} for LLM function
* calling.
*
* @author Jeongho Nam - https://github.com/samchon
*/
export namespace OpenApi {
/**
* HTTP method supported by OpenAPI operations.
*
* Standard HTTP methods used in REST APIs. Each path can have multiple
* operations, one per HTTP method.
*/
export type Method =
| "get"
| "post"
| "put"
| "delete"
| "options"
| "head"
| "patch"
| "trace";
/**
* Root document structure for emended OpenAPI v3.1.
*
* Contains all API metadata, paths, operations, and reusable components. The
* `x-samchon-emended-v4` marker indicates this document has been processed by
* `samchon/typia` to normalize schema formats.
*/
export interface IDocument {
/** OpenAPI version. */
openapi: `3.1.${number}`;
/** List of servers providing the API. */
servers?: IServer[];
/** API metadata. */
info?: IDocument.IInfo;
/** Reusable components (schemas, security schemes). */
components: IComponents;
/** Available API paths and operations. */
paths?: Record<string, IPath>;
/** Webhook definitions. */
webhooks?: Record<string, IPath>;
/** Global security requirements. */
security?: Record<string, string[]>[];
/** Tag definitions for grouping operations. */
tags?: IDocument.ITag[];
/** Marker for emended document by `@samchon/openapi`. */
"x-samchon-emended-v4": true;
}
export namespace IDocument {
/**
* API metadata and identification.
*
* Contains essential information about the API including title, version,
* contact information, and licensing details.
*/
export interface IInfo {
/** API title. */
title: string;
/** Short summary. */
summary?: string;
/** Full description. */
description?: string;
/** Terms of service URL. */
termsOfService?: string;
/** Contact information. */
contact?: IContact;
/** License information. */
license?: ILicense;
/** API version. */
version: string;
}
/** Tag for grouping operations. */
export interface ITag {
/** Tag name. */
name: string;
/** Tag description. */
description?: string;
}
/** Contact information. */
export interface IContact {
/** Contact name. */
name?: string;
/** Contact URL. */
url?: string;
/** Contact email. */
email?: string & tags.Format<"email">;
}
/** License information. */
export interface ILicense {
/** License name. */
name: string;
/** SPDX license identifier. */
identifier?: string;
/** License URL. */
url?: string;
}
}
/** Server providing the API. */
export interface IServer {
/** Server URL. */
url: string;
/** Server description. */
description?: string;
/** URL template variables. */
variables?: Record<string, IServer.IVariable>;
}
export namespace IServer {
/** URL template variable. */
export interface IVariable {
/** Default value. */
default: string;
/** Allowed values. */
enum?: string[];
/** Variable description. */
description?: string;
}
}
/** Path item containing operations by HTTP method. */
export interface IPath extends Partial<Record<Method, IOperation>> {
/** Path-level servers. */
servers?: IServer[];
/** Path summary. */
summary?: string;
/** Path description. */
description?: string;
}
/** API operation metadata. */
export interface IOperation {
/** Unique operation identifier. */
operationId?: string;
/** Operation parameters. */
parameters?: IOperation.IParameter[];
/** Request body. */
requestBody?: IOperation.IRequestBody;
/** Response definitions by status code. */
responses?: Record<string, IOperation.IResponse>;
/** Operation-level servers. */
servers?: IServer[];
/** Short summary. */
summary?: string;
/** Full description. */
description?: string;
/** Security requirements. */
security?: Record<string, string[]>[];
/** Operation tags for grouping. */
tags?: string[];
/** Whether deprecated. */
deprecated?: boolean;
/** Excludes from LLM function calling when `true`. */
"x-samchon-human"?: boolean;
/** Custom accessor path for migration. */
"x-samchon-accessor"?: string[];
/** Controller name for code generation. */
"x-samchon-controller"?: string;
}
export namespace IOperation {
/** Operation parameter. */
export interface IParameter {
/** Parameter name. */
name?: string;
/** Parameter location. */
in: "path" | "query" | "header" | "cookie";
/** Parameter schema. */
schema: IJsonSchema;
/** Whether required. */
required?: boolean;
/** Parameter description. */
description?: string;
/** Example value. */
example?: any;
/** Named examples. */
examples?: Record<string, IExample>;
}
/** Request body. */
export interface IRequestBody {
/** Body content by media type. */
content?: IContent;
/** Body description. */
description?: string;
/** Whether required. */
required?: boolean;
/** Nestia encryption flag. */
"x-nestia-encrypted"?: boolean;
}
/** Response definition. */
export interface IResponse {
/** Response headers. */
headers?: Record<string, IOperation.IParameter>;
/** Response content by media type. */
content?: IContent;
/** Response description. */
description?: string;
/** Nestia encryption flag. */
"x-nestia-encrypted"?: boolean;
}
/** Content by media type. */
export interface IContent extends Partial<
Record<ContentType, IMediaType>
> {}
/** Media type definition. */
export interface IMediaType {
/** Content schema. */
schema?: IJsonSchema;
/** Example value. */
example?: any;
/** Named examples. */
examples?: Record<string, IExample>;
}
/** Supported content types. */
export type ContentType =
| "text/plain"
| "application/json"
| "application/x-www-form-url-encoded"
| "multipart/form-data"
| "*/*"
| (string & {});
}
/** Example value definition. */
export interface IExample {
/** Example summary. */
summary?: string;
/** Example description. */
description?: string;
/** Example value. */
value?: any;
/** External value URL. */
externalValue?: string;
}
/** Reusable components storage. */
export interface IComponents {
/** Named schemas. */
schemas?: Record<string, IJsonSchema>;
/** Named security schemes. */
securitySchemes?: Record<string, ISecurityScheme>;
}
/**
* JSON Schema type for emended OpenAPI v3.1.
*
* Represents all possible JSON Schema types in the normalized OpenAPI format.
* This is a discriminated union - check the `type` property or use type
* guards to narrow to specific schema types.
*
* Unlike raw JSON Schema, this format:
*
* - Uses `oneOf` instead of `anyOf` for union types
* - Separates `IArray` (homogeneous) from `ITuple` (heterogeneous)
* - Normalizes nullable types to `oneOf` with null schema
*/
export type IJsonSchema =
| IJsonSchema.IConstant
| IJsonSchema.IBoolean
| IJsonSchema.IInteger
| IJsonSchema.INumber
| IJsonSchema.IString
| IJsonSchema.IArray
| IJsonSchema.ITuple
| IJsonSchema.IObject
| IJsonSchema.IReference
| IJsonSchema.IOneOf
| IJsonSchema.INull
| IJsonSchema.IUnknown;
export namespace IJsonSchema {
/** Constant value type. */
export interface IConstant extends IJsonSchemaAttribute {
/** Constant value. */
const: boolean | number | string;
}
/** Boolean type. */
export interface IBoolean extends IJsonSchemaAttribute.IBoolean {
/** Default value. */
default?: boolean;
}
/** Integer type. */
export interface IInteger extends IJsonSchemaAttribute.IInteger {
/** Default value. */
default?: number & tags.Type<"int64">;
/** Minimum value. */
minimum?: number & tags.Type<"int64">;
/** Maximum value. */
maximum?: number & tags.Type<"int64">;
/** Exclusive minimum. */
exclusiveMinimum?: number & tags.Type<"int64">;
/** Exclusive maximum. */
exclusiveMaximum?: number & tags.Type<"int64">;
/** Multiple of constraint. */
multipleOf?: number & tags.ExclusiveMinimum<0>;
}
/** Number (double) type. */
export interface INumber extends IJsonSchemaAttribute.INumber {
/** Default value. */
default?: number;
/** Minimum value. */
minimum?: number;
/** Maximum value. */
maximum?: number;
/** Exclusive minimum. */
exclusiveMinimum?: number;
/** Exclusive maximum. */
exclusiveMaximum?: number;
/** Multiple of constraint. */
multipleOf?: number & tags.ExclusiveMinimum<0>;
}
/** String type. */
export interface IString extends IJsonSchemaAttribute.IString {
/** Default value. */
default?: string;
/** String format. */
format?:
| "binary"
| "byte"
| "password"
| "regex"
| "uuid"
| "email"
| "hostname"
| "idn-email"
| "idn-hostname"
| "iri"
| "iri-reference"
| "ipv4"
| "ipv6"
| "uri"
| "uri-reference"
| "uri-template"
| "url"
| "date-time"
| "date"
| "time"
| "duration"
| "json-pointer"
| "relative-json-pointer"
| (string & {});
/** Regex pattern. */
pattern?: string;
/** Content media type. */
contentMediaType?: string;
/** Minimum length. */
minLength?: number & tags.Type<"uint64">;
/** Maximum length. */
maxLength?: number & tags.Type<"uint64">;
}
/** Array type. */
export interface IArray extends IJsonSchemaAttribute.IArray {
/** Element type. */
items: IJsonSchema;
/** Whether elements must be unique. */
uniqueItems?: boolean;
/** Minimum items. */
minItems?: number & tags.Type<"uint64">;
/** Maximum items. */
maxItems?: number & tags.Type<"uint64">;
}
/** Tuple type. */
export interface ITuple extends IJsonSchemaAttribute {
/** Type discriminator. */
type: "array";
/** Tuple element types. */
prefixItems: IJsonSchema[];
/** Rest element type or `true` for any. */
additionalItems?: boolean | IJsonSchema;
/** Whether elements must be unique. */
uniqueItems?: boolean;
/** Minimum items. */
minItems?: number & tags.Type<"uint64">;
/** Maximum items. */
maxItems?: number & tags.Type<"uint64">;
}
/** Object type. */
export interface IObject extends IJsonSchemaAttribute.IObject {
/** Property schemas. */
properties?: Record<string, IJsonSchema>;
/** Additional properties schema or `true` for any. */
additionalProperties?: boolean | IJsonSchema;
/** Required property names. */
required?: string[];
}
/** Reference to named schema. */
export interface IReference<Key = string> extends IJsonSchemaAttribute {
/** Reference path (e.g., `#/components/schemas/TypeName`). */
$ref: Key;
}
/** Union type (`oneOf`). */
export interface IOneOf extends IJsonSchemaAttribute {
/** Union member schemas. */
oneOf: Exclude<IJsonSchema, IJsonSchema.IOneOf>[];
/** Discriminator for tagged unions. */
discriminator?: IOneOf.IDiscriminator;
}
export namespace IOneOf {
/** Discriminator for tagged unions. */
export interface IDiscriminator {
/** Discriminator property name. */
propertyName: string;
/** Value to schema mapping. */
mapping?: Record<string, string>;
}
}
/** Null type. */
export interface INull extends IJsonSchemaAttribute.INull {
/** Default value. */
default?: null;
}
/** Unknown (`any`) type. */
export interface IUnknown extends IJsonSchemaAttribute.IUnknown {
/** Default value. */
default?: any;
}
}
/** Security scheme types. */
export type ISecurityScheme =
| ISecurityScheme.IApiKey
| ISecurityScheme.IHttpBasic
| ISecurityScheme.IHttpBearer
| ISecurityScheme.IOAuth2
| ISecurityScheme.IOpenId;
export namespace ISecurityScheme {
/** API key authentication. */
export interface IApiKey {
/** Scheme type. */
type: "apiKey";
/** Key location. */
in?: "header" | "query" | "cookie";
/** Key name. */
name?: string;
/** Scheme description. */
description?: string;
}
/** HTTP basic authentication. */
export interface IHttpBasic {
/** Scheme type. */
type: "http";
/** Authentication scheme. */
scheme: "basic";
/** Scheme description. */
description?: string;
}
/** HTTP bearer authentication. */
export interface IHttpBearer {
/** Scheme type. */
type: "http";
/** Authentication scheme. */
scheme: "bearer";
/** Bearer token format hint. */
bearerFormat?: string;
/** Scheme description. */
description?: string;
}
/** OAuth2 authentication. */
export interface IOAuth2 {
/** Scheme type. */
type: "oauth2";
/** OAuth2 flows. */
flows: IOAuth2.IFlowSet;
/** Scheme description. */
description?: string;
}
/** OpenID Connect authentication. */
export interface IOpenId {
/** Scheme type. */
type: "openIdConnect";
/** OpenID Connect discovery URL. */
openIdConnectUrl: string;
/** Scheme description. */
description?: string;
}
export namespace IOAuth2 {
/** OAuth2 flow configurations. */
export interface IFlowSet {
/** Authorization code flow. */
authorizationCode?: IFlow;
/** Implicit flow. */
implicit?: Omit<IFlow, "tokenUrl">;
/** Password flow. */
password?: Omit<IFlow, "authorizationUrl">;
/** Client credentials flow. */
clientCredentials?: Omit<IFlow, "authorizationUrl">;
}
/** OAuth2 flow configuration. */
export interface IFlow {
/** Authorization URL. */
authorizationUrl?: string;
/** Token URL. */
tokenUrl?: string;
/** Refresh URL. */
refreshUrl?: string;
/** Available scopes. */
scopes?: Record<string, string>;
}
}
}
}undefined
import { IJsonSchemaAttribute } from "../schema/IJsonSchemaAttribute";
import * as tags from "../tags";
/**
* OpenAPI v3.0 specification types.
*
* `OpenApiV3` contains TypeScript type definitions for OpenAPI v3.0 documents.
* Used for parsing and generating OpenAPI v3.0 specifications. For a normalized
* format that unifies all OpenAPI versions, use {@link OpenApi} instead.
*
* Key differences from v3.1:
*
* - Uses `nullable: true` instead of `type: ["string", "null"]`
* - No `const` keyword support
* - No `prefixItems` for tuples
*
* @author Jeongho Nam - https://github.com/samchon
*/
export namespace OpenApiV3 {
/** HTTP method of the operation. */
export type Method =
| "get"
| "post"
| "put"
| "delete"
| "options"
| "head"
| "patch"
| "trace";
/* -----------------------------------------------------------
DOCUMENTS
----------------------------------------------------------- */
/** OpenAPI document structure. */
export interface IDocument {
/** OpenAPI version. */
openapi: "3.0" | `3.0.${number}`;
/** List of servers. */
servers?: IServer[];
/** API metadata. */
info?: IDocument.IInfo;
/** Reusable components. */
components?: IComponents;
/** API paths and operations. */
paths?: Record<string, IPath>;
/** Global security requirements. */
security?: Record<string, string[]>[];
/** Tag definitions. */
tags?: IDocument.ITag[];
}
export namespace IDocument {
/** API metadata. */
export interface IInfo {
/** API title. */
title: string;
/** API description. */
description?: string;
/** Terms of service URL. */
termsOfService?: string;
/** Contact information. */
contact?: IContact;
/** License information. */
license?: ILicense;
/** API version. */
version: string;
}
/** Tag for grouping operations. */
export interface ITag {
/** Tag name. */
name: string;
/** Tag description. */
description?: string;
}
/** Contact information. */
export interface IContact {
/** Contact name. */
name?: string;
/** Contact URL. */
url?: string;
/** Contact email. */
email?: string & tags.Format<"email">;
}
/** License information. */
export interface ILicense {
/** License name. */
name: string;
/** License URL. */
url?: string;
}
}
/** Server providing the API. */
export interface IServer {
/** Server URL. */
url: string;
/** Server description. */
description?: string;
/** URL template variables. */
variables?: Record<string, IServer.IVariable>;
}
export namespace IServer {
/** URL template variable. */
export interface IVariable {
/** Default value. */
default: string;
/** Allowed values. */
enum?: string[];
/** Variable description. */
description?: string;
}
}
/* -----------------------------------------------------------
PATH ITEMS
----------------------------------------------------------- */
/** Path item containing operations by HTTP method. */
export interface IPath extends Partial<
Record<Method, IOperation | undefined>
> {
/** Path-level parameters. */
parameters?: Array<
| IOperation.IParameter
| IJsonSchema.IReference<`#/components/headers/${string}`>
| IJsonSchema.IReference<`#/components/parameters/${string}`>
>;
/** Path-level servers. */
servers?: IServer[];
/** Path summary. */
summary?: string;
/** Path description. */
description?: string;
}
/** API operation metadata. */
export interface IOperation {
/** Unique operation identifier. */
operationId?: string;
/** Operation parameters. */
parameters?: Array<
| IOperation.IParameter
| IJsonSchema.IReference<`#/components/headers/${string}`>
| IJsonSchema.IReference<`#/components/parameters/${string}`>
>;
/** Request body. */
requestBody?:
| IOperation.IRequestBody
| IJsonSchema.IReference<`#/components/requestBodies/${string}`>;
/** Response definitions by status code. */
responses?: Record<
string,
| IOperation.IResponse
| IJsonSchema.IReference<`#/components/responses/${string}`>
>;
/** Operation-level servers. */
servers?: IServer[];
/** Short summary. */
summary?: string;
/** Full description. */
description?: string;
/** Security requirements. */
security?: Record<string, string[]>[];
/** Operation tags. */
tags?: string[];
/** Whether deprecated. */
deprecated?: boolean;
}
export namespace IOperation {
/** Operation parameter. */
export interface IParameter {
/** Parameter name. */
name?: string;
/** Parameter location. */
in: "path" | "query" | "header" | "cookie";
/** Parameter schema. */
schema: IJsonSchema;
/** Whether required. */
required?: boolean;
/** Parameter description. */
description?: string;
/** Example value. */
example?: any;
/** Named examples. */
examples?: Record<
string,
IExample | IJsonSchema.IReference<`#/components/examples/${string}`>
>;
}
/** Request body. */
export interface IRequestBody {
/** Body description. */
description?: string;
/** Whether required. */
required?: boolean;
/** Body content by media type. */
content?: Record<string, IMediaType>;
}
/** Response definition. */
export interface IResponse {
/** Response content by media type. */
content?: Record<string, IMediaType>;
/** Response headers. */
headers?: Record<
string,
| Omit<IOperation.IParameter, "in">
| IJsonSchema.IReference<`#/components/headers/${string}`>
>;
/** Response description. */
description?: string;
}
/** Media type definition. */
export interface IMediaType {
/** Content schema. */
schema?: IJsonSchema;
/** Example value. */
example?: any;
/** Named examples. */
examples?: Record<
string,
IExample | IJsonSchema.IReference<`#/components/examples/${string}`>
>;
}
}
/** Example value definition. */
export interface IExample {
/** Example summary. */
summary?: string;
/** Example description. */
description?: string;
/** Example value. */
value?: any;
/** External value URL. */
externalValue?: string;
}
/* -----------------------------------------------------------
SCHEMA DEFINITIONS
----------------------------------------------------------- */
/** Reusable components storage. */
export interface IComponents {
/** Named schemas. */
schemas?: Record<string, IJsonSchema>;
/** Named responses. */
responses?: Record<string, IOperation.IResponse>;
/** Named parameters. */
parameters?: Record<string, IOperation.IParameter>;
/** Named request bodies. */
requestBodies?: Record<string, IOperation.IRequestBody>;
/** Named security schemes. */
securitySchemes?: Record<string, ISecurityScheme>;
/** Named headers. */
headers?: Record<string, Omit<IOperation.IParameter, "in">>;
/** Named examples. */
examples?: Record<string, IExample>;
}
/** JSON Schema type for OpenAPI v3.0. */
export type IJsonSchema =
| IJsonSchema.IBoolean
| IJsonSchema.IInteger
| IJsonSchema.INumber
| IJsonSchema.IString
| IJsonSchema.IArray
| IJsonSchema.IObject
| IJsonSchema.IReference
| IJsonSchema.IAllOf
| IJsonSchema.IAnyOf
| IJsonSchema.IOneOf
| IJsonSchema.INullOnly
| IJsonSchema.IUnknown;
export namespace IJsonSchema {
/** Boolean type. */
export interface IBoolean
extends Omit<IJsonSchemaAttribute.IBoolean, "examples">, __IAttribute {
/** Whether nullable. */
nullable?: boolean;
/** Default value. */
default?: boolean | null;
/** Allowed values. */
enum?: Array<boolean | null>;
}
/** Integer type. */
export interface IInteger
extends Omit<IJsonSchemaAttribute.IInteger, "examples">, __IAttribute {
/** Whether nullable. */
nullable?: boolean;
/** Default value. */
default?: (number & tags.Type<"int64">) | null;
/** Allowed values. */
enum?: Array<(number & tags.Type<"int64">) | null>;
/** Minimum value. */
minimum?: number & tags.Type<"int64">;
/** Maximum value. */
maximum?: number & tags.Type<"int64">;
/** Exclusive minimum. */
exclusiveMinimum?: number | boolean;
/** Exclusive maximum. */
exclusiveMaximum?: number | boolean;
/** Multiple of constraint. */
multipleOf?: number & tags.ExclusiveMinimum<0>;
}
/** Number (double) type. */
export interface INumber
extends Omit<IJsonSchemaAttribute.INumber, "examples">, __IAttribute {
/** Whether nullable. */
nullable?: boolean;
/** Default value. */
default?: number | null;
/** Allowed values. */
enum?: Array<number | null>;
/** Minimum value. */
minimum?: number;
/** Maximum value. */
maximum?: number;
/** Exclusive minimum. */
exclusiveMinimum?: number | boolean;
/** Exclusive maximum. */
exclusiveMaximum?: number | boolean;
/** Multiple of constraint. */
multipleOf?: number & tags.ExclusiveMinimum<0>;
}
/** String type. */
export interface IString
extends Omit<IJsonSchemaAttribute.IString, "examples">, __IAttribute {
/** Whether nullable. */
nullable?: boolean;
/** Default value. */
default?: string | null;
/** Allowed values. */
enum?: Array<string | null>;
/** String format. */
format?:
| "binary"
| "byte"
| "password"
| "regex"
| "uuid"
| "email"
| "hostname"
| "idn-email"
| "idn-hostname"
| "iri"
| "iri-reference"
| "ipv4"
| "ipv6"
| "uri"
| "uri-reference"
| "uri-template"
| "url"
| "date-time"
| "date"
| "time"
| "duration"
| "json-pointer"
| "relative-json-pointer"
| (string & {});
/** Regex pattern. */
pattern?: string;
/** Minimum length. */
minLength?: number & tags.Type<"uint64">;
/** Maximum length. */
maxLength?: number & tags.Type<"uint64">;
}
/** Array type. */
export interface IArray
extends Omit<IJsonSchemaAttribute.IArray, "examples">, __IAttribute {
/** Whether nullable. */
nullable?: boolean;
/** Element type. */
items: IJsonSchema;
/** Whether elements must be unique. */
uniqueItems?: boolean;
/** Minimum items. */
minItems?: number & tags.Type<"uint64">;
/** Maximum items. */
maxItems?: number & tags.Type<"uint64">;
}
/** Object type. */
export interface IObject
extends Omit<IJsonSchemaAttribute.IObject, "examples">, __IAttribute {
/** Whether nullable. */
nullable?: boolean;
/** Property schemas. */
properties?: Record<string, IJsonSchema>;
/** Required property names. */
required?: string[];
/** Additional properties schema. */
additionalProperties?: boolean | IJsonSchema;
/** Maximum properties. */
maxProperties?: number;
/** Minimum properties. */
minProperties?: number;
}
/** Reference to a named schema. */
export interface IReference<Key = string> extends __IAttribute {
/** Reference path. */
$ref: Key;
}
/** All-of combination. */
export interface IAllOf extends __IAttribute {
/** Schemas to combine. */
allOf: IJsonSchema[];
}
/** Any-of union. */
export interface IAnyOf extends __IAttribute {
/** Union member schemas. */
anyOf: IJsonSchema[];
}
/** One-of union. */
export interface IOneOf extends __IAttribute {
/** Union member schemas. */
oneOf: IJsonSchema[];
/** Discriminator for tagged unions. */
discriminator?: IOneOf.IDiscriminator;
}
export namespace IOneOf {
/** Discriminator for tagged unions. */
export interface IDiscriminator {
/** Discriminator property name. */
propertyName: string;
/** Value to schema mapping. */
mapping?: Record<string, string>;
}
}
/** Null type. */
export interface INullOnly
extends Omit<IJsonSchemaAttribute.INull, "examples">, __IAttribute {
/** Default value. */
default?: null;
}
/** Unknown type. */
export interface IUnknown
extends Omit<IJsonSchemaAttribute.IUnknown, "examples">, __IAttribute {
/** Default value. */
default?: any;
}
/** @internal Base attribute interface. */
export interface __IAttribute extends Omit<
IJsonSchemaAttribute,
"examples"
> {
/** Example values. */
examples?: any[] | Record<string, any>;
}
}
/** Security scheme types. */
export type ISecurityScheme =
| ISecurityScheme.IApiKey
| ISecurityScheme.IHttpBasic
| ISecurityScheme.IHttpBearer
| ISecurityScheme.IOAuth2
| ISecurityScheme.IOpenId;
export namespace ISecurityScheme {
/** API key authentication. */
export interface IApiKey {
/** Scheme type. */
type: "apiKey";
/** Key location. */
in?: "header" | "query" | "cookie";
/** Key name. */
name?: string;
/** Scheme description. */
description?: string;
}
/** HTTP basic authentication. */
export interface IHttpBasic {
/** Scheme type. */
type: "http";
/** Authentication scheme. */
scheme: "basic";
/** Scheme description. */
description?: string;
}
/** HTTP bearer authentication. */
export interface IHttpBearer {
/** Scheme type. */
type: "http";
/** Authentication scheme. */
scheme: "bearer";
/** Bearer token format hint. */
bearerFormat?: string;
/** Scheme description. */
description?: string;
}
/** OAuth2 authentication. */
export interface IOAuth2 {
/** Scheme type. */
type: "oauth2";
/** OAuth2 flows. */
flows: IOAuth2.IFlowSet;
/** Scheme description. */
description?: string;
}
/** OpenID Connect authentication. */
export interface IOpenId {
/** Scheme type. */
type: "openIdConnect";
/** OpenID Connect discovery URL. */
openIdConnectUrl: string;
/** Scheme description. */
description?: string;
}
export namespace IOAuth2 {
/** OAuth2 flow configurations. */
export interface IFlowSet {
/** Authorization code flow. */
authorizationCode?: IFlow;
/** Implicit flow. */
implicit?: Omit<IFlow, "tokenUrl">;
/** Password flow. */
password?: Omit<IFlow, "authorizationUrl">;
/** Client credentials flow. */
clientCredentials?: Omit<IFlow, "authorizationUrl">;
}
/** OAuth2 flow configuration. */
export interface IFlow {
/** Authorization URL. */
authorizationUrl?: string;
/** Token URL. */
tokenUrl?: string;
/** Refresh URL. */
refreshUrl?: string;
/** Available scopes. */
scopes?: Record<string, string>;
}
}
}
}JSON schema generator.
- Definitions:
When you need JSON schema, do not write it by yourself, but just call typia.json.schemas() function.
If you call the typia.json.schemas() with specialization of target Schemas, typia will analyze your Schemas and generate JSON schema definition in the compilation level. However, note that, JSON schema definitions of “OpenAPI v3.0” and “OpenAPI v3.1” are a little bit different. Therefore, you have to consider which value to assign in the Version argument.
- Swagger can’t express tuple type
- Swagger can’t express pattern property
TypeScript Source Code
import typia, { tags } from "typia";
export const MemberSchema = typia.json.schemas<[IMember], "3.0">();
interface IMember {
/** Unique user ID generated by server. */
id: string & tags.Format<"uuid">;
/** Email address of the member. */
email: string & tags.Format<"email">;
/**
* Age of the member.
*
* For reference, only adult can be a member.
*/
age: number &
tags.Type<"uint32"> &
tags.ExclusiveMinimum<19> &
tags.Maximum<100>;
}Compiled JavaScript File
import typia from "typia";
export const MemberSchema = {
version: "3.0",
components: {
schemas: {
IMember: {
type: "object",
properties: {
id: {
type: "string",
format: "uuid",
description: "Unique user ID generated by server.",
},
email: {
type: "string",
format: "email",
description: "Email address of the member.",
},
age: {
type: "integer",
exclusiveMinimum: 19,
maximum: 100,
description:
"Age of the member.\n\nFor reference, only adult can be a member.",
},
},
required: ["id", "email", "age"],
},
},
},
schemas: [
{
$ref: "#/components/schemas/IMember",
},
],
};Specialization
You can utilize type tags (or validator’s comment tags) to constructing special fields of JSON schema.
If you write any comment on a property, it would fill the IJsonSchema.description value. Also, there’re special comment tags only for JSON schema definition that are different with validator’s comment tags like below.
@deprecated@hidden@internal@title {string}
Let’s see how those type tags, comment tags and description comments are working with example code.
TypeScript Source Code
import typia, { tags } from "typia";
export const SpecialCommentTagSchema = typia.json.schemas<[Special], "3.1">();
interface Special {
/**
* Deprecated tags are just used for marking.
*
* @deprecated
* @title Unsigned integer
*/
type: number & tags.Type<"int32">;
/**
* Internal tagged property never be shown in JSON schema.
*
* It even doesn't be shown in other `typia` functions like `assert<T>()`.
*
* @internal
*/
internal: number[];
/**
* Hidden tagged property never be shown in JSON schema.
*
* However, it would be shown in other `typia` functions like
* `stringify<T>()`.
*
* @ignore
*/
hidden: boolean;
/**
* You can limit the range of number.
*
* @exclusiveMinimum 19
* @maximum 100
*/
number?: number;
/**
* You can limit the length of string.
*
* Also, multiple range conditions are also possible.
*/
string: string &
(
| (tags.MinLength<3> & tags.MaxLength<24>)
| (tags.MinLength<40> & tags.MaxLength<100>)
);
/**
* You can limit the pattern of string.
*
* @pattern ^[a-z]+$
*/
pattern: string;
/**
* You can limit the format of string.
*
* @format date-time
*/
format: string | null;
/** In the Array case, possible to restrict its elements. */
array: Array<string & tags.Format<"uuid">> & tags.MinItems<3>;
}Compiled JavaScript File
import typia from "typia";
export const SpecialCommentTagSchema = {
version: "3.1",
components: {
schemas: {
Special: {
type: "object",
properties: {
type: {
type: "integer",
deprecated: true,
title: "Unsigned integer",
description: "Deprecated tags are just used for marking.",
},
number: {
type: "number",
exclusiveMinimum: 19,
maximum: 100,
description: "You can limit the range of number.",
},
string: {
oneOf: [
{
type: "string",
minLength: 3,
maxLength: 24,
},
{
type: "string",
minLength: 40,
maxLength: 100,
},
],
description:
"You can limit the length of string.\n\nAlso, multiple range conditions are also possible.",
},
pattern: {
type: "string",
pattern: "^[a-z]+$",
description: "You can limit the pattern of string.",
},
format: {
oneOf: [
{
type: "null",
},
{
type: "string",
format: "date-time",
},
],
description: "You can limit the format of string.",
},
array: {
type: "array",
items: {
type: "string",
format: "uuid",
},
minItems: 3,
description:
"In the Array case, possible to restrict its elements.",
},
},
required: ["type", "string", "pattern", "format", "array"],
},
},
},
schemas: [
{
$ref: "#/components/schemas/Special",
},
],
};Customization
If what you want is not just filling regular properties of JSON schema specification, but to adding custom properties into the JSON schema definition, you can do it through the tags.TagBase.schema property type or tags.JsonSchemaPlugin type.
For reference, the custom property must be started with x- prefix. It’s a rule of JSON schema.
TypeScript Source Code
import typia, { tags } from "typia";
export const SpecialTypeTagSchema = typia.json.schemas<[IAccount]>();
type Monetary<Value extends string> = tags.TagBase<{
target: "number";
kind: "monetary";
value: Value;
schema: {
"x-monetary": Value;
};
}>;
type Placeholder<Value extends string> = tags.JsonSchemaPlugin<{
"x-placeholder": Value;
}>;
interface IAccount {
code: string & Placeholder<"Write your account code please">;
balance: number & Monetary<"dollar">;
}Compiled JavaScript File
import typia from "typia";
export const SpecialTypeTagSchema = {
version: "3.1",
components: {
schemas: {
IAccount: {
type: "object",
properties: {
code: {
type: "string",
"x-placeholder": "Write your account code please",
},
balance: {
type: "number",
"x-monetary": "dollar",
},
},
required: ["code", "balance"],
},
},
},
schemas: [
{
$ref: "#/components/schemas/IAccount",
},
],
};Restrictions
JSON schema does not support bigint type.
So if you use bigint type in one of your onetarget schemas, typia will make compile error like below.
TypeScript Source Code
import typia, { tags } from "typia";
interface Something {
bigint: bigint;
array: bigint[];
nested: Nested;
}
interface Nested {
uint64: bigint & tags.Type<"uint64">;
}
typia.json.schemas<[Something]>();Console Output
main.ts:12:1 - error TS(typia.json.schemas): unsupported type detected
- Something.bigint: bigint
- JSON does not support bigint type.
- Something.array: bigint
- JSON does not support bigint type.
- Nested.uint64: (bigint & Type<"uint64">)
- JSON does not support bigint type.Also, if you put any type of native classes like Map or Uint8Array, it would also be error, either. By the way, only Date class is exceptional, and it would be considered as string & Format<"date-time"> type like below.
TypeScript Source Code
import typia from "typia";
interface Native {
date: Date;
}
typia.json.schemas<[Native]>();Console Output
import typia from "typia";
({
version: "3.1",
components: {
schemas: {
Native: {
type: "object",
properties: {
date: {
type: "string",
format: "date-time",
},
},
required: ["date"],
},
},
},
schemas: [
{
$ref: "#/components/schemas/Native",
},
],
});