@trayio/tray-schema 0.0.1-beta
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/LICENSE.txt +22 -0
- package/README.md +70 -0
- package/dist/schema/LegacyTraySchemaConverter.d.ts +11 -0
- package/dist/schema/LegacyTraySchemaConverter.d.ts.map +1 -0
- package/dist/schema/LegacyTraySchemaConverter.js +61 -0
- package/dist/schema/LegacyTraySchemaConverter.unit.test.d.ts +2 -0
- package/dist/schema/LegacyTraySchemaConverter.unit.test.d.ts.map +1 -0
- package/dist/schema/LegacyTraySchemaConverter.unit.test.js +469 -0
- package/dist/schema/TJSTraySchemaGenerator.d.ts +9 -0
- package/dist/schema/TJSTraySchemaGenerator.d.ts.map +1 -0
- package/dist/schema/TJSTraySchemaGenerator.js +163 -0
- package/dist/schema/TJSTraySchemaGenerator.unit.test.d.ts +2 -0
- package/dist/schema/TJSTraySchemaGenerator.unit.test.d.ts.map +1 -0
- package/dist/schema/TJSTraySchemaGenerator.unit.test.js +432 -0
- package/dist/schema/TraySchemaGenerator.d.ts +6 -0
- package/dist/schema/TraySchemaGenerator.d.ts.map +1 -0
- package/dist/schema/TraySchemaGenerator.js +2 -0
- package/dist/schema/advanced.test.data.d.ts +14 -0
- package/dist/schema/advanced.test.data.d.ts.map +1 -0
- package/dist/schema/advanced.test.data.js +2 -0
- package/dist/schema/dataTypes.test.data.d.ts +12 -0
- package/dist/schema/dataTypes.test.data.d.ts.map +1 -0
- package/dist/schema/dataTypes.test.data.js +2 -0
- package/dist/schema/ddl.test.data.d.ts +8 -0
- package/dist/schema/ddl.test.data.d.ts.map +1 -0
- package/dist/schema/ddl.test.data.js +2 -0
- package/dist/schema/ddlAuth.test.data.d.ts +9 -0
- package/dist/schema/ddlAuth.test.data.d.ts.map +1 -0
- package/dist/schema/ddlAuth.test.data.js +2 -0
- package/dist/schema/default.test.data.d.ts +28 -0
- package/dist/schema/default.test.data.d.ts.map +1 -0
- package/dist/schema/default.test.data.js +2 -0
- package/dist/schema/enum.test.data.d.ts +17 -0
- package/dist/schema/enum.test.data.d.ts.map +1 -0
- package/dist/schema/enum.test.data.js +8 -0
- package/dist/schema/enumLabels.test.data.d.ts +20 -0
- package/dist/schema/enumLabels.test.data.d.ts.map +1 -0
- package/dist/schema/enumLabels.test.data.js +11 -0
- package/dist/schema/format.test.data.d.ts +23 -0
- package/dist/schema/format.test.data.d.ts.map +1 -0
- package/dist/schema/format.test.data.js +2 -0
- package/dist/schema/importsReusable.test.data.d.ts +9 -0
- package/dist/schema/importsReusable.test.data.d.ts.map +1 -0
- package/dist/schema/importsReusable.test.data.js +2 -0
- package/dist/schema/oneOf.test.data.d.ts +28 -0
- package/dist/schema/oneOf.test.data.d.ts.map +1 -0
- package/dist/schema/oneOf.test.data.js +2 -0
- package/dist/schema/required.test.data.d.ts +5 -0
- package/dist/schema/required.test.data.d.ts.map +1 -0
- package/dist/schema/required.test.data.js +2 -0
- package/dist/schema/reusableField.test.data.d.ts +5 -0
- package/dist/schema/reusableField.test.data.d.ts.map +1 -0
- package/dist/schema/reusableField.test.data.js +2 -0
- package/dist/schema/reusableSchema.test.data.d.ts +5 -0
- package/dist/schema/reusableSchema.test.data.d.ts.map +1 -0
- package/dist/schema/reusableSchema.test.data.js +2 -0
- package/package.json +30 -0
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.TJSTraySchemaGenerator = void 0;
|
|
27
|
+
const path_1 = require("path");
|
|
28
|
+
const TJS = __importStar(require("typescript-json-schema"));
|
|
29
|
+
const Either_1 = require("fp-ts/Either");
|
|
30
|
+
const lodash_1 = require("lodash");
|
|
31
|
+
class TJSTraySchemaGenerator {
|
|
32
|
+
generateFromType(type, path) {
|
|
33
|
+
const settings = {
|
|
34
|
+
// this will prevent creating references in the schema when references are used in the types
|
|
35
|
+
ref: false,
|
|
36
|
+
// adds support for DDLs and more in the JSdoc annotations
|
|
37
|
+
validationKeywords: [
|
|
38
|
+
'lookupInput',
|
|
39
|
+
'lookupOperation',
|
|
40
|
+
'lookupAuthRequired',
|
|
41
|
+
'enumLabels',
|
|
42
|
+
'advanced',
|
|
43
|
+
],
|
|
44
|
+
// adds a required array to every entitiy in the schema which describes which children are required
|
|
45
|
+
required: true,
|
|
46
|
+
// adds "additionalProperties: false" to the schemas
|
|
47
|
+
noExtraProps: true,
|
|
48
|
+
};
|
|
49
|
+
const compilerOptions = {
|
|
50
|
+
strictNullChecks: true,
|
|
51
|
+
esModuleInterop: true,
|
|
52
|
+
// skip checking .d.ts files for type checking to speed up compilation
|
|
53
|
+
skipLibCheck: true,
|
|
54
|
+
// don't emit output files; just produce the schema
|
|
55
|
+
noEmit: true,
|
|
56
|
+
};
|
|
57
|
+
const program = TJS.getProgramFromFiles([(0, path_1.resolve)(path)], compilerOptions);
|
|
58
|
+
const schema = TJS.generateSchema(program, type, settings);
|
|
59
|
+
if (schema !== null) {
|
|
60
|
+
const traySchema = this.jsonSchemaToTraySchema(schema);
|
|
61
|
+
return (0, Either_1.right)(traySchema);
|
|
62
|
+
}
|
|
63
|
+
return (0, Either_1.left)(new Error('Failed to generate JSON schema from type'));
|
|
64
|
+
}
|
|
65
|
+
jsonSchemaToTraySchema(schema) {
|
|
66
|
+
const transformLookup = (object) => {
|
|
67
|
+
if (object.lookupOperation && object.lookupInput) {
|
|
68
|
+
object.lookup = {
|
|
69
|
+
operation: object.lookupOperation,
|
|
70
|
+
input: object.lookupInput,
|
|
71
|
+
authRequired: object.lookupAuthRequired,
|
|
72
|
+
};
|
|
73
|
+
delete object.lookupOperation;
|
|
74
|
+
delete object.lookupInput;
|
|
75
|
+
delete object.lookupAuthRequired;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
const transformEnumLabels = (object) => {
|
|
79
|
+
if (object.enumLabels && typeof object.enumLabels === 'string') {
|
|
80
|
+
const enumLabelsString = object.enumLabels;
|
|
81
|
+
const enumLabelsArray = enumLabelsString.split(', ');
|
|
82
|
+
object.enumLabels = enumLabelsArray;
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
const generateEnumLabels = (object) => {
|
|
86
|
+
if (object.enum && !object.enumLabels && Array.isArray(object.enum)) {
|
|
87
|
+
const enumLabels = [];
|
|
88
|
+
const enums = object.enum;
|
|
89
|
+
enums.forEach((enumItem) => {
|
|
90
|
+
if (typeof enumItem === 'string') {
|
|
91
|
+
enumLabels.push((0, lodash_1.startCase)((0, lodash_1.camelCase)(enumItem)));
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
enumLabels.push(enumItem.toString());
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
object.enum = enums;
|
|
98
|
+
object.enumLabels = enumLabels;
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
const transformOneOf = (object) => {
|
|
102
|
+
if (object.anyOf) {
|
|
103
|
+
object.oneOf = object.anyOf;
|
|
104
|
+
delete object.anyOf;
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
const transformAllOf = (object) => {
|
|
108
|
+
if (object.allOf && Array.isArray(object.allOf)) {
|
|
109
|
+
object.allOf.forEach((part) => {
|
|
110
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
111
|
+
for (const [key, value] of Object.entries(part)) {
|
|
112
|
+
if (Array.isArray(value)) {
|
|
113
|
+
if (!object[key]) {
|
|
114
|
+
object[key] = value;
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
object[key] = [
|
|
118
|
+
...object[key],
|
|
119
|
+
...value,
|
|
120
|
+
];
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
else if (typeof value === 'object' && value !== null) {
|
|
124
|
+
object[key] = Object.assign(Object.assign({}, object[key]), value);
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
object[key] = value;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
delete object.allOf;
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
const transformAdvanced = (object) => {
|
|
135
|
+
if (Object.prototype.hasOwnProperty.call(object, 'properties') &&
|
|
136
|
+
object.properties !== null) {
|
|
137
|
+
object.advanced = [];
|
|
138
|
+
for (const [key, value] of Object.entries(object.properties)) {
|
|
139
|
+
if (value.advanced === true) {
|
|
140
|
+
object.advanced.push(key);
|
|
141
|
+
}
|
|
142
|
+
delete value.advanced;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
const recursiveObjectSearch = (object) => {
|
|
147
|
+
transformAllOf(object);
|
|
148
|
+
transformAdvanced(object);
|
|
149
|
+
for (const [key, value] of Object.entries(object)) {
|
|
150
|
+
if (typeof value === 'object' && value !== null) {
|
|
151
|
+
transformLookup(value);
|
|
152
|
+
transformEnumLabels(value);
|
|
153
|
+
generateEnumLabels(value);
|
|
154
|
+
transformOneOf(value);
|
|
155
|
+
recursiveObjectSearch(value);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
recursiveObjectSearch(schema);
|
|
160
|
+
return schema;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
exports.TJSTraySchemaGenerator = TJSTraySchemaGenerator;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TJSTraySchemaGenerator.unit.test.d.ts","sourceRoot":"","sources":["../../src/schema/TJSTraySchemaGenerator.unit.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,432 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
const E = __importStar(require("fp-ts/Either"));
|
|
27
|
+
const path_1 = require("path");
|
|
28
|
+
const TJSTraySchemaGenerator_1 = require("./TJSTraySchemaGenerator");
|
|
29
|
+
describe('TJSTraySchemaGenerator', () => {
|
|
30
|
+
it('Generates data types', () => {
|
|
31
|
+
const type = 'Input';
|
|
32
|
+
const dirPath = __dirname;
|
|
33
|
+
const path = (0, path_1.join)(dirPath, 'dataTypes.test.data.ts');
|
|
34
|
+
const result = new TJSTraySchemaGenerator_1.TJSTraySchemaGenerator().generateFromType(type, path);
|
|
35
|
+
const actual = E.getOrElse((error) => {
|
|
36
|
+
console.warn(error);
|
|
37
|
+
throw new Error(`Failed to generate JSON schema from type`);
|
|
38
|
+
})(result);
|
|
39
|
+
const expected = {
|
|
40
|
+
type: 'object',
|
|
41
|
+
properties: {
|
|
42
|
+
str: { type: 'string' },
|
|
43
|
+
num: { type: 'number' },
|
|
44
|
+
int: { type: 'integer' },
|
|
45
|
+
bool: { type: 'boolean' },
|
|
46
|
+
obj: {
|
|
47
|
+
type: 'object',
|
|
48
|
+
properties: {},
|
|
49
|
+
advanced: [],
|
|
50
|
+
additionalProperties: true,
|
|
51
|
+
},
|
|
52
|
+
arr: {
|
|
53
|
+
type: 'array',
|
|
54
|
+
items: {
|
|
55
|
+
type: 'object',
|
|
56
|
+
properties: {},
|
|
57
|
+
advanced: [],
|
|
58
|
+
additionalProperties: true,
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
advanced: [],
|
|
63
|
+
required: ['arr', 'bool', 'int', 'num', 'obj', 'str'],
|
|
64
|
+
additionalProperties: false,
|
|
65
|
+
$schema: 'http://json-schema.org/draft-07/schema#',
|
|
66
|
+
};
|
|
67
|
+
expect(actual).toEqual(expected);
|
|
68
|
+
});
|
|
69
|
+
it('Generates reusable parts of the schema by importing types and using typescript intersections', () => {
|
|
70
|
+
const type = 'Input';
|
|
71
|
+
const dirPath = __dirname;
|
|
72
|
+
const path = (0, path_1.join)(dirPath, 'importsReusable.test.data.ts');
|
|
73
|
+
const result = new TJSTraySchemaGenerator_1.TJSTraySchemaGenerator().generateFromType(type, path);
|
|
74
|
+
const actual = E.getOrElse((error) => {
|
|
75
|
+
console.warn(error);
|
|
76
|
+
throw new Error(`Failed to generate JSON schema from type`);
|
|
77
|
+
})(result);
|
|
78
|
+
const expected = {
|
|
79
|
+
type: 'object',
|
|
80
|
+
properties: {
|
|
81
|
+
specificField: { type: 'string' },
|
|
82
|
+
reusableFields: {
|
|
83
|
+
type: 'object',
|
|
84
|
+
properties: {
|
|
85
|
+
reusableFieldA: { type: 'number' },
|
|
86
|
+
reusableFieldB: { type: 'string' },
|
|
87
|
+
},
|
|
88
|
+
advanced: [],
|
|
89
|
+
required: ['reusableFieldA', 'reusableFieldB'],
|
|
90
|
+
additionalProperties: false,
|
|
91
|
+
},
|
|
92
|
+
reusableSchemaA: { type: 'number' },
|
|
93
|
+
reusableSchemaB: { type: 'string' },
|
|
94
|
+
},
|
|
95
|
+
advanced: [],
|
|
96
|
+
required: [
|
|
97
|
+
'reusableFields',
|
|
98
|
+
'reusableSchemaA',
|
|
99
|
+
'reusableSchemaB',
|
|
100
|
+
'specificField',
|
|
101
|
+
],
|
|
102
|
+
additionalProperties: false,
|
|
103
|
+
$schema: 'http://json-schema.org/draft-07/schema#',
|
|
104
|
+
};
|
|
105
|
+
expect(actual).toEqual(expected);
|
|
106
|
+
});
|
|
107
|
+
it('Generates oneOfs and nested oneOfs from typescript union types by converting anyOfs during post processing', () => {
|
|
108
|
+
const type = 'Input';
|
|
109
|
+
const dirPath = __dirname;
|
|
110
|
+
const path = (0, path_1.join)(dirPath, 'oneOf.test.data.ts');
|
|
111
|
+
const result = new TJSTraySchemaGenerator_1.TJSTraySchemaGenerator().generateFromType(type, path);
|
|
112
|
+
const actual = E.getOrElse((error) => {
|
|
113
|
+
console.warn(error);
|
|
114
|
+
throw new Error(`Failed to generate JSON schema from type`);
|
|
115
|
+
})(result);
|
|
116
|
+
const expected = {
|
|
117
|
+
type: 'object',
|
|
118
|
+
properties: {
|
|
119
|
+
elements: {
|
|
120
|
+
title: 'Elements',
|
|
121
|
+
description: 'Add images or text to the context. Maximum number of items is 10.',
|
|
122
|
+
type: 'array',
|
|
123
|
+
items: {
|
|
124
|
+
oneOf: [
|
|
125
|
+
{
|
|
126
|
+
type: 'object',
|
|
127
|
+
properties: {
|
|
128
|
+
src: {
|
|
129
|
+
oneOf: [
|
|
130
|
+
{
|
|
131
|
+
type: 'object',
|
|
132
|
+
properties: {
|
|
133
|
+
path: { type: 'string' },
|
|
134
|
+
metadata: { type: 'string' },
|
|
135
|
+
},
|
|
136
|
+
advanced: [],
|
|
137
|
+
required: ['metadata', 'path'],
|
|
138
|
+
additionalProperties: false,
|
|
139
|
+
},
|
|
140
|
+
{
|
|
141
|
+
type: 'object',
|
|
142
|
+
properties: { imgId: { type: 'number' } },
|
|
143
|
+
advanced: [],
|
|
144
|
+
required: ['imgId'],
|
|
145
|
+
additionalProperties: false,
|
|
146
|
+
},
|
|
147
|
+
],
|
|
148
|
+
},
|
|
149
|
+
},
|
|
150
|
+
advanced: [],
|
|
151
|
+
required: ['src'],
|
|
152
|
+
additionalProperties: false,
|
|
153
|
+
},
|
|
154
|
+
{
|
|
155
|
+
type: 'object',
|
|
156
|
+
properties: {
|
|
157
|
+
msg: {
|
|
158
|
+
title: 'myText',
|
|
159
|
+
description: 'the text.',
|
|
160
|
+
type: 'string',
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
|
+
advanced: [],
|
|
164
|
+
required: ['msg'],
|
|
165
|
+
additionalProperties: false,
|
|
166
|
+
},
|
|
167
|
+
],
|
|
168
|
+
},
|
|
169
|
+
},
|
|
170
|
+
},
|
|
171
|
+
advanced: [],
|
|
172
|
+
required: ['elements'],
|
|
173
|
+
additionalProperties: false,
|
|
174
|
+
$schema: 'http://json-schema.org/draft-07/schema#',
|
|
175
|
+
};
|
|
176
|
+
expect(actual).toEqual(expected);
|
|
177
|
+
});
|
|
178
|
+
it('Generates enums and enumLabels', () => {
|
|
179
|
+
const type = 'Input';
|
|
180
|
+
const dirPath = __dirname;
|
|
181
|
+
const path = (0, path_1.join)(dirPath, 'enum.test.data.ts');
|
|
182
|
+
const result = new TJSTraySchemaGenerator_1.TJSTraySchemaGenerator().generateFromType(type, path);
|
|
183
|
+
const actual = E.getOrElse((error) => {
|
|
184
|
+
console.warn(error);
|
|
185
|
+
throw new Error(`Failed to generate JSON schema from type`);
|
|
186
|
+
})(result);
|
|
187
|
+
const expected = {
|
|
188
|
+
type: 'object',
|
|
189
|
+
properties: {
|
|
190
|
+
actions: {
|
|
191
|
+
description: 'example dropdown',
|
|
192
|
+
type: 'array',
|
|
193
|
+
items: {
|
|
194
|
+
type: 'object',
|
|
195
|
+
properties: {
|
|
196
|
+
text: { type: 'string' },
|
|
197
|
+
type: {
|
|
198
|
+
enumLabels: ['Default Always', 'The Danger', 'Primary One'],
|
|
199
|
+
enum: ['default always', 'the_danger', 'primary-one'],
|
|
200
|
+
type: 'string',
|
|
201
|
+
},
|
|
202
|
+
},
|
|
203
|
+
advanced: [],
|
|
204
|
+
required: ['text', 'type'],
|
|
205
|
+
additionalProperties: false,
|
|
206
|
+
},
|
|
207
|
+
},
|
|
208
|
+
},
|
|
209
|
+
advanced: [],
|
|
210
|
+
required: ['actions'],
|
|
211
|
+
additionalProperties: false,
|
|
212
|
+
$schema: 'http://json-schema.org/draft-07/schema#',
|
|
213
|
+
};
|
|
214
|
+
expect(actual).toEqual(expected);
|
|
215
|
+
});
|
|
216
|
+
it('Generates enums and custom enumLabels', () => {
|
|
217
|
+
const type = 'Input';
|
|
218
|
+
const dirPath = __dirname;
|
|
219
|
+
const path = (0, path_1.join)(dirPath, 'enumLabels.test.data.ts');
|
|
220
|
+
const result = new TJSTraySchemaGenerator_1.TJSTraySchemaGenerator().generateFromType(type, path);
|
|
221
|
+
const actual = E.getOrElse((error) => {
|
|
222
|
+
console.warn(error);
|
|
223
|
+
throw new Error(`Failed to generate JSON schema from type`);
|
|
224
|
+
})(result);
|
|
225
|
+
const expected = {
|
|
226
|
+
type: 'object',
|
|
227
|
+
properties: {
|
|
228
|
+
actions: {
|
|
229
|
+
description: 'example dropdown',
|
|
230
|
+
type: 'array',
|
|
231
|
+
items: {
|
|
232
|
+
type: 'object',
|
|
233
|
+
properties: {
|
|
234
|
+
text: { type: 'string' },
|
|
235
|
+
type: {
|
|
236
|
+
enumLabels: ['The Default', 'The Danger', 'The Primary'],
|
|
237
|
+
enum: ['default', 'danger', 'primary'],
|
|
238
|
+
type: 'string',
|
|
239
|
+
},
|
|
240
|
+
},
|
|
241
|
+
advanced: [],
|
|
242
|
+
required: ['text', 'type'],
|
|
243
|
+
additionalProperties: false,
|
|
244
|
+
},
|
|
245
|
+
},
|
|
246
|
+
},
|
|
247
|
+
advanced: [],
|
|
248
|
+
required: ['actions'],
|
|
249
|
+
additionalProperties: false,
|
|
250
|
+
$schema: 'http://json-schema.org/draft-07/schema#',
|
|
251
|
+
};
|
|
252
|
+
expect(actual).toEqual(expected);
|
|
253
|
+
});
|
|
254
|
+
it('Generates DDL lookup format during post processing', () => {
|
|
255
|
+
const type = 'Input';
|
|
256
|
+
const dirPath = __dirname;
|
|
257
|
+
const path = (0, path_1.join)(dirPath, 'ddl.test.data.ts');
|
|
258
|
+
const result = new TJSTraySchemaGenerator_1.TJSTraySchemaGenerator().generateFromType(type, path);
|
|
259
|
+
const actual = E.getOrElse((error) => {
|
|
260
|
+
console.warn(error);
|
|
261
|
+
throw new Error(`Failed to generate JSON schema from type`);
|
|
262
|
+
})(result);
|
|
263
|
+
const expected = {
|
|
264
|
+
type: 'object',
|
|
265
|
+
properties: {
|
|
266
|
+
emails: {
|
|
267
|
+
type: 'string',
|
|
268
|
+
lookup: {
|
|
269
|
+
operation: 'list_users_actual_ddl',
|
|
270
|
+
input: {
|
|
271
|
+
include_private_channels: false,
|
|
272
|
+
workspace_id: '{{{workspace_id}}}',
|
|
273
|
+
},
|
|
274
|
+
},
|
|
275
|
+
},
|
|
276
|
+
},
|
|
277
|
+
advanced: [],
|
|
278
|
+
required: ['emails'],
|
|
279
|
+
additionalProperties: false,
|
|
280
|
+
$schema: 'http://json-schema.org/draft-07/schema#',
|
|
281
|
+
};
|
|
282
|
+
expect(actual).toEqual(expected);
|
|
283
|
+
});
|
|
284
|
+
it('Generates DDL lookup format including authRequired during post processing', () => {
|
|
285
|
+
const type = 'Input';
|
|
286
|
+
const dirPath = __dirname;
|
|
287
|
+
const path = (0, path_1.join)(dirPath, 'ddlAuth.test.data.ts');
|
|
288
|
+
const result = new TJSTraySchemaGenerator_1.TJSTraySchemaGenerator().generateFromType(type, path);
|
|
289
|
+
const actual = E.getOrElse((error) => {
|
|
290
|
+
console.warn(error);
|
|
291
|
+
throw new Error(`Failed to generate JSON schema from type`);
|
|
292
|
+
})(result);
|
|
293
|
+
const expected = {
|
|
294
|
+
type: 'object',
|
|
295
|
+
properties: {
|
|
296
|
+
emails: {
|
|
297
|
+
type: 'string',
|
|
298
|
+
lookup: {
|
|
299
|
+
operation: 'list_users_actual_ddl',
|
|
300
|
+
input: {
|
|
301
|
+
include_private_channels: false,
|
|
302
|
+
workspace_id: '{{{workspace_id}}}',
|
|
303
|
+
},
|
|
304
|
+
authRequired: true,
|
|
305
|
+
},
|
|
306
|
+
},
|
|
307
|
+
},
|
|
308
|
+
advanced: [],
|
|
309
|
+
required: ['emails'],
|
|
310
|
+
additionalProperties: false,
|
|
311
|
+
$schema: 'http://json-schema.org/draft-07/schema#',
|
|
312
|
+
};
|
|
313
|
+
expect(actual).toEqual(expected);
|
|
314
|
+
});
|
|
315
|
+
it('Generates required fields array based on typescripts optional (?) symbol', () => {
|
|
316
|
+
const type = 'Input';
|
|
317
|
+
const dirPath = __dirname;
|
|
318
|
+
const path = (0, path_1.join)(dirPath, 'required.test.data.ts');
|
|
319
|
+
const result = new TJSTraySchemaGenerator_1.TJSTraySchemaGenerator().generateFromType(type, path);
|
|
320
|
+
const actual = E.getOrElse((error) => {
|
|
321
|
+
console.warn(error);
|
|
322
|
+
throw new Error(`Failed to generate JSON schema from type`);
|
|
323
|
+
})(result);
|
|
324
|
+
const expected = {
|
|
325
|
+
type: 'object',
|
|
326
|
+
properties: {
|
|
327
|
+
mandatoryField: { type: 'string' },
|
|
328
|
+
optionalField: { type: 'string' },
|
|
329
|
+
},
|
|
330
|
+
advanced: [],
|
|
331
|
+
required: ['mandatoryField'],
|
|
332
|
+
additionalProperties: false,
|
|
333
|
+
$schema: 'http://json-schema.org/draft-07/schema#',
|
|
334
|
+
};
|
|
335
|
+
expect(actual).toEqual(expected);
|
|
336
|
+
});
|
|
337
|
+
it('Generates format properties from JSdoc annotations', () => {
|
|
338
|
+
const type = 'Input';
|
|
339
|
+
const dirPath = __dirname;
|
|
340
|
+
const path = (0, path_1.join)(dirPath, 'format.test.data.ts');
|
|
341
|
+
const result = new TJSTraySchemaGenerator_1.TJSTraySchemaGenerator().generateFromType(type, path);
|
|
342
|
+
const actual = E.getOrElse((error) => {
|
|
343
|
+
console.warn(error);
|
|
344
|
+
throw new Error(`Failed to generate JSON schema from type`);
|
|
345
|
+
})(result);
|
|
346
|
+
const expected = {
|
|
347
|
+
type: 'object',
|
|
348
|
+
properties: {
|
|
349
|
+
timestamp: { format: 'datetime', type: 'string' },
|
|
350
|
+
html: { format: 'code', type: 'string' },
|
|
351
|
+
longNotes: { format: 'text', type: 'string' },
|
|
352
|
+
password: { format: 'password', type: 'string' },
|
|
353
|
+
file: { format: 'file', type: 'string' },
|
|
354
|
+
},
|
|
355
|
+
advanced: [],
|
|
356
|
+
required: ['file', 'html', 'longNotes', 'password', 'timestamp'],
|
|
357
|
+
additionalProperties: false,
|
|
358
|
+
$schema: 'http://json-schema.org/draft-07/schema#',
|
|
359
|
+
};
|
|
360
|
+
expect(actual).toEqual(expected);
|
|
361
|
+
});
|
|
362
|
+
it('Generates default properties from JSdoc annotations', () => {
|
|
363
|
+
const type = 'Input';
|
|
364
|
+
const dirPath = __dirname;
|
|
365
|
+
const path = (0, path_1.join)(dirPath, 'default.test.data.ts');
|
|
366
|
+
const result = new TJSTraySchemaGenerator_1.TJSTraySchemaGenerator().generateFromType(type, path);
|
|
367
|
+
const actual = E.getOrElse((error) => {
|
|
368
|
+
console.warn(error);
|
|
369
|
+
throw new Error(`Failed to generate JSON schema from type`);
|
|
370
|
+
})(result);
|
|
371
|
+
const expected = {
|
|
372
|
+
type: 'object',
|
|
373
|
+
properties: {
|
|
374
|
+
str: { default: 'string default', type: 'string' },
|
|
375
|
+
num: { default: 0.1, type: 'number' },
|
|
376
|
+
int: { type: 'integer', default: 1 },
|
|
377
|
+
bool: { default: true, type: 'boolean' },
|
|
378
|
+
obj: {
|
|
379
|
+
default: { default: true },
|
|
380
|
+
type: 'object',
|
|
381
|
+
properties: {},
|
|
382
|
+
advanced: [],
|
|
383
|
+
additionalProperties: true,
|
|
384
|
+
},
|
|
385
|
+
arr: {
|
|
386
|
+
default: ['default'],
|
|
387
|
+
type: 'array',
|
|
388
|
+
items: {
|
|
389
|
+
type: 'object',
|
|
390
|
+
properties: {},
|
|
391
|
+
advanced: [],
|
|
392
|
+
additionalProperties: true,
|
|
393
|
+
},
|
|
394
|
+
},
|
|
395
|
+
},
|
|
396
|
+
advanced: [],
|
|
397
|
+
required: ['arr', 'bool', 'int', 'num', 'obj', 'str'],
|
|
398
|
+
additionalProperties: false,
|
|
399
|
+
$schema: 'http://json-schema.org/draft-07/schema#',
|
|
400
|
+
};
|
|
401
|
+
expect(actual).toEqual(expected);
|
|
402
|
+
});
|
|
403
|
+
it('Generates advanced fields array', () => {
|
|
404
|
+
const type = 'Input';
|
|
405
|
+
const dirPath = __dirname;
|
|
406
|
+
const path = (0, path_1.join)(dirPath, 'advanced.test.data.ts');
|
|
407
|
+
const result = new TJSTraySchemaGenerator_1.TJSTraySchemaGenerator().generateFromType(type, path);
|
|
408
|
+
const actual = E.getOrElse((error) => {
|
|
409
|
+
console.warn(error);
|
|
410
|
+
throw new Error(`Failed to generate JSON schema from type`);
|
|
411
|
+
})(result);
|
|
412
|
+
const expected = {
|
|
413
|
+
type: 'object',
|
|
414
|
+
properties: {
|
|
415
|
+
advancedField: { type: 'string' },
|
|
416
|
+
normalField: {
|
|
417
|
+
type: 'object',
|
|
418
|
+
properties: {
|
|
419
|
+
normalField: { type: 'string' },
|
|
420
|
+
advancedField: { type: 'string' },
|
|
421
|
+
},
|
|
422
|
+
advanced: ['advancedField'],
|
|
423
|
+
additionalProperties: false,
|
|
424
|
+
},
|
|
425
|
+
},
|
|
426
|
+
advanced: ['advancedField'],
|
|
427
|
+
additionalProperties: false,
|
|
428
|
+
$schema: 'http://json-schema.org/draft-07/schema#',
|
|
429
|
+
};
|
|
430
|
+
expect(actual).toEqual(expected);
|
|
431
|
+
});
|
|
432
|
+
});
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Either } from 'fp-ts/lib/Either';
|
|
2
|
+
import { DynamicObject } from '@trayio/commons/dynamictype/DynamicType';
|
|
3
|
+
export interface TraySchemaGenerator {
|
|
4
|
+
generateFromType(type: string, path: string): Either<Error, DynamicObject>;
|
|
5
|
+
}
|
|
6
|
+
//# sourceMappingURL=TraySchemaGenerator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TraySchemaGenerator.d.ts","sourceRoot":"","sources":["../../src/schema/TraySchemaGenerator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC1C,OAAO,EAAE,aAAa,EAAE,MAAM,yCAAyC,CAAC;AAExE,MAAM,WAAW,mBAAmB;IACnC,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC,KAAK,EAAE,aAAa,CAAC,CAAC;CAC3E"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"advanced.test.data.d.ts","sourceRoot":"","sources":["../../src/schema/advanced.test.data.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,KAAK,GAAG;IACnB;;OAEG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,WAAW,CAAC,EAAE;QACb;;WAEG;QACH,aAAa,CAAC,EAAE,MAAM,CAAC;QACvB,WAAW,CAAC,EAAE,MAAM,CAAC;KACrB,CAAC;CACF,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dataTypes.test.data.d.ts","sourceRoot":"","sources":["../../src/schema/dataTypes.test.data.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,KAAK,GAAG;IACnB,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,OAAO,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,EAAE,CAAC;CACd,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ddl.test.data.d.ts","sourceRoot":"","sources":["../../src/schema/ddl.test.data.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,KAAK,GAAG;IACnB;;;OAGG;IACH,MAAM,EAAE,MAAM,CAAC;CACf,CAAC"}
|