@autobe/agent 0.30.3 → 0.30.4-dev.20260324

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,763 +1,763 @@
1
- import { AutoBeOpenApi } from "@autobe/interface";
2
- import { AutoBeOpenApiTypeChecker, StringUtil } from "@autobe/utils";
3
- import { NamingConvention } from "@typia/utils";
4
- import { IValidation } from "typia";
5
-
6
- import { AutoBeJsonSchemaFactory } from "./AutoBeJsonSchemaFactory";
7
-
8
- export namespace AutoBeJsonSchemaValidator {
9
- export const isObjectType = (props: {
10
- operations: AutoBeOpenApi.IOperation[];
11
- typeName: string;
12
- }): boolean =>
13
- props.typeName.endsWith(".IAuthorized") ||
14
- props.typeName.endsWith(".IRequest") ||
15
- props.typeName.endsWith(".ISummary") ||
16
- props.typeName.endsWith(".IInvert") ||
17
- props.typeName.endsWith(".ICreate") ||
18
- props.typeName.endsWith(".IUpdate") ||
19
- props.typeName.endsWith(".IJoin") ||
20
- props.typeName.endsWith(".ILogin") ||
21
- props.operations.some(
22
- (op) =>
23
- op.requestBody?.typeName === props.typeName ||
24
- op.responseBody?.typeName === props.typeName,
25
- );
26
-
27
- export const isPage = (key: string): boolean =>
28
- key.startsWith("IPage") === true &&
29
- key.startsWith("IPage.") === false &&
30
- key !== "IPage";
31
-
32
- export const isPreset = (typeName: string): boolean =>
33
- AutoBeJsonSchemaFactory.DEFAULT_SCHEMAS[typeName] !== undefined ||
34
- AutoBeJsonSchemaValidator.isPage(typeName) === true;
35
-
36
- export interface IProps {
37
- errors: IValidation.IError[];
38
- operations: AutoBeOpenApi.IOperation[];
39
- typeName: string;
40
- schema: AutoBeOpenApi.IJsonSchema;
41
- path: string;
42
- }
43
-
44
- export const validateSchema = (props: IProps): void => {
45
- const vo = validateObjectType({
46
- errors: props.errors,
47
- operations: props.operations,
48
- path: props.path,
49
- });
50
- validateAuthorization(props);
51
- validateRecursive(props);
52
- validateReferenceId(props);
53
- validatePropertyNames(props);
54
- validateNumericRanges(props);
55
- validateEmptyProperties(props);
56
-
57
- vo(props.typeName, props.schema);
58
- AutoBeOpenApiTypeChecker.skim({
59
- schema: props.schema,
60
- closure: (next, accessor) => {
61
- if (AutoBeOpenApiTypeChecker.isReference(next) === false) return;
62
- const key: string = next.$ref.split("/").pop()!;
63
- validateKey({
64
- errors: props.errors,
65
- path: `${accessor}.$ref`,
66
- key,
67
- transform: (typeName) => `#/components/schemas/${typeName}`,
68
- });
69
- },
70
- accessor: props.path,
71
- });
72
- };
73
-
74
- export const validateKey = (props: {
75
- errors: IValidation.IError[];
76
- key: string;
77
- path: string;
78
- transform?: (typeName: string) => string;
79
- }): void => {
80
- const transform = props.transform ?? ((typeName: string) => typeName);
81
- const elements: string[] = props.key.split(".");
82
- if (elements.length > 2)
83
- props.errors.push({
84
- path: props.path,
85
- expected: "At most one dot(.) character allowed in type name",
86
- value: transform(props.key),
87
- description: StringUtil.trim`
88
- JSON schema type name allows at most one dot(.) character to separate
89
- module name and interface name.
90
-
91
- However, current key name ${transform(JSON.stringify(props.key))}
92
- contains multiple dot(.) characters (${elements.length - 1} times).
93
-
94
- Change it to a valid type name with at most one dot(.) character at the next time.
95
- Note that, this is not a recommendation, but an instruction you must follow.
96
- `,
97
- });
98
- if (elements.every(NamingConvention.variable) === false)
99
- props.errors.push({
100
- path: props.path,
101
- expected: StringUtil.trim`
102
- Valid variable name
103
-
104
- ${elements.map((s) => `- ${s}: ${NamingConvention.variable(s) ? "valid" : "invalid"}`).join("\n")}
105
- `,
106
- value: transform(props.key),
107
- description: StringUtil.trim`
108
- JSON schema type name must be a valid variable name.
109
-
110
- Even though JSON schema type name allows dot(.) character, but
111
- each segment separated by dot(.) must be a valid variable name.
112
-
113
- Current key name ${transform(JSON.stringify(props.key))} is not valid.
114
- Change it to a valid variable name at the next time.
115
- Note that, this is not a recommendation, but an instruction you must follow.
116
- `,
117
- });
118
- if (props.key.endsWith(".IPage")) {
119
- const expected: string = `IPage${props.key.substring(0, props.key.length - 6)}`;
120
- props.errors.push({
121
- path: props.path,
122
- expected: `"IPage" must be followed by another interface name. Use ${transform(JSON.stringify(expected))} instead.`,
123
- value: transform(props.key),
124
- description: StringUtil.trim`
125
- "IPage" is a reserved type name for pagination response.
126
- The pagination data type name must be post-fixed after "IPage".
127
-
128
- However, you've defined ${transform(JSON.stringify(props.key))},
129
- post-fixing ".IPage" after the pagination data type name.
130
-
131
- Change it to a valid pagination type name to be
132
- ${transform(JSON.stringify(expected))} at the next time.
133
- Note that, this is not a recommendation, but an instruction you must follow.
134
- `,
135
- });
136
- }
137
- if (props.key === "IPageIRequest")
138
- props.errors.push({
139
- path: props.path,
140
- expected: `"IPageIRequest" is a mistake. Use "IPage.IRequest" instead.`,
141
- value: transform(props.key),
142
- description: StringUtil.trim`
143
- You've taken a mistake that defines "${transform("IPageIRequest")}" as a type name.
144
- However, as you've intended to define a pagination request type,
145
- the correct type name is "${transform("IPage.IRequest")}" instead of "${transform("IPageIRequest")}".
146
-
147
- Change it to "${transform("IPage.IRequest")}" at the next time.
148
- Note that, this is not a recommendation, but an instruction you must follow.
149
- `,
150
- });
151
- if (
152
- props.key.startsWith("IPage") &&
153
- props.key.startsWith("IPageI") === false &&
154
- props.key !== "IPage.IPagination" &&
155
- props.key !== "IPage.IRequest"
156
- ) {
157
- const expected: string = `IPage${props.key
158
- .substring(5)
159
- .split(".")
160
- .map((s) => (s.startsWith("I") ? s : `I${s}`))
161
- .join(".")}`;
162
- props.errors.push({
163
- path: props.path,
164
- expected: `Interface name starting with 'I' even after 'IPage': ${JSON.stringify(expected)}`,
165
- value: transform(props.key),
166
- description: StringUtil.trim`
167
- JSON schema type name must be an interface name starting with 'I'.
168
- Even though JSON schema type name allows dot(.) character, but
169
- each segment separated by dot(.) must be an interface name starting
170
- with 'I'.
171
-
172
- Even in the case of pagination response, after 'IPage' prefix,
173
- the remaining part must be an interface name starting with 'I'.
174
-
175
- Current key name ${JSON.stringify(props.key)} is not valid. Change
176
- it to a valid interface name to be ${JSON.stringify(expected)},
177
- or change it to another valid interface name at the next time.
178
- Note that, this is not a recommendation, but an instruction you must follow.
179
- `,
180
- });
181
- }
182
- if (elements.some((s) => s.startsWith("I") === false) === true) {
183
- const expected: string = elements
184
- .map((s) => (s.startsWith("I") ? s : `I${s}`))
185
- .join(".");
186
- props.errors.push({
187
- path: props.path,
188
- expected: `Interface name starting with 'I': ${JSON.stringify(expected)}`,
189
- value: transform(props.key),
190
- description: StringUtil.trim`
191
- JSON schema type name must be an interface name starting with 'I'.
192
- Even though JSON schema type name allows dot(.) character, but
193
- each segment separated by dot(.) must be an interface name starting
194
- with 'I'.
195
-
196
- Current key name ${transform(JSON.stringify(props.key))} is not valid.
197
- Change it to a valid interface name to be ${transform(JSON.stringify(expected))},
198
- or change it to another valid interface name at the next time.
199
-
200
- Note that, this is not a recommendation, but an instruction you must follow.
201
- `,
202
- });
203
- }
204
- if (
205
- elements.length === 2 &&
206
- (elements[1] === "IJoin" ||
207
- elements[1] === "ILogin" ||
208
- elements[1] === "IAuthorized" ||
209
- elements[1] === "IRefresh") &&
210
- elements[0].endsWith("Session") === true
211
- )
212
- props.errors.push({
213
- path: props.path,
214
- expected: JSON.stringify(
215
- `${elements[0].replace("Session", "")}.${elements[1]}`,
216
- ),
217
- value: transform(props.key),
218
- description: StringUtil.trim`
219
- You have attached ${elements[1]} to a Session type ${transform(JSON.stringify(props.key))},
220
- but this is architecturally incorrect.
221
-
222
- In production authentication systems, Actor and Session are separate concepts:
223
- - **Actor** (e.g., User, Seller, Admin): The persistent user identity that performs
224
- authentication actions - joining (registering), logging in, and receiving authorized tokens.
225
- - **Session** (e.g., UserSession, SellerSession): The temporary authentication state that
226
- tracks active login instances. Sessions are CREATED as a result of join/login operations,
227
- but they do not perform these actions themselves.
228
-
229
- Think about it semantically: An ACTOR joins the system and logs in. A SESSION is merely
230
- a record that gets created when the actor authenticates. It makes no sense for a session
231
- to "join" or "login" - only actors do that.
232
-
233
- Therefore, authentication-related DTO types (IJoin, ILogin, IAuthorized, IRefresh) MUST
234
- be attached to the Actor type, NEVER to the Session type.
235
-
236
- Change ${transform(JSON.stringify(props.key))} to ${transform(JSON.stringify(`${elements[0].replace("Session", "")}.${elements[1]}`))} at the next time.
237
-
238
- Note that, this is not a recommendation, but an instruction you must follow.
239
- `,
240
- });
241
- };
242
-
243
- const validateAuthorization = (props: IProps): void => {
244
- if (props.typeName.endsWith(".IAuthorized") === true) {
245
- if (AutoBeOpenApiTypeChecker.isObject(props.schema) === false) {
246
- props.errors.push({
247
- path: props.path,
248
- expected: `AutoBeOpenApi.IJsonSchemaDescriptive<AutoBeOpenApi.IJsonSchema.IObject>`,
249
- value: props.schema,
250
- description: `${props.typeName} must be an object type for authorization responses. Note that, this is not a recommendation, but an instruction you must follow.`,
251
- });
252
- } else {
253
- // Check if token property exists
254
- props.schema.properties ??= {};
255
- props.schema.properties["token"] = {
256
- "x-autobe-specification":
257
- "JWT token information for authentication. Server generates this token upon successful login/join.",
258
- description: "JWT token information for authentication.",
259
- $ref: "#/components/schemas/IAuthorizationToken",
260
- } as AutoBeOpenApi.IJsonSchemaProperty.IReference;
261
-
262
- props.schema.required ??= [];
263
- if (props.schema.required.includes("token") === false)
264
- props.schema.required.push("token");
265
- }
266
- }
267
-
268
- AutoBeOpenApiTypeChecker.skim({
269
- schema: props.schema,
270
- accessor: props.path,
271
- closure: (next, accessor) => {
272
- if (AutoBeOpenApiTypeChecker.isReference(next) === false) return;
273
- const key: string = next.$ref.split("/").pop()!;
274
- if (
275
- key.endsWith(".IAuthorized") === false &&
276
- key.endsWith(".ILogin") === false &&
277
- key.endsWith(".IJoin") === false
278
- )
279
- return;
280
- const candidates: Set<string> = new Set(
281
- props.operations
282
- .map((op) => [
283
- op.requestBody?.typeName.endsWith(key.split(".").pop()!)
284
- ? op.requestBody!.typeName
285
- : null,
286
- op.responseBody?.typeName.endsWith(key.split(".").pop()!)
287
- ? op.responseBody!.typeName
288
- : null,
289
- ])
290
- .flat()
291
- .filter((v) => v !== null),
292
- );
293
- if (candidates.has(key) === false)
294
- props.errors.push({
295
- path: `${accessor}.$ref`,
296
- expected: Array.from(candidates)
297
- .map((s) => JSON.stringify(`#/components/schemas/${s}`))
298
- .join(" | "),
299
- value: key,
300
- description: StringUtil.trim`
301
- You've referenced an authorization-related type ${JSON.stringify(key)}
302
- that is not used in any operation's requestBody or responseBody.
303
-
304
- Authorization-related types must be used in at least one operation's
305
- requestBody or responseBody. Make sure to use the type appropriately
306
- in your API design.
307
-
308
- Existing authorization-related types used in operations are:
309
- - ${Array.from(candidates)
310
- .map((s) => `#/components/schemas/${s}`)
311
- .join("\n- ")}
312
-
313
- Note that, this is not a recommendation, but an instruction you must follow.
314
- `,
315
- });
316
- },
317
- });
318
- };
319
-
320
- const validateRecursive = (props: IProps): void => {
321
- const report = (description: string) =>
322
- props.errors.push({
323
- path: props.path,
324
- expected: "Non-infinite recursive schema definition",
325
- value: props.schema,
326
- description,
327
- });
328
- if (
329
- AutoBeOpenApiTypeChecker.isReference(props.schema) &&
330
- props.schema.$ref === `#/components/schemas/${props.typeName}`
331
- )
332
- report(StringUtil.trim`
333
- You have defined a nonsensible type like below:
334
-
335
- \`\`\`typescript
336
- type ${props.typeName} = ${props.typeName};
337
- \`\`\`
338
-
339
- This is an infinite recursive type definition that cannot exist in any
340
- programming language. A type cannot be defined as itself - this creates
341
- a circular definition with no base case, making the type impossible to
342
- instantiate or validate.
343
-
344
- If you need tree or graph structures, use explicit relationships with
345
- ID references (e.g., parentId: string) instead of recursive type definitions.
346
- Remove the self-reference and redesign the schema at the next time.
347
- Note that, this is not a recommendation, but an instruction you must follow.
348
- `);
349
- else if (
350
- AutoBeOpenApiTypeChecker.isArray(props.schema) &&
351
- AutoBeOpenApiTypeChecker.isReference(props.schema.items) &&
352
- props.schema.items.$ref === `#/components/schemas/${props.typeName}`
353
- )
354
- report(StringUtil.trim`
355
- You have defined a nonsensible type like below:
356
-
357
- \`\`\`typescript
358
- type ${props.typeName} = Array<${props.typeName}>;
359
- \`\`\`
360
-
361
- This is an infinite recursive array type that cannot exist in any
362
- programming language. An array of itself creates a circular definition
363
- with no base case, making the type impossible to instantiate or validate.
364
-
365
- If you need nested structures, define explicit depth levels with separate
366
- types, or use parent-child relationships with ID references.
367
- Remove the self-reference and redesign the schema at the next time.
368
- Note that, this is not a recommendation, but an instruction you must follow.
369
- `);
370
- else if (
371
- AutoBeOpenApiTypeChecker.isOneOf(props.schema) &&
372
- props.schema.oneOf.some(
373
- (v) =>
374
- AutoBeOpenApiTypeChecker.isReference(v) &&
375
- v.$ref === `#/components/schemas/${props.typeName}`,
376
- ) === true
377
- )
378
- report(StringUtil.trim`
379
- You have defined a nonsensible type like below:
380
-
381
- \`\`\`typescript
382
- type ${props.typeName} = ${props.typeName} | ...;
383
- \`\`\`
384
-
385
- This is an infinite recursive union type that cannot exist in any
386
- programming language. A union that includes itself as a variant creates
387
- a circular definition with no base case, making the type impossible to
388
- instantiate or validate.
389
-
390
- If you need polymorphic hierarchies, define separate concrete types for
391
- each variant without including the union type itself as a variant.
392
- Remove the self-reference and redesign the schema at the next time.
393
- Note that, this is not a recommendation, but an instruction you must follow.
394
- `);
395
- else if (
396
- AutoBeOpenApiTypeChecker.isObject(props.schema) &&
397
- props.schema.properties &&
398
- props.schema.required &&
399
- Object.entries(props.schema.properties).some(
400
- ([k, v]) =>
401
- AutoBeOpenApiTypeChecker.isReference(v) &&
402
- v.$ref === `#/components/schemas/${props.typeName}` &&
403
- (props.schema as AutoBeOpenApi.IJsonSchema.IObject).required.includes(
404
- k,
405
- ),
406
- )
407
- )
408
- report(StringUtil.trim`
409
- You have defined a nonsensible type like below:
410
-
411
- \`\`\`typescript
412
- interface ${props.typeName} {
413
- someProperty: ${props.typeName}; // required, non-nullable
414
- }
415
- \`\`\`
416
-
417
- This is an infinite recursive object type that cannot exist in any
418
- programming language. A required non-nullable property referencing its
419
- own type creates a circular definition with no base case, making the
420
- type impossible to instantiate.
421
-
422
- To create an instance of ${props.typeName}, you would need an instance of ${props.typeName},
423
- which requires another instance of ${props.typeName}, infinitely. This is logically
424
- impossible.
425
-
426
- If you need parent-child or graph relationships, make the self-referencing
427
- property either nullable or optional, or use ID references (e.g., parentId: string).
428
- Remove the required self-reference and redesign the schema at the next time.
429
- Note that, this is not a recommendation, but an instruction you must follow.
430
- `);
431
- };
432
-
433
- const validateObjectType = (props: {
434
- errors: IValidation.IError[];
435
- operations: AutoBeOpenApi.IOperation[];
436
- path: string;
437
- }) => {
438
- const root: Set<string> = new Set();
439
- for (const o of props.operations) {
440
- if (o.requestBody) root.add(o.requestBody.typeName);
441
- if (o.responseBody) root.add(o.responseBody.typeName);
442
- }
443
- return (key: string, schema: AutoBeOpenApi.IJsonSchema): void => {
444
- if (AutoBeOpenApiTypeChecker.isObject(schema) === true) return;
445
- if (root.has(key))
446
- props.errors.push({
447
- path: props.path,
448
- expected: `AutoBeOpenApi.IJsonSchemaDescriptive.IObject`,
449
- value: schema,
450
- description: StringUtil.trim`
451
- Root schema types (used in requestBody or responseBody of operations)
452
- must be defined as object types.
453
-
454
- This is the rule enforced to ensure consistent API design and to facilitate easier data handling.
455
- Even though you think that defining a non-object type is more convenient for your specific use case,
456
- just follow the rule without any resistance.
457
-
458
- Note that, this is not a recommendation, but an instruction you must follow.
459
-
460
- If current type is hard to be defined as an object type, just wrap it in an object type like below:
461
-
462
- \`\`\`typescript
463
- {
464
- value: T;
465
- }
466
- \`\`\`
467
- `,
468
- });
469
- else if (
470
- key.endsWith(".IRequest") ||
471
- key.endsWith(".ISummary") ||
472
- key.endsWith(".IInvert") ||
473
- key.endsWith(".ICreate") ||
474
- key.endsWith(".IUpdate") ||
475
- key.endsWith(".IJoin") ||
476
- key.endsWith(".ILogin") ||
477
- key.endsWith(".IAuthorized")
478
- )
479
- props.errors.push({
480
- path: props.path,
481
- expected: `AutoBeOpenApi.IJsonSchemaDescriptive.IObject`,
482
- value: schema,
483
- description: StringUtil.trim`
484
- DTO type of .${key.split(".").pop()} suffix must be defined as an object type.
485
-
486
- This is the rule enforced to ensure consistent API design and to facilitate easier data handling.
487
- Even though you think that defining a non-object type is more convenient for your specific use case,
488
- just follow the rule without any resistance.
489
-
490
- Note that, this is not a recommendation, but an instruction you must follow.
491
-
492
- If current type is hard to be defined as an object type, just wrap it in an object type like below:
493
-
494
- \`\`\`typescript
495
- {
496
- value: T;
497
- }
498
- \`\`\`
499
- `,
500
- });
501
- };
502
- };
503
-
504
- const validateReferenceId = (props: {
505
- errors: IValidation.IError[];
506
- schema: AutoBeOpenApi.IJsonSchema;
507
- path: string;
508
- }): void => {
509
- if (AutoBeOpenApiTypeChecker.isObject(props.schema) === false) return;
510
- for (const [key, value] of Object.entries(props.schema.properties)) {
511
- if (key !== "id" && key.endsWith("_id") === false) continue;
512
-
513
- const accessor: string = `${props.path}.properties${
514
- NamingConvention.variable(key) ? `.${key}` : `[${JSON.stringify(key)}]`
515
- }`;
516
- const inspect = (schema: AutoBeOpenApi.IJsonSchema): boolean =>
517
- AutoBeOpenApiTypeChecker.isString(schema) ||
518
- AutoBeOpenApiTypeChecker.isNull(schema) ||
519
- (AutoBeOpenApiTypeChecker.isOneOf(schema) &&
520
- schema.oneOf.every((v) => inspect(v)));
521
- if (inspect(value) === false)
522
- props.errors.push({
523
- path: accessor,
524
- expected: StringUtil.trim`
525
- | { type: "string"; format: "uuid"; description: string; }
526
- | {
527
- oneOf: [
528
- { type: "string"; format: "uuid"; },
529
- { type: "null"; },
530
- ];
531
- description: string;
532
- }`,
533
- value,
534
- description: StringUtil.trim`
535
- Property names "id" or ending with "_id" must be defined as
536
- UUID string type, or nullable UUID string type.
537
-
538
- This is the rule enforced to ensure consistent identification of
539
- resources across the API. Even though you think that defining a
540
- different type is more convenient for your specific use case,
541
- just follow the rule without any resistance.
542
-
543
- Note that, this is not a recommendation, but an instruction you
544
- must follow.
545
- `,
546
- });
547
- }
548
- };
549
-
550
- const validatePropertyNames = (props: {
551
- errors: IValidation.IError[];
552
- schema: AutoBeOpenApi.IJsonSchema;
553
- path: string;
554
- }): void => {
555
- if (AutoBeOpenApiTypeChecker.isObject(props.schema) === false) return;
556
- for (const key of Object.keys(props.schema.properties)) {
557
- if (NamingConvention.reserved(key))
558
- props.errors.push({
559
- path: `${props.path}.properties${NamingConvention.variable(key) ? `.${key}` : `[${JSON.stringify(key)}]`}`,
560
- expected: `none system reserved word`,
561
- value: key,
562
- description: StringUtil.trim`
563
- Property name ${JSON.stringify(key)} is a system reserved word.
564
-
565
- Avoid using system reserved words as property names to prevent
566
- potential conflicts and ensure clarity in your API design.
567
-
568
- Change the property name ${JSON.stringify(key)} to a non-reserved
569
- word at the next time.
570
-
571
- Note that, this is not a recommendation, but an instruction you
572
- must follow.
573
- `,
574
- });
575
- else if (NamingConvention.variable(key) === false)
576
- props.errors.push({
577
- path: `${props.path}.properties${NamingConvention.variable(key) ? `.${key}` : `[${JSON.stringify(key)}]`}`,
578
- expected: `valid variable name`,
579
- value: key,
580
- description: StringUtil.trim`
581
- Property name ${JSON.stringify(key)} must be a valid variable name.
582
-
583
- Valid variable names start with a letter, underscore (_), or dollar sign ($),
584
- followed by letters, digits, underscores, or dollar signs. They cannot
585
- contain spaces or special characters.
586
-
587
- Change the property name ${JSON.stringify(key)} to a valid variable
588
- name at the next time.
589
-
590
- Note that, this is not a recommendation, but an instruction you
591
- must follow.
592
- `,
593
- });
594
- }
595
- };
596
-
597
- const validateNumericRanges = (props: IProps): void => {
598
- AutoBeOpenApiTypeChecker.skim({
599
- schema: props.schema,
600
- accessor: `${props.path}[${JSON.stringify(props.typeName)}]`,
601
- closure: (schema, accessor) => {
602
- if (
603
- AutoBeOpenApiTypeChecker.isInteger(schema) === false &&
604
- AutoBeOpenApiTypeChecker.isNumber(schema) === false
605
- )
606
- return;
607
-
608
- const { minimum, maximum, exclusiveMinimum, exclusiveMaximum } = schema;
609
-
610
- // Case 1: minimum > maximum
611
- if (minimum !== undefined && maximum !== undefined && minimum > maximum)
612
- props.errors.push({
613
- path: accessor,
614
- expected: "minimum <= maximum",
615
- value: schema,
616
- description: StringUtil.trim`
617
- Invalid numeric range: minimum (${minimum}) is greater than maximum (${maximum}).
618
-
619
- This creates an impossible range where no value can satisfy both constraints.
620
- Either increase maximum or decrease minimum to create a valid range.
621
- Note that, this is not a recommendation, but an instruction you must follow.
622
- `,
623
- });
624
-
625
- // Case 2: exclusiveMinimum >= exclusiveMaximum
626
- if (
627
- exclusiveMinimum !== undefined &&
628
- exclusiveMaximum !== undefined &&
629
- exclusiveMinimum >= exclusiveMaximum
630
- )
631
- props.errors.push({
632
- path: accessor,
633
- expected: "exclusiveMinimum < exclusiveMaximum",
634
- value: schema,
635
- description: StringUtil.trim`
636
- Invalid numeric range: exclusiveMinimum (${exclusiveMinimum}) is greater than
637
- or equal to exclusiveMaximum (${exclusiveMaximum}).
638
-
639
- This creates an impossible range where no value can satisfy both constraints.
640
- Either increase exclusiveMaximum or decrease exclusiveMinimum to create a valid range.
641
- Note that, this is not a recommendation, but an instruction you must follow.
642
- `,
643
- });
644
-
645
- // Case 3: minimum >= exclusiveMaximum
646
- if (
647
- minimum !== undefined &&
648
- exclusiveMaximum !== undefined &&
649
- minimum >= exclusiveMaximum
650
- )
651
- props.errors.push({
652
- path: accessor,
653
- expected: "minimum < exclusiveMaximum",
654
- value: schema,
655
- description: StringUtil.trim`
656
- Invalid numeric range: minimum (${minimum}) is greater than or equal to
657
- exclusiveMaximum (${exclusiveMaximum}).
658
-
659
- This creates an impossible range. A value cannot be >= ${minimum} and < ${exclusiveMaximum}
660
- at the same time when minimum >= exclusiveMaximum.
661
- Either increase exclusiveMaximum or decrease minimum to create a valid range.
662
- Note that, this is not a recommendation, but an instruction you must follow.
663
- `,
664
- });
665
-
666
- // Case 4: exclusiveMinimum >= maximum
667
- if (
668
- exclusiveMinimum !== undefined &&
669
- maximum !== undefined &&
670
- exclusiveMinimum >= maximum
671
- )
672
- props.errors.push({
673
- path: accessor,
674
- expected: "exclusiveMinimum < maximum",
675
- value: schema,
676
- description: StringUtil.trim`
677
- Invalid numeric range: exclusiveMinimum (${exclusiveMinimum}) is greater than
678
- or equal to maximum (${maximum}).
679
-
680
- This creates an impossible range. A value cannot be > ${exclusiveMinimum} and <= ${maximum}
681
- at the same time when exclusiveMinimum >= maximum.
682
- Either increase maximum or decrease exclusiveMinimum to create a valid range.
683
- Note that, this is not a recommendation, but an instruction you must follow.
684
- `,
685
- });
686
-
687
- // Case 5: minimum === maximum with exclusive constraints
688
- if (
689
- minimum !== undefined &&
690
- maximum !== undefined &&
691
- minimum === maximum &&
692
- (exclusiveMinimum !== undefined || exclusiveMaximum !== undefined)
693
- )
694
- props.errors.push({
695
- path: accessor,
696
- expected: "no exclusive constraints when minimum equals maximum",
697
- value: schema,
698
- description: StringUtil.trim`
699
- Invalid numeric range: minimum equals maximum (${minimum}), but exclusive
700
- constraints are also defined.
701
-
702
- When minimum === maximum, the only valid value is exactly ${minimum}.
703
- Adding exclusiveMinimum or exclusiveMaximum makes this impossible.
704
- Remove the exclusive constraints or adjust minimum/maximum to create a valid range.
705
- Note that, this is not a recommendation, but an instruction you must follow.
706
- `,
707
- });
708
-
709
- // Case 6: negative multipleOf
710
- if (schema.multipleOf !== undefined && schema.multipleOf <= 0)
711
- props.errors.push({
712
- path: accessor,
713
- expected: "multipleOf > 0",
714
- value: schema,
715
- description: StringUtil.trim`
716
- Invalid multipleOf value: ${schema.multipleOf}.
717
-
718
- The multipleOf constraint must be a positive number greater than zero.
719
- Change multipleOf to a positive value.
720
- Note that, this is not a recommendation, but an instruction you must follow.
721
- `,
722
- });
723
- },
724
- });
725
- };
726
-
727
- const validateEmptyProperties = (props: IProps): void => {
728
- if (AutoBeOpenApiTypeChecker.isObject(props.schema) === false) return;
729
- if (Object.keys(props.schema.properties).length !== 0) return;
730
- if (
731
- isObjectType({
732
- operations: props.operations,
733
- typeName: props.typeName,
734
- }) === false
735
- )
736
- return;
737
-
738
- props.errors.push({
739
- path: props.path,
740
- expected: "At least 1 property in properties",
741
- value: props.schema,
742
- description: StringUtil.trim`
743
- Schema ${JSON.stringify(props.typeName)} has zero properties but is used
744
- as a request body or response body in API operations.
745
-
746
- Empty properties will cause TypeScript compilation errors (TS2339) in the
747
- downstream Realize stage because implementation code will try to access
748
- properties that don't exist on the type.
749
-
750
- You MUST define at least one property in the schema. Load the database
751
- schema and add the appropriate properties based on the DTO type:
752
- - ICreate: User-provided business fields (exclude id, timestamps, actor FKs)
753
- - IUpdate: All mutable business fields (all optional)
754
- - ISummary: Essential display fields for list views
755
- - IEntity (root): All public fields including relations
756
- - IRequest: Pagination and filter parameters
757
- - IJoin/ILogin: Credentials and session context fields
758
-
759
- Note that, this is not a recommendation, but an instruction you must follow.
760
- `,
761
- });
762
- };
763
- }
1
+ import { AutoBeOpenApi } from "@autobe/interface";
2
+ import { AutoBeOpenApiTypeChecker, StringUtil } from "@autobe/utils";
3
+ import { NamingConvention } from "@typia/utils";
4
+ import { IValidation } from "typia";
5
+
6
+ import { AutoBeJsonSchemaFactory } from "./AutoBeJsonSchemaFactory";
7
+
8
+ export namespace AutoBeJsonSchemaValidator {
9
+ export const isObjectType = (props: {
10
+ operations: AutoBeOpenApi.IOperation[];
11
+ typeName: string;
12
+ }): boolean =>
13
+ props.typeName.endsWith(".IAuthorized") ||
14
+ props.typeName.endsWith(".IRequest") ||
15
+ props.typeName.endsWith(".ISummary") ||
16
+ props.typeName.endsWith(".IInvert") ||
17
+ props.typeName.endsWith(".ICreate") ||
18
+ props.typeName.endsWith(".IUpdate") ||
19
+ props.typeName.endsWith(".IJoin") ||
20
+ props.typeName.endsWith(".ILogin") ||
21
+ props.operations.some(
22
+ (op) =>
23
+ op.requestBody?.typeName === props.typeName ||
24
+ op.responseBody?.typeName === props.typeName,
25
+ );
26
+
27
+ export const isPage = (key: string): boolean =>
28
+ key.startsWith("IPage") === true &&
29
+ key.startsWith("IPage.") === false &&
30
+ key !== "IPage";
31
+
32
+ export const isPreset = (typeName: string): boolean =>
33
+ AutoBeJsonSchemaFactory.DEFAULT_SCHEMAS[typeName] !== undefined ||
34
+ AutoBeJsonSchemaValidator.isPage(typeName) === true;
35
+
36
+ export interface IProps {
37
+ errors: IValidation.IError[];
38
+ operations: AutoBeOpenApi.IOperation[];
39
+ typeName: string;
40
+ schema: AutoBeOpenApi.IJsonSchema;
41
+ path: string;
42
+ }
43
+
44
+ export const validateSchema = (props: IProps): void => {
45
+ const vo = validateObjectType({
46
+ errors: props.errors,
47
+ operations: props.operations,
48
+ path: props.path,
49
+ });
50
+ validateAuthorization(props);
51
+ validateRecursive(props);
52
+ validateReferenceId(props);
53
+ validatePropertyNames(props);
54
+ validateNumericRanges(props);
55
+ // validateEmptyProperties(props);
56
+
57
+ vo(props.typeName, props.schema);
58
+ AutoBeOpenApiTypeChecker.skim({
59
+ schema: props.schema,
60
+ closure: (next, accessor) => {
61
+ if (AutoBeOpenApiTypeChecker.isReference(next) === false) return;
62
+ const key: string = next.$ref.split("/").pop()!;
63
+ validateKey({
64
+ errors: props.errors,
65
+ path: `${accessor}.$ref`,
66
+ key,
67
+ transform: (typeName) => `#/components/schemas/${typeName}`,
68
+ });
69
+ },
70
+ accessor: props.path,
71
+ });
72
+ };
73
+
74
+ export const validateKey = (props: {
75
+ errors: IValidation.IError[];
76
+ key: string;
77
+ path: string;
78
+ transform?: (typeName: string) => string;
79
+ }): void => {
80
+ const transform = props.transform ?? ((typeName: string) => typeName);
81
+ const elements: string[] = props.key.split(".");
82
+ if (elements.length > 2)
83
+ props.errors.push({
84
+ path: props.path,
85
+ expected: "At most one dot(.) character allowed in type name",
86
+ value: transform(props.key),
87
+ description: StringUtil.trim`
88
+ JSON schema type name allows at most one dot(.) character to separate
89
+ module name and interface name.
90
+
91
+ However, current key name ${transform(JSON.stringify(props.key))}
92
+ contains multiple dot(.) characters (${elements.length - 1} times).
93
+
94
+ Change it to a valid type name with at most one dot(.) character at the next time.
95
+ Note that, this is not a recommendation, but an instruction you must follow.
96
+ `,
97
+ });
98
+ if (elements.every(NamingConvention.variable) === false)
99
+ props.errors.push({
100
+ path: props.path,
101
+ expected: StringUtil.trim`
102
+ Valid variable name
103
+
104
+ ${elements.map((s) => `- ${s}: ${NamingConvention.variable(s) ? "valid" : "invalid"}`).join("\n")}
105
+ `,
106
+ value: transform(props.key),
107
+ description: StringUtil.trim`
108
+ JSON schema type name must be a valid variable name.
109
+
110
+ Even though JSON schema type name allows dot(.) character, but
111
+ each segment separated by dot(.) must be a valid variable name.
112
+
113
+ Current key name ${transform(JSON.stringify(props.key))} is not valid.
114
+ Change it to a valid variable name at the next time.
115
+ Note that, this is not a recommendation, but an instruction you must follow.
116
+ `,
117
+ });
118
+ if (props.key.endsWith(".IPage")) {
119
+ const expected: string = `IPage${props.key.substring(0, props.key.length - 6)}`;
120
+ props.errors.push({
121
+ path: props.path,
122
+ expected: `"IPage" must be followed by another interface name. Use ${transform(JSON.stringify(expected))} instead.`,
123
+ value: transform(props.key),
124
+ description: StringUtil.trim`
125
+ "IPage" is a reserved type name for pagination response.
126
+ The pagination data type name must be post-fixed after "IPage".
127
+
128
+ However, you've defined ${transform(JSON.stringify(props.key))},
129
+ post-fixing ".IPage" after the pagination data type name.
130
+
131
+ Change it to a valid pagination type name to be
132
+ ${transform(JSON.stringify(expected))} at the next time.
133
+ Note that, this is not a recommendation, but an instruction you must follow.
134
+ `,
135
+ });
136
+ }
137
+ if (props.key === "IPageIRequest")
138
+ props.errors.push({
139
+ path: props.path,
140
+ expected: `"IPageIRequest" is a mistake. Use "IPage.IRequest" instead.`,
141
+ value: transform(props.key),
142
+ description: StringUtil.trim`
143
+ You've taken a mistake that defines "${transform("IPageIRequest")}" as a type name.
144
+ However, as you've intended to define a pagination request type,
145
+ the correct type name is "${transform("IPage.IRequest")}" instead of "${transform("IPageIRequest")}".
146
+
147
+ Change it to "${transform("IPage.IRequest")}" at the next time.
148
+ Note that, this is not a recommendation, but an instruction you must follow.
149
+ `,
150
+ });
151
+ if (
152
+ props.key.startsWith("IPage") &&
153
+ props.key.startsWith("IPageI") === false &&
154
+ props.key !== "IPage.IPagination" &&
155
+ props.key !== "IPage.IRequest"
156
+ ) {
157
+ const expected: string = `IPage${props.key
158
+ .substring(5)
159
+ .split(".")
160
+ .map((s) => (s.startsWith("I") ? s : `I${s}`))
161
+ .join(".")}`;
162
+ props.errors.push({
163
+ path: props.path,
164
+ expected: `Interface name starting with 'I' even after 'IPage': ${JSON.stringify(expected)}`,
165
+ value: transform(props.key),
166
+ description: StringUtil.trim`
167
+ JSON schema type name must be an interface name starting with 'I'.
168
+ Even though JSON schema type name allows dot(.) character, but
169
+ each segment separated by dot(.) must be an interface name starting
170
+ with 'I'.
171
+
172
+ Even in the case of pagination response, after 'IPage' prefix,
173
+ the remaining part must be an interface name starting with 'I'.
174
+
175
+ Current key name ${JSON.stringify(props.key)} is not valid. Change
176
+ it to a valid interface name to be ${JSON.stringify(expected)},
177
+ or change it to another valid interface name at the next time.
178
+ Note that, this is not a recommendation, but an instruction you must follow.
179
+ `,
180
+ });
181
+ }
182
+ if (elements.some((s) => s.startsWith("I") === false) === true) {
183
+ const expected: string = elements
184
+ .map((s) => (s.startsWith("I") ? s : `I${s}`))
185
+ .join(".");
186
+ props.errors.push({
187
+ path: props.path,
188
+ expected: `Interface name starting with 'I': ${JSON.stringify(expected)}`,
189
+ value: transform(props.key),
190
+ description: StringUtil.trim`
191
+ JSON schema type name must be an interface name starting with 'I'.
192
+ Even though JSON schema type name allows dot(.) character, but
193
+ each segment separated by dot(.) must be an interface name starting
194
+ with 'I'.
195
+
196
+ Current key name ${transform(JSON.stringify(props.key))} is not valid.
197
+ Change it to a valid interface name to be ${transform(JSON.stringify(expected))},
198
+ or change it to another valid interface name at the next time.
199
+
200
+ Note that, this is not a recommendation, but an instruction you must follow.
201
+ `,
202
+ });
203
+ }
204
+ if (
205
+ elements.length === 2 &&
206
+ (elements[1] === "IJoin" ||
207
+ elements[1] === "ILogin" ||
208
+ elements[1] === "IAuthorized" ||
209
+ elements[1] === "IRefresh") &&
210
+ elements[0].endsWith("Session") === true
211
+ )
212
+ props.errors.push({
213
+ path: props.path,
214
+ expected: JSON.stringify(
215
+ `${elements[0].replace("Session", "")}.${elements[1]}`,
216
+ ),
217
+ value: transform(props.key),
218
+ description: StringUtil.trim`
219
+ You have attached ${elements[1]} to a Session type ${transform(JSON.stringify(props.key))},
220
+ but this is architecturally incorrect.
221
+
222
+ In production authentication systems, Actor and Session are separate concepts:
223
+ - **Actor** (e.g., User, Seller, Admin): The persistent user identity that performs
224
+ authentication actions - joining (registering), logging in, and receiving authorized tokens.
225
+ - **Session** (e.g., UserSession, SellerSession): The temporary authentication state that
226
+ tracks active login instances. Sessions are CREATED as a result of join/login operations,
227
+ but they do not perform these actions themselves.
228
+
229
+ Think about it semantically: An ACTOR joins the system and logs in. A SESSION is merely
230
+ a record that gets created when the actor authenticates. It makes no sense for a session
231
+ to "join" or "login" - only actors do that.
232
+
233
+ Therefore, authentication-related DTO types (IJoin, ILogin, IAuthorized, IRefresh) MUST
234
+ be attached to the Actor type, NEVER to the Session type.
235
+
236
+ Change ${transform(JSON.stringify(props.key))} to ${transform(JSON.stringify(`${elements[0].replace("Session", "")}.${elements[1]}`))} at the next time.
237
+
238
+ Note that, this is not a recommendation, but an instruction you must follow.
239
+ `,
240
+ });
241
+ };
242
+
243
+ const validateAuthorization = (props: IProps): void => {
244
+ if (props.typeName.endsWith(".IAuthorized") === true) {
245
+ if (AutoBeOpenApiTypeChecker.isObject(props.schema) === false) {
246
+ props.errors.push({
247
+ path: props.path,
248
+ expected: `AutoBeOpenApi.IJsonSchemaDescriptive<AutoBeOpenApi.IJsonSchema.IObject>`,
249
+ value: props.schema,
250
+ description: `${props.typeName} must be an object type for authorization responses. Note that, this is not a recommendation, but an instruction you must follow.`,
251
+ });
252
+ } else {
253
+ // Check if token property exists
254
+ props.schema.properties ??= {};
255
+ props.schema.properties["token"] = {
256
+ "x-autobe-specification":
257
+ "JWT token information for authentication. Server generates this token upon successful login/join.",
258
+ description: "JWT token information for authentication.",
259
+ $ref: "#/components/schemas/IAuthorizationToken",
260
+ } as AutoBeOpenApi.IJsonSchemaProperty.IReference;
261
+
262
+ props.schema.required ??= [];
263
+ if (props.schema.required.includes("token") === false)
264
+ props.schema.required.push("token");
265
+ }
266
+ }
267
+
268
+ AutoBeOpenApiTypeChecker.skim({
269
+ schema: props.schema,
270
+ accessor: props.path,
271
+ closure: (next, accessor) => {
272
+ if (AutoBeOpenApiTypeChecker.isReference(next) === false) return;
273
+ const key: string = next.$ref.split("/").pop()!;
274
+ if (
275
+ key.endsWith(".IAuthorized") === false &&
276
+ key.endsWith(".ILogin") === false &&
277
+ key.endsWith(".IJoin") === false
278
+ )
279
+ return;
280
+ const candidates: Set<string> = new Set(
281
+ props.operations
282
+ .map((op) => [
283
+ op.requestBody?.typeName.endsWith(key.split(".").pop()!)
284
+ ? op.requestBody!.typeName
285
+ : null,
286
+ op.responseBody?.typeName.endsWith(key.split(".").pop()!)
287
+ ? op.responseBody!.typeName
288
+ : null,
289
+ ])
290
+ .flat()
291
+ .filter((v) => v !== null),
292
+ );
293
+ if (candidates.has(key) === false)
294
+ props.errors.push({
295
+ path: `${accessor}.$ref`,
296
+ expected: Array.from(candidates)
297
+ .map((s) => JSON.stringify(`#/components/schemas/${s}`))
298
+ .join(" | "),
299
+ value: key,
300
+ description: StringUtil.trim`
301
+ You've referenced an authorization-related type ${JSON.stringify(key)}
302
+ that is not used in any operation's requestBody or responseBody.
303
+
304
+ Authorization-related types must be used in at least one operation's
305
+ requestBody or responseBody. Make sure to use the type appropriately
306
+ in your API design.
307
+
308
+ Existing authorization-related types used in operations are:
309
+ - ${Array.from(candidates)
310
+ .map((s) => `#/components/schemas/${s}`)
311
+ .join("\n- ")}
312
+
313
+ Note that, this is not a recommendation, but an instruction you must follow.
314
+ `,
315
+ });
316
+ },
317
+ });
318
+ };
319
+
320
+ const validateRecursive = (props: IProps): void => {
321
+ const report = (description: string) =>
322
+ props.errors.push({
323
+ path: props.path,
324
+ expected: "Non-infinite recursive schema definition",
325
+ value: props.schema,
326
+ description,
327
+ });
328
+ if (
329
+ AutoBeOpenApiTypeChecker.isReference(props.schema) &&
330
+ props.schema.$ref === `#/components/schemas/${props.typeName}`
331
+ )
332
+ report(StringUtil.trim`
333
+ You have defined a nonsensible type like below:
334
+
335
+ \`\`\`typescript
336
+ type ${props.typeName} = ${props.typeName};
337
+ \`\`\`
338
+
339
+ This is an infinite recursive type definition that cannot exist in any
340
+ programming language. A type cannot be defined as itself - this creates
341
+ a circular definition with no base case, making the type impossible to
342
+ instantiate or validate.
343
+
344
+ If you need tree or graph structures, use explicit relationships with
345
+ ID references (e.g., parentId: string) instead of recursive type definitions.
346
+ Remove the self-reference and redesign the schema at the next time.
347
+ Note that, this is not a recommendation, but an instruction you must follow.
348
+ `);
349
+ else if (
350
+ AutoBeOpenApiTypeChecker.isArray(props.schema) &&
351
+ AutoBeOpenApiTypeChecker.isReference(props.schema.items) &&
352
+ props.schema.items.$ref === `#/components/schemas/${props.typeName}`
353
+ )
354
+ report(StringUtil.trim`
355
+ You have defined a nonsensible type like below:
356
+
357
+ \`\`\`typescript
358
+ type ${props.typeName} = Array<${props.typeName}>;
359
+ \`\`\`
360
+
361
+ This is an infinite recursive array type that cannot exist in any
362
+ programming language. An array of itself creates a circular definition
363
+ with no base case, making the type impossible to instantiate or validate.
364
+
365
+ If you need nested structures, define explicit depth levels with separate
366
+ types, or use parent-child relationships with ID references.
367
+ Remove the self-reference and redesign the schema at the next time.
368
+ Note that, this is not a recommendation, but an instruction you must follow.
369
+ `);
370
+ else if (
371
+ AutoBeOpenApiTypeChecker.isOneOf(props.schema) &&
372
+ props.schema.oneOf.some(
373
+ (v) =>
374
+ AutoBeOpenApiTypeChecker.isReference(v) &&
375
+ v.$ref === `#/components/schemas/${props.typeName}`,
376
+ ) === true
377
+ )
378
+ report(StringUtil.trim`
379
+ You have defined a nonsensible type like below:
380
+
381
+ \`\`\`typescript
382
+ type ${props.typeName} = ${props.typeName} | ...;
383
+ \`\`\`
384
+
385
+ This is an infinite recursive union type that cannot exist in any
386
+ programming language. A union that includes itself as a variant creates
387
+ a circular definition with no base case, making the type impossible to
388
+ instantiate or validate.
389
+
390
+ If you need polymorphic hierarchies, define separate concrete types for
391
+ each variant without including the union type itself as a variant.
392
+ Remove the self-reference and redesign the schema at the next time.
393
+ Note that, this is not a recommendation, but an instruction you must follow.
394
+ `);
395
+ else if (
396
+ AutoBeOpenApiTypeChecker.isObject(props.schema) &&
397
+ props.schema.properties &&
398
+ props.schema.required &&
399
+ Object.entries(props.schema.properties).some(
400
+ ([k, v]) =>
401
+ AutoBeOpenApiTypeChecker.isReference(v) &&
402
+ v.$ref === `#/components/schemas/${props.typeName}` &&
403
+ (props.schema as AutoBeOpenApi.IJsonSchema.IObject).required.includes(
404
+ k,
405
+ ),
406
+ )
407
+ )
408
+ report(StringUtil.trim`
409
+ You have defined a nonsensible type like below:
410
+
411
+ \`\`\`typescript
412
+ interface ${props.typeName} {
413
+ someProperty: ${props.typeName}; // required, non-nullable
414
+ }
415
+ \`\`\`
416
+
417
+ This is an infinite recursive object type that cannot exist in any
418
+ programming language. A required non-nullable property referencing its
419
+ own type creates a circular definition with no base case, making the
420
+ type impossible to instantiate.
421
+
422
+ To create an instance of ${props.typeName}, you would need an instance of ${props.typeName},
423
+ which requires another instance of ${props.typeName}, infinitely. This is logically
424
+ impossible.
425
+
426
+ If you need parent-child or graph relationships, make the self-referencing
427
+ property either nullable or optional, or use ID references (e.g., parentId: string).
428
+ Remove the required self-reference and redesign the schema at the next time.
429
+ Note that, this is not a recommendation, but an instruction you must follow.
430
+ `);
431
+ };
432
+
433
+ const validateObjectType = (props: {
434
+ errors: IValidation.IError[];
435
+ operations: AutoBeOpenApi.IOperation[];
436
+ path: string;
437
+ }) => {
438
+ const root: Set<string> = new Set();
439
+ for (const o of props.operations) {
440
+ if (o.requestBody) root.add(o.requestBody.typeName);
441
+ if (o.responseBody) root.add(o.responseBody.typeName);
442
+ }
443
+ return (key: string, schema: AutoBeOpenApi.IJsonSchema): void => {
444
+ if (AutoBeOpenApiTypeChecker.isObject(schema) === true) return;
445
+ if (root.has(key))
446
+ props.errors.push({
447
+ path: props.path,
448
+ expected: `AutoBeOpenApi.IJsonSchemaDescriptive.IObject`,
449
+ value: schema,
450
+ description: StringUtil.trim`
451
+ Root schema types (used in requestBody or responseBody of operations)
452
+ must be defined as object types.
453
+
454
+ This is the rule enforced to ensure consistent API design and to facilitate easier data handling.
455
+ Even though you think that defining a non-object type is more convenient for your specific use case,
456
+ just follow the rule without any resistance.
457
+
458
+ Note that, this is not a recommendation, but an instruction you must follow.
459
+
460
+ If current type is hard to be defined as an object type, just wrap it in an object type like below:
461
+
462
+ \`\`\`typescript
463
+ {
464
+ value: T;
465
+ }
466
+ \`\`\`
467
+ `,
468
+ });
469
+ else if (
470
+ key.endsWith(".IRequest") ||
471
+ key.endsWith(".ISummary") ||
472
+ key.endsWith(".IInvert") ||
473
+ key.endsWith(".ICreate") ||
474
+ key.endsWith(".IUpdate") ||
475
+ key.endsWith(".IJoin") ||
476
+ key.endsWith(".ILogin") ||
477
+ key.endsWith(".IAuthorized")
478
+ )
479
+ props.errors.push({
480
+ path: props.path,
481
+ expected: `AutoBeOpenApi.IJsonSchemaDescriptive.IObject`,
482
+ value: schema,
483
+ description: StringUtil.trim`
484
+ DTO type of .${key.split(".").pop()} suffix must be defined as an object type.
485
+
486
+ This is the rule enforced to ensure consistent API design and to facilitate easier data handling.
487
+ Even though you think that defining a non-object type is more convenient for your specific use case,
488
+ just follow the rule without any resistance.
489
+
490
+ Note that, this is not a recommendation, but an instruction you must follow.
491
+
492
+ If current type is hard to be defined as an object type, just wrap it in an object type like below:
493
+
494
+ \`\`\`typescript
495
+ {
496
+ value: T;
497
+ }
498
+ \`\`\`
499
+ `,
500
+ });
501
+ };
502
+ };
503
+
504
+ const validateReferenceId = (props: {
505
+ errors: IValidation.IError[];
506
+ schema: AutoBeOpenApi.IJsonSchema;
507
+ path: string;
508
+ }): void => {
509
+ if (AutoBeOpenApiTypeChecker.isObject(props.schema) === false) return;
510
+ for (const [key, value] of Object.entries(props.schema.properties)) {
511
+ if (key !== "id" && key.endsWith("_id") === false) continue;
512
+
513
+ const accessor: string = `${props.path}.properties${
514
+ NamingConvention.variable(key) ? `.${key}` : `[${JSON.stringify(key)}]`
515
+ }`;
516
+ const inspect = (schema: AutoBeOpenApi.IJsonSchema): boolean =>
517
+ AutoBeOpenApiTypeChecker.isString(schema) ||
518
+ AutoBeOpenApiTypeChecker.isNull(schema) ||
519
+ (AutoBeOpenApiTypeChecker.isOneOf(schema) &&
520
+ schema.oneOf.every((v) => inspect(v)));
521
+ if (inspect(value) === false)
522
+ props.errors.push({
523
+ path: accessor,
524
+ expected: StringUtil.trim`
525
+ | { type: "string"; format: "uuid"; description: string; }
526
+ | {
527
+ oneOf: [
528
+ { type: "string"; format: "uuid"; },
529
+ { type: "null"; },
530
+ ];
531
+ description: string;
532
+ }`,
533
+ value,
534
+ description: StringUtil.trim`
535
+ Property names "id" or ending with "_id" must be defined as
536
+ UUID string type, or nullable UUID string type.
537
+
538
+ This is the rule enforced to ensure consistent identification of
539
+ resources across the API. Even though you think that defining a
540
+ different type is more convenient for your specific use case,
541
+ just follow the rule without any resistance.
542
+
543
+ Note that, this is not a recommendation, but an instruction you
544
+ must follow.
545
+ `,
546
+ });
547
+ }
548
+ };
549
+
550
+ const validatePropertyNames = (props: {
551
+ errors: IValidation.IError[];
552
+ schema: AutoBeOpenApi.IJsonSchema;
553
+ path: string;
554
+ }): void => {
555
+ if (AutoBeOpenApiTypeChecker.isObject(props.schema) === false) return;
556
+ for (const key of Object.keys(props.schema.properties)) {
557
+ if (NamingConvention.reserved(key))
558
+ props.errors.push({
559
+ path: `${props.path}.properties${NamingConvention.variable(key) ? `.${key}` : `[${JSON.stringify(key)}]`}`,
560
+ expected: `none system reserved word`,
561
+ value: key,
562
+ description: StringUtil.trim`
563
+ Property name ${JSON.stringify(key)} is a system reserved word.
564
+
565
+ Avoid using system reserved words as property names to prevent
566
+ potential conflicts and ensure clarity in your API design.
567
+
568
+ Change the property name ${JSON.stringify(key)} to a non-reserved
569
+ word at the next time.
570
+
571
+ Note that, this is not a recommendation, but an instruction you
572
+ must follow.
573
+ `,
574
+ });
575
+ else if (NamingConvention.variable(key) === false)
576
+ props.errors.push({
577
+ path: `${props.path}.properties${NamingConvention.variable(key) ? `.${key}` : `[${JSON.stringify(key)}]`}`,
578
+ expected: `valid variable name`,
579
+ value: key,
580
+ description: StringUtil.trim`
581
+ Property name ${JSON.stringify(key)} must be a valid variable name.
582
+
583
+ Valid variable names start with a letter, underscore (_), or dollar sign ($),
584
+ followed by letters, digits, underscores, or dollar signs. They cannot
585
+ contain spaces or special characters.
586
+
587
+ Change the property name ${JSON.stringify(key)} to a valid variable
588
+ name at the next time.
589
+
590
+ Note that, this is not a recommendation, but an instruction you
591
+ must follow.
592
+ `,
593
+ });
594
+ }
595
+ };
596
+
597
+ const validateNumericRanges = (props: IProps): void => {
598
+ AutoBeOpenApiTypeChecker.skim({
599
+ schema: props.schema,
600
+ accessor: `${props.path}[${JSON.stringify(props.typeName)}]`,
601
+ closure: (schema, accessor) => {
602
+ if (
603
+ AutoBeOpenApiTypeChecker.isInteger(schema) === false &&
604
+ AutoBeOpenApiTypeChecker.isNumber(schema) === false
605
+ )
606
+ return;
607
+
608
+ const { minimum, maximum, exclusiveMinimum, exclusiveMaximum } = schema;
609
+
610
+ // Case 1: minimum > maximum
611
+ if (minimum !== undefined && maximum !== undefined && minimum > maximum)
612
+ props.errors.push({
613
+ path: accessor,
614
+ expected: "minimum <= maximum",
615
+ value: schema,
616
+ description: StringUtil.trim`
617
+ Invalid numeric range: minimum (${minimum}) is greater than maximum (${maximum}).
618
+
619
+ This creates an impossible range where no value can satisfy both constraints.
620
+ Either increase maximum or decrease minimum to create a valid range.
621
+ Note that, this is not a recommendation, but an instruction you must follow.
622
+ `,
623
+ });
624
+
625
+ // Case 2: exclusiveMinimum >= exclusiveMaximum
626
+ if (
627
+ exclusiveMinimum !== undefined &&
628
+ exclusiveMaximum !== undefined &&
629
+ exclusiveMinimum >= exclusiveMaximum
630
+ )
631
+ props.errors.push({
632
+ path: accessor,
633
+ expected: "exclusiveMinimum < exclusiveMaximum",
634
+ value: schema,
635
+ description: StringUtil.trim`
636
+ Invalid numeric range: exclusiveMinimum (${exclusiveMinimum}) is greater than
637
+ or equal to exclusiveMaximum (${exclusiveMaximum}).
638
+
639
+ This creates an impossible range where no value can satisfy both constraints.
640
+ Either increase exclusiveMaximum or decrease exclusiveMinimum to create a valid range.
641
+ Note that, this is not a recommendation, but an instruction you must follow.
642
+ `,
643
+ });
644
+
645
+ // Case 3: minimum >= exclusiveMaximum
646
+ if (
647
+ minimum !== undefined &&
648
+ exclusiveMaximum !== undefined &&
649
+ minimum >= exclusiveMaximum
650
+ )
651
+ props.errors.push({
652
+ path: accessor,
653
+ expected: "minimum < exclusiveMaximum",
654
+ value: schema,
655
+ description: StringUtil.trim`
656
+ Invalid numeric range: minimum (${minimum}) is greater than or equal to
657
+ exclusiveMaximum (${exclusiveMaximum}).
658
+
659
+ This creates an impossible range. A value cannot be >= ${minimum} and < ${exclusiveMaximum}
660
+ at the same time when minimum >= exclusiveMaximum.
661
+ Either increase exclusiveMaximum or decrease minimum to create a valid range.
662
+ Note that, this is not a recommendation, but an instruction you must follow.
663
+ `,
664
+ });
665
+
666
+ // Case 4: exclusiveMinimum >= maximum
667
+ if (
668
+ exclusiveMinimum !== undefined &&
669
+ maximum !== undefined &&
670
+ exclusiveMinimum >= maximum
671
+ )
672
+ props.errors.push({
673
+ path: accessor,
674
+ expected: "exclusiveMinimum < maximum",
675
+ value: schema,
676
+ description: StringUtil.trim`
677
+ Invalid numeric range: exclusiveMinimum (${exclusiveMinimum}) is greater than
678
+ or equal to maximum (${maximum}).
679
+
680
+ This creates an impossible range. A value cannot be > ${exclusiveMinimum} and <= ${maximum}
681
+ at the same time when exclusiveMinimum >= maximum.
682
+ Either increase maximum or decrease exclusiveMinimum to create a valid range.
683
+ Note that, this is not a recommendation, but an instruction you must follow.
684
+ `,
685
+ });
686
+
687
+ // Case 5: minimum === maximum with exclusive constraints
688
+ if (
689
+ minimum !== undefined &&
690
+ maximum !== undefined &&
691
+ minimum === maximum &&
692
+ (exclusiveMinimum !== undefined || exclusiveMaximum !== undefined)
693
+ )
694
+ props.errors.push({
695
+ path: accessor,
696
+ expected: "no exclusive constraints when minimum equals maximum",
697
+ value: schema,
698
+ description: StringUtil.trim`
699
+ Invalid numeric range: minimum equals maximum (${minimum}), but exclusive
700
+ constraints are also defined.
701
+
702
+ When minimum === maximum, the only valid value is exactly ${minimum}.
703
+ Adding exclusiveMinimum or exclusiveMaximum makes this impossible.
704
+ Remove the exclusive constraints or adjust minimum/maximum to create a valid range.
705
+ Note that, this is not a recommendation, but an instruction you must follow.
706
+ `,
707
+ });
708
+
709
+ // Case 6: negative multipleOf
710
+ if (schema.multipleOf !== undefined && schema.multipleOf <= 0)
711
+ props.errors.push({
712
+ path: accessor,
713
+ expected: "multipleOf > 0",
714
+ value: schema,
715
+ description: StringUtil.trim`
716
+ Invalid multipleOf value: ${schema.multipleOf}.
717
+
718
+ The multipleOf constraint must be a positive number greater than zero.
719
+ Change multipleOf to a positive value.
720
+ Note that, this is not a recommendation, but an instruction you must follow.
721
+ `,
722
+ });
723
+ },
724
+ });
725
+ };
726
+
727
+ // const validateEmptyProperties = (props: IProps): void => {
728
+ // if (AutoBeOpenApiTypeChecker.isObject(props.schema) === false) return;
729
+ // if (Object.keys(props.schema.properties).length !== 0) return;
730
+ // if (
731
+ // isObjectType({
732
+ // operations: props.operations,
733
+ // typeName: props.typeName,
734
+ // }) === false
735
+ // )
736
+ // return;
737
+
738
+ // props.errors.push({
739
+ // path: `${props.path}.properties`,
740
+ // expected: "At least 1 property in properties",
741
+ // value: props.schema.properties,
742
+ // description: StringUtil.trim`
743
+ // Schema ${JSON.stringify(props.typeName)} has zero properties but is used
744
+ // as a request body or response body in API operations.
745
+
746
+ // Empty properties will cause TypeScript compilation errors (TS2339) in the
747
+ // downstream Realize stage because implementation code will try to access
748
+ // properties that don't exist on the type.
749
+
750
+ // You MUST define at least one property in the schema. Load the database
751
+ // schema and add the appropriate properties based on the DTO type:
752
+ // - ICreate: User-provided business fields (exclude id, timestamps, actor FKs)
753
+ // - IUpdate: All mutable business fields (all optional)
754
+ // - ISummary: Essential display fields for list views
755
+ // - IEntity (root): All public fields including relations
756
+ // - IRequest: Pagination and filter parameters
757
+ // - IJoin/ILogin: Credentials and session context fields
758
+
759
+ // Note that, this is not a recommendation, but an instruction you must follow.
760
+ // `,
761
+ // });
762
+ // };
763
+ }