@tsofist/schema-forge 2.5.0 → 2.6.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,7 +1,6 @@
1
1
  import '../util/patch.extended-annotations-reader';
2
2
  import { SchemaObject } from 'ajv';
3
- import { ChainNodeParser, CompletedConfig, Context, SubNodeParser, TupleType } from 'ts-json-schema-generator';
4
- import { Node, TupleTypeNode } from 'typescript';
3
+ import { CompletedConfig } from 'ts-json-schema-generator';
5
4
  import { SchemaForgeOptions } from '../types';
6
5
  import { TypeExposeKind } from './types';
7
6
  interface Options {
@@ -18,11 +17,4 @@ interface Options {
18
17
  shrinkDefinitionNames: SchemaForgeOptions['shrinkDefinitionNames'];
19
18
  }
20
19
  export declare function generateSchemaByDraftTypes(options: Options): Promise<SchemaObject>;
21
- export declare class TupleTypeParser implements SubNodeParser {
22
- protected readonly childNodeParser: ChainNodeParser;
23
- protected readonly allowUseFallbackDescription: boolean | undefined;
24
- constructor(childNodeParser: ChainNodeParser, allowUseFallbackDescription: boolean | undefined);
25
- supportsNode(node: Node): boolean;
26
- createType(node: TupleTypeNode, context: Context): TupleType;
27
- }
28
20
  export {};
@@ -1,6 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TupleTypeParser = void 0;
4
3
  exports.generateSchemaByDraftTypes = generateSchemaByDraftTypes;
5
4
  require("../util/patch.extended-annotations-reader");
6
5
  const error_1 = require("@tsofist/stem/lib/error");
@@ -59,7 +58,6 @@ async function generateSchemaByDraftTypes(options) {
59
58
  const replacement = new Set();
60
59
  // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
61
60
  for (const name of Object.keys(result.definitions)) {
62
- console.log('DEF::::', name);
63
61
  const r = options.shrinkDefinitionNames(name);
64
62
  if (r) {
65
63
  if (replacement.has(r) || r in result.definitions) {
@@ -72,11 +70,11 @@ async function generateSchemaByDraftTypes(options) {
72
70
  delete result.definitions[name];
73
71
  // rename references
74
72
  const targets = (0, jsonpath_plus_1.JSONPath)({
75
- path: `$..[?(@ && @.$ref == "#/definitions/${name}")]`,
73
+ path: `$..[?(@ && @.$ref == "#/definitions/${escapeDefinitionNameForJSONPath(name)}")]`,
76
74
  json: result,
77
75
  eval: 'safe',
78
76
  });
79
- targets.forEach((item) => {
77
+ targets?.forEach((item) => {
80
78
  item.$ref = item.$ref.replace(`#/definitions/${name}`, `#/definitions/${r}`);
81
79
  });
82
80
  }
@@ -95,6 +93,9 @@ async function generateSchemaByDraftTypes(options) {
95
93
  }).validateSchema(result, true);
96
94
  return result;
97
95
  }
96
+ function escapeDefinitionNameForJSONPath(value) {
97
+ return value.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
98
+ }
98
99
  class TupleTypeParser {
99
100
  constructor(childNodeParser, allowUseFallbackDescription) {
100
101
  Object.defineProperty(this, "childNodeParser", {
@@ -119,8 +120,6 @@ class TupleTypeParser {
119
120
  if ((0, typescript_1.isNamedTupleMember)(element)) {
120
121
  const description = (0, tsc_1.readJSDocDescription)(element, this.allowUseFallbackDescription);
121
122
  const nullable = type instanceof ts_json_schema_generator_1.AnnotatedType ? type.isNullable() : false;
122
- if (nullable)
123
- console.log(description, nullable);
124
123
  return description ? new ts_json_schema_generator_1.AnnotatedType(type, { description }, nullable) : type;
125
124
  }
126
125
  return type;
@@ -128,7 +127,6 @@ class TupleTypeParser {
128
127
  return new ts_json_schema_generator_1.TupleType(items);
129
128
  }
130
129
  }
131
- exports.TupleTypeParser = TupleTypeParser;
132
130
  class ArrayLiteralExpressionIdentifierParser {
133
131
  constructor(checker) {
134
132
  Object.defineProperty(this, "checker", {
@@ -95,7 +95,7 @@ describe('validator for a7', () => {
95
95
  expect(initial).toStrictEqual(2);
96
96
  validator.warmupCacheSync();
97
97
  const warmed = validator.compilationArtifactCount;
98
- expect(warmed).toStrictEqual(10);
98
+ expect(warmed).toStrictEqual(9);
99
99
  validator.clear();
100
100
  const cleared = validator.compilationArtifactCount;
101
101
  expect(cleared).toStrictEqual(1);
@@ -471,7 +471,8 @@ describe('generator for a1', () => {
471
471
  expect(forgeSchemaResult.refs.length).toStrictEqual(10);
472
472
  });
473
473
  it('getSchema', () => {
474
- expect(validator.getValidator('test#/definitions/Int').schema).toStrictEqual({
474
+ expect(validator.getValidator('test#/definitions/PositiveInt').schema).toStrictEqual({
475
+ minimum: 1,
475
476
  type: 'integer',
476
477
  });
477
478
  expect(validator.getValidator('#/definitions/NotExists')).toStrictEqual(undefined);
@@ -486,14 +487,16 @@ describe('generator for a1', () => {
486
487
  });
487
488
  });
488
489
  it('hasSchema', () => {
489
- expect(validator.hasValidator('test#/definitions/Int')).toStrictEqual(true);
490
+ expect(validator.hasValidator('test#/definitions/SomeType1')).toStrictEqual(true);
491
+ expect(validator.hasValidator('test#/definitions/PositiveInt')).toStrictEqual(true);
492
+ expect(validator.hasValidator('test#/definitions/Int')).toStrictEqual(false);
490
493
  expect(validator.hasValidator('test#/definitions/!Int')).toStrictEqual(false);
491
494
  });
492
495
  it('checkBySchema', () => {
493
496
  expect(() => validator.checkBySchema('test#/definitions/ExportedInterfaceB_InterfaceDeclaration', {})).toThrow(types_1.SchemaForgeValidationErrorCode);
494
- expect(validator.checkBySchema('test#/definitions/Int', 1)).toStrictEqual(true);
495
- expect(() => validator.checkBySchema('test#/definitions/Int', 1.1)).toThrow(types_1.SchemaForgeValidationErrorCode);
496
- expect(() => validator.checkBySchema('!test#/definitions/Int', 1)).toThrow(types_1.SchemaNotFoundErrorCode);
497
+ expect(validator.checkBySchema('test#/definitions/PositiveInt', 1)).toStrictEqual(true);
498
+ expect(() => validator.checkBySchema('test#/definitions/PositiveInt', 1.1)).toThrow(types_1.SchemaForgeValidationErrorCode);
499
+ expect(() => validator.checkBySchema('!test#/definitions/PositiveInt', 1)).toThrow(types_1.SchemaNotFoundErrorCode);
497
500
  {
498
501
  const schema = '!test#/definitions/Int';
499
502
  try {
@@ -510,7 +513,7 @@ describe('generator for a1', () => {
510
513
  }
511
514
  }
512
515
  {
513
- const schema = 'test#/definitions/Int';
516
+ const schema = 'test#/definitions/PositiveInt';
514
517
  const message = 'ERROR!';
515
518
  try {
516
519
  validator.checkBySchema(schema, 1.1, { errorMessage: message });
@@ -568,13 +571,6 @@ describe('generator for a1', () => {
568
571
  interface: 'ExportedInterfaceB',
569
572
  method: 'methodB',
570
573
  },
571
- {
572
- ref: 'test#/definitions/Int',
573
- kind: 0,
574
- name: 'Int',
575
- schemaId: 'test',
576
- type: 'Int',
577
- },
578
574
  {
579
575
  ref: 'test#/definitions/NonExportedInterfaceD_InterfaceDeclaration',
580
576
  kind: 1,
@@ -625,7 +621,7 @@ describe('generator for a1', () => {
625
621
  defsByName[def.name] = def;
626
622
  }
627
623
  expect(validator.listDefinitions()).toStrictEqual(defs);
628
- expect(validator.listDefinitions((info) => info.kind === types_1.SchemaDefinitionKind.Type && !info.name.startsWith('Some'))).toStrictEqual([defsByName['Int'], defsByName['PositiveInt']]);
624
+ expect(validator.listDefinitions((info) => info.kind === types_1.SchemaDefinitionKind.Type && !info.name.startsWith('Some'))).toStrictEqual([defsByName['PositiveInt']]);
629
625
  expect(validator.listDefinitions((info) => info.kind === types_1.SchemaDefinitionKind.API)).toStrictEqual([
630
626
  defsByName['ExportedInterfaceB_InterfaceDeclaration'],
631
627
  defsByName['NonExportedInterfaceD_InterfaceDeclaration'],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsofist/schema-forge",
3
- "version": "2.5.0",
3
+ "version": "2.6.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,10 +21,10 @@
21
21
  },
22
22
  "dependencies": {
23
23
  "@faker-js/faker": "^9.6.0",
24
- "@tsofist/stem": "^2.2.0",
24
+ "@tsofist/stem": "^3.0.2",
25
25
  "ajv": "^8.17.1",
26
26
  "ajv-formats": "^3.0.1",
27
- "json-schema-faker": "^0.5.8",
27
+ "json-schema-faker": "^0.5.9",
28
28
  "jsonpath-plus": "^10.3.0",
29
29
  "ts-json-schema-generator": "~2.3.0",
30
30
  "tslib": "^2.8.1"
@@ -32,13 +32,13 @@
32
32
  "devDependencies": {
33
33
  "@tsofist/web-buddy": "^1.21.0",
34
34
  "@types/jest": "^29.5.14",
35
- "@types/node": "^20.17.28",
35
+ "@types/node": "^20.17.30",
36
36
  "@types/supertest": "^6.0.3",
37
37
  "jest": "^29.7.0",
38
38
  "rimraf": "^6.0.1",
39
39
  "supertest": "^7.1.0",
40
- "ts-jest": "^29.3.0",
41
- "typescript": "~5.8.2"
40
+ "ts-jest": "^29.3.1",
41
+ "typescript": "~5.8.3"
42
42
  },
43
43
  "publishConfig": {
44
44
  "registry": "https://registry.npmjs.org/",