@camera.ui/browser 0.0.1
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/CHANGELOG.md +8 -0
- package/LICENSE.md +22 -0
- package/README.md +1 -0
- package/dist/bundle.browser.js +1 -0
- package/dist/client/node/src/api.d.ts +5 -0
- package/dist/client/node/src/api.js +27 -0
- package/dist/client/node/src/api.js.map +1 -0
- package/dist/client/node/src/client.d.ts +20 -0
- package/dist/client/node/src/client.js +106 -0
- package/dist/client/node/src/client.js.map +1 -0
- package/dist/client/node/src/index.d.ts +2 -0
- package/dist/client/node/src/index.js +19 -0
- package/dist/client/node/src/index.js.map +1 -0
- package/dist/client/node/src/proxy/cameraDevice/cameraDevice.browser.d.ts +48 -0
- package/dist/client/node/src/proxy/cameraDevice/cameraDevice.browser.js +176 -0
- package/dist/client/node/src/proxy/cameraDevice/cameraDevice.browser.js.map +1 -0
- package/dist/client/node/src/proxy/cameraDevice/cameraDevice.mock.d.ts +1 -0
- package/dist/client/node/src/proxy/cameraDevice/cameraDevice.mock.js +3 -0
- package/dist/client/node/src/proxy/cameraDevice/cameraDevice.mock.js.map +1 -0
- package/dist/client/node/src/proxy/cameraDevice/cameraDevice.node.d.ts +11 -0
- package/dist/client/node/src/proxy/cameraDevice/cameraDevice.node.js +51 -0
- package/dist/client/node/src/proxy/cameraDevice/cameraDevice.node.js.map +1 -0
- package/dist/client/node/src/proxy/cameraDevice/index.d.ts +6 -0
- package/dist/client/node/src/proxy/cameraDevice/index.js +11 -0
- package/dist/client/node/src/proxy/cameraDevice/index.js.map +1 -0
- package/dist/client/node/src/proxy/deviceManager.d.ts +15 -0
- package/dist/client/node/src/proxy/deviceManager.js +67 -0
- package/dist/client/node/src/proxy/deviceManager.js.map +1 -0
- package/dist/client/node/src/proxy/index.d.ts +4 -0
- package/dist/client/node/src/proxy/index.js +21 -0
- package/dist/client/node/src/proxy/index.js.map +1 -0
- package/dist/client/node/src/proxy/pluginsManager.d.ts +9 -0
- package/dist/client/node/src/proxy/pluginsManager.js +25 -0
- package/dist/client/node/src/proxy/pluginsManager.js.map +1 -0
- package/dist/client/node/src/proxy/systemManager.d.ts +9 -0
- package/dist/client/node/src/proxy/systemManager.js +25 -0
- package/dist/client/node/src/proxy/systemManager.js.map +1 -0
- package/dist/client/node/src/socket.d.ts +9 -0
- package/dist/client/node/src/socket.js +45 -0
- package/dist/client/node/src/socket.js.map +1 -0
- package/dist/client/node/src/types.d.ts +22 -0
- package/dist/client/node/src/types.js +3 -0
- package/dist/client/node/src/types.js.map +1 -0
- package/dist/package.json +149 -0
- package/dist/shared/common/utils.d.ts +6 -0
- package/dist/shared/common/utils.js +66 -0
- package/dist/shared/common/utils.js.map +1 -0
- package/dist/shared/types/index.d.ts +8 -0
- package/dist/shared/types/index.js +23 -0
- package/dist/shared/types/index.js.map +1 -0
- package/dist/src/api/database/index.d.ts +25 -0
- package/dist/src/api/database/index.js +203 -0
- package/dist/src/api/database/index.js.map +1 -0
- package/dist/src/api/database/types.d.ts +153 -0
- package/dist/src/api/database/types.js +3 -0
- package/dist/src/api/database/types.js.map +1 -0
- package/dist/src/api/go2rtc/api/application.d.ts +8 -0
- package/dist/src/api/go2rtc/api/application.js +32 -0
- package/dist/src/api/go2rtc/api/application.js.map +1 -0
- package/dist/src/api/go2rtc/api/index.d.ts +3 -0
- package/dist/src/api/go2rtc/api/index.js +33 -0
- package/dist/src/api/go2rtc/api/index.js.map +1 -0
- package/dist/src/api/go2rtc/api/snapshot.d.ts +6 -0
- package/dist/src/api/go2rtc/api/snapshot.js +22 -0
- package/dist/src/api/go2rtc/api/snapshot.js.map +1 -0
- package/dist/src/api/go2rtc/api/streams.d.ts +10 -0
- package/dist/src/api/go2rtc/api/streams.js +32 -0
- package/dist/src/api/go2rtc/api/streams.js.map +1 -0
- package/dist/src/api/go2rtc/index.d.ts +3 -0
- package/dist/src/api/go2rtc/index.js +13 -0
- package/dist/src/api/go2rtc/index.js.map +1 -0
- package/dist/src/api/go2rtc/types.d.ts +43 -0
- package/dist/src/api/go2rtc/types.js +3 -0
- package/dist/src/api/go2rtc/types.js.map +1 -0
- package/dist/src/api/middlewares/socketAuth.middleware.d.ts +41 -0
- package/dist/src/api/middlewares/socketAuth.middleware.js +90 -0
- package/dist/src/api/middlewares/socketAuth.middleware.js.map +1 -0
- package/dist/src/api/schemas/backup.schema.d.ts +220 -0
- package/dist/src/api/schemas/backup.schema.js +94 -0
- package/dist/src/api/schemas/backup.schema.js.map +1 -0
- package/dist/src/api/schemas/cameras.schema.d.ts +316 -0
- package/dist/src/api/schemas/cameras.schema.js +124 -0
- package/dist/src/api/schemas/cameras.schema.js.map +1 -0
- package/dist/src/api/schemas/config.schema.d.ts +202 -0
- package/dist/src/api/schemas/config.schema.js +94 -0
- package/dist/src/api/schemas/config.schema.js.map +1 -0
- package/dist/src/api/schemas/go2rtc.schema.d.ts +604 -0
- package/dist/src/api/schemas/go2rtc.schema.js +204 -0
- package/dist/src/api/schemas/go2rtc.schema.js.map +1 -0
- package/dist/src/api/schemas/users.schema.d.ts +515 -0
- package/dist/src/api/schemas/users.schema.js +131 -0
- package/dist/src/api/schemas/users.schema.js.map +1 -0
- package/dist/src/api/services/auth.service.d.ts +20 -0
- package/dist/src/api/services/auth.service.js +104 -0
- package/dist/src/api/services/auth.service.js.map +1 -0
- package/dist/src/api/services/cameras.service.d.ts +40 -0
- package/dist/src/api/services/cameras.service.js +535 -0
- package/dist/src/api/services/cameras.service.js.map +1 -0
- package/dist/src/api/services/plugins.service.d.ts +23 -0
- package/dist/src/api/services/plugins.service.js +115 -0
- package/dist/src/api/services/plugins.service.js.map +1 -0
- package/dist/src/api/services/users.service.d.ts +13 -0
- package/dist/src/api/services/users.service.js +54 -0
- package/dist/src/api/services/users.service.js.map +1 -0
- package/dist/src/api/types/index.d.ts +522 -0
- package/dist/src/api/types/index.js +12 -0
- package/dist/src/api/types/index.js.map +1 -0
- package/dist/src/api/utils/cert.d.ts +17 -0
- package/dist/src/api/utils/cert.js +437 -0
- package/dist/src/api/utils/cert.js.map +1 -0
- package/dist/src/api/ws/index.d.ts +15 -0
- package/dist/src/api/ws/index.js +89 -0
- package/dist/src/api/ws/index.js.map +1 -0
- package/dist/src/api/ws/nsp/logs.d.ts +11 -0
- package/dist/src/api/ws/nsp/logs.js +121 -0
- package/dist/src/api/ws/nsp/logs.js.map +1 -0
- package/dist/src/api/ws/nsp/main.d.ts +9 -0
- package/dist/src/api/ws/nsp/main.js +49 -0
- package/dist/src/api/ws/nsp/main.js.map +1 -0
- package/dist/src/api/ws/nsp/metrics.d.ts +33 -0
- package/dist/src/api/ws/nsp/metrics.js +225 -0
- package/dist/src/api/ws/nsp/metrics.js.map +1 -0
- package/dist/src/api/ws/nsp/notifications.d.ts +11 -0
- package/dist/src/api/ws/nsp/notifications.js +39 -0
- package/dist/src/api/ws/nsp/notifications.js.map +1 -0
- package/dist/src/api/ws/nsp/plugins.d.ts +13 -0
- package/dist/src/api/ws/nsp/plugins.js +33 -0
- package/dist/src/api/ws/nsp/plugins.js.map +1 -0
- package/dist/src/api/ws/nsp/status.d.ts +12 -0
- package/dist/src/api/ws/nsp/status.js +41 -0
- package/dist/src/api/ws/nsp/status.js.map +1 -0
- package/dist/src/api/ws/nsp/streams.d.ts +30 -0
- package/dist/src/api/ws/nsp/streams.js +140 -0
- package/dist/src/api/ws/nsp/streams.js.map +1 -0
- package/dist/src/api/ws/types.d.ts +82 -0
- package/dist/src/api/ws/types.js +3 -0
- package/dist/src/api/ws/types.js.map +1 -0
- package/dist/src/api.d.ts +60 -0
- package/dist/src/api.js +66 -0
- package/dist/src/api.js.map +1 -0
- package/dist/src/camera/index.d.ts +54 -0
- package/dist/src/camera/index.js +210 -0
- package/dist/src/camera/index.js.map +1 -0
- package/dist/src/camera/streaming/peer-connection.d.ts +31 -0
- package/dist/src/camera/streaming/peer-connection.js +157 -0
- package/dist/src/camera/streaming/peer-connection.js.map +1 -0
- package/dist/src/camera/streaming/streaming-session.d.ts +32 -0
- package/dist/src/camera/streaming/streaming-session.js +172 -0
- package/dist/src/camera/streaming/streaming-session.js.map +1 -0
- package/dist/src/camera/streaming/webrtc-connection.d.ts +33 -0
- package/dist/src/camera/streaming/webrtc-connection.js +156 -0
- package/dist/src/camera/streaming/webrtc-connection.js.map +1 -0
- package/dist/src/camera/subscribed.d.ts +6 -0
- package/dist/src/camera/subscribed.js +14 -0
- package/dist/src/camera/subscribed.js.map +1 -0
- package/dist/src/camera/types.d.ts +130 -0
- package/dist/src/camera/types.js +3 -0
- package/dist/src/camera/types.js.map +1 -0
- package/dist/src/go2rtc/index.d.ts +24 -0
- package/dist/src/go2rtc/index.js +220 -0
- package/dist/src/go2rtc/index.js.map +1 -0
- package/dist/src/go2rtc/types.d.ts +14 -0
- package/dist/src/go2rtc/types.js +76 -0
- package/dist/src/go2rtc/types.js.map +1 -0
- package/dist/src/plugins/base.d.ts +19 -0
- package/dist/src/plugins/base.js +26 -0
- package/dist/src/plugins/base.js.map +1 -0
- package/dist/src/plugins/cameraStorage.d.ts +97 -0
- package/dist/src/plugins/cameraStorage.js +290 -0
- package/dist/src/plugins/cameraStorage.js.map +1 -0
- package/dist/src/plugins/child.d.ts +18 -0
- package/dist/src/plugins/child.js +140 -0
- package/dist/src/plugins/child.js.map +1 -0
- package/dist/src/plugins/config.d.ts +113 -0
- package/dist/src/plugins/config.js +234 -0
- package/dist/src/plugins/config.js.map +1 -0
- package/dist/src/plugins/index.d.ts +36 -0
- package/dist/src/plugins/index.js +328 -0
- package/dist/src/plugins/index.js.map +1 -0
- package/dist/src/plugins/plugin.d.ts +47 -0
- package/dist/src/plugins/plugin.js +215 -0
- package/dist/src/plugins/plugin.js.map +1 -0
- package/dist/src/plugins/schema.d.ts +268 -0
- package/dist/src/plugins/schema.js +717 -0
- package/dist/src/plugins/schema.js.map +1 -0
- package/dist/src/plugins/storage.d.ts +28 -0
- package/dist/src/plugins/storage.js +45 -0
- package/dist/src/plugins/storage.js.map +1 -0
- package/dist/src/plugins/types.d.ts +162 -0
- package/dist/src/plugins/types.js +21 -0
- package/dist/src/plugins/types.js.map +1 -0
- package/dist/src/plugins/worker.d.ts +27 -0
- package/dist/src/plugins/worker.js +236 -0
- package/dist/src/plugins/worker.js.map +1 -0
- package/dist/src/proxy/client/cameraDevice.d.ts +29 -0
- package/dist/src/proxy/client/cameraDevice.js +171 -0
- package/dist/src/proxy/client/cameraDevice.js.map +1 -0
- package/dist/src/proxy/client/deviceManager.d.ts +40 -0
- package/dist/src/proxy/client/deviceManager.js +239 -0
- package/dist/src/proxy/client/deviceManager.js.map +1 -0
- package/dist/src/proxy/client/index.d.ts +4 -0
- package/dist/src/proxy/client/index.js +21 -0
- package/dist/src/proxy/client/index.js.map +1 -0
- package/dist/src/proxy/client/pluginsManager.d.ts +35 -0
- package/dist/src/proxy/client/pluginsManager.js +237 -0
- package/dist/src/proxy/client/pluginsManager.js.map +1 -0
- package/dist/src/proxy/client/systemManager.d.ts +29 -0
- package/dist/src/proxy/client/systemManager.js +163 -0
- package/dist/src/proxy/client/systemManager.js.map +1 -0
- package/dist/src/proxy/index.d.ts +20 -0
- package/dist/src/proxy/index.js +98 -0
- package/dist/src/proxy/index.js.map +1 -0
- package/dist/src/proxy/proxies/camera.d.ts +26 -0
- package/dist/src/proxy/proxies/camera.js +72 -0
- package/dist/src/proxy/proxies/camera.js.map +1 -0
- package/dist/src/proxy/proxies/plugin.d.ts +30 -0
- package/dist/src/proxy/proxies/plugin.js +110 -0
- package/dist/src/proxy/proxies/plugin.js.map +1 -0
- package/dist/src/proxy/proxies/server.d.ts +34 -0
- package/dist/src/proxy/proxies/server.js +92 -0
- package/dist/src/proxy/proxies/server.js.map +1 -0
- package/dist/src/proxy/types.d.ts +189 -0
- package/dist/src/proxy/types.js +3 -0
- package/dist/src/proxy/types.js.map +1 -0
- package/dist/src/proxy/utils/generateKeys.d.ts +5 -0
- package/dist/src/proxy/utils/generateKeys.js +13 -0
- package/dist/src/proxy/utils/generateKeys.js.map +1 -0
- package/dist/src/proxy/ws/index.d.ts +21 -0
- package/dist/src/proxy/ws/index.js +75 -0
- package/dist/src/proxy/ws/index.js.map +1 -0
- package/dist/src/services/config/index.d.ts +60 -0
- package/dist/src/services/config/index.js +319 -0
- package/dist/src/services/config/index.js.map +1 -0
- package/dist/src/services/config/types.d.ts +153 -0
- package/dist/src/services/config/types.js +13 -0
- package/dist/src/services/config/types.js.map +1 -0
- package/dist/src/services/logger/index.d.ts +37 -0
- package/dist/src/services/logger/index.js +181 -0
- package/dist/src/services/logger/index.js.map +1 -0
- package/package.json +46 -0
- package/webpack.config.js +29 -0
|
@@ -0,0 +1,717 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createZodArraySchema = exports.createZodObjectSchema = exports.createEnumSchema = exports.createZodBooleanSchema = exports.createZodNumberSchema = exports.createZodStringSchema = exports.isPrimitiveType = exports.isAnyOfSchema = exports.isArrayType = exports.isObjectTypeWithButtons = exports.isObjectType = exports.isEnumType = exports.isBooleanType = exports.isNumberType = exports.isStringType = exports.generateArraySchema = exports.generateObjectSchemaWithButtons = exports.generateObjectSchema = exports.generateEnumSchema = exports.generateStringSchema = exports.genereateBooleanSchema = exports.generateNumberSchema = exports.generateZodSchema = exports.generateZodSchemaFromRoot = exports.getValueByPath = exports.schemaPathToConfigPath = exports.configPathToSchemaPath = exports.removeFunctionsFromSchema = exports.removeFunctionsFromSchemaForm = exports.generateJsonFromSchemaWithConfig = exports.generateJsonFromSchema = exports.generateJsonFromRootSchema = exports.generateSchema = exports.generateRootSchema = void 0;
|
|
7
|
+
const zod_1 = require("zod");
|
|
8
|
+
const validator_1 = __importDefault(require("validator"));
|
|
9
|
+
/**
|
|
10
|
+
* Generate root JSON schema from value.
|
|
11
|
+
*
|
|
12
|
+
* @param value - Value.
|
|
13
|
+
* @returns - Root JSON schema.
|
|
14
|
+
*/
|
|
15
|
+
const generateRootSchema = (value) => {
|
|
16
|
+
const schema = {};
|
|
17
|
+
Object.entries(value).forEach(([key, subValue]) => {
|
|
18
|
+
schema[key] = (0, exports.generateSchema)(subValue);
|
|
19
|
+
});
|
|
20
|
+
return { schema };
|
|
21
|
+
};
|
|
22
|
+
exports.generateRootSchema = generateRootSchema;
|
|
23
|
+
/**
|
|
24
|
+
* Generate JSON schema from value.
|
|
25
|
+
*
|
|
26
|
+
* @param value - Value.
|
|
27
|
+
* @returns - JSON schema.
|
|
28
|
+
*/
|
|
29
|
+
const generateSchema = (value) => {
|
|
30
|
+
switch (true) {
|
|
31
|
+
case value === undefined:
|
|
32
|
+
case typeof value === 'undefined':
|
|
33
|
+
case typeof value === 'function':
|
|
34
|
+
case typeof value === 'symbol':
|
|
35
|
+
case value instanceof Date:
|
|
36
|
+
throw new TypeError(`Invalid JSON value: ${String(value)}`);
|
|
37
|
+
// /**
|
|
38
|
+
// * @see https://json-schema.org/understanding-json-schema/reference/null.html
|
|
39
|
+
// */
|
|
40
|
+
// case value === null:
|
|
41
|
+
// return { type: 'null' };
|
|
42
|
+
/**
|
|
43
|
+
* @see https://json-schema.org/understanding-json-schema/reference/numeric.html
|
|
44
|
+
*/
|
|
45
|
+
case typeof value === 'number':
|
|
46
|
+
return (0, exports.generateNumberSchema)(value);
|
|
47
|
+
/**
|
|
48
|
+
* @see https://json-schema.org/understanding-json-schema/reference/boolean.html
|
|
49
|
+
*/
|
|
50
|
+
case typeof value === 'boolean':
|
|
51
|
+
return (0, exports.genereateBooleanSchema)(value);
|
|
52
|
+
/**
|
|
53
|
+
* @see https://json-schema.org/understanding-json-schema/reference/string.html
|
|
54
|
+
*/
|
|
55
|
+
case typeof value === 'string':
|
|
56
|
+
return (0, exports.generateStringSchema)(value);
|
|
57
|
+
/**
|
|
58
|
+
* @see https://json-schema.org/understanding-json-schema/reference/array.html
|
|
59
|
+
*/
|
|
60
|
+
case Array.isArray(value):
|
|
61
|
+
const allPrimitives = value.every((item) => typeof item === 'string' || typeof item === 'number' || typeof item === 'boolean');
|
|
62
|
+
if (allPrimitives) {
|
|
63
|
+
return (0, exports.generateEnumSchema)(value, typeof value[0]);
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
return (0, exports.generateArraySchema)(value);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* @see https://json-schema.org/understanding-json-schema/reference/object.html
|
|
70
|
+
*/
|
|
71
|
+
case value instanceof Object:
|
|
72
|
+
return (0, exports.generateObjectSchema)(value);
|
|
73
|
+
default:
|
|
74
|
+
throw new TypeError(`Invalid JSON value: ${value}`);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
exports.generateSchema = generateSchema;
|
|
78
|
+
/**
|
|
79
|
+
* Generate a JSON object from a given Root JSON schema.
|
|
80
|
+
*
|
|
81
|
+
* @param rootSchema - The Root JSON schema.
|
|
82
|
+
* @returns - The generated JSON object.
|
|
83
|
+
*/
|
|
84
|
+
const generateJsonFromRootSchema = (rootSchema) => {
|
|
85
|
+
const jsonObject = {};
|
|
86
|
+
Object.entries(rootSchema.schema).forEach(([key, jsonSchema]) => {
|
|
87
|
+
jsonObject[key] = (0, exports.generateJsonFromSchema)(jsonSchema);
|
|
88
|
+
});
|
|
89
|
+
return jsonObject;
|
|
90
|
+
};
|
|
91
|
+
exports.generateJsonFromRootSchema = generateJsonFromRootSchema;
|
|
92
|
+
/**
|
|
93
|
+
* Generate a JSON object from a JSON schema.
|
|
94
|
+
*
|
|
95
|
+
* @param jsonSchema - The JSON schema.
|
|
96
|
+
* @returns - The generated JSON object.
|
|
97
|
+
*/
|
|
98
|
+
const generateJsonFromSchema = (jsonSchema) => {
|
|
99
|
+
switch (jsonSchema.type) {
|
|
100
|
+
case 'string':
|
|
101
|
+
case 'number':
|
|
102
|
+
case 'boolean':
|
|
103
|
+
if ((0, exports.isEnumType)(jsonSchema)) {
|
|
104
|
+
if (jsonSchema.multiple) {
|
|
105
|
+
return jsonSchema.defaultValue !== undefined ? [jsonSchema.defaultValue] : undefined;
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
return jsonSchema.defaultValue ?? jsonSchema.enum[0];
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
else if ((0, exports.isStringType)(jsonSchema)) {
|
|
112
|
+
return jsonSchema.defaultValue;
|
|
113
|
+
}
|
|
114
|
+
else if ((0, exports.isNumberType)(jsonSchema)) {
|
|
115
|
+
return jsonSchema.defaultValue;
|
|
116
|
+
}
|
|
117
|
+
else if ((0, exports.isBooleanType)(jsonSchema)) {
|
|
118
|
+
return jsonSchema.defaultValue ?? false;
|
|
119
|
+
}
|
|
120
|
+
case 'object':
|
|
121
|
+
if (jsonSchema.properties) {
|
|
122
|
+
return Object.entries(jsonSchema.properties).reduce((obj, [key, value]) => {
|
|
123
|
+
obj[key] = (0, exports.generateJsonFromSchema)(value);
|
|
124
|
+
return obj;
|
|
125
|
+
}, {});
|
|
126
|
+
}
|
|
127
|
+
return {};
|
|
128
|
+
case 'array':
|
|
129
|
+
if (jsonSchema.items) {
|
|
130
|
+
if ('anyOf' in jsonSchema.items) {
|
|
131
|
+
return jsonSchema.items.anyOf.map(exports.generateJsonFromSchema);
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
return [(0, exports.generateJsonFromSchema)(jsonSchema.items)];
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return [];
|
|
138
|
+
default:
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
exports.generateJsonFromSchema = generateJsonFromSchema;
|
|
143
|
+
/**
|
|
144
|
+
* Generate a JSON object from a given JSON schema and use values from a config file.
|
|
145
|
+
*
|
|
146
|
+
* @param jsonSchema - The JSON schema.
|
|
147
|
+
* @param config - The config file with values to use.
|
|
148
|
+
* @returns - The generated JSON object.
|
|
149
|
+
*/
|
|
150
|
+
const generateJsonFromSchemaWithConfig = (jsonSchema, config, path = '') => {
|
|
151
|
+
switch (jsonSchema.type) {
|
|
152
|
+
case 'string':
|
|
153
|
+
case 'number':
|
|
154
|
+
case 'boolean':
|
|
155
|
+
const value = (0, exports.getValueByPath)(config, path);
|
|
156
|
+
return value !== undefined ? value : (0, exports.generateJsonFromSchema)(jsonSchema);
|
|
157
|
+
case 'object':
|
|
158
|
+
if (jsonSchema.properties) {
|
|
159
|
+
return Object.entries(jsonSchema.properties).reduce((obj, [key, schema]) => {
|
|
160
|
+
const newPath = path + (path.length > 0 ? '.' : '') + key;
|
|
161
|
+
obj[key] = (0, exports.generateJsonFromSchemaWithConfig)(schema, config, newPath);
|
|
162
|
+
return obj;
|
|
163
|
+
}, {});
|
|
164
|
+
}
|
|
165
|
+
return {};
|
|
166
|
+
case 'array':
|
|
167
|
+
if (jsonSchema.items) {
|
|
168
|
+
const items = jsonSchema.items;
|
|
169
|
+
const arrayConfig = (0, exports.getValueByPath)(config, path) ?? [];
|
|
170
|
+
if ((0, exports.isAnyOfSchema)(items)) {
|
|
171
|
+
return arrayConfig.map((itemConfig, index) => (0, exports.generateJsonFromSchemaWithConfig)(items.anyOf[index], itemConfig, path + `[${index}]`));
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
return arrayConfig.map((itemConfig, index) => (0, exports.generateJsonFromSchemaWithConfig)(items, itemConfig, path + `[${index}]`));
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return [];
|
|
178
|
+
default:
|
|
179
|
+
return null;
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
exports.generateJsonFromSchemaWithConfig = generateJsonFromSchemaWithConfig;
|
|
183
|
+
/**
|
|
184
|
+
* Removes all functions from a given Plugin JSON schema form.
|
|
185
|
+
*
|
|
186
|
+
* @param schemaForm - The Plugin JSON schema form from which the functions should be removed.
|
|
187
|
+
* @returns - The Plugin JSON schema form without functions.
|
|
188
|
+
*/
|
|
189
|
+
const removeFunctionsFromSchemaForm = (schemaForm) => {
|
|
190
|
+
const filteredSchemaForm = {};
|
|
191
|
+
Object.keys(schemaForm).forEach((key) => {
|
|
192
|
+
filteredSchemaForm[key] = (0, exports.removeFunctionsFromSchema)(schemaForm[key]);
|
|
193
|
+
});
|
|
194
|
+
return filteredSchemaForm;
|
|
195
|
+
};
|
|
196
|
+
exports.removeFunctionsFromSchemaForm = removeFunctionsFromSchemaForm;
|
|
197
|
+
/**
|
|
198
|
+
* Removes all functions from a given Plugin JSON schema.
|
|
199
|
+
*
|
|
200
|
+
* @param jsonSchema - The JSON schema from which the functions should be removed.
|
|
201
|
+
* @returns - The JSON schema without functions.
|
|
202
|
+
*/
|
|
203
|
+
const removeFunctionsFromSchema = (jsonSchema) => {
|
|
204
|
+
const { onSet, onGet, ...rest } = jsonSchema;
|
|
205
|
+
return rest;
|
|
206
|
+
};
|
|
207
|
+
exports.removeFunctionsFromSchema = removeFunctionsFromSchema;
|
|
208
|
+
/**
|
|
209
|
+
* Converts a config path to a schema path.
|
|
210
|
+
*
|
|
211
|
+
* @param configPath - The config path to be converted.
|
|
212
|
+
* @returns - The converted schema path.
|
|
213
|
+
*/
|
|
214
|
+
const configPathToSchemaPath = (configPath) => {
|
|
215
|
+
const pathParts = configPath.split('.');
|
|
216
|
+
const schemaPathParts = [];
|
|
217
|
+
pathParts.forEach((part, index) => {
|
|
218
|
+
const isNumericIndex = /^\d+$/.test(part);
|
|
219
|
+
const isLastPart = index === pathParts.length - 1;
|
|
220
|
+
if (isNumericIndex) {
|
|
221
|
+
schemaPathParts.push('items', 'properties');
|
|
222
|
+
}
|
|
223
|
+
else {
|
|
224
|
+
schemaPathParts.push(part);
|
|
225
|
+
const isNumericIndex = /^\d+$/.test(pathParts[index + 1]);
|
|
226
|
+
// if (!isLastPart && isNaN(parseInt(pathParts[index + 1]))) {
|
|
227
|
+
if (!isLastPart && !isNumericIndex) {
|
|
228
|
+
schemaPathParts.push('properties');
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
});
|
|
232
|
+
return schemaPathParts.join('.');
|
|
233
|
+
};
|
|
234
|
+
exports.configPathToSchemaPath = configPathToSchemaPath;
|
|
235
|
+
/**
|
|
236
|
+
* Converts a schema path to a config path.
|
|
237
|
+
*
|
|
238
|
+
* @param schemaPath - The schema path to be converted.
|
|
239
|
+
* @returns - The converted config path.
|
|
240
|
+
*/
|
|
241
|
+
const schemaPathToConfigPath = (schemaPath) => {
|
|
242
|
+
const pathParts = schemaPath.split('.');
|
|
243
|
+
const configPathParts = [];
|
|
244
|
+
pathParts.forEach((part, index) => {
|
|
245
|
+
// Exclude 'properties' and 'items' keywords from the path
|
|
246
|
+
if (part !== 'properties' && part !== 'items') {
|
|
247
|
+
// Check if the part is a numeric index and format it as an array index
|
|
248
|
+
const isNumericIndex = /^\d+$/.test(part);
|
|
249
|
+
if (isNumericIndex) {
|
|
250
|
+
configPathParts.push(`[${part}]`);
|
|
251
|
+
}
|
|
252
|
+
else {
|
|
253
|
+
configPathParts.push(part);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
});
|
|
257
|
+
return configPathParts.join('.');
|
|
258
|
+
};
|
|
259
|
+
exports.schemaPathToConfigPath = schemaPathToConfigPath;
|
|
260
|
+
/**
|
|
261
|
+
* Recursively searches a JSON object for a specific, potentially nested key path and returns its value.
|
|
262
|
+
*
|
|
263
|
+
* @param obj - The JSON object to be searched.
|
|
264
|
+
* @param path - The key path to search for.
|
|
265
|
+
* @returns - The value of the searched key path, if found, otherwise undefined.
|
|
266
|
+
*/
|
|
267
|
+
const getValueByPath = (obj, path) => {
|
|
268
|
+
const keys = path.split('.').reduce((acc, key) => {
|
|
269
|
+
if (key.includes('[')) {
|
|
270
|
+
const [pathKey, index] = key.split('[');
|
|
271
|
+
acc.push(pathKey, index.slice(0, -1));
|
|
272
|
+
}
|
|
273
|
+
else {
|
|
274
|
+
acc.push(key);
|
|
275
|
+
}
|
|
276
|
+
return acc;
|
|
277
|
+
}, []);
|
|
278
|
+
let current = obj;
|
|
279
|
+
for (const key of keys) {
|
|
280
|
+
if (current === undefined) {
|
|
281
|
+
return undefined;
|
|
282
|
+
}
|
|
283
|
+
current = current[key];
|
|
284
|
+
}
|
|
285
|
+
return current;
|
|
286
|
+
};
|
|
287
|
+
exports.getValueByPath = getValueByPath;
|
|
288
|
+
/**
|
|
289
|
+
* Generates a Zod schema from a given JSON schema.
|
|
290
|
+
*
|
|
291
|
+
* @param jsonSchema - The JSON schema from which the Zod schema should be generated.
|
|
292
|
+
* @returns - The generated Zod schema.
|
|
293
|
+
*/
|
|
294
|
+
const generateZodSchemaFromRoot = (rootSchema) => {
|
|
295
|
+
const zodObjectSchema = {};
|
|
296
|
+
Object.entries(rootSchema.schema).forEach(([key, jsonSchema]) => {
|
|
297
|
+
zodObjectSchema[key] = (0, exports.generateZodSchema)(jsonSchema);
|
|
298
|
+
});
|
|
299
|
+
return zod_1.z.object(zodObjectSchema).strict();
|
|
300
|
+
};
|
|
301
|
+
exports.generateZodSchemaFromRoot = generateZodSchemaFromRoot;
|
|
302
|
+
/**
|
|
303
|
+
* Generates a Zod schema from a given JSON schema.
|
|
304
|
+
*
|
|
305
|
+
* @param jsonSchema - The JSON schema from which the Zod schema should be generated.
|
|
306
|
+
* @param initial - A boolean indicating whether this is the initial call to the function.
|
|
307
|
+
* This is used for handling nested schemas.
|
|
308
|
+
* @returns - The generated Zod schema.
|
|
309
|
+
*/
|
|
310
|
+
const generateZodSchema = (jsonSchema) => {
|
|
311
|
+
switch (jsonSchema.type) {
|
|
312
|
+
case 'string':
|
|
313
|
+
case 'number':
|
|
314
|
+
case 'boolean':
|
|
315
|
+
if ((0, exports.isEnumType)(jsonSchema)) {
|
|
316
|
+
return (0, exports.createEnumSchema)(jsonSchema);
|
|
317
|
+
}
|
|
318
|
+
else if ((0, exports.isStringType)(jsonSchema)) {
|
|
319
|
+
return (0, exports.createZodStringSchema)(jsonSchema);
|
|
320
|
+
}
|
|
321
|
+
else if ((0, exports.isNumberType)(jsonSchema)) {
|
|
322
|
+
return (0, exports.createZodNumberSchema)(jsonSchema);
|
|
323
|
+
}
|
|
324
|
+
else if ((0, exports.isBooleanType)(jsonSchema)) {
|
|
325
|
+
return (0, exports.createZodBooleanSchema)(jsonSchema);
|
|
326
|
+
}
|
|
327
|
+
case 'object':
|
|
328
|
+
return (0, exports.createZodObjectSchema)(jsonSchema);
|
|
329
|
+
case 'array':
|
|
330
|
+
return (0, exports.createZodArraySchema)(jsonSchema);
|
|
331
|
+
}
|
|
332
|
+
};
|
|
333
|
+
exports.generateZodSchema = generateZodSchema;
|
|
334
|
+
/**
|
|
335
|
+
* Generates a JSON schema for a number.
|
|
336
|
+
*
|
|
337
|
+
* @param value - The number for which the JSON schema should be generated.
|
|
338
|
+
* @returns - The generated JSON schema for the number.
|
|
339
|
+
*/
|
|
340
|
+
const generateNumberSchema = (value) => {
|
|
341
|
+
// return { type: Number.isInteger(value) ? 'integer' : 'number' };
|
|
342
|
+
return { type: 'number' };
|
|
343
|
+
};
|
|
344
|
+
exports.generateNumberSchema = generateNumberSchema;
|
|
345
|
+
/**
|
|
346
|
+
* Generates a JSON schema for a boolean.
|
|
347
|
+
*
|
|
348
|
+
* @param value - The boolean for which the JSON schema should be generated.
|
|
349
|
+
* @returns - The generated JSON schema for the boolean.
|
|
350
|
+
*/
|
|
351
|
+
const genereateBooleanSchema = (value) => {
|
|
352
|
+
return { type: 'boolean' };
|
|
353
|
+
};
|
|
354
|
+
exports.genereateBooleanSchema = genereateBooleanSchema;
|
|
355
|
+
/**
|
|
356
|
+
* Generates a JSON schema for a string.
|
|
357
|
+
*
|
|
358
|
+
* @param value - The string for which the JSON schema should be generated.
|
|
359
|
+
* @returns - The generated JSON schema for the string.
|
|
360
|
+
*/
|
|
361
|
+
const generateStringSchema = (value) => {
|
|
362
|
+
const stringObject = { type: 'string' };
|
|
363
|
+
if (validator_1.default.isISO8601(value)) {
|
|
364
|
+
stringObject.format = value.includes('T') ? 'date-time' : 'date';
|
|
365
|
+
}
|
|
366
|
+
if (validator_1.default.isTime(value.split('+')[0], { mode: 'withSeconds' })) {
|
|
367
|
+
stringObject.format = 'time';
|
|
368
|
+
}
|
|
369
|
+
if (validator_1.default.isEmail(value)) {
|
|
370
|
+
stringObject.format = 'email';
|
|
371
|
+
}
|
|
372
|
+
if (validator_1.default.isUUID(value)) {
|
|
373
|
+
stringObject.format = 'uuid';
|
|
374
|
+
}
|
|
375
|
+
if (validator_1.default.isIP(value, 4)) {
|
|
376
|
+
stringObject.format = 'ipv4';
|
|
377
|
+
}
|
|
378
|
+
if (validator_1.default.isIP(value, 6)) {
|
|
379
|
+
stringObject.format = 'ipv6';
|
|
380
|
+
}
|
|
381
|
+
return stringObject;
|
|
382
|
+
};
|
|
383
|
+
exports.generateStringSchema = generateStringSchema;
|
|
384
|
+
/**
|
|
385
|
+
* Generates a JSON schema for an enum.
|
|
386
|
+
*
|
|
387
|
+
* @param value - The enum for which the JSON schema should be generated.
|
|
388
|
+
* @param type - The type of the enum.
|
|
389
|
+
* @returns - The generated JSON schema for the enum.
|
|
390
|
+
*/
|
|
391
|
+
const generateEnumSchema = (value, type) => {
|
|
392
|
+
return { type, enum: value };
|
|
393
|
+
};
|
|
394
|
+
exports.generateEnumSchema = generateEnumSchema;
|
|
395
|
+
/**
|
|
396
|
+
* Generates a JSON schema for an object.
|
|
397
|
+
*
|
|
398
|
+
* @param value - The object for which the JSON schema should be generated.
|
|
399
|
+
* @returns - The generated JSON schema for the object.
|
|
400
|
+
*/
|
|
401
|
+
const generateObjectSchema = (value) => {
|
|
402
|
+
if (!Object.keys(value).length) {
|
|
403
|
+
return { type: 'object' };
|
|
404
|
+
}
|
|
405
|
+
return {
|
|
406
|
+
type: 'object',
|
|
407
|
+
properties: Object.entries(value).reduce((accumulator, [key, value]) => {
|
|
408
|
+
accumulator[key] = (0, exports.generateSchema)(value);
|
|
409
|
+
accumulator[key].key = key;
|
|
410
|
+
accumulator[key].label = accumulator[key].label || `Label for ${key}`;
|
|
411
|
+
accumulator[key].description = accumulator[key].description || `Description for ${key}`;
|
|
412
|
+
accumulator[key].required = accumulator[key].required === undefined ? true : accumulator[key].required;
|
|
413
|
+
accumulator[key].readonly = accumulator[key].readonly || false;
|
|
414
|
+
return accumulator;
|
|
415
|
+
}, {}),
|
|
416
|
+
};
|
|
417
|
+
};
|
|
418
|
+
exports.generateObjectSchema = generateObjectSchema;
|
|
419
|
+
/**
|
|
420
|
+
* Generates a JSON schema for an object including up to two buttons.
|
|
421
|
+
*
|
|
422
|
+
* @param value - The object for which the JSON schema should be generated.
|
|
423
|
+
* @param buttons - Array of min one button and up to two buttons to be included in the schema.
|
|
424
|
+
* @returns - The generated JSON schema for the object.
|
|
425
|
+
*/
|
|
426
|
+
const generateObjectSchemaWithButtons = (value, buttons) => {
|
|
427
|
+
const baseSchema = (0, exports.generateObjectSchema)(value);
|
|
428
|
+
if (buttons && buttons.length > 0) {
|
|
429
|
+
const firstButton = buttons[0];
|
|
430
|
+
const secondButton = buttons[1];
|
|
431
|
+
if (firstButton) {
|
|
432
|
+
baseSchema.buttons = [firstButton];
|
|
433
|
+
}
|
|
434
|
+
if (secondButton) {
|
|
435
|
+
baseSchema.buttons = [firstButton, secondButton];
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
return baseSchema;
|
|
439
|
+
};
|
|
440
|
+
exports.generateObjectSchemaWithButtons = generateObjectSchemaWithButtons;
|
|
441
|
+
/**
|
|
442
|
+
* Generates a JSON schema for an array.
|
|
443
|
+
*
|
|
444
|
+
* @param array - The array for which the JSON schema should be generated.
|
|
445
|
+
* @returns - The generated JSON schema for the array.
|
|
446
|
+
*/
|
|
447
|
+
const generateArraySchema = (array) => {
|
|
448
|
+
if (array.length === 0) {
|
|
449
|
+
return { type: 'array' };
|
|
450
|
+
}
|
|
451
|
+
const allObjects = array.every((item) => typeof item === 'object' && item !== null && !Array.isArray(item));
|
|
452
|
+
if (allObjects) {
|
|
453
|
+
const schemaProperties = {};
|
|
454
|
+
const keyPresenceCount = {};
|
|
455
|
+
// Gather all keys and their presence count
|
|
456
|
+
array.forEach((obj) => {
|
|
457
|
+
Object.keys(obj).forEach((key) => {
|
|
458
|
+
keyPresenceCount[key] = (keyPresenceCount[key] || 0) + 1;
|
|
459
|
+
});
|
|
460
|
+
});
|
|
461
|
+
array.forEach((obj) => {
|
|
462
|
+
Object.entries(obj).forEach(([key, value]) => {
|
|
463
|
+
if (!schemaProperties[key]) {
|
|
464
|
+
const isRequired = keyPresenceCount[key] === array.length;
|
|
465
|
+
schemaProperties[key] = {
|
|
466
|
+
...(0, exports.generateSchema)(value),
|
|
467
|
+
required: isRequired,
|
|
468
|
+
title: key,
|
|
469
|
+
description: `Description for ${key}`,
|
|
470
|
+
readonly: false,
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
});
|
|
474
|
+
});
|
|
475
|
+
return {
|
|
476
|
+
type: 'array',
|
|
477
|
+
items: { type: 'object', properties: schemaProperties },
|
|
478
|
+
};
|
|
479
|
+
}
|
|
480
|
+
else {
|
|
481
|
+
const uniqueTypes = new Set(array.map((item) => typeof item));
|
|
482
|
+
if (uniqueTypes.size === 1) {
|
|
483
|
+
return { type: 'array', items: (0, exports.generateSchema)(array[0]) };
|
|
484
|
+
}
|
|
485
|
+
else {
|
|
486
|
+
const itemSchemas = array.map(exports.generateSchema);
|
|
487
|
+
return { type: 'array', items: { anyOf: itemSchemas } };
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
};
|
|
491
|
+
exports.generateArraySchema = generateArraySchema;
|
|
492
|
+
/**
|
|
493
|
+
* Checks if the given JSON schema is of string type.
|
|
494
|
+
*
|
|
495
|
+
* @param jsonSchema - The JSON schema to check.
|
|
496
|
+
* @returns - True if the JSON schema is of string type, false otherwise.
|
|
497
|
+
*/
|
|
498
|
+
const isStringType = (jsonSchema) => {
|
|
499
|
+
return jsonSchema.type === 'string' && !('enum' in jsonSchema);
|
|
500
|
+
};
|
|
501
|
+
exports.isStringType = isStringType;
|
|
502
|
+
/**
|
|
503
|
+
* Checks if the given JSON schema is of number type.
|
|
504
|
+
*
|
|
505
|
+
* @param jsonSchema - The JSON schema to check.
|
|
506
|
+
* @returns - True if the JSON schema is of number type, false otherwise.
|
|
507
|
+
*/
|
|
508
|
+
const isNumberType = (jsonSchema) => {
|
|
509
|
+
return jsonSchema.type === 'number' && !('enum' in jsonSchema);
|
|
510
|
+
};
|
|
511
|
+
exports.isNumberType = isNumberType;
|
|
512
|
+
/**
|
|
513
|
+
* Checks if the given JSON schema is of boolean type.
|
|
514
|
+
*
|
|
515
|
+
* @param jsonSchema - The JSON schema to check.
|
|
516
|
+
* @returns - True if the JSON schema is of boolean type, false otherwise.
|
|
517
|
+
*/
|
|
518
|
+
const isBooleanType = (jsonSchema) => {
|
|
519
|
+
return jsonSchema.type === 'boolean' && !('enum' in jsonSchema);
|
|
520
|
+
};
|
|
521
|
+
exports.isBooleanType = isBooleanType;
|
|
522
|
+
/**
|
|
523
|
+
* Checks if the given JSON schema is of enum type.
|
|
524
|
+
*
|
|
525
|
+
* @param jsonSchema - The JSON schema to check.
|
|
526
|
+
* @returns - True if the JSON schema is of enum type, false otherwise.
|
|
527
|
+
*/
|
|
528
|
+
const isEnumType = (jsonSchema) => {
|
|
529
|
+
return (jsonSchema.type === 'string' || jsonSchema.type === 'number' || jsonSchema.type === 'boolean') && 'enum' in jsonSchema;
|
|
530
|
+
};
|
|
531
|
+
exports.isEnumType = isEnumType;
|
|
532
|
+
/**
|
|
533
|
+
* Checks if the given JSON schema is of object type.
|
|
534
|
+
*
|
|
535
|
+
* @param jsonSchema - The JSON schema to check.
|
|
536
|
+
* @returns - True if the JSON schema is of object type, false otherwise.
|
|
537
|
+
*/
|
|
538
|
+
const isObjectType = (jsonSchema) => {
|
|
539
|
+
return jsonSchema.type === 'object';
|
|
540
|
+
};
|
|
541
|
+
exports.isObjectType = isObjectType;
|
|
542
|
+
/**
|
|
543
|
+
* Checks if the given JSON schema is of object type with buttons.
|
|
544
|
+
*
|
|
545
|
+
* @param jsonSchema - The JSON schema to check.
|
|
546
|
+
* @returns - True if the JSON schema is of object type with buttons, false otherwise.
|
|
547
|
+
*/
|
|
548
|
+
const isObjectTypeWithButtons = (jsonSchema) => {
|
|
549
|
+
return (0, exports.isObjectType)(jsonSchema) && 'buttons' in jsonSchema;
|
|
550
|
+
};
|
|
551
|
+
exports.isObjectTypeWithButtons = isObjectTypeWithButtons;
|
|
552
|
+
/**
|
|
553
|
+
* Checks if the given JSON schema is of array type.
|
|
554
|
+
*
|
|
555
|
+
* @param jsonSchema - The JSON schema to check.
|
|
556
|
+
* @returns - True if the JSON schema is of array type, false otherwise.
|
|
557
|
+
*/
|
|
558
|
+
const isArrayType = (jsonSchema) => {
|
|
559
|
+
return jsonSchema.type === 'array';
|
|
560
|
+
};
|
|
561
|
+
exports.isArrayType = isArrayType;
|
|
562
|
+
/**
|
|
563
|
+
* Checks if the given value is a JSON AnyOf Schema.
|
|
564
|
+
*
|
|
565
|
+
* @param value - The value to check.
|
|
566
|
+
* @returns - True if the value is a JSON AnyOf schema, false otherwise.
|
|
567
|
+
*/
|
|
568
|
+
const isAnyOfSchema = (value) => {
|
|
569
|
+
return value && 'anyOf' in value;
|
|
570
|
+
};
|
|
571
|
+
exports.isAnyOfSchema = isAnyOfSchema;
|
|
572
|
+
/**
|
|
573
|
+
* Checks if the given value is a JSON Primitive schema.
|
|
574
|
+
*
|
|
575
|
+
* @param value - The value to check.
|
|
576
|
+
* @returns - True if the value is a JSON schema, false otherwise.
|
|
577
|
+
*/
|
|
578
|
+
const isPrimitiveType = (value) => {
|
|
579
|
+
return 'onGet' in value || 'onSet' in value;
|
|
580
|
+
};
|
|
581
|
+
exports.isPrimitiveType = isPrimitiveType;
|
|
582
|
+
/**
|
|
583
|
+
* Creates a Zod string schema from a given JSON schema.
|
|
584
|
+
*
|
|
585
|
+
* @param jsonSchema - The JSON schema from which the Zod schema should be created.
|
|
586
|
+
* @returns - The created Zod string schema.
|
|
587
|
+
*/
|
|
588
|
+
const createZodStringSchema = (jsonSchema) => {
|
|
589
|
+
let schema = zod_1.z.string();
|
|
590
|
+
if (jsonSchema.required)
|
|
591
|
+
schema = schema.nonempty('Required');
|
|
592
|
+
if (jsonSchema.minLength !== undefined)
|
|
593
|
+
schema = schema.min(jsonSchema.minLength);
|
|
594
|
+
if (jsonSchema.maxLength !== undefined)
|
|
595
|
+
schema = schema.max(jsonSchema.maxLength);
|
|
596
|
+
if (jsonSchema.format) {
|
|
597
|
+
switch (jsonSchema.format) {
|
|
598
|
+
case 'date-time':
|
|
599
|
+
schema = schema.refine((value) => validator_1.default.isISO8601(value) && value.includes('T'), { message: 'Invalid date-time' });
|
|
600
|
+
break;
|
|
601
|
+
case 'date':
|
|
602
|
+
schema = schema.refine((value) => validator_1.default.isISO8601(value), { message: 'Invalid date' });
|
|
603
|
+
break;
|
|
604
|
+
case 'time':
|
|
605
|
+
schema = schema.refine((value) => validator_1.default.isTime(value.split('+')[0], { mode: 'withSeconds' }), { message: 'Invalid time' });
|
|
606
|
+
break;
|
|
607
|
+
case 'email':
|
|
608
|
+
schema = schema.refine((value) => validator_1.default.isEmail(value), { message: 'Invalid email' });
|
|
609
|
+
break;
|
|
610
|
+
case 'uuid':
|
|
611
|
+
schema = schema.refine((value) => validator_1.default.isUUID(value), { message: 'Invalid uuid' });
|
|
612
|
+
break;
|
|
613
|
+
case 'ipv4':
|
|
614
|
+
schema = schema.refine((value) => validator_1.default.isIP(value, 4), { message: 'Invalid ipv4' });
|
|
615
|
+
break;
|
|
616
|
+
case 'ipv6':
|
|
617
|
+
schema = schema.refine((value) => validator_1.default.isIP(value, 6), { message: 'Invalid ipv6' });
|
|
618
|
+
break;
|
|
619
|
+
// case 'password':
|
|
620
|
+
// schema = schema.refine((value) => validator.isStrongPassword(value), { message: 'Invalid password' });
|
|
621
|
+
// break;
|
|
622
|
+
// case 'qrCode':
|
|
623
|
+
// schema = schema.refine((value) => validator.isBase64(value), { message: 'Invalid qrCode' });
|
|
624
|
+
// break;
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
return jsonSchema.required ? schema : schema.optional();
|
|
628
|
+
};
|
|
629
|
+
exports.createZodStringSchema = createZodStringSchema;
|
|
630
|
+
/**
|
|
631
|
+
* Creates a Zod number schema from a given JSON schema.
|
|
632
|
+
*
|
|
633
|
+
* @param jsonSchema - The JSON schema from which the Zod schema should be created.
|
|
634
|
+
* @returns - The created Zod number schema.
|
|
635
|
+
*/
|
|
636
|
+
const createZodNumberSchema = (jsonSchema) => {
|
|
637
|
+
let schema = zod_1.z.number();
|
|
638
|
+
if (jsonSchema.minimum !== undefined)
|
|
639
|
+
schema = schema.min(jsonSchema.minimum);
|
|
640
|
+
if (jsonSchema.maximum !== undefined)
|
|
641
|
+
schema = schema.max(jsonSchema.maximum);
|
|
642
|
+
if (jsonSchema.required)
|
|
643
|
+
schema = schema.refine((value) => value !== undefined && typeof value === 'number', { message: 'Required' });
|
|
644
|
+
return jsonSchema.required ? schema : schema.optional();
|
|
645
|
+
};
|
|
646
|
+
exports.createZodNumberSchema = createZodNumberSchema;
|
|
647
|
+
/**
|
|
648
|
+
* Creates a Zod boolean schema from a given JSON schema.
|
|
649
|
+
*
|
|
650
|
+
* @param jsonSchema - The JSON schema from which the Zod schema should be created.
|
|
651
|
+
* @returns - The created Zod boolean schema.
|
|
652
|
+
*/
|
|
653
|
+
const createZodBooleanSchema = (jsonSchema) => {
|
|
654
|
+
const schema = zod_1.z.boolean();
|
|
655
|
+
return jsonSchema.required ? schema : schema.optional();
|
|
656
|
+
};
|
|
657
|
+
exports.createZodBooleanSchema = createZodBooleanSchema;
|
|
658
|
+
/**
|
|
659
|
+
* Creates a Zod union schema from a given JSON schema.
|
|
660
|
+
*
|
|
661
|
+
* @param jsonSchema - The JSON schema from which the Zod schema should be created.
|
|
662
|
+
* @returns - The created Zod union schema.
|
|
663
|
+
*/
|
|
664
|
+
const createEnumSchema = (jsonSchema) => {
|
|
665
|
+
const enumValues = jsonSchema.enum.map((item) => zod_1.z.literal(item));
|
|
666
|
+
const schema = zod_1.z.union(enumValues);
|
|
667
|
+
if (jsonSchema.multiple) {
|
|
668
|
+
return jsonSchema.required ? schema.array() : schema.array().optional();
|
|
669
|
+
}
|
|
670
|
+
return jsonSchema.required ? schema : schema.optional();
|
|
671
|
+
};
|
|
672
|
+
exports.createEnumSchema = createEnumSchema;
|
|
673
|
+
/**
|
|
674
|
+
* Creates a Zod object schema from a given JSON schema.
|
|
675
|
+
*
|
|
676
|
+
* @param jsonSchema - The JSON schema from which the Zod schema should be created.
|
|
677
|
+
* @returns - The created Zod object schema.
|
|
678
|
+
*/
|
|
679
|
+
const createZodObjectSchema = (jsonSchema) => {
|
|
680
|
+
let schema;
|
|
681
|
+
if (jsonSchema.properties) {
|
|
682
|
+
const zodObjectProperties = Object.entries(jsonSchema.properties).reduce((acc, [key, value]) => {
|
|
683
|
+
acc[key] = (0, exports.generateZodSchema)(value);
|
|
684
|
+
return acc;
|
|
685
|
+
}, {});
|
|
686
|
+
schema = zod_1.z.object(zodObjectProperties).strict();
|
|
687
|
+
}
|
|
688
|
+
else {
|
|
689
|
+
schema = zod_1.z.record(zod_1.z.any());
|
|
690
|
+
}
|
|
691
|
+
return jsonSchema.required ? schema : schema.optional();
|
|
692
|
+
};
|
|
693
|
+
exports.createZodObjectSchema = createZodObjectSchema;
|
|
694
|
+
/**
|
|
695
|
+
* Creates a Zod array schema from a given JSON schema.
|
|
696
|
+
*
|
|
697
|
+
* @param jsonSchema - The JSON schema from which the Zod schema should be created.
|
|
698
|
+
* @returns - The created Zod array schema.
|
|
699
|
+
*/
|
|
700
|
+
const createZodArraySchema = (jsonSchema) => {
|
|
701
|
+
let schema;
|
|
702
|
+
if (jsonSchema.items) {
|
|
703
|
+
if ('anyOf' in jsonSchema.items) {
|
|
704
|
+
const schemas = jsonSchema.items.anyOf.map((itemSchema) => (0, exports.generateZodSchema)(itemSchema));
|
|
705
|
+
schema = zod_1.z.union(schemas).array();
|
|
706
|
+
}
|
|
707
|
+
else {
|
|
708
|
+
schema = (0, exports.generateZodSchema)(jsonSchema.items).array();
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
else {
|
|
712
|
+
schema = zod_1.z.any().array();
|
|
713
|
+
}
|
|
714
|
+
return jsonSchema.required ? schema : schema.optional();
|
|
715
|
+
};
|
|
716
|
+
exports.createZodArraySchema = createZodArraySchema;
|
|
717
|
+
//# sourceMappingURL=schema.js.map
|