@tsed/schema 8.26.2 → 8.28.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.
@@ -4,7 +4,7 @@ import { JsonEntityFn } from "./jsonEntityFn.js";
4
4
  *
5
5
  * The `@CustomKey()` decorator allows adding vendor-specific extensions or custom metadata
6
6
  * to schemas. These custom keys are only included in the generated schema when explicitly
7
- * requested with the `customKeys: true` option in `getJsonSchema()`.
7
+ * requested with the `customKeys: true` option in `compile()`.
8
8
  *
9
9
  * ### Basic Usage
10
10
  *
@@ -56,12 +56,12 @@ import { JsonEntityFn } from "./jsonEntityFn.js";
56
56
  * Custom keys must be explicitly requested:
57
57
  *
58
58
  * ```typescript
59
- * import { getJsonSchema } from "@tsed/schema";
59
+ * import { compile } from "@tsed/schema";
60
60
  *
61
- * const schema = getJsonSchema(UserModel, { customKeys: true });
61
+ * const schema = compile(UserModel, { customKeys: true });
62
62
  * // Schema will include x-internal-id and other custom keys
63
63
  *
64
- * const standardSchema = getJsonSchema(UserModel);
64
+ * const standardSchema = compile(UserModel);
65
65
  * // Custom keys are omitted by default
66
66
  * ```
67
67
  *
@@ -0,0 +1,15 @@
1
+ import { JsonEntityFn } from "./jsonEntityFn.js";
2
+ /**
3
+ * Set a `$ref` value on the current schema property/parameter.
4
+ *
5
+ * Supports local references (`#/...`) and external URLs.
6
+ *
7
+ * @param ref - Reference URI
8
+ * @decorator
9
+ */
10
+ export function Ref(ref) {
11
+ return JsonEntityFn((entity) => {
12
+ entity.itemSchema.delete("type");
13
+ entity.itemSchema.$ref(ref);
14
+ });
15
+ }
@@ -1,6 +1,6 @@
1
1
  import { JsonEntityStore } from "../../domain/JsonEntityStore.js";
2
2
  /**
3
- * Define generics list. This list is used by @@GenericOf@@ and the @@getJsonSchema@@ function to build the correct JsonSchema.
3
+ * Define generics list. This list is used by @@GenericOf@@ and the @@compile@@ function to build the correct JsonSchema.
4
4
  *
5
5
  * See @@GenericOf@@ decorator for more details.
6
6
  *
@@ -17,7 +17,7 @@
17
17
  * ```typescript
18
18
  * import {SpecTypes} from "@tsed/schema";
19
19
  *
20
- * const schema = getJsonSchema(MyModel, {
20
+ * const schema = compile(MyModel, {
21
21
  * specType: SpecTypes.OPENAPI
22
22
  * });
23
23
  * ```
@@ -12,6 +12,7 @@ import { lazyRef } from "./lazyRef.js";
12
12
  import { number } from "./number.js";
13
13
  import { object } from "./object.js";
14
14
  import { oneOf } from "./oneOf.js";
15
+ import { ref } from "./ref.js";
15
16
  import { string } from "./string.js";
16
17
  import { uri } from "./uri.js";
17
18
  import { url } from "./url.js";
@@ -39,6 +40,7 @@ export const s = {
39
40
  lazyRef,
40
41
  object,
41
42
  oneOf,
43
+ $ref: ref,
42
44
  string,
43
45
  uri,
44
46
  url
@@ -0,0 +1,17 @@
1
+ import { JsonSchema } from "../domain/JsonSchema.js";
2
+ /**
3
+ * Create a schema reference from an external or internal `$ref` URI.
4
+ *
5
+ * ### Example
6
+ *
7
+ * ```typescript
8
+ * const UserRef = ref("https://api.example.com/doc/swagger.json#/components/schemas/User");
9
+ * ```
10
+ *
11
+ * @param path - The reference URI
12
+ * @returns A JsonSchema containing the `$ref`
13
+ * @schemaFunctional
14
+ */
15
+ export function ref(path) {
16
+ return new JsonSchema().$ref(path);
17
+ }
package/lib/esm/index.js CHANGED
@@ -80,6 +80,7 @@ export * from "./decorators/common/pattern.js";
80
80
  export * from "./decorators/common/property.js";
81
81
  export * from "./decorators/common/readOnly.js";
82
82
  export * from "./decorators/common/recordOf.js";
83
+ export * from "./decorators/common/ref.js";
83
84
  export * from "./decorators/common/required.js";
84
85
  export * from "./decorators/common/requiredGroups.js";
85
86
  export * from "./decorators/common/schema.js";
@@ -150,6 +151,7 @@ export * from "./fn/lazyRef.js";
150
151
  export * from "./fn/number.js";
151
152
  export * from "./fn/object.js";
152
153
  export * from "./fn/oneOf.js";
154
+ export * from "./fn/ref.js";
153
155
  export * from "./fn/string.js";
154
156
  export * from "./fn/uri.js";
155
157
  export * from "./fn/url.js";
@@ -163,6 +165,7 @@ export * from "./interfaces/JsonSchemaOptions.js";
163
165
  export * from "./registries/enumRegistries.js";
164
166
  export * from "./registries/JsonSchemaMapperContainer.js";
165
167
  export * from "./utils/buildPath.js";
168
+ export * from "./utils/compile.js";
166
169
  export * from "./utils/concatParameters.js";
167
170
  export * from "./utils/concatPath.js";
168
171
  export * from "./utils/defineStatusModel.js";
@@ -11,7 +11,7 @@ export function registerJsonSchemaMapper(type, mapper, spec) {
11
11
  /**
12
12
  * @ignore
13
13
  */
14
- export function getJsonSchemaMapper(type, options) {
14
+ export function compileMapper(type, options) {
15
15
  const mapper = JsonSchemaMappersContainer.get(`${options?.specType}:${type}`) || JsonSchemaMappersContainer.get(type);
16
16
  if (mapper) {
17
17
  return mapper;
@@ -23,7 +23,7 @@ export function getJsonSchemaMapper(type, options) {
23
23
  * @ignore
24
24
  */
25
25
  export function execMapper(type, args, options, parent) {
26
- return getJsonSchemaMapper(type, options)(...args, options, parent);
26
+ return compileMapper(type, options)(...args, options, parent);
27
27
  }
28
28
  export function hasMapper(type) {
29
29
  return JsonSchemaMappersContainer.has(type);
@@ -0,0 +1,13 @@
1
+ import { getJsonSchema } from "./getJsonSchema.js";
2
+ /**
3
+ * Compile a class, parameter store, or `JsonSchema` into a plain JSON schema object.
4
+ *
5
+ * This is an alias of `getJsonSchema(...)`.
6
+ *
7
+ * @param model Class, parameter store, or `JsonSchema` to compile.
8
+ * @param options JSON schema generation options.
9
+ * @returns Compiled JSON schema object.
10
+ */
11
+ export function compile(model, options = {}) {
12
+ return getJsonSchema(model, options);
13
+ }
@@ -1,6 +1,7 @@
1
1
  import "../components/index.js";
2
2
  import { getValue, isClass, isPlainObject, nameOf } from "@tsed/core";
3
3
  import { JsonParameterStore } from "../domain/JsonParameterStore.js";
4
+ import { JsonSchema } from "../domain/JsonSchema.js";
4
5
  import { SpecTypes } from "../domain/SpecTypes.js";
5
6
  import { execMapper } from "../registries/JsonSchemaMapperContainer.js";
6
7
  import { getJsonEntityStore } from "./getJsonEntityStore.js";
@@ -27,6 +28,10 @@ function get(model, options) {
27
28
  CACHES.set(model, cache);
28
29
  const key = getKey(options);
29
30
  if (!cache.has(key)) {
31
+ if (model instanceof JsonSchema) {
32
+ cache.set(key, model.toJSON(options));
33
+ return cache.get(key);
34
+ }
30
35
  const entity = getJsonEntityStore(model);
31
36
  let mapper = "schema";
32
37
  if (entity instanceof JsonParameterStore) {
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * The `@CustomKey()` decorator allows adding vendor-specific extensions or custom metadata
5
5
  * to schemas. These custom keys are only included in the generated schema when explicitly
6
- * requested with the `customKeys: true` option in `getJsonSchema()`.
6
+ * requested with the `customKeys: true` option in `compile()`.
7
7
  *
8
8
  * ### Basic Usage
9
9
  *
@@ -55,12 +55,12 @@
55
55
  * Custom keys must be explicitly requested:
56
56
  *
57
57
  * ```typescript
58
- * import { getJsonSchema } from "@tsed/schema";
58
+ * import { compile } from "@tsed/schema";
59
59
  *
60
- * const schema = getJsonSchema(UserModel, { customKeys: true });
60
+ * const schema = compile(UserModel, { customKeys: true });
61
61
  * // Schema will include x-internal-id and other custom keys
62
62
  *
63
- * const standardSchema = getJsonSchema(UserModel);
63
+ * const standardSchema = compile(UserModel);
64
64
  * // Custom keys are omitted by default
65
65
  * ```
66
66
  *
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Set a `$ref` value on the current schema property/parameter.
3
+ *
4
+ * Supports local references (`#/...`) and external URLs.
5
+ *
6
+ * @param ref - Reference URI
7
+ * @decorator
8
+ */
9
+ export declare function Ref(ref: string): (...args: any[]) => any;
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Define generics list. This list is used by @@GenericOf@@ and the @@getJsonSchema@@ function to build the correct JsonSchema.
2
+ * Define generics list. This list is used by @@GenericOf@@ and the @@compile@@ function to build the correct JsonSchema.
3
3
  *
4
4
  * See @@GenericOf@@ decorator for more details.
5
5
  *
@@ -17,7 +17,7 @@
17
17
  * ```typescript
18
18
  * import {SpecTypes} from "@tsed/schema";
19
19
  *
20
- * const schema = getJsonSchema(MyModel, {
20
+ * const schema = compile(MyModel, {
21
21
  * specType: SpecTypes.OPENAPI
22
22
  * });
23
23
  * ```
@@ -13,6 +13,7 @@ import { lazyRef } from "./lazyRef.js";
13
13
  import { number } from "./number.js";
14
14
  import { object } from "./object.js";
15
15
  import { oneOf } from "./oneOf.js";
16
+ import { ref } from "./ref.js";
16
17
  import { string } from "./string.js";
17
18
  import { uri } from "./uri.js";
18
19
  import { url } from "./url.js";
@@ -40,6 +41,7 @@ export declare const s: {
40
41
  readonly lazyRef: typeof lazyRef;
41
42
  readonly object: typeof object;
42
43
  readonly oneOf: typeof oneOf;
44
+ readonly $ref: typeof ref;
43
45
  readonly string: typeof string;
44
46
  readonly uri: typeof uri;
45
47
  readonly url: typeof url;
@@ -0,0 +1,15 @@
1
+ import { JsonSchema } from "../domain/JsonSchema.js";
2
+ /**
3
+ * Create a schema reference from an external or internal `$ref` URI.
4
+ *
5
+ * ### Example
6
+ *
7
+ * ```typescript
8
+ * const UserRef = ref("https://api.example.com/doc/swagger.json#/components/schemas/User");
9
+ * ```
10
+ *
11
+ * @param path - The reference URI
12
+ * @returns A JsonSchema containing the `$ref`
13
+ * @schemaFunctional
14
+ */
15
+ export declare function ref<T = unknown>(path: string): JsonSchema<T>;
@@ -80,6 +80,7 @@ export * from "./decorators/common/pattern.js";
80
80
  export * from "./decorators/common/property.js";
81
81
  export * from "./decorators/common/readOnly.js";
82
82
  export * from "./decorators/common/recordOf.js";
83
+ export * from "./decorators/common/ref.js";
83
84
  export * from "./decorators/common/required.js";
84
85
  export * from "./decorators/common/requiredGroups.js";
85
86
  export * from "./decorators/common/schema.js";
@@ -150,6 +151,7 @@ export * from "./fn/lazyRef.js";
150
151
  export * from "./fn/number.js";
151
152
  export * from "./fn/object.js";
152
153
  export * from "./fn/oneOf.js";
154
+ export * from "./fn/ref.js";
153
155
  export * from "./fn/string.js";
154
156
  export * from "./fn/uri.js";
155
157
  export * from "./fn/url.js";
@@ -163,6 +165,7 @@ export * from "./interfaces/JsonSchemaOptions.js";
163
165
  export * from "./registries/enumRegistries.js";
164
166
  export * from "./registries/JsonSchemaMapperContainer.js";
165
167
  export * from "./utils/buildPath.js";
168
+ export * from "./utils/compile.js";
166
169
  export * from "./utils/concatParameters.js";
167
170
  export * from "./utils/concatPath.js";
168
171
  export * from "./utils/defineStatusModel.js";
@@ -11,7 +11,7 @@ import type { GenericsMap } from "../utils/generics.js";
11
11
  * ### Usage
12
12
  *
13
13
  * ```typescript
14
- * import {getJsonSchema, JsonSchemaOptions} from "@tsed/schema";
14
+ * import {compile, JsonSchemaOptions} from "@tsed/schema";
15
15
  *
16
16
  * const options: JsonSchemaOptions = {
17
17
  * specType: SpecTypes.OPENAPI,
@@ -21,7 +21,7 @@ import type { GenericsMap } from "../utils/generics.js";
21
21
  * customKeys: true
22
22
  * };
23
23
  *
24
- * const schema = getJsonSchema(MyModel, options);
24
+ * const schema = compile(MyModel, options);
25
25
  * ```
26
26
  *
27
27
  * @public
@@ -12,7 +12,7 @@ export declare function registerJsonSchemaMapper(type: string, mapper: JsonSchem
12
12
  /**
13
13
  * @ignore
14
14
  */
15
- export declare function getJsonSchemaMapper(type: string, options: any): JsonSchemaMapper;
15
+ export declare function compileMapper(type: string, options: any): JsonSchemaMapper;
16
16
  /**
17
17
  * @ignore
18
18
  */
@@ -0,0 +1,14 @@
1
+ import { Type } from "@tsed/core";
2
+ import { JsonParameterStore } from "../domain/JsonParameterStore.js";
3
+ import { JsonSchema } from "../domain/JsonSchema.js";
4
+ import { JsonSchemaOptions } from "../interfaces/JsonSchemaOptions.js";
5
+ /**
6
+ * Compile a class, parameter store, or `JsonSchema` into a plain JSON schema object.
7
+ *
8
+ * This is an alias of `getJsonSchema(...)`.
9
+ *
10
+ * @param model Class, parameter store, or `JsonSchema` to compile.
11
+ * @param options JSON schema generation options.
12
+ * @returns Compiled JSON schema object.
13
+ */
14
+ export declare function compile(model: Type<any> | JsonParameterStore | JsonSchema, options?: JsonSchemaOptions): any;
@@ -1,5 +1,6 @@
1
1
  import "../components/index.js";
2
2
  import { Type } from "@tsed/core";
3
3
  import { JsonParameterStore } from "../domain/JsonParameterStore.js";
4
+ import { JsonSchema } from "../domain/JsonSchema.js";
4
5
  import { JsonSchemaOptions } from "../interfaces/JsonSchemaOptions.js";
5
- export declare function getJsonSchema(model: Type<any> | JsonParameterStore, options?: JsonSchemaOptions): any;
6
+ export declare function getJsonSchema(model: Type<any> | JsonParameterStore | JsonSchema, options?: JsonSchemaOptions): any;
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@tsed/schema",
3
3
  "description": "JsonSchema module for Ts.ED Framework",
4
4
  "type": "module",
5
- "version": "8.26.2",
5
+ "version": "8.28.0",
6
6
  "source": "./src/index.ts",
7
7
  "main": "./lib/esm/index.js",
8
8
  "module": "./lib/esm/index.js",
@@ -34,7 +34,7 @@
34
34
  "tsed"
35
35
  ],
36
36
  "dependencies": {
37
- "@tsed/openspec": "8.26.2",
37
+ "@tsed/openspec": "8.28.0",
38
38
  "change-case": "^5.4.4",
39
39
  "json-schema": "0.4.0",
40
40
  "picomatch": "4.0.4",
@@ -43,11 +43,11 @@
43
43
  },
44
44
  "devDependencies": {
45
45
  "@apidevtools/swagger-parser": "10.1.0",
46
- "@tsed/barrels": "8.26.2",
47
- "@tsed/core": "8.26.2",
48
- "@tsed/hooks": "8.26.2",
49
- "@tsed/openspec": "8.26.2",
50
- "@tsed/typescript": "8.26.2",
46
+ "@tsed/barrels": "8.28.0",
47
+ "@tsed/core": "8.28.0",
48
+ "@tsed/hooks": "8.28.0",
49
+ "@tsed/openspec": "8.28.0",
50
+ "@tsed/typescript": "8.28.0",
51
51
  "@types/fs-extra": "11.0.4",
52
52
  "@types/json-schema": "7.0.15",
53
53
  "@types/picomatch": "3.0.2",
@@ -55,7 +55,7 @@
55
55
  "ajv": "^8.17.1",
56
56
  "eslint": "9.12.0",
57
57
  "typescript": "6.0.2",
58
- "vitest": "3.2.4"
58
+ "vitest": "4.1.4"
59
59
  },
60
60
  "peerDependencies": {
61
61
  "@tsed/core": ">=8.0.0",