json-schema-to-atd 0.70.1
Sign up to get free protection for your applications and to get access to all the features.
- package/LICENSE +9 -0
- package/README.md +11 -0
- package/dist/index.cjs +302 -0
- package/dist/index.d.cts +75 -0
- package/dist/index.d.mts +75 -0
- package/dist/index.d.ts +75 -0
- package/dist/index.mjs +284 -0
- package/package.json +27 -0
package/LICENSE
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright 2024 Joshua Sosso
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
6
|
+
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
8
|
+
|
9
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
# json-schema-to-atd
|
2
|
+
|
3
|
+
This library was generated with [Nx](https://nx.dev).
|
4
|
+
|
5
|
+
## Building
|
6
|
+
|
7
|
+
Run `nx build json-schema-to-atd` to build the library.
|
8
|
+
|
9
|
+
## Running unit tests
|
10
|
+
|
11
|
+
Run `nx test json-schema-to-atd` to execute the unit tests via [Vitest](https://vitest.dev).
|
package/dist/index.cjs
ADDED
@@ -0,0 +1,302 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
const JsonSchemaScalarTypeValues = [
|
4
|
+
"integer",
|
5
|
+
"number",
|
6
|
+
"bigint",
|
7
|
+
"string",
|
8
|
+
"boolean",
|
9
|
+
"Date"
|
10
|
+
];
|
11
|
+
const JsonSchemaNullTypeValues = ["null", "undefined"];
|
12
|
+
const JsonSchemaComplexTypeValues = [
|
13
|
+
"object",
|
14
|
+
"list",
|
15
|
+
"array",
|
16
|
+
"Uint8Array"
|
17
|
+
];
|
18
|
+
function isJsonSchemaScalarType(input) {
|
19
|
+
if (typeof input !== "object" || input === null) {
|
20
|
+
return false;
|
21
|
+
}
|
22
|
+
if (!("type" in input)) {
|
23
|
+
return false;
|
24
|
+
}
|
25
|
+
return JsonSchemaScalarTypeValues.includes(input.type);
|
26
|
+
}
|
27
|
+
function isJsonSchemaNullType(input) {
|
28
|
+
if (typeof input !== "object" || input === null) {
|
29
|
+
return false;
|
30
|
+
}
|
31
|
+
if (!("type" in input)) {
|
32
|
+
return false;
|
33
|
+
}
|
34
|
+
return JsonSchemaNullTypeValues.includes(input.type);
|
35
|
+
}
|
36
|
+
const isJsonSchemaObject = (input) => {
|
37
|
+
if (typeof input !== "object") {
|
38
|
+
return false;
|
39
|
+
}
|
40
|
+
return "type" in input && input.type === "object" && "properties" in input && typeof input.properties === "object";
|
41
|
+
};
|
42
|
+
const isJsonSchemaRecord = (input) => {
|
43
|
+
if (typeof input !== "object") {
|
44
|
+
return false;
|
45
|
+
}
|
46
|
+
if ("type" in input && input.type === "object") {
|
47
|
+
return "patternProperties" in input && typeof input.patternProperties === "object" || "additionalProperties" in input && typeof input.additionalProperties === "object";
|
48
|
+
}
|
49
|
+
return false;
|
50
|
+
};
|
51
|
+
function isJsonSchemaArray(input) {
|
52
|
+
if (typeof input !== "object") {
|
53
|
+
return false;
|
54
|
+
}
|
55
|
+
if ("type" in input && input.type === "array") {
|
56
|
+
return true;
|
57
|
+
}
|
58
|
+
return false;
|
59
|
+
}
|
60
|
+
function isJsonSchemaEnum(input) {
|
61
|
+
if (typeof input !== "object") {
|
62
|
+
return false;
|
63
|
+
}
|
64
|
+
if (!("anyOf" in input) || !Array.isArray(input.anyOf)) {
|
65
|
+
return false;
|
66
|
+
}
|
67
|
+
let prevType;
|
68
|
+
for (const item of input.anyOf) {
|
69
|
+
if (!isJsonSchemaScalarType(item)) {
|
70
|
+
return false;
|
71
|
+
}
|
72
|
+
if (item.type !== "string" && item.type !== "integer" && item.type !== "number") {
|
73
|
+
return false;
|
74
|
+
}
|
75
|
+
if (!prevType) {
|
76
|
+
prevType = item.type;
|
77
|
+
} else if (prevType !== item.type) {
|
78
|
+
return false;
|
79
|
+
}
|
80
|
+
}
|
81
|
+
return true;
|
82
|
+
}
|
83
|
+
function isJsonSchemaRef(input) {
|
84
|
+
if (typeof input !== "object" || !input) {
|
85
|
+
return false;
|
86
|
+
}
|
87
|
+
return "$ref" in input && typeof input.$ref === "string";
|
88
|
+
}
|
89
|
+
|
90
|
+
function jsonSchemaToJtdSchema(input, context = { parentRefs: [] }) {
|
91
|
+
if (isJsonSchemaScalarType(input)) {
|
92
|
+
return jsonSchemaScalarToJtdScalar(input);
|
93
|
+
}
|
94
|
+
if (isJsonSchemaEnum(input)) {
|
95
|
+
return jsonSchemaEnumToJtdEnum(input);
|
96
|
+
}
|
97
|
+
if (isJsonSchemaObject(input)) {
|
98
|
+
return jsonSchemaObjectToJtdObject(input);
|
99
|
+
}
|
100
|
+
if (isJsonSchemaArray(input)) {
|
101
|
+
return jsonSchemaArrayToJtdArray(input);
|
102
|
+
}
|
103
|
+
if (isJsonSchemaRecord(input)) {
|
104
|
+
return jsonSchemaRecordToJtdRecord(input);
|
105
|
+
}
|
106
|
+
if (isJsonSchemaRef(input)) {
|
107
|
+
return jsonSchemaRefToJtdRef(input, context);
|
108
|
+
}
|
109
|
+
console.warn(
|
110
|
+
`WARNING: unable to determine type for ${input}. Falling back to "any" type.`
|
111
|
+
);
|
112
|
+
return {
|
113
|
+
metadata: {
|
114
|
+
id: input.$id ?? input.title,
|
115
|
+
description: input.description
|
116
|
+
}
|
117
|
+
};
|
118
|
+
}
|
119
|
+
function jsonSchemaEnumToJtdEnum(input, _) {
|
120
|
+
const enumTypes = input.anyOf.map((val) => val.type);
|
121
|
+
const isNotStringEnum = enumTypes.includes("integer") || enumTypes.includes("number");
|
122
|
+
if (isNotStringEnum) {
|
123
|
+
console.error(
|
124
|
+
`WARNING: Cannot convert non string enums. This key will be treated as an "any" by generated clients.`
|
125
|
+
);
|
126
|
+
const output2 = {};
|
127
|
+
return output2;
|
128
|
+
}
|
129
|
+
const output = {
|
130
|
+
enum: input.anyOf.map((val) => val.const),
|
131
|
+
metadata: {
|
132
|
+
id: input.$id ?? input.title,
|
133
|
+
description: input.description
|
134
|
+
}
|
135
|
+
};
|
136
|
+
return output;
|
137
|
+
}
|
138
|
+
function jsonSchemaScalarToJtdScalar(input, _) {
|
139
|
+
const meta = {
|
140
|
+
id: input.$id ?? input.title,
|
141
|
+
description: input.description
|
142
|
+
};
|
143
|
+
switch (input.type) {
|
144
|
+
case "Date":
|
145
|
+
return {
|
146
|
+
type: "timestamp",
|
147
|
+
nullable: input.nullable,
|
148
|
+
metadata: meta
|
149
|
+
};
|
150
|
+
case "bigint":
|
151
|
+
case "integer":
|
152
|
+
return {
|
153
|
+
type: "int32",
|
154
|
+
nullable: input.nullable,
|
155
|
+
metadata: meta
|
156
|
+
};
|
157
|
+
case "number":
|
158
|
+
return {
|
159
|
+
type: "float64",
|
160
|
+
nullable: input.nullable,
|
161
|
+
metadata: meta
|
162
|
+
};
|
163
|
+
case "boolean":
|
164
|
+
return {
|
165
|
+
type: "boolean",
|
166
|
+
nullable: input.nullable,
|
167
|
+
metadata: meta
|
168
|
+
};
|
169
|
+
case "string":
|
170
|
+
if (input.format === "date-time") {
|
171
|
+
return {
|
172
|
+
type: "timestamp",
|
173
|
+
nullable: input.nullable,
|
174
|
+
metadata: meta
|
175
|
+
};
|
176
|
+
}
|
177
|
+
return {
|
178
|
+
type: "string",
|
179
|
+
nullable: input.nullable,
|
180
|
+
metadata: meta
|
181
|
+
};
|
182
|
+
default:
|
183
|
+
return {};
|
184
|
+
}
|
185
|
+
}
|
186
|
+
function jsonSchemaObjectToJtdObject(input, _) {
|
187
|
+
const result = {
|
188
|
+
properties: {},
|
189
|
+
nullable: input.nullable,
|
190
|
+
strict: typeof input.additionalProperties === "boolean" ? !input.additionalProperties : void 0,
|
191
|
+
metadata: {
|
192
|
+
id: input.$id ?? input.title,
|
193
|
+
description: input.description
|
194
|
+
}
|
195
|
+
};
|
196
|
+
Object.keys(input.properties).forEach((key) => {
|
197
|
+
const prop = input.properties[key];
|
198
|
+
if (!prop) {
|
199
|
+
return;
|
200
|
+
}
|
201
|
+
const isOptional = !(input.required ?? []).includes(key);
|
202
|
+
if (isOptional) {
|
203
|
+
if (!result.optionalProperties) {
|
204
|
+
result.optionalProperties = {};
|
205
|
+
}
|
206
|
+
result.optionalProperties[key] = jsonSchemaToJtdSchema(prop);
|
207
|
+
return;
|
208
|
+
}
|
209
|
+
result.properties[key] = jsonSchemaToJtdSchema(prop);
|
210
|
+
});
|
211
|
+
return result;
|
212
|
+
}
|
213
|
+
function jsonSchemaArrayToJtdArray(input, _) {
|
214
|
+
const result = {
|
215
|
+
elements: jsonSchemaToJtdSchema(input.items),
|
216
|
+
nullable: input.nullable,
|
217
|
+
metadata: {
|
218
|
+
id: input.$id ?? input.title,
|
219
|
+
description: input.description
|
220
|
+
}
|
221
|
+
};
|
222
|
+
return result;
|
223
|
+
}
|
224
|
+
function jsonSchemaRecordToJtdRecord(input, _) {
|
225
|
+
if (input.additionalProperties) {
|
226
|
+
const type = jsonSchemaToJtdSchema(input.additionalProperties);
|
227
|
+
return {
|
228
|
+
values: type,
|
229
|
+
nullable: input.nullable,
|
230
|
+
metadata: {
|
231
|
+
id: input.$id ?? input.title,
|
232
|
+
description: input.description
|
233
|
+
}
|
234
|
+
};
|
235
|
+
}
|
236
|
+
const types = [];
|
237
|
+
Object.keys(input.patternProperties ?? {}).forEach((key) => {
|
238
|
+
const pattern = input.patternProperties[key];
|
239
|
+
if (!pattern) {
|
240
|
+
return;
|
241
|
+
}
|
242
|
+
types.push(jsonSchemaToJtdSchema(pattern));
|
243
|
+
});
|
244
|
+
if (types.length === 0) {
|
245
|
+
console.warn(
|
246
|
+
'WARNING: unable to determine record type values. This key will be treated as "any" by client generators.'
|
247
|
+
);
|
248
|
+
return {};
|
249
|
+
}
|
250
|
+
const result = {
|
251
|
+
values: types[0],
|
252
|
+
nullable: input.nullable,
|
253
|
+
metadata: {
|
254
|
+
id: input.$id ?? input.title,
|
255
|
+
description: input.description
|
256
|
+
}
|
257
|
+
};
|
258
|
+
return result;
|
259
|
+
}
|
260
|
+
function jsonSchemaRefToJtdRef(input, context) {
|
261
|
+
if (context.parentRefs.includes(input.$ref)) {
|
262
|
+
const parts2 = input.$ref.split("/");
|
263
|
+
const refId = parts2[parts2.length - 1];
|
264
|
+
if (!refId) return {};
|
265
|
+
return {
|
266
|
+
nullable: input.nullable,
|
267
|
+
ref: refId
|
268
|
+
};
|
269
|
+
}
|
270
|
+
const parts = input.$ref.split("/");
|
271
|
+
let subSchema = context.rootSchema ?? {};
|
272
|
+
for (const part of parts) {
|
273
|
+
if (part === "#") continue;
|
274
|
+
if (!subSchema[part]) return {};
|
275
|
+
subSchema = subSchema[part];
|
276
|
+
}
|
277
|
+
const r = context.parentRefs;
|
278
|
+
r.push(input.$ref);
|
279
|
+
const result = jsonSchemaToJtdSchema(subSchema, {
|
280
|
+
...context,
|
281
|
+
parentRefs: r
|
282
|
+
});
|
283
|
+
return result;
|
284
|
+
}
|
285
|
+
|
286
|
+
exports.JsonSchemaComplexTypeValues = JsonSchemaComplexTypeValues;
|
287
|
+
exports.JsonSchemaNullTypeValues = JsonSchemaNullTypeValues;
|
288
|
+
exports.JsonSchemaScalarTypeValues = JsonSchemaScalarTypeValues;
|
289
|
+
exports.isJsonSchemaArray = isJsonSchemaArray;
|
290
|
+
exports.isJsonSchemaEnum = isJsonSchemaEnum;
|
291
|
+
exports.isJsonSchemaNullType = isJsonSchemaNullType;
|
292
|
+
exports.isJsonSchemaObject = isJsonSchemaObject;
|
293
|
+
exports.isJsonSchemaRecord = isJsonSchemaRecord;
|
294
|
+
exports.isJsonSchemaRef = isJsonSchemaRef;
|
295
|
+
exports.isJsonSchemaScalarType = isJsonSchemaScalarType;
|
296
|
+
exports.jsonSchemaArrayToJtdArray = jsonSchemaArrayToJtdArray;
|
297
|
+
exports.jsonSchemaEnumToJtdEnum = jsonSchemaEnumToJtdEnum;
|
298
|
+
exports.jsonSchemaObjectToJtdObject = jsonSchemaObjectToJtdObject;
|
299
|
+
exports.jsonSchemaRecordToJtdRecord = jsonSchemaRecordToJtdRecord;
|
300
|
+
exports.jsonSchemaRefToJtdRef = jsonSchemaRefToJtdRef;
|
301
|
+
exports.jsonSchemaScalarToJtdScalar = jsonSchemaScalarToJtdScalar;
|
302
|
+
exports.jsonSchemaToJtdSchema = jsonSchemaToJtdSchema;
|
package/dist/index.d.cts
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
import { Schema, SchemaFormElements } from '@arrirpc/type-defs';
|
2
|
+
|
3
|
+
declare const JsonSchemaScalarTypeValues: readonly ["integer", "number", "bigint", "string", "boolean", "Date"];
|
4
|
+
declare const JsonSchemaNullTypeValues: readonly ["null", "undefined"];
|
5
|
+
declare const JsonSchemaComplexTypeValues: readonly ["object", "list", "array", "Uint8Array"];
|
6
|
+
type JsonSchemaType = JsonSchemaScalarType | JsonSchemaNullType | JsonSchemaComplexType;
|
7
|
+
type JsonSchemaTypeValue = (typeof JsonSchemaScalarTypeValues)[number] | (typeof JsonSchemaNullTypeValues)[number] | (typeof JsonSchemaComplexTypeValues)[number];
|
8
|
+
interface JsonSchemaTypeBase {
|
9
|
+
$id?: string;
|
10
|
+
title?: string;
|
11
|
+
description?: string;
|
12
|
+
nullable?: boolean;
|
13
|
+
format?: string;
|
14
|
+
}
|
15
|
+
interface JsonSchemaScalarType extends JsonSchemaTypeBase {
|
16
|
+
type: (typeof JsonSchemaScalarTypeValues)[number];
|
17
|
+
}
|
18
|
+
declare function isJsonSchemaScalarType(input: unknown): input is JsonSchemaScalarType;
|
19
|
+
interface JsonSchemaNullType extends JsonSchemaTypeBase {
|
20
|
+
type: (typeof JsonSchemaNullTypeValues)[number];
|
21
|
+
}
|
22
|
+
declare function isJsonSchemaNullType(input: any): input is JsonSchemaNullType;
|
23
|
+
type JsonSchemaComplexType = JsonSchemaObject | JsonSchemaArray | JsonSchemaRecord;
|
24
|
+
interface JsonSchemaObject extends JsonSchemaTypeBase {
|
25
|
+
type: "object";
|
26
|
+
properties: Record<string, JsonSchemaType>;
|
27
|
+
required?: string[];
|
28
|
+
additionalProperties?: boolean;
|
29
|
+
}
|
30
|
+
declare const isJsonSchemaObject: (input: any) => input is JsonSchemaObject;
|
31
|
+
interface JsonSchemaRecord extends JsonSchemaTypeBase {
|
32
|
+
type: "object";
|
33
|
+
patternProperties?: Record<string, JsonSchemaType>;
|
34
|
+
additionalProperties?: JsonSchemaType;
|
35
|
+
}
|
36
|
+
declare const isJsonSchemaRecord: (input: any) => input is JsonSchemaRecord;
|
37
|
+
interface JsonSchemaArray extends JsonSchemaTypeBase {
|
38
|
+
type: "array";
|
39
|
+
items: JsonSchemaType;
|
40
|
+
}
|
41
|
+
declare function isJsonSchemaArray(input: any): input is JsonSchemaArray;
|
42
|
+
interface JsonSchemaUint8Array extends JsonSchemaTypeBase {
|
43
|
+
type: "Uint8Array";
|
44
|
+
}
|
45
|
+
interface JsonSchemaEnum extends JsonSchemaTypeBase {
|
46
|
+
anyOf: Array<{
|
47
|
+
type: "string";
|
48
|
+
const: string;
|
49
|
+
}> | Array<{
|
50
|
+
type: "number";
|
51
|
+
const: number;
|
52
|
+
}> | Array<{
|
53
|
+
type: "integer";
|
54
|
+
const: number;
|
55
|
+
}>;
|
56
|
+
}
|
57
|
+
declare function isJsonSchemaEnum(input: any): input is JsonSchemaEnum;
|
58
|
+
interface JsonSchemaRef extends JsonSchemaTypeBase {
|
59
|
+
$ref: string;
|
60
|
+
}
|
61
|
+
declare function isJsonSchemaRef(input: unknown): input is JsonSchemaRef;
|
62
|
+
|
63
|
+
interface JsonSchemaContext {
|
64
|
+
parentRefs: string[];
|
65
|
+
rootSchema?: any;
|
66
|
+
}
|
67
|
+
declare function jsonSchemaToJtdSchema(input: JsonSchemaType, context?: JsonSchemaContext): Schema;
|
68
|
+
declare function jsonSchemaEnumToJtdEnum(input: JsonSchemaEnum, _: JsonSchemaContext): Schema;
|
69
|
+
declare function jsonSchemaScalarToJtdScalar(input: JsonSchemaScalarType, _: JsonSchemaContext): Schema;
|
70
|
+
declare function jsonSchemaObjectToJtdObject(input: JsonSchemaObject, _: JsonSchemaContext): Schema;
|
71
|
+
declare function jsonSchemaArrayToJtdArray(input: JsonSchemaArray, _: JsonSchemaContext): SchemaFormElements;
|
72
|
+
declare function jsonSchemaRecordToJtdRecord(input: JsonSchemaRecord, _: JsonSchemaContext): Schema;
|
73
|
+
declare function jsonSchemaRefToJtdRef(input: JsonSchemaRef, context: JsonSchemaContext): Schema;
|
74
|
+
|
75
|
+
export { type JsonSchemaArray, type JsonSchemaComplexType, JsonSchemaComplexTypeValues, type JsonSchemaContext, type JsonSchemaEnum, type JsonSchemaNullType, JsonSchemaNullTypeValues, type JsonSchemaObject, type JsonSchemaRecord, type JsonSchemaRef, type JsonSchemaScalarType, JsonSchemaScalarTypeValues, type JsonSchemaType, type JsonSchemaTypeBase, type JsonSchemaTypeValue, type JsonSchemaUint8Array, isJsonSchemaArray, isJsonSchemaEnum, isJsonSchemaNullType, isJsonSchemaObject, isJsonSchemaRecord, isJsonSchemaRef, isJsonSchemaScalarType, jsonSchemaArrayToJtdArray, jsonSchemaEnumToJtdEnum, jsonSchemaObjectToJtdObject, jsonSchemaRecordToJtdRecord, jsonSchemaRefToJtdRef, jsonSchemaScalarToJtdScalar, jsonSchemaToJtdSchema };
|
package/dist/index.d.mts
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
import { Schema, SchemaFormElements } from '@arrirpc/type-defs';
|
2
|
+
|
3
|
+
declare const JsonSchemaScalarTypeValues: readonly ["integer", "number", "bigint", "string", "boolean", "Date"];
|
4
|
+
declare const JsonSchemaNullTypeValues: readonly ["null", "undefined"];
|
5
|
+
declare const JsonSchemaComplexTypeValues: readonly ["object", "list", "array", "Uint8Array"];
|
6
|
+
type JsonSchemaType = JsonSchemaScalarType | JsonSchemaNullType | JsonSchemaComplexType;
|
7
|
+
type JsonSchemaTypeValue = (typeof JsonSchemaScalarTypeValues)[number] | (typeof JsonSchemaNullTypeValues)[number] | (typeof JsonSchemaComplexTypeValues)[number];
|
8
|
+
interface JsonSchemaTypeBase {
|
9
|
+
$id?: string;
|
10
|
+
title?: string;
|
11
|
+
description?: string;
|
12
|
+
nullable?: boolean;
|
13
|
+
format?: string;
|
14
|
+
}
|
15
|
+
interface JsonSchemaScalarType extends JsonSchemaTypeBase {
|
16
|
+
type: (typeof JsonSchemaScalarTypeValues)[number];
|
17
|
+
}
|
18
|
+
declare function isJsonSchemaScalarType(input: unknown): input is JsonSchemaScalarType;
|
19
|
+
interface JsonSchemaNullType extends JsonSchemaTypeBase {
|
20
|
+
type: (typeof JsonSchemaNullTypeValues)[number];
|
21
|
+
}
|
22
|
+
declare function isJsonSchemaNullType(input: any): input is JsonSchemaNullType;
|
23
|
+
type JsonSchemaComplexType = JsonSchemaObject | JsonSchemaArray | JsonSchemaRecord;
|
24
|
+
interface JsonSchemaObject extends JsonSchemaTypeBase {
|
25
|
+
type: "object";
|
26
|
+
properties: Record<string, JsonSchemaType>;
|
27
|
+
required?: string[];
|
28
|
+
additionalProperties?: boolean;
|
29
|
+
}
|
30
|
+
declare const isJsonSchemaObject: (input: any) => input is JsonSchemaObject;
|
31
|
+
interface JsonSchemaRecord extends JsonSchemaTypeBase {
|
32
|
+
type: "object";
|
33
|
+
patternProperties?: Record<string, JsonSchemaType>;
|
34
|
+
additionalProperties?: JsonSchemaType;
|
35
|
+
}
|
36
|
+
declare const isJsonSchemaRecord: (input: any) => input is JsonSchemaRecord;
|
37
|
+
interface JsonSchemaArray extends JsonSchemaTypeBase {
|
38
|
+
type: "array";
|
39
|
+
items: JsonSchemaType;
|
40
|
+
}
|
41
|
+
declare function isJsonSchemaArray(input: any): input is JsonSchemaArray;
|
42
|
+
interface JsonSchemaUint8Array extends JsonSchemaTypeBase {
|
43
|
+
type: "Uint8Array";
|
44
|
+
}
|
45
|
+
interface JsonSchemaEnum extends JsonSchemaTypeBase {
|
46
|
+
anyOf: Array<{
|
47
|
+
type: "string";
|
48
|
+
const: string;
|
49
|
+
}> | Array<{
|
50
|
+
type: "number";
|
51
|
+
const: number;
|
52
|
+
}> | Array<{
|
53
|
+
type: "integer";
|
54
|
+
const: number;
|
55
|
+
}>;
|
56
|
+
}
|
57
|
+
declare function isJsonSchemaEnum(input: any): input is JsonSchemaEnum;
|
58
|
+
interface JsonSchemaRef extends JsonSchemaTypeBase {
|
59
|
+
$ref: string;
|
60
|
+
}
|
61
|
+
declare function isJsonSchemaRef(input: unknown): input is JsonSchemaRef;
|
62
|
+
|
63
|
+
interface JsonSchemaContext {
|
64
|
+
parentRefs: string[];
|
65
|
+
rootSchema?: any;
|
66
|
+
}
|
67
|
+
declare function jsonSchemaToJtdSchema(input: JsonSchemaType, context?: JsonSchemaContext): Schema;
|
68
|
+
declare function jsonSchemaEnumToJtdEnum(input: JsonSchemaEnum, _: JsonSchemaContext): Schema;
|
69
|
+
declare function jsonSchemaScalarToJtdScalar(input: JsonSchemaScalarType, _: JsonSchemaContext): Schema;
|
70
|
+
declare function jsonSchemaObjectToJtdObject(input: JsonSchemaObject, _: JsonSchemaContext): Schema;
|
71
|
+
declare function jsonSchemaArrayToJtdArray(input: JsonSchemaArray, _: JsonSchemaContext): SchemaFormElements;
|
72
|
+
declare function jsonSchemaRecordToJtdRecord(input: JsonSchemaRecord, _: JsonSchemaContext): Schema;
|
73
|
+
declare function jsonSchemaRefToJtdRef(input: JsonSchemaRef, context: JsonSchemaContext): Schema;
|
74
|
+
|
75
|
+
export { type JsonSchemaArray, type JsonSchemaComplexType, JsonSchemaComplexTypeValues, type JsonSchemaContext, type JsonSchemaEnum, type JsonSchemaNullType, JsonSchemaNullTypeValues, type JsonSchemaObject, type JsonSchemaRecord, type JsonSchemaRef, type JsonSchemaScalarType, JsonSchemaScalarTypeValues, type JsonSchemaType, type JsonSchemaTypeBase, type JsonSchemaTypeValue, type JsonSchemaUint8Array, isJsonSchemaArray, isJsonSchemaEnum, isJsonSchemaNullType, isJsonSchemaObject, isJsonSchemaRecord, isJsonSchemaRef, isJsonSchemaScalarType, jsonSchemaArrayToJtdArray, jsonSchemaEnumToJtdEnum, jsonSchemaObjectToJtdObject, jsonSchemaRecordToJtdRecord, jsonSchemaRefToJtdRef, jsonSchemaScalarToJtdScalar, jsonSchemaToJtdSchema };
|
package/dist/index.d.ts
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
import { Schema, SchemaFormElements } from '@arrirpc/type-defs';
|
2
|
+
|
3
|
+
declare const JsonSchemaScalarTypeValues: readonly ["integer", "number", "bigint", "string", "boolean", "Date"];
|
4
|
+
declare const JsonSchemaNullTypeValues: readonly ["null", "undefined"];
|
5
|
+
declare const JsonSchemaComplexTypeValues: readonly ["object", "list", "array", "Uint8Array"];
|
6
|
+
type JsonSchemaType = JsonSchemaScalarType | JsonSchemaNullType | JsonSchemaComplexType;
|
7
|
+
type JsonSchemaTypeValue = (typeof JsonSchemaScalarTypeValues)[number] | (typeof JsonSchemaNullTypeValues)[number] | (typeof JsonSchemaComplexTypeValues)[number];
|
8
|
+
interface JsonSchemaTypeBase {
|
9
|
+
$id?: string;
|
10
|
+
title?: string;
|
11
|
+
description?: string;
|
12
|
+
nullable?: boolean;
|
13
|
+
format?: string;
|
14
|
+
}
|
15
|
+
interface JsonSchemaScalarType extends JsonSchemaTypeBase {
|
16
|
+
type: (typeof JsonSchemaScalarTypeValues)[number];
|
17
|
+
}
|
18
|
+
declare function isJsonSchemaScalarType(input: unknown): input is JsonSchemaScalarType;
|
19
|
+
interface JsonSchemaNullType extends JsonSchemaTypeBase {
|
20
|
+
type: (typeof JsonSchemaNullTypeValues)[number];
|
21
|
+
}
|
22
|
+
declare function isJsonSchemaNullType(input: any): input is JsonSchemaNullType;
|
23
|
+
type JsonSchemaComplexType = JsonSchemaObject | JsonSchemaArray | JsonSchemaRecord;
|
24
|
+
interface JsonSchemaObject extends JsonSchemaTypeBase {
|
25
|
+
type: "object";
|
26
|
+
properties: Record<string, JsonSchemaType>;
|
27
|
+
required?: string[];
|
28
|
+
additionalProperties?: boolean;
|
29
|
+
}
|
30
|
+
declare const isJsonSchemaObject: (input: any) => input is JsonSchemaObject;
|
31
|
+
interface JsonSchemaRecord extends JsonSchemaTypeBase {
|
32
|
+
type: "object";
|
33
|
+
patternProperties?: Record<string, JsonSchemaType>;
|
34
|
+
additionalProperties?: JsonSchemaType;
|
35
|
+
}
|
36
|
+
declare const isJsonSchemaRecord: (input: any) => input is JsonSchemaRecord;
|
37
|
+
interface JsonSchemaArray extends JsonSchemaTypeBase {
|
38
|
+
type: "array";
|
39
|
+
items: JsonSchemaType;
|
40
|
+
}
|
41
|
+
declare function isJsonSchemaArray(input: any): input is JsonSchemaArray;
|
42
|
+
interface JsonSchemaUint8Array extends JsonSchemaTypeBase {
|
43
|
+
type: "Uint8Array";
|
44
|
+
}
|
45
|
+
interface JsonSchemaEnum extends JsonSchemaTypeBase {
|
46
|
+
anyOf: Array<{
|
47
|
+
type: "string";
|
48
|
+
const: string;
|
49
|
+
}> | Array<{
|
50
|
+
type: "number";
|
51
|
+
const: number;
|
52
|
+
}> | Array<{
|
53
|
+
type: "integer";
|
54
|
+
const: number;
|
55
|
+
}>;
|
56
|
+
}
|
57
|
+
declare function isJsonSchemaEnum(input: any): input is JsonSchemaEnum;
|
58
|
+
interface JsonSchemaRef extends JsonSchemaTypeBase {
|
59
|
+
$ref: string;
|
60
|
+
}
|
61
|
+
declare function isJsonSchemaRef(input: unknown): input is JsonSchemaRef;
|
62
|
+
|
63
|
+
interface JsonSchemaContext {
|
64
|
+
parentRefs: string[];
|
65
|
+
rootSchema?: any;
|
66
|
+
}
|
67
|
+
declare function jsonSchemaToJtdSchema(input: JsonSchemaType, context?: JsonSchemaContext): Schema;
|
68
|
+
declare function jsonSchemaEnumToJtdEnum(input: JsonSchemaEnum, _: JsonSchemaContext): Schema;
|
69
|
+
declare function jsonSchemaScalarToJtdScalar(input: JsonSchemaScalarType, _: JsonSchemaContext): Schema;
|
70
|
+
declare function jsonSchemaObjectToJtdObject(input: JsonSchemaObject, _: JsonSchemaContext): Schema;
|
71
|
+
declare function jsonSchemaArrayToJtdArray(input: JsonSchemaArray, _: JsonSchemaContext): SchemaFormElements;
|
72
|
+
declare function jsonSchemaRecordToJtdRecord(input: JsonSchemaRecord, _: JsonSchemaContext): Schema;
|
73
|
+
declare function jsonSchemaRefToJtdRef(input: JsonSchemaRef, context: JsonSchemaContext): Schema;
|
74
|
+
|
75
|
+
export { type JsonSchemaArray, type JsonSchemaComplexType, JsonSchemaComplexTypeValues, type JsonSchemaContext, type JsonSchemaEnum, type JsonSchemaNullType, JsonSchemaNullTypeValues, type JsonSchemaObject, type JsonSchemaRecord, type JsonSchemaRef, type JsonSchemaScalarType, JsonSchemaScalarTypeValues, type JsonSchemaType, type JsonSchemaTypeBase, type JsonSchemaTypeValue, type JsonSchemaUint8Array, isJsonSchemaArray, isJsonSchemaEnum, isJsonSchemaNullType, isJsonSchemaObject, isJsonSchemaRecord, isJsonSchemaRef, isJsonSchemaScalarType, jsonSchemaArrayToJtdArray, jsonSchemaEnumToJtdEnum, jsonSchemaObjectToJtdObject, jsonSchemaRecordToJtdRecord, jsonSchemaRefToJtdRef, jsonSchemaScalarToJtdScalar, jsonSchemaToJtdSchema };
|
package/dist/index.mjs
ADDED
@@ -0,0 +1,284 @@
|
|
1
|
+
const JsonSchemaScalarTypeValues = [
|
2
|
+
"integer",
|
3
|
+
"number",
|
4
|
+
"bigint",
|
5
|
+
"string",
|
6
|
+
"boolean",
|
7
|
+
"Date"
|
8
|
+
];
|
9
|
+
const JsonSchemaNullTypeValues = ["null", "undefined"];
|
10
|
+
const JsonSchemaComplexTypeValues = [
|
11
|
+
"object",
|
12
|
+
"list",
|
13
|
+
"array",
|
14
|
+
"Uint8Array"
|
15
|
+
];
|
16
|
+
function isJsonSchemaScalarType(input) {
|
17
|
+
if (typeof input !== "object" || input === null) {
|
18
|
+
return false;
|
19
|
+
}
|
20
|
+
if (!("type" in input)) {
|
21
|
+
return false;
|
22
|
+
}
|
23
|
+
return JsonSchemaScalarTypeValues.includes(input.type);
|
24
|
+
}
|
25
|
+
function isJsonSchemaNullType(input) {
|
26
|
+
if (typeof input !== "object" || input === null) {
|
27
|
+
return false;
|
28
|
+
}
|
29
|
+
if (!("type" in input)) {
|
30
|
+
return false;
|
31
|
+
}
|
32
|
+
return JsonSchemaNullTypeValues.includes(input.type);
|
33
|
+
}
|
34
|
+
const isJsonSchemaObject = (input) => {
|
35
|
+
if (typeof input !== "object") {
|
36
|
+
return false;
|
37
|
+
}
|
38
|
+
return "type" in input && input.type === "object" && "properties" in input && typeof input.properties === "object";
|
39
|
+
};
|
40
|
+
const isJsonSchemaRecord = (input) => {
|
41
|
+
if (typeof input !== "object") {
|
42
|
+
return false;
|
43
|
+
}
|
44
|
+
if ("type" in input && input.type === "object") {
|
45
|
+
return "patternProperties" in input && typeof input.patternProperties === "object" || "additionalProperties" in input && typeof input.additionalProperties === "object";
|
46
|
+
}
|
47
|
+
return false;
|
48
|
+
};
|
49
|
+
function isJsonSchemaArray(input) {
|
50
|
+
if (typeof input !== "object") {
|
51
|
+
return false;
|
52
|
+
}
|
53
|
+
if ("type" in input && input.type === "array") {
|
54
|
+
return true;
|
55
|
+
}
|
56
|
+
return false;
|
57
|
+
}
|
58
|
+
function isJsonSchemaEnum(input) {
|
59
|
+
if (typeof input !== "object") {
|
60
|
+
return false;
|
61
|
+
}
|
62
|
+
if (!("anyOf" in input) || !Array.isArray(input.anyOf)) {
|
63
|
+
return false;
|
64
|
+
}
|
65
|
+
let prevType;
|
66
|
+
for (const item of input.anyOf) {
|
67
|
+
if (!isJsonSchemaScalarType(item)) {
|
68
|
+
return false;
|
69
|
+
}
|
70
|
+
if (item.type !== "string" && item.type !== "integer" && item.type !== "number") {
|
71
|
+
return false;
|
72
|
+
}
|
73
|
+
if (!prevType) {
|
74
|
+
prevType = item.type;
|
75
|
+
} else if (prevType !== item.type) {
|
76
|
+
return false;
|
77
|
+
}
|
78
|
+
}
|
79
|
+
return true;
|
80
|
+
}
|
81
|
+
function isJsonSchemaRef(input) {
|
82
|
+
if (typeof input !== "object" || !input) {
|
83
|
+
return false;
|
84
|
+
}
|
85
|
+
return "$ref" in input && typeof input.$ref === "string";
|
86
|
+
}
|
87
|
+
|
88
|
+
function jsonSchemaToJtdSchema(input, context = { parentRefs: [] }) {
|
89
|
+
if (isJsonSchemaScalarType(input)) {
|
90
|
+
return jsonSchemaScalarToJtdScalar(input);
|
91
|
+
}
|
92
|
+
if (isJsonSchemaEnum(input)) {
|
93
|
+
return jsonSchemaEnumToJtdEnum(input);
|
94
|
+
}
|
95
|
+
if (isJsonSchemaObject(input)) {
|
96
|
+
return jsonSchemaObjectToJtdObject(input);
|
97
|
+
}
|
98
|
+
if (isJsonSchemaArray(input)) {
|
99
|
+
return jsonSchemaArrayToJtdArray(input);
|
100
|
+
}
|
101
|
+
if (isJsonSchemaRecord(input)) {
|
102
|
+
return jsonSchemaRecordToJtdRecord(input);
|
103
|
+
}
|
104
|
+
if (isJsonSchemaRef(input)) {
|
105
|
+
return jsonSchemaRefToJtdRef(input, context);
|
106
|
+
}
|
107
|
+
console.warn(
|
108
|
+
`WARNING: unable to determine type for ${input}. Falling back to "any" type.`
|
109
|
+
);
|
110
|
+
return {
|
111
|
+
metadata: {
|
112
|
+
id: input.$id ?? input.title,
|
113
|
+
description: input.description
|
114
|
+
}
|
115
|
+
};
|
116
|
+
}
|
117
|
+
function jsonSchemaEnumToJtdEnum(input, _) {
|
118
|
+
const enumTypes = input.anyOf.map((val) => val.type);
|
119
|
+
const isNotStringEnum = enumTypes.includes("integer") || enumTypes.includes("number");
|
120
|
+
if (isNotStringEnum) {
|
121
|
+
console.error(
|
122
|
+
`WARNING: Cannot convert non string enums. This key will be treated as an "any" by generated clients.`
|
123
|
+
);
|
124
|
+
const output2 = {};
|
125
|
+
return output2;
|
126
|
+
}
|
127
|
+
const output = {
|
128
|
+
enum: input.anyOf.map((val) => val.const),
|
129
|
+
metadata: {
|
130
|
+
id: input.$id ?? input.title,
|
131
|
+
description: input.description
|
132
|
+
}
|
133
|
+
};
|
134
|
+
return output;
|
135
|
+
}
|
136
|
+
function jsonSchemaScalarToJtdScalar(input, _) {
|
137
|
+
const meta = {
|
138
|
+
id: input.$id ?? input.title,
|
139
|
+
description: input.description
|
140
|
+
};
|
141
|
+
switch (input.type) {
|
142
|
+
case "Date":
|
143
|
+
return {
|
144
|
+
type: "timestamp",
|
145
|
+
nullable: input.nullable,
|
146
|
+
metadata: meta
|
147
|
+
};
|
148
|
+
case "bigint":
|
149
|
+
case "integer":
|
150
|
+
return {
|
151
|
+
type: "int32",
|
152
|
+
nullable: input.nullable,
|
153
|
+
metadata: meta
|
154
|
+
};
|
155
|
+
case "number":
|
156
|
+
return {
|
157
|
+
type: "float64",
|
158
|
+
nullable: input.nullable,
|
159
|
+
metadata: meta
|
160
|
+
};
|
161
|
+
case "boolean":
|
162
|
+
return {
|
163
|
+
type: "boolean",
|
164
|
+
nullable: input.nullable,
|
165
|
+
metadata: meta
|
166
|
+
};
|
167
|
+
case "string":
|
168
|
+
if (input.format === "date-time") {
|
169
|
+
return {
|
170
|
+
type: "timestamp",
|
171
|
+
nullable: input.nullable,
|
172
|
+
metadata: meta
|
173
|
+
};
|
174
|
+
}
|
175
|
+
return {
|
176
|
+
type: "string",
|
177
|
+
nullable: input.nullable,
|
178
|
+
metadata: meta
|
179
|
+
};
|
180
|
+
default:
|
181
|
+
return {};
|
182
|
+
}
|
183
|
+
}
|
184
|
+
function jsonSchemaObjectToJtdObject(input, _) {
|
185
|
+
const result = {
|
186
|
+
properties: {},
|
187
|
+
nullable: input.nullable,
|
188
|
+
strict: typeof input.additionalProperties === "boolean" ? !input.additionalProperties : void 0,
|
189
|
+
metadata: {
|
190
|
+
id: input.$id ?? input.title,
|
191
|
+
description: input.description
|
192
|
+
}
|
193
|
+
};
|
194
|
+
Object.keys(input.properties).forEach((key) => {
|
195
|
+
const prop = input.properties[key];
|
196
|
+
if (!prop) {
|
197
|
+
return;
|
198
|
+
}
|
199
|
+
const isOptional = !(input.required ?? []).includes(key);
|
200
|
+
if (isOptional) {
|
201
|
+
if (!result.optionalProperties) {
|
202
|
+
result.optionalProperties = {};
|
203
|
+
}
|
204
|
+
result.optionalProperties[key] = jsonSchemaToJtdSchema(prop);
|
205
|
+
return;
|
206
|
+
}
|
207
|
+
result.properties[key] = jsonSchemaToJtdSchema(prop);
|
208
|
+
});
|
209
|
+
return result;
|
210
|
+
}
|
211
|
+
function jsonSchemaArrayToJtdArray(input, _) {
|
212
|
+
const result = {
|
213
|
+
elements: jsonSchemaToJtdSchema(input.items),
|
214
|
+
nullable: input.nullable,
|
215
|
+
metadata: {
|
216
|
+
id: input.$id ?? input.title,
|
217
|
+
description: input.description
|
218
|
+
}
|
219
|
+
};
|
220
|
+
return result;
|
221
|
+
}
|
222
|
+
function jsonSchemaRecordToJtdRecord(input, _) {
|
223
|
+
if (input.additionalProperties) {
|
224
|
+
const type = jsonSchemaToJtdSchema(input.additionalProperties);
|
225
|
+
return {
|
226
|
+
values: type,
|
227
|
+
nullable: input.nullable,
|
228
|
+
metadata: {
|
229
|
+
id: input.$id ?? input.title,
|
230
|
+
description: input.description
|
231
|
+
}
|
232
|
+
};
|
233
|
+
}
|
234
|
+
const types = [];
|
235
|
+
Object.keys(input.patternProperties ?? {}).forEach((key) => {
|
236
|
+
const pattern = input.patternProperties[key];
|
237
|
+
if (!pattern) {
|
238
|
+
return;
|
239
|
+
}
|
240
|
+
types.push(jsonSchemaToJtdSchema(pattern));
|
241
|
+
});
|
242
|
+
if (types.length === 0) {
|
243
|
+
console.warn(
|
244
|
+
'WARNING: unable to determine record type values. This key will be treated as "any" by client generators.'
|
245
|
+
);
|
246
|
+
return {};
|
247
|
+
}
|
248
|
+
const result = {
|
249
|
+
values: types[0],
|
250
|
+
nullable: input.nullable,
|
251
|
+
metadata: {
|
252
|
+
id: input.$id ?? input.title,
|
253
|
+
description: input.description
|
254
|
+
}
|
255
|
+
};
|
256
|
+
return result;
|
257
|
+
}
|
258
|
+
function jsonSchemaRefToJtdRef(input, context) {
|
259
|
+
if (context.parentRefs.includes(input.$ref)) {
|
260
|
+
const parts2 = input.$ref.split("/");
|
261
|
+
const refId = parts2[parts2.length - 1];
|
262
|
+
if (!refId) return {};
|
263
|
+
return {
|
264
|
+
nullable: input.nullable,
|
265
|
+
ref: refId
|
266
|
+
};
|
267
|
+
}
|
268
|
+
const parts = input.$ref.split("/");
|
269
|
+
let subSchema = context.rootSchema ?? {};
|
270
|
+
for (const part of parts) {
|
271
|
+
if (part === "#") continue;
|
272
|
+
if (!subSchema[part]) return {};
|
273
|
+
subSchema = subSchema[part];
|
274
|
+
}
|
275
|
+
const r = context.parentRefs;
|
276
|
+
r.push(input.$ref);
|
277
|
+
const result = jsonSchemaToJtdSchema(subSchema, {
|
278
|
+
...context,
|
279
|
+
parentRefs: r
|
280
|
+
});
|
281
|
+
return result;
|
282
|
+
}
|
283
|
+
|
284
|
+
export { JsonSchemaComplexTypeValues, JsonSchemaNullTypeValues, JsonSchemaScalarTypeValues, isJsonSchemaArray, isJsonSchemaEnum, isJsonSchemaNullType, isJsonSchemaObject, isJsonSchemaRecord, isJsonSchemaRef, isJsonSchemaScalarType, jsonSchemaArrayToJtdArray, jsonSchemaEnumToJtdEnum, jsonSchemaObjectToJtdObject, jsonSchemaRecordToJtdRecord, jsonSchemaRefToJtdRef, jsonSchemaScalarToJtdScalar, jsonSchemaToJtdSchema };
|
package/package.json
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
{
|
2
|
+
"name": "json-schema-to-atd",
|
3
|
+
"version": "0.70.1",
|
4
|
+
"license": "MIT",
|
5
|
+
"author": {
|
6
|
+
"name": "joshmossas",
|
7
|
+
"url": "https://github.com/joshmossas"
|
8
|
+
},
|
9
|
+
"bugs": {
|
10
|
+
"url": "https://github.com/modiimedia/arri/issues"
|
11
|
+
},
|
12
|
+
"repository": {
|
13
|
+
"type": "git",
|
14
|
+
"url": "https://github.com/modiimedia/arri.git",
|
15
|
+
"directory": "tooling/json-schema-to-jtd"
|
16
|
+
},
|
17
|
+
"type": "module",
|
18
|
+
"main": "./dist/index.cjs",
|
19
|
+
"module": "./dist/index.mjs",
|
20
|
+
"types": "./dist/index.d.ts",
|
21
|
+
"files": [
|
22
|
+
"dist"
|
23
|
+
],
|
24
|
+
"dependencies": {
|
25
|
+
"@arrirpc/type-defs": "0.70.1"
|
26
|
+
}
|
27
|
+
}
|