typia 9.3.1 → 9.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,26 +1,236 @@
1
1
  import { OpenApi, OpenApiV3 } from "@samchon/openapi";
2
2
  /**
3
- * Unit of JSON schema.
3
+ * Single unit of JSON schema representation.
4
4
  *
5
- * `IJsonSchemaUnit` is a type that represents a single JSON schema unit
6
- * containing the schema and components.
5
+ * `IJsonSchemaUnit` represents a self-contained JSON schema unit that encapsulates
6
+ * a single schema definition along with its associated reusable components. This is
7
+ * typically used when generating a JSON schema for a single TypeScript type, as
8
+ * opposed to a collection of multiple types.
7
9
  *
8
- * @template Version Version of the OpenAPI specification
9
- * @template Type Original TypeScript type used in the JSON schema
10
+ * Unlike {@link IJsonSchemaCollection} which handles multiple schemas, `IJsonSchemaUnit`
11
+ * focuses on representing a single schema with its dependencies. This makes it ideal
12
+ * for scenarios where you need to work with individual type definitions or when
13
+ * integrating with systems that expect single schema documents.
14
+ *
15
+ * The unit contains:
16
+ * - A single JSON schema definition for the specified TypeScript type
17
+ * - All necessary reusable components that the schema may reference
18
+ * - Version-specific formatting for either OpenAPI v3.0 or v3.1 compatibility
19
+ * - Optional type metadata for compile-time type safety
20
+ *
21
+ * Key differences from collection:
22
+ * - Contains only one schema instead of an array of schemas
23
+ * - More lightweight for single-type use cases
24
+ * - Simpler structure for direct schema consumption
25
+ * - Still maintains full component reference support
26
+ *
27
+ * @template Version The OpenAPI specification version to target ("3.0" or "3.1").
28
+ * Defaults to "3.1" for enhanced JSON Schema Draft 2020-12 compatibility.
29
+ * This determines the schema format, validation capabilities, and
30
+ * available features like tuple support and null type handling.
31
+ * @template Type The original TypeScript type that was analyzed to generate this
32
+ * JSON schema unit. This provides compile-time type safety and
33
+ * enables IDEs to provide better intellisense and validation.
34
+ *
35
+ * @example
36
+ * ```typescript
37
+ * interface User {
38
+ * id: string;
39
+ * name: string;
40
+ * email?: string;
41
+ * }
42
+ *
43
+ * // Generate a single schema unit for OpenAPI v3.1 (default)
44
+ * const userSchema = typia.json.schema<User>();
45
+ * // Type: IJsonSchemaUnit<"3.1", User>
46
+ *
47
+ * // Generate a single schema unit for OpenAPI v3.0 (Swagger compatibility)
48
+ * const swaggerUserSchema = typia.json.schema<User, "3.0">();
49
+ * // Type: IJsonSchemaUnit<"3.0", User>
50
+ * ```
51
+ *
52
+ * @see {@link IJsonSchemaCollection} For handling multiple schemas at once
10
53
  * @author Jeongho Nam - https://github.com/samchon
11
54
  */
12
55
  export type IJsonSchemaUnit<Version extends "3.0" | "3.1" = "3.1", Type = unknown> = Version extends "3.0" ? IJsonSchemaUnit.IV3_0<Type> : IJsonSchemaUnit.IV3_1<Type>;
13
56
  export declare namespace IJsonSchemaUnit {
57
+ /**
58
+ * JSON Schema unit formatted for OpenAPI v3.0 specification.
59
+ *
60
+ * This interface represents a single JSON schema unit that complies with
61
+ * OpenAPI v3.0 standards. It contains one schema definition along with
62
+ * any reusable components that the schema references, formatted according
63
+ * to OpenAPI v3.0 constraints and limitations.
64
+ *
65
+ * OpenAPI v3.0 characteristics affecting this unit:
66
+ * - Schema follows OpenAPI v3.0 JSON Schema subset
67
+ * - Limited support for advanced JSON Schema features
68
+ * - Uses nullable property for optional null values
69
+ * - Cannot natively express tuple types or pattern properties
70
+ * - Based on JSON Schema Draft 4 with OpenAPI-specific extensions
71
+ *
72
+ * Use cases for v3.0:
73
+ * - Integration with legacy Swagger tooling
74
+ * - Compatibility with older OpenAPI implementations
75
+ * - Systems that specifically require OpenAPI v3.0 format
76
+ * - Code generation tools that expect v3.0 schemas
77
+ *
78
+ * @template Type The original TypeScript type represented by this schema unit.
79
+ * Provides compile-time type information and enables type-safe
80
+ * operations on the schema.
81
+ */
14
82
  interface IV3_0<Type> {
83
+ /**
84
+ * OpenAPI specification version identifier.
85
+ *
86
+ * Always set to "3.0" to indicate this schema unit uses OpenAPI v3.0
87
+ * format and adheres to its specific constraints and limitations.
88
+ */
15
89
  version: "3.0";
90
+ /**
91
+ * The primary JSON schema definition.
92
+ *
93
+ * Contains the main JSON schema that represents the TypeScript type specified
94
+ * in the `Type` template parameter. This schema follows OpenAPI v3.0 format
95
+ * and may contain references to reusable components defined in the
96
+ * {@link components} property.
97
+ *
98
+ * The schema structure includes:
99
+ * - Type definitions following OpenAPI v3.0 constraints
100
+ * - Property definitions with v3.0-compatible validation rules
101
+ * - References to shared components using $ref syntax
102
+ * - Nullable properties for optional fields that can be null
103
+ *
104
+ * Example schema reference: `{ "$ref": "#/components/schemas/NestedType" }`
105
+ */
16
106
  schema: OpenApiV3.IJsonSchema;
107
+ /**
108
+ * Reusable schema components for OpenAPI v3.0.
109
+ *
110
+ * Contains all reusable schema definitions and components that may be
111
+ * referenced by the main schema. This enables schema modularity and
112
+ * prevents duplication when the same types are used in multiple places
113
+ * within the schema definition.
114
+ *
115
+ * Component categories include:
116
+ * - schemas: Named type definitions for complex objects, arrays, and unions
117
+ * - securitySchemes: Authentication and authorization definitions
118
+ * - parameters: Reusable parameter specifications
119
+ * - requestBodies: Reusable request body definitions
120
+ * - responses: Reusable response specifications
121
+ * - headers: Reusable header definitions
122
+ * - examples: Reusable example values
123
+ *
124
+ * All components follow OpenAPI v3.0 format restrictions and capabilities.
125
+ */
17
126
  components: OpenApiV3.IComponents;
127
+ /**
128
+ * Type metadata for compile-time type safety.
129
+ *
130
+ * This optional property maintains a reference to the original TypeScript
131
+ * type that was used to generate this schema unit. It provides compile-time
132
+ * type information without affecting the runtime JSON representation.
133
+ *
134
+ * Benefits of type metadata:
135
+ * - Enables type-safe schema validation and usage
136
+ * - Provides IDE intellisense and autocompletion
137
+ * - Allows compile-time checking of schema operations
138
+ * - Maintains traceability to original TypeScript definitions
139
+ *
140
+ * The property is intentionally marked as optional and undefined to ensure
141
+ * it doesn't appear in serialized JSON output while preserving type information
142
+ * at compile time.
143
+ */
18
144
  __type?: Type | undefined;
19
145
  }
146
+ /**
147
+ * JSON Schema unit formatted for OpenAPI v3.1 specification.
148
+ *
149
+ * This interface represents a single JSON schema unit that takes advantage
150
+ * of OpenAPI v3.1's enhanced capabilities and improved JSON Schema compatibility.
151
+ * It provides a more feature-rich and accurate representation of TypeScript
152
+ * types compared to the v3.0 format.
153
+ *
154
+ * OpenAPI v3.1 advantages for this unit:
155
+ * - Full JSON Schema Draft 2020-12 compatibility
156
+ * - Native tuple type support using prefixItems
157
+ * - Proper null type handling via union types
158
+ * - Pattern properties for dynamic object keys
159
+ * - Enhanced const, enum, and validation capabilities
160
+ * - Better support for complex nested structures
161
+ *
162
+ * Use cases for v3.1:
163
+ * - Modern OpenAPI implementations and tooling
164
+ * - Systems requiring accurate TypeScript type representation
165
+ * - Applications needing advanced JSON Schema features
166
+ * - New projects without legacy compatibility requirements
167
+ *
168
+ * @template Type The original TypeScript type represented by this schema unit.
169
+ * Enables compile-time type safety and provides enhanced
170
+ * development experience with better IDE support.
171
+ */
20
172
  interface IV3_1<Type> {
173
+ /**
174
+ * OpenAPI specification version identifier.
175
+ *
176
+ * Always set to "3.1" to indicate this schema unit uses OpenAPI v3.1
177
+ * format with enhanced JSON Schema compatibility and modern features.
178
+ */
21
179
  version: "3.1";
180
+ /**
181
+ * The primary JSON schema definition with v3.1 enhancements.
182
+ *
183
+ * Contains the main JSON schema that accurately represents the TypeScript
184
+ * type using OpenAPI v3.1's enhanced capabilities. This schema can express
185
+ * complex TypeScript constructs that were not possible or accurate in v3.0.
186
+ *
187
+ * Enhanced schema features include:
188
+ * - Tuple types using prefixItems for exact array structure
189
+ * - Union types with proper null handling via oneOf
190
+ * - Const values for literal types
191
+ * - Pattern properties for Record<string, T> types
192
+ * - Advanced validation constraints and metadata
193
+ * - Recursive type definitions with proper $ref handling
194
+ *
195
+ * The schema follows the emended OpenAPI v3.1 format used by typia,
196
+ * which removes ambiguous expressions while maintaining full compatibility.
197
+ */
22
198
  schema: OpenApi.IJsonSchema;
199
+ /**
200
+ * Reusable schema components for OpenAPI v3.1.
201
+ *
202
+ * Contains reusable schema definitions and components that leverage
203
+ * OpenAPI v3.1's enhanced capabilities. These components provide better
204
+ * type representation and more accurate schema definitions compared to v3.0.
205
+ *
206
+ * Enhanced component features:
207
+ * - schemas: More accurate type definitions with v3.1 JSON Schema features
208
+ * - securitySchemes: Enhanced authentication scheme definitions
209
+ * - Better support for complex nested references
210
+ * - Improved handling of recursive and circular type dependencies
211
+ *
212
+ * The components structure follows the emended OpenAPI v3.1 specification
213
+ * that simplifies certain patterns while maintaining full expressiveness.
214
+ */
23
215
  components: OpenApi.IComponents;
216
+ /**
217
+ * Type metadata for enhanced compile-time type safety.
218
+ *
219
+ * This optional property preserves the original TypeScript type information
220
+ * for compile-time type checking and enhanced development experience. It
221
+ * enables type-safe operations and better IDE support without affecting
222
+ * the runtime JSON schema representation.
223
+ *
224
+ * Enhanced type safety features:
225
+ * - Strong typing connection to original TypeScript definitions
226
+ * - Better IDE intellisense and error detection
227
+ * - Compile-time validation of schema usage patterns
228
+ * - Type-safe integration with validation and serialization libraries
229
+ * - Enhanced debugging and development experience
230
+ *
231
+ * The property remains optional and undefined to maintain clean JSON
232
+ * serialization while preserving valuable compile-time information.
233
+ */
24
234
  __type?: Type | undefined;
25
235
  }
26
236
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typia",
3
- "version": "9.3.1",
3
+ "version": "9.4.0",
4
4
  "description": "Superfast runtime validators with only one line",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
@@ -41,7 +41,7 @@
41
41
  },
42
42
  "homepage": "https://typia.io",
43
43
  "dependencies": {
44
- "@samchon/openapi": "^4.3.1",
44
+ "@samchon/openapi": "^4.4.1",
45
45
  "@standard-schema/spec": "^1.0.0",
46
46
  "commander": "^10.0.0",
47
47
  "comment-json": "^4.2.3",
@@ -50,7 +50,7 @@
50
50
  "randexp": "^0.5.3"
51
51
  },
52
52
  "peerDependencies": {
53
- "@samchon/openapi": ">=4.3.1 <5.0.0",
53
+ "@samchon/openapi": ">=4.4.1 <5.0.0",
54
54
  "typescript": ">=4.8.0 <5.9.0"
55
55
  },
56
56
  "devDependencies": {
@@ -1 +1,39 @@
1
+ /**
2
+ * Type definition for assertion guard functions in `typia`.
3
+ *
4
+ * An assertion guard is a function that asserts an input value's type at runtime
5
+ * and performs a TypeScript type assertion if validation passes. Unlike regular
6
+ * assertion functions that return the validated value, assertion guards return
7
+ * nothing but automatically cast the input parameter to the expected type `T`.
8
+ *
9
+ * This type is used by `typia.createAssertGuard<T>()` and `typia.createAssertGuardEquals<T>()`
10
+ * to generate reusable assertion guard functions.
11
+ *
12
+ * @template T - The expected type to validate and assert against
13
+ * @param input - The value to validate (type unknown)
14
+ * @throws {TypeGuardError} When the input value doesn't match the expected type T
15
+ * @returns void - Returns nothing, but asserts that input is type T
16
+ *
17
+ * @example
18
+ * ```typescript
19
+ * interface IMember {
20
+ * name: string;
21
+ * age: number;
22
+ * }
23
+ *
24
+ * // Create reusable assertion guard
25
+ * const assertMember: AssertionGuard<IMember> = typia.createAssertGuard<IMember>();
26
+ *
27
+ * // Usage - input will be automatically cast to IMember if validation passes
28
+ * const unknownData: unknown = { name: "John", age: 25 };
29
+ *
30
+ * assertMember(unknownData);
31
+ * // After this line, unknownData is automatically treated as IMember type
32
+ * console.log(unknownData.name); // TypeScript knows this is safe
33
+ * ```
34
+ *
35
+ * @see {@link https://github.com/samchon/typia#assertguard-functions} Typia assertion guards documentation
36
+ * @see {@link TypeGuardError} Error thrown when assertion fails
37
+ * @author Jeongho Nam - https://github.com/samchon
38
+ */
1
39
  export type AssertionGuard<T> = (input: unknown) => asserts input is T;
@@ -1,12 +1,111 @@
1
+ /**
2
+ * Custom error class thrown when runtime assertion fails in `typia.assert<T>()` function.
3
+ *
4
+ * This error is thrown by the `typia.assert<T>()` function when the input value
5
+ * doesn't match the expected type.
6
+ *
7
+ * The error provides detailed information about the first assertion failure encountered,
8
+ * including the access path where the error occurred, the expected type, and the actual value.
9
+ *
10
+ * @template T - The expected type (generic for type safety)
11
+ * @author Jeongho Nam - https://github.com/samchon
12
+ * @example
13
+ * ```typescript
14
+ * interface IMember {
15
+ * name: string;
16
+ * age: number & ExclusiveMinimum<19>;
17
+ * }
18
+ *
19
+ * try {
20
+ * typia.assert<IMember>({ name: "John", age: 18 });
21
+ * } catch (error) {
22
+ * if (error instanceof TypeGuardError) {
23
+ * console.log(error.method); // "typia.assert"
24
+ * console.log(error.path); // "input.age"
25
+ * console.log(error.expected); // "number & ExclusiveMinimum<19>"
26
+ * console.log(error.value); // 18
27
+ * }
28
+ * }
29
+ * ```
30
+ */
1
31
  export class TypeGuardError<T = any> extends Error {
32
+ /**
33
+ * The name of the typia method that threw this error.
34
+ *
35
+ * @example "typia.assert"
36
+ */
2
37
  public readonly method: string;
38
+
39
+ /**
40
+ * The access path to the property where the assertion error occurred.
41
+ *
42
+ * Uses dot notation to indicate the path for nested object properties.
43
+ * May be `undefined` if the error occurred at the root level.
44
+ *
45
+ * @example
46
+ * - `"input.age"` - Error in the age property of the object
47
+ * - `"input.profile.email"` - Error in the email property of a nested object
48
+ * - `"input[0].name"` - Error in the name property of the first array element
49
+ * - `undefined` - Error occurred at the root level
50
+ */
3
51
  public readonly path: string | undefined;
52
+
53
+ /**
54
+ * String representation of the expected type at the error location.
55
+ *
56
+ * Represents TypeScript types as strings, including detailed type information
57
+ * for complex types.
58
+ *
59
+ * @example
60
+ * - `"string"` - Expected string type
61
+ * - `"number & ExclusiveMinimum<19>"` - Expected number greater than 19
62
+ * - `"undefined"` - Expected undefined (when superfluous property found in assertion)
63
+ * - `"{ name: string; age: number }"` - Expected object type
64
+ */
4
65
  public readonly expected: string;
5
- public readonly value: any;
66
+
67
+ /**
68
+ * The actual value that failed assertion.
69
+ *
70
+ * Stores the actual value at the error path as-is.
71
+ * Useful for debugging by comparing the expected type with the actual value.
72
+ *
73
+ * @example
74
+ * - `18` - Numeric value
75
+ * - `"invalid"` - String value
76
+ * - `{ name: "John", age: 18, sex: 1 }` - Object value
77
+ */
78
+ public readonly value: unknown;
79
+
80
+ /**
81
+ * Phantom property for type safety purposes.
82
+ *
83
+ * This property is not actually used and exists only to maintain
84
+ * the generic type T in TypeScript's type system.
85
+ * Always has an `undefined` value at runtime.
86
+ *
87
+ * @internal
88
+ */
6
89
  protected readonly fake_expected_typed_value_?: T | undefined;
7
90
 
91
+ /**
92
+ * Creates a new TypeGuardError instance.
93
+ *
94
+ * @param props - Object containing the properties needed to create the error
95
+ *
96
+ * @example
97
+ * ```typescript
98
+ * const error = new TypeGuardError({
99
+ * method: "typia.assert",
100
+ * path: "input.age",
101
+ * expected: "number & ExclusiveMinimum<19>",
102
+ * value: 18
103
+ * });
104
+ * ```
105
+ */
8
106
  public constructor(props: TypeGuardError.IProps) {
9
107
  // MESSAGE CONSTRUCTION
108
+ // Use custom message if provided, otherwise generate default format
10
109
  super(
11
110
  props.message ||
12
111
  `Error on ${props.method}(): invalid type${
@@ -15,6 +114,7 @@ export class TypeGuardError<T = any> extends Error {
15
114
  );
16
115
 
17
116
  // INHERITANCE POLYFILL
117
+ // Set up prototype for compatibility across different JavaScript environments
18
118
  const proto = new.target.prototype;
19
119
  if (Object.setPrototypeOf) Object.setPrototypeOf(this, proto);
20
120
  else (this as any).__proto__ = proto;
@@ -26,12 +126,54 @@ export class TypeGuardError<T = any> extends Error {
26
126
  this.value = props.value;
27
127
  }
28
128
  }
129
+
29
130
  export namespace TypeGuardError {
131
+ /**
132
+ * Interface for properties passed to the TypeGuardError constructor.
133
+ *
134
+ * @example
135
+ * ```typescript
136
+ * const props: TypeGuardError.IProps = {
137
+ * method: "typia.assertEquals",
138
+ * path: "input.sex",
139
+ * expected: "undefined",
140
+ * value: 1,
141
+ * message: "Custom error message" // optional
142
+ * };
143
+ * ```
144
+ */
30
145
  export interface IProps {
146
+ /**
147
+ * The name of the typia method that threw the error.
148
+ *
149
+ * @example "typia.assert", "typia.assertEquals"
150
+ */
31
151
  method: string;
152
+
153
+ /**
154
+ * The access path to the property where the assertion error occurred (optional).
155
+ *
156
+ * @example "input.age", "input.profile.email"
157
+ */
32
158
  path?: undefined | string;
159
+
160
+ /**
161
+ * String representation of the expected type at the error location.
162
+ *
163
+ * @example "string", "number & ExclusiveMinimum<19>"
164
+ */
33
165
  expected: string;
34
- value: any;
166
+
167
+ /**
168
+ * The actual value that failed assertion.
169
+ */
170
+ value: unknown;
171
+
172
+ /**
173
+ * Custom error message (optional).
174
+ *
175
+ * If not provided, a default format message will be automatically generated.
176
+ */
35
177
  message?: undefined | string;
36
178
  }
37
179
  }
@@ -13,7 +13,7 @@ const USAGE = `Wrong command has been detected. Use like below:
13
13
  --input {directory} \\
14
14
  --output {directory}
15
15
 
16
- --npx typia generate --input src/templates --output src/functinoal
16
+ --npx typia generate --input src/templates --output src/functional
17
17
  `;
18
18
 
19
19
  const halt = (desc: string): never => {
@@ -48,9 +48,9 @@ export namespace JsonSchemasProgrammer {
48
48
  : (writeV3_1(props.metadatas) as IJsonSchemaCollection<Version>);
49
49
 
50
50
  const writeV3_0 = (
51
- medadataList: Array<Metadata>,
51
+ metadataList: Array<Metadata>,
52
52
  ): IJsonSchemaCollection<"3.0"> => {
53
- const collection: IJsonSchemaCollection<"3.1"> = writeV3_1(medadataList);
53
+ const collection: IJsonSchemaCollection<"3.1"> = writeV3_1(metadataList);
54
54
  const asset: OpenApiV3Downgrader.IComponentsCollection =
55
55
  OpenApiV3Downgrader.downgradeComponents(collection.components);
56
56
  const caster = OpenApiV3Downgrader.downgradeSchema(asset);