@trayio/tray-schema 3.13.2-unstable → 3.13.3-unstable

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,9 +1,8 @@
1
- import * as TJS from 'typescript-json-schema';
2
1
  import { Either } from 'fp-ts/Either';
3
2
  import { DynamicObject } from '@trayio/commons/dynamictype/DynamicType';
4
3
  import { TraySchemaGenerator } from './TraySchemaGenerator';
5
4
  export declare class TJSTraySchemaGenerator implements TraySchemaGenerator {
6
5
  generateFromType(type: string, path: string): Either<Error, DynamicObject>;
7
- jsonSchemaToTraySchema(schema: TJS.Definition): TJS.Definition;
6
+ jsonSchemaToTraySchema(schema: DynamicObject): DynamicObject;
8
7
  }
9
8
  //# sourceMappingURL=TJSTraySchemaGenerator.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"TJSTraySchemaGenerator.d.ts","sourceRoot":"","sources":["../../src/schema/TJSTraySchemaGenerator.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,GAAG,MAAM,wBAAwB,CAAC;AAC9C,OAAO,EAAE,MAAM,EAAe,MAAM,cAAc,CAAC;AAEnD,OAAO,EAEN,aAAa,EAEb,MAAM,yCAAyC,CAAC;AACjD,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAE5D,qBAAa,sBAAuB,YAAW,mBAAmB;IACjE,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC,KAAK,EAAE,aAAa,CAAC;IAuC1E,sBAAsB,CAAC,MAAM,EAAE,GAAG,CAAC,UAAU,GAAG,GAAG,CAAC,UAAU;CA0H9D"}
1
+ {"version":3,"file":"TJSTraySchemaGenerator.d.ts","sourceRoot":"","sources":["../../src/schema/TJSTraySchemaGenerator.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,EAAe,MAAM,cAAc,CAAC;AAEnD,OAAO,EAEN,aAAa,EAEb,MAAM,yCAAyC,CAAC;AAEjD,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAE5D,qBAAa,sBAAuB,YAAW,mBAAmB;IACjE,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC,KAAK,EAAE,aAAa,CAAC;IA2C1E,sBAAsB,CAAC,MAAM,EAAE,aAAa,GAAG,aAAa;CA4J5D"}
@@ -28,6 +28,7 @@ const path_1 = require("path");
28
28
  const TJS = __importStar(require("typescript-json-schema"));
29
29
  const Either_1 = require("fp-ts/Either");
30
30
  const lodash_1 = require("lodash");
31
+ const Try_1 = require("@trayio/commons/try/Try");
31
32
  class TJSTraySchemaGenerator {
32
33
  generateFromType(type, path) {
33
34
  const settings = {
@@ -41,11 +42,14 @@ class TJSTraySchemaGenerator {
41
42
  'enumLabels',
42
43
  'advanced',
43
44
  'dropdown',
45
+ 'dynamicSchemaReference',
44
46
  ],
45
47
  // adds a required array to every entitiy in the schema which describes which children are required
46
48
  required: true,
47
49
  // adds "additionalProperties: false" to the schemas
48
50
  noExtraProps: true,
51
+ // allows for using literal/const types by converting them to single element enums in json schema
52
+ constAsEnum: true,
49
53
  };
50
54
  const compilerOptions = {
51
55
  strictNullChecks: true,
@@ -58,8 +62,7 @@ class TJSTraySchemaGenerator {
58
62
  const program = TJS.getProgramFromFiles([(0, path_1.resolve)(path)], compilerOptions);
59
63
  const schema = TJS.generateSchema(program, type, settings);
60
64
  if (schema !== null) {
61
- const traySchema = this.jsonSchemaToTraySchema(schema);
62
- return (0, Either_1.right)(traySchema);
65
+ return (0, Try_1.tryToEither)(() => this.jsonSchemaToTraySchema(schema));
63
66
  }
64
67
  return (0, Either_1.left)(new Error('Failed to generate JSON schema from type'));
65
68
  }
@@ -149,11 +152,14 @@ class TJSTraySchemaGenerator {
149
152
  delete object.allOf;
150
153
  }
151
154
  };
152
- const transformAdvanced = (object) => {
155
+ const transformProperties = (object) => {
153
156
  if (Object.prototype.hasOwnProperty.call(object, 'properties') &&
154
157
  object.properties !== null) {
155
158
  object.advanced = [];
156
159
  for (const [key, value] of Object.entries(object.properties)) {
160
+ if (key.startsWith('__@_brand')) {
161
+ throw new Error(`Branded types not supported, change or remove [${key}] property`);
162
+ }
157
163
  if (value.advanced === true) {
158
164
  object.advanced.push(key);
159
165
  }
@@ -161,9 +167,33 @@ class TJSTraySchemaGenerator {
161
167
  }
162
168
  }
163
169
  };
170
+ const transformSpecialTypes = (object) => {
171
+ if (Object.prototype.hasOwnProperty.call(object, '$ref')) {
172
+ throw new Error(`Recursive types not supported, make sure none of the types being used are recursive`);
173
+ }
174
+ if (Object.prototype.hasOwnProperty.call(object, 'type') &&
175
+ object.type !== null &&
176
+ typeof object.type === 'string') {
177
+ switch (object.type) {
178
+ case 'DynamicObject':
179
+ object.type = 'object';
180
+ object.tag = 'DynamicObject';
181
+ object.properties = {};
182
+ break;
183
+ case 'DynamicSchema':
184
+ object.type = 'object';
185
+ object.tag = 'DynamicSchema';
186
+ object.properties = {};
187
+ break;
188
+ default:
189
+ break;
190
+ }
191
+ }
192
+ };
164
193
  const recursiveObjectSearch = (object) => {
194
+ transformSpecialTypes(object);
165
195
  transformAllOf(object);
166
- transformAdvanced(object);
196
+ transformProperties(object);
167
197
  for (const [key, value] of Object.entries(object)) {
168
198
  if (typeof value === 'object' && value !== null) {
169
199
  transformLookup(value);
@@ -199,15 +199,25 @@ describe('TJSTraySchemaGenerator', () => {
199
199
  enum: ['default always', 'the_danger', 'primary-one'],
200
200
  type: 'string',
201
201
  },
202
+ nestedConstProp: {
203
+ type: 'string',
204
+ enum: ['NestedConstProperty'],
205
+ enumLabels: ['Nested Const Property'],
206
+ },
202
207
  },
203
208
  advanced: [],
204
- required: ['text', 'type'],
209
+ required: ['nestedConstProp', 'text', 'type'],
205
210
  additionalProperties: false,
206
211
  },
207
212
  },
213
+ constProp: {
214
+ type: 'string',
215
+ enum: ['ConstProperty'],
216
+ enumLabels: ['Const Property'],
217
+ },
208
218
  },
209
219
  advanced: [],
210
- required: ['actions'],
220
+ required: ['actions', 'constProp'],
211
221
  additionalProperties: false,
212
222
  $schema: 'http://json-schema.org/draft-07/schema#',
213
223
  };
@@ -478,4 +488,68 @@ describe('TJSTraySchemaGenerator', () => {
478
488
  };
479
489
  expect(actual).toEqual(expected);
480
490
  });
491
+ it('Adds tags to fields of special types', () => {
492
+ const type = 'Input';
493
+ const dirPath = __dirname;
494
+ const path = (0, path_1.join)(dirPath, 'tag.test.data.ts');
495
+ const result = new TJSTraySchemaGenerator_1.TJSTraySchemaGenerator().generateFromType(type, path);
496
+ const actual = E.getOrElse((error) => {
497
+ console.warn(error);
498
+ throw new Error(`Failed to generate JSON schema from type`);
499
+ })(result);
500
+ const expected = {
501
+ type: 'object',
502
+ properties: {
503
+ schema: {
504
+ type: 'object',
505
+ tag: 'DynamicSchema',
506
+ properties: {},
507
+ advanced: [],
508
+ },
509
+ obj: {
510
+ type: 'object',
511
+ dynamicSchemaReference: 'input.value',
512
+ tag: 'DynamicObject',
513
+ properties: {},
514
+ advanced: [],
515
+ },
516
+ nested: {
517
+ type: 'object',
518
+ properties: {
519
+ nestedSchema: {
520
+ type: 'object',
521
+ tag: 'DynamicSchema',
522
+ properties: {},
523
+ advanced: [],
524
+ },
525
+ nestedObj: {
526
+ type: 'object',
527
+ dynamicSchemaReference: 'input.test.value',
528
+ tag: 'DynamicObject',
529
+ properties: {},
530
+ advanced: [],
531
+ },
532
+ nestedConstProp: {
533
+ type: 'string',
534
+ enum: ['NestedConstProperty'],
535
+ enumLabels: ['Nested Const Property'],
536
+ },
537
+ },
538
+ additionalProperties: false,
539
+ required: ['nestedConstProp', 'nestedObj', 'nestedSchema'],
540
+ advanced: [],
541
+ },
542
+ constProp: {
543
+ type: 'string',
544
+ enum: ['ConstProperty'],
545
+ enumLabels: ['Const Property'],
546
+ },
547
+ },
548
+ additionalProperties: false,
549
+ required: ['constProp', 'nested', 'obj', 'schema'],
550
+ $schema: 'http://json-schema.org/draft-07/schema#',
551
+ advanced: [],
552
+ };
553
+ expect(actual).toEqual(expected);
554
+ });
481
555
  });
@@ -3,10 +3,12 @@ export type Input = {
3
3
  * @description example dropdown
4
4
  */
5
5
  actions: Action[];
6
+ constProp: 'ConstProperty';
6
7
  };
7
8
  type Action = {
8
9
  text: string;
9
10
  type: ActionType;
11
+ nestedConstProp: 'NestedConstProperty';
10
12
  };
11
13
  declare enum ActionType {
12
14
  default = "default always",
@@ -1 +1 @@
1
- {"version":3,"file":"enum.test.data.d.ts","sourceRoot":"","sources":["../../src/schema/enum.test.data.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,KAAK,GAAG;IACnB;;OAEG;IACH,OAAO,EAAE,MAAM,EAAE,CAAC;CAClB,CAAC;AAEF,KAAK,MAAM,GAAG;IACb,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,UAAU,CAAC;CACjB,CAAC;AAEF,aAAK,UAAU;IACd,OAAO,mBAAmB;IAC1B,MAAM,eAAe;IACrB,OAAO,gBAAgB;CACvB"}
1
+ {"version":3,"file":"enum.test.data.d.ts","sourceRoot":"","sources":["../../src/schema/enum.test.data.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,KAAK,GAAG;IACnB;;OAEG;IACH,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,SAAS,EAAE,eAAe,CAAC;CAC3B,CAAC;AAEF,KAAK,MAAM,GAAG;IACb,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,UAAU,CAAC;IACjB,eAAe,EAAE,qBAAqB,CAAC;CACvC,CAAC;AAEF,aAAK,UAAU;IACd,OAAO,mBAAmB;IAC1B,MAAM,eAAe;IACrB,OAAO,gBAAgB;CACvB"}
@@ -0,0 +1,27 @@
1
+ import { DynamicObject } from '@trayio/commons/dynamictype/DynamicType';
2
+ export type Input = {
3
+ /**
4
+ * @TJS-type DynamicSchema
5
+ */
6
+ schema: DynamicObject;
7
+ /**
8
+ * @TJS-type DynamicObject
9
+ * @dynamicSchemaReference input.value
10
+ */
11
+ obj: DynamicObject;
12
+ nested: Nested;
13
+ constProp: 'ConstProperty';
14
+ };
15
+ export type Nested = {
16
+ /**
17
+ * @TJS-type DynamicSchema
18
+ */
19
+ nestedSchema: DynamicObject;
20
+ /**
21
+ * @TJS-type DynamicObject
22
+ * @dynamicSchemaReference input.test.value
23
+ */
24
+ nestedObj: DynamicObject;
25
+ nestedConstProp: 'NestedConstProperty';
26
+ };
27
+ //# sourceMappingURL=tag.test.data.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tag.test.data.d.ts","sourceRoot":"","sources":["../../src/schema/tag.test.data.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,yCAAyC,CAAC;AAExE,MAAM,MAAM,KAAK,GAAG;IACnB;;OAEG;IACH,MAAM,EAAE,aAAa,CAAC;IACtB;;;OAGG;IACH,GAAG,EAAE,aAAa,CAAC;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,eAAe,CAAC;CAC3B,CAAC;AAEF,MAAM,MAAM,MAAM,GAAG;IACpB;;OAEG;IACH,YAAY,EAAE,aAAa,CAAC;IAC5B;;;OAGG;IACH,SAAS,EAAE,aAAa,CAAC;IACzB,eAAe,EAAE,qBAAqB,CAAC;CACvC,CAAC"}
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trayio/tray-schema",
3
- "version": "3.13.2-unstable",
3
+ "version": "3.13.3-unstable",
4
4
  "description": "Schema for Tray connectors and authentications",
5
5
  "exports": {
6
6
  "./*": "./dist/*.js"
@@ -11,7 +11,7 @@
11
11
  "access": "public"
12
12
  },
13
13
  "dependencies": {
14
- "@trayio/commons": "3.13.2-unstable",
14
+ "@trayio/commons": "3.13.3-unstable",
15
15
  "@types/node": "18.11.9",
16
16
  "lodash": "4.17.21",
17
17
  "typescript-json-schema": "0.62.0"