@tsofist/schema-forge 2.10.0 → 2.12.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.
- package/lib/generator/schema-generator.js +8 -2
- package/lib/generator.spec.js +4 -0
- package/lib/shrink-names.d.ts +6 -0
- package/lib/shrink-names.js +21 -0
- package/lib/types.d.ts +3 -2
- package/package.json +4 -4
|
@@ -7,6 +7,7 @@ const ajv_1 = require("ajv");
|
|
|
7
7
|
const jsonpath_plus_1 = require("jsonpath-plus");
|
|
8
8
|
const ts_json_schema_generator_1 = require("ts-json-schema-generator");
|
|
9
9
|
const typescript_1 = require("typescript");
|
|
10
|
+
const shrink_names_1 = require("../shrink-names");
|
|
10
11
|
const sort_properties_1 = require("../util/sort-properties");
|
|
11
12
|
const tsc_1 = require("../util/tsc");
|
|
12
13
|
const types_1 = require("./types");
|
|
@@ -55,11 +56,16 @@ async function generateSchemaByDraftTypes(options) {
|
|
|
55
56
|
const schema = generator.createSchema(definitionName);
|
|
56
57
|
Object.assign(result.definitions, schema.definitions);
|
|
57
58
|
}
|
|
58
|
-
|
|
59
|
+
const shrinkDefinitionNames = !options.shrinkDefinitionNames
|
|
60
|
+
? undefined
|
|
61
|
+
: options.shrinkDefinitionNames === true
|
|
62
|
+
? shrink_names_1.shrinkDefinitionName
|
|
63
|
+
: options.shrinkDefinitionNames;
|
|
64
|
+
if (shrinkDefinitionNames) {
|
|
59
65
|
const replacement = new Set();
|
|
60
66
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
61
67
|
for (const name of Object.keys(result.definitions)) {
|
|
62
|
-
const r =
|
|
68
|
+
const r = shrinkDefinitionNames(name);
|
|
63
69
|
if (r) {
|
|
64
70
|
if (replacement.has(r) || r in result.definitions) {
|
|
65
71
|
(0, error_1.raise)(`Duplicate replacement definition name: ${r}`);
|
package/lib/generator.spec.js
CHANGED
|
@@ -399,6 +399,10 @@ describe('generator for a3', () => {
|
|
|
399
399
|
it('generated schema should be valid', () => {
|
|
400
400
|
expect(forgeSchemaResult).toBeTruthy();
|
|
401
401
|
});
|
|
402
|
+
it('should generate root schema normally', () => {
|
|
403
|
+
const registry = (0, validator_1.createSchemaForgeValidator)({ schemas: [forgeSchemaResult.schema] });
|
|
404
|
+
expect(registry.getRootSchema('')).toBeTruthy();
|
|
405
|
+
});
|
|
402
406
|
it('interface generics should works', () => {
|
|
403
407
|
const props = forgeSchemaResult.schema.definitions?.InterfaceWithGeneric_InterfaceDeclaration
|
|
404
408
|
.properties;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { NonEmptyString } from '@tsofist/stem';
|
|
2
|
+
/**
|
|
3
|
+
* Shrink definition name by removing generic type parameters
|
|
4
|
+
* and appending a digest of the removed part.
|
|
5
|
+
*/
|
|
6
|
+
export declare function shrinkDefinitionName(definitionName: string, suffixLength?: number): NonEmptyString | undefined;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.shrinkDefinitionName = shrinkDefinitionName;
|
|
4
|
+
const node_crypto_1 = require("node:crypto");
|
|
5
|
+
/**
|
|
6
|
+
* Shrink definition name by removing generic type parameters
|
|
7
|
+
* and appending a digest of the removed part.
|
|
8
|
+
*/
|
|
9
|
+
function shrinkDefinitionName(definitionName, suffixLength = 8) {
|
|
10
|
+
const gPos = definitionName.indexOf('<');
|
|
11
|
+
if (gPos >= 0) {
|
|
12
|
+
const prefix = definitionName.substring(0, gPos);
|
|
13
|
+
const hashSource = definitionName.substring(gPos);
|
|
14
|
+
const digest = (0, node_crypto_1.createHash)('sha512')
|
|
15
|
+
.update(hashSource)
|
|
16
|
+
.digest('hex')
|
|
17
|
+
.substring(0, suffixLength);
|
|
18
|
+
return `${prefix}_H${digest}`;
|
|
19
|
+
}
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
package/lib/types.d.ts
CHANGED
|
@@ -75,10 +75,11 @@ export interface SchemaForgeOptions extends SchemaForgeBaseOptions {
|
|
|
75
75
|
readonly sortObjectProperties?: boolean;
|
|
76
76
|
/**
|
|
77
77
|
* If you want to shrink the schema definition names, you have to provide a replacement function.
|
|
78
|
-
*
|
|
79
78
|
* WARN: this functionality is not compatible (yet) with `encodeRefs=true` option.
|
|
79
|
+
*
|
|
80
|
+
* @see shrinkDefinitionName
|
|
80
81
|
*/
|
|
81
|
-
readonly shrinkDefinitionNames?: (definitionName: string) => undefined | NonEmptyString;
|
|
82
|
+
readonly shrinkDefinitionNames?: boolean | ((definitionName: string) => undefined | NonEmptyString);
|
|
82
83
|
}
|
|
83
84
|
export interface SchemaForgeMetadata {
|
|
84
85
|
$id: string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsofist/schema-forge",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.12.0",
|
|
4
4
|
"description": "Generate JSON schema from TypeScript types",
|
|
5
5
|
"author": "Andrew Berdnikov <tsofistgudmen@gmail.com>",
|
|
6
6
|
"license": "LGPL-3.0",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
23
|
"@faker-js/faker": "^9.8.0",
|
|
24
|
-
"@tsofist/stem": "^
|
|
24
|
+
"@tsofist/stem": "^5.1.0",
|
|
25
25
|
"ajv": "^8.17.1",
|
|
26
26
|
"ajv-formats": "^3.0.1",
|
|
27
27
|
"json-schema-faker": "^0.5.9",
|
|
@@ -31,8 +31,8 @@
|
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"@tsofist/web-buddy": "^1.21.0",
|
|
34
|
-
"@types/jest": "
|
|
35
|
-
"@types/node": "^20.
|
|
34
|
+
"@types/jest": "~29.5.14",
|
|
35
|
+
"@types/node": "^20.19.0",
|
|
36
36
|
"@types/supertest": "^6.0.3",
|
|
37
37
|
"jest": "~29.7.0",
|
|
38
38
|
"rimraf": "^6.0.1",
|