@wener/common 1.0.4 → 2.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/lib/cn/DivisionCode.js +55 -32
- package/lib/cn/DivisionCode.test.js +140 -0
- package/lib/cn/Mod11Checksum.js +80 -37
- package/lib/cn/Mod31Checksum.js +89 -40
- package/lib/cn/ResidentIdentityCardNumber.js +16 -16
- package/lib/cn/ResidentIdentityCardNumber.test.js +21 -0
- package/lib/cn/UnifiedSocialCreditCode.js +32 -26
- package/lib/cn/UnifiedSocialCreditCode.test.js +16 -0
- package/lib/cn/formatDate.js +5 -7
- package/lib/cn/index.js +0 -1
- package/lib/cn/parseSex.js +0 -2
- package/lib/cn/types.d.js +0 -2
- package/lib/consola/createStandardConsolaReporter.js +18 -0
- package/lib/consola/formatLogObject.js +226 -0
- package/lib/consola/index.js +2 -0
- package/lib/data/formatSort.js +5 -6
- package/lib/data/formatSort.test.js +34 -0
- package/lib/data/index.js +0 -1
- package/lib/data/maybeNumber.js +11 -5
- package/lib/data/parseSort.js +28 -22
- package/lib/data/parseSort.test.js +188 -0
- package/lib/data/resolvePagination.js +27 -16
- package/lib/data/resolvePagination.test.js +232 -0
- package/lib/data/types.d.js +0 -2
- package/lib/index.js +0 -1
- package/lib/jsonschema/JsonSchema.js +80 -54
- package/lib/jsonschema/JsonSchema.test.js +137 -0
- package/lib/jsonschema/index.js +0 -1
- package/lib/jsonschema/types.d.js +5 -3
- package/lib/meta/defineFileType.js +103 -20
- package/lib/meta/defineInit.js +250 -31
- package/lib/meta/defineMetadata.js +140 -24
- package/lib/meta/defineMetadata.test.js +13 -0
- package/lib/meta/index.js +0 -1
- package/lib/password/PHC.js +87 -63
- package/lib/password/PHC.test.js +539 -0
- package/lib/password/Password.js +291 -29
- package/lib/password/Password.test.js +362 -0
- package/lib/password/createArgon2PasswordAlgorithm.js +191 -35
- package/lib/password/createBase64PasswordAlgorithm.js +141 -8
- package/lib/password/createBcryptPasswordAlgorithm.js +168 -13
- package/lib/password/createPBKDF2PasswordAlgorithm.js +228 -46
- package/lib/password/createScryptPasswordAlgorithm.js +211 -55
- package/lib/password/index.js +0 -1
- package/lib/password/server/index.js +0 -1
- package/lib/resource/Identifiable.js +1 -0
- package/lib/resource/getTitleOfResource.js +10 -0
- package/lib/resource/index.js +1 -0
- package/lib/resource/schema/AnyResourceSchema.js +89 -0
- package/lib/resource/schema/BaseResourceSchema.js +29 -0
- package/lib/resource/schema/ResourceActionType.js +118 -0
- package/lib/resource/schema/ResourceStatus.js +93 -0
- package/lib/resource/schema/ResourceType.js +24 -0
- package/lib/resource/schema/SchemaRegistry.js +38 -0
- package/lib/resource/schema/SexType.js +10 -0
- package/lib/resource/schema/types.js +89 -0
- package/lib/resource/schema/types.test.js +14 -0
- package/lib/schema/TypeSchema.d.js +1 -0
- package/lib/schema/createSchemaData.js +173 -0
- package/lib/schema/findJsonSchemaByPath.js +49 -0
- package/lib/schema/getSchemaCache.js +11 -0
- package/lib/schema/getSchemaOptions.js +24 -0
- package/lib/schema/index.js +5 -0
- package/lib/schema/toJsonSchema.js +441 -0
- package/lib/schema/toJsonSchema.test.js +27 -0
- package/lib/schema/validate.js +124 -0
- package/lib/search/AdvanceSearch.js +0 -1
- package/lib/search/AdvanceSearch.test.js +435 -0
- package/lib/search/formatAdvanceSearch.js +41 -27
- package/lib/search/index.js +0 -1
- package/lib/search/optimizeAdvanceSearch.js +79 -25
- package/lib/search/parseAdvanceSearch.js +5 -5
- package/lib/search/parser.d.js +0 -2
- package/lib/search/parser.js +97 -74
- package/lib/search/types.d.js +0 -2
- package/lib/tools/generateSchema.js +201 -0
- package/lib/tools/renderJsonSchemaToMarkdownDoc.js +143 -55
- package/package.json +30 -9
- package/src/consola/createStandardConsolaReporter.ts +31 -0
- package/src/consola/formatLogObject.ts +171 -0
- package/src/consola/index.ts +2 -0
- package/src/data/maybeNumber.ts +5 -1
- package/src/data/resolvePagination.test.ts +1 -1
- package/src/data/resolvePagination.ts +18 -7
- package/src/data/types.d.ts +12 -0
- package/src/jsonschema/JsonSchema.test.ts +17 -0
- package/src/jsonschema/JsonSchema.ts +4 -4
- package/src/jsonschema/types.d.ts +63 -12
- package/src/resource/Identifiable.ts +3 -0
- package/src/resource/getTitleOfResource.tsx +6 -0
- package/src/resource/index.ts +3 -0
- package/src/resource/schema/AnyResourceSchema.ts +113 -0
- package/src/resource/schema/BaseResourceSchema.ts +32 -0
- package/src/resource/schema/ResourceActionType.ts +123 -0
- package/src/resource/schema/ResourceStatus.ts +94 -0
- package/src/resource/schema/ResourceType.ts +25 -0
- package/src/resource/schema/SchemaRegistry.ts +42 -0
- package/src/resource/schema/SexType.ts +13 -0
- package/src/resource/schema/types.test.ts +18 -0
- package/src/resource/schema/types.ts +105 -0
- package/src/schema/TypeSchema.d.ts +32 -0
- package/src/schema/createSchemaData.ts +81 -0
- package/src/schema/findJsonSchemaByPath.ts +37 -0
- package/src/schema/getSchemaCache.ts +21 -0
- package/src/schema/getSchemaOptions.ts +24 -0
- package/src/schema/index.ts +6 -0
- package/src/schema/toJsonSchema.test.ts +37 -0
- package/src/schema/toJsonSchema.ts +200 -0
- package/src/schema/validate.ts +135 -0
- package/src/tools/generateSchema.ts +39 -0
- package/lib/cn/DivisionCode.js.map +0 -1
- package/lib/cn/Mod11Checksum.js.map +0 -1
- package/lib/cn/Mod31Checksum.js.map +0 -1
- package/lib/cn/ResidentIdentityCardNumber.js.map +0 -1
- package/lib/cn/UnifiedSocialCreditCode.js.map +0 -1
- package/lib/cn/formatDate.js.map +0 -1
- package/lib/cn/index.js.map +0 -1
- package/lib/cn/parseSex.js.map +0 -1
- package/lib/cn/types.d.js.map +0 -1
- package/lib/data/formatSort.js.map +0 -1
- package/lib/data/index.js.map +0 -1
- package/lib/data/maybeNumber.js.map +0 -1
- package/lib/data/parseSort.js.map +0 -1
- package/lib/data/resolvePagination.js.map +0 -1
- package/lib/data/types.d.js.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/jsonschema/JsonSchema.js.map +0 -1
- package/lib/jsonschema/index.js.map +0 -1
- package/lib/jsonschema/types.d.js.map +0 -1
- package/lib/meta/defineFileType.js.map +0 -1
- package/lib/meta/defineInit.js.map +0 -1
- package/lib/meta/defineMetadata.js.map +0 -1
- package/lib/meta/index.js.map +0 -1
- package/lib/password/PHC.js.map +0 -1
- package/lib/password/Password.js.map +0 -1
- package/lib/password/createArgon2PasswordAlgorithm.js.map +0 -1
- package/lib/password/createBase64PasswordAlgorithm.js.map +0 -1
- package/lib/password/createBcryptPasswordAlgorithm.js.map +0 -1
- package/lib/password/createPBKDF2PasswordAlgorithm.js.map +0 -1
- package/lib/password/createScryptPasswordAlgorithm.js.map +0 -1
- package/lib/password/index.js.map +0 -1
- package/lib/password/server/index.js.map +0 -1
- package/lib/search/AdvanceSearch.js.map +0 -1
- package/lib/search/formatAdvanceSearch.js.map +0 -1
- package/lib/search/index.js.map +0 -1
- package/lib/search/optimizeAdvanceSearch.js.map +0 -1
- package/lib/search/parseAdvanceSearch.js.map +0 -1
- package/lib/search/parser.d.js.map +0 -1
- package/lib/search/parser.js.map +0 -1
- package/lib/search/types.d.js.map +0 -1
- package/lib/tools/renderJsonSchemaToMarkdownDoc.js.map +0 -1
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { getGlobalStates } from "@wener/utils";
|
|
2
|
+
import { toJsonSchema } from "../../schema/index.js";
|
|
3
|
+
var types = getGlobalStates("@wener/common/resource/schema/SchemaRegistry", function () {
|
|
4
|
+
return new Map();
|
|
5
|
+
});
|
|
6
|
+
export function get(needle) {
|
|
7
|
+
var key = getKey(needle);
|
|
8
|
+
var found = types.get(key);
|
|
9
|
+
if (found) {
|
|
10
|
+
return found;
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
if (typeof needle !== "string") {
|
|
14
|
+
return toJsonSchema(needle);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
throw new Error("Schema not found: ".concat(key));
|
|
18
|
+
}
|
|
19
|
+
function getKey(s) {
|
|
20
|
+
var key;
|
|
21
|
+
if (typeof s === "string") {
|
|
22
|
+
key = s;
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
var js = toJsonSchema(s);
|
|
26
|
+
key = js.$id || js.title;
|
|
27
|
+
}
|
|
28
|
+
if (!key) {
|
|
29
|
+
throw new Error("Schema must have $id or title");
|
|
30
|
+
}
|
|
31
|
+
return key;
|
|
32
|
+
}
|
|
33
|
+
export function add(def) {
|
|
34
|
+
types.set(getKey(def), toJsonSchema(def));
|
|
35
|
+
}
|
|
36
|
+
export function set(key, def) {
|
|
37
|
+
types.set(getKey(key), toJsonSchema(def));
|
|
38
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { z } from 'zod/v4';
|
|
2
|
+
export var SexType = Object.freeze({
|
|
3
|
+
__proto__: null,
|
|
4
|
+
Male: 'Male',
|
|
5
|
+
Female: 'Female'
|
|
6
|
+
});
|
|
7
|
+
export var SexTypeSchema = z.union([
|
|
8
|
+
z.literal(SexType.Male).describe('男'),
|
|
9
|
+
z.literal(SexType.Female).describe('女')
|
|
10
|
+
]).describe('性别');
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { isValid } from 'date-fns';
|
|
2
|
+
import dayjs from 'dayjs';
|
|
3
|
+
import { z } from 'zod/v4';
|
|
4
|
+
var TypeIdSchema = z.string().regex(/^[a-zA-Z0-9]+_[a-zA-Z0-9]+$/, {
|
|
5
|
+
error: 'ID格式错误'
|
|
6
|
+
}).describe('ID');
|
|
7
|
+
/*
|
|
8
|
+
XXX-XXX-XXXX
|
|
9
|
+
National: (XXX) XXX-XXXX
|
|
10
|
+
Human: XXX.XXX.XXXX xXXX
|
|
11
|
+
International: +XX (XXX) XXX-XXXX
|
|
12
|
+
|
|
13
|
+
中国
|
|
14
|
+
XXX XXXX XXXX
|
|
15
|
+
XXX-XXXX-XXXX
|
|
16
|
+
XXXXXXXXXXX
|
|
17
|
+
+86 XXX XXXX XXXX
|
|
18
|
+
+86 XXXXXXXXXXX
|
|
19
|
+
|
|
20
|
+
0XX-XXXXXXXX
|
|
21
|
+
0XXX-XXXXXXXX
|
|
22
|
+
(0XX) XXXXXXXX
|
|
23
|
+
0XX XXXX XXXX
|
|
24
|
+
0XXXXXXXX
|
|
25
|
+
|
|
26
|
+
https://uibakery.io/regex-library/phone-number
|
|
27
|
+
https://fakerjs.dev/api/phone
|
|
28
|
+
*/ var PhoneNumberSchema = z.string().trim().max(20).regex(/(^$)|(^[0-9]*$)/, {
|
|
29
|
+
error: '电话号码只能包含数字'
|
|
30
|
+
}).meta({
|
|
31
|
+
format: 'phone'
|
|
32
|
+
});
|
|
33
|
+
function resourceIdOf(entity) {
|
|
34
|
+
return rz.resourceId.meta({
|
|
35
|
+
'x-ref-entity': entity
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
// allow empty string
|
|
39
|
+
var JsonDateSchema = z.coerce.string().trim().overwrite(function(v) {
|
|
40
|
+
if (v && !/^\d{4}-\d{2}-\d{2}$/.test(v)) {
|
|
41
|
+
var val = dayjs(v);
|
|
42
|
+
if (val.isValid()) {
|
|
43
|
+
return val.format('YYYY-MM-DD');
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return v;
|
|
47
|
+
}).regex(/(^$)|(^\d{4}-\d{2}-\d{2}$)/, {
|
|
48
|
+
error: '错误的日期格式'
|
|
49
|
+
}).refine(function(value) {
|
|
50
|
+
return !value || isValid(new Date(value));
|
|
51
|
+
}, {
|
|
52
|
+
error: '错误的日期'
|
|
53
|
+
}).nullish().overwrite(function(v) {
|
|
54
|
+
return v || undefined;
|
|
55
|
+
}).meta({
|
|
56
|
+
format: 'date'
|
|
57
|
+
});
|
|
58
|
+
var JsonDateTimeSchema = z.coerce.date().meta({
|
|
59
|
+
format: 'date-time',
|
|
60
|
+
type: 'string'
|
|
61
|
+
});
|
|
62
|
+
var LoginNameSchema = z.string().trim().regex(/^[a-z0-9]{3,16}$/, {
|
|
63
|
+
error: '登录名只能包含小写字母和数字,长度为3-16位'
|
|
64
|
+
});
|
|
65
|
+
// maybe check valid cjk
|
|
66
|
+
var FriendlyNameSchema = z.string().trim().max(50).refine(function(v) {
|
|
67
|
+
return !RegExp("\\p{C}", "u").test(v);
|
|
68
|
+
}, {
|
|
69
|
+
message: '包含无效字符'
|
|
70
|
+
}).describe('名称');
|
|
71
|
+
var PasswordSchema = z.string().min(6, {
|
|
72
|
+
error: '密码长度至少 6 位'
|
|
73
|
+
}).max(36, {
|
|
74
|
+
error: '密码长度最多 36 位'
|
|
75
|
+
}).regex(/^\S*$/, {
|
|
76
|
+
error: '密码不能包含空格'
|
|
77
|
+
}).describe('密码').meta({
|
|
78
|
+
sensitive: true
|
|
79
|
+
});
|
|
80
|
+
export var rz = {
|
|
81
|
+
resourceIdOf: resourceIdOf,
|
|
82
|
+
date: JsonDateSchema,
|
|
83
|
+
dateTime: JsonDateTimeSchema,
|
|
84
|
+
resourceId: TypeIdSchema,
|
|
85
|
+
phoneNumber: PhoneNumberSchema,
|
|
86
|
+
loginName: LoginNameSchema,
|
|
87
|
+
password: PasswordSchema,
|
|
88
|
+
friendlyName: FriendlyNameSchema
|
|
89
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { describe, it } from "vitest";
|
|
2
|
+
import { createSchemaData, toJsonSchema } from "../../schema/index.js";
|
|
3
|
+
import { renderJsonSchemaToMarkdownDoc } from "../../tools/renderJsonSchemaToMarkdownDoc.js";
|
|
4
|
+
import { AnyResourceSchema } from "./AnyResourceSchema.js";
|
|
5
|
+
describe("schema", function () {
|
|
6
|
+
it("should be convert to jsonschema", function () {
|
|
7
|
+
console.log(renderJsonSchemaToMarkdownDoc(toJsonSchema(AnyResourceSchema)));
|
|
8
|
+
});
|
|
9
|
+
it("should create from schema", function () {
|
|
10
|
+
console.log(createSchemaData(AnyResourceSchema, {
|
|
11
|
+
all: true
|
|
12
|
+
}));
|
|
13
|
+
});
|
|
14
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
function _array_like_to_array(arr, len) {
|
|
2
|
+
if (len == null || len > arr.length)
|
|
3
|
+
len = arr.length;
|
|
4
|
+
for (var i = 0, arr2 = new Array(len); i < len; i++)
|
|
5
|
+
arr2[i] = arr[i];
|
|
6
|
+
return arr2;
|
|
7
|
+
}
|
|
8
|
+
function _array_with_holes(arr) {
|
|
9
|
+
if (Array.isArray(arr))
|
|
10
|
+
return arr;
|
|
11
|
+
}
|
|
12
|
+
function _iterable_to_array_limit(arr, i) {
|
|
13
|
+
var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
|
|
14
|
+
if (_i == null)
|
|
15
|
+
return;
|
|
16
|
+
var _arr = [];
|
|
17
|
+
var _n = true;
|
|
18
|
+
var _d = false;
|
|
19
|
+
var _s, _e;
|
|
20
|
+
try {
|
|
21
|
+
for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) {
|
|
22
|
+
_arr.push(_s.value);
|
|
23
|
+
if (i && _arr.length === i)
|
|
24
|
+
break;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
catch (err) {
|
|
28
|
+
_d = true;
|
|
29
|
+
_e = err;
|
|
30
|
+
}
|
|
31
|
+
finally {
|
|
32
|
+
try {
|
|
33
|
+
if (!_n && _i["return"] != null)
|
|
34
|
+
_i["return"]();
|
|
35
|
+
}
|
|
36
|
+
finally {
|
|
37
|
+
if (_d)
|
|
38
|
+
throw _e;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return _arr;
|
|
42
|
+
}
|
|
43
|
+
function _non_iterable_rest() {
|
|
44
|
+
throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
45
|
+
}
|
|
46
|
+
function _sliced_to_array(arr, i) {
|
|
47
|
+
return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
|
|
48
|
+
}
|
|
49
|
+
function _unsupported_iterable_to_array(o, minLen) {
|
|
50
|
+
if (!o)
|
|
51
|
+
return;
|
|
52
|
+
if (typeof o === "string")
|
|
53
|
+
return _array_like_to_array(o, minLen);
|
|
54
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
55
|
+
if (n === "Object" && o.constructor)
|
|
56
|
+
n = o.constructor.name;
|
|
57
|
+
if (n === "Map" || n === "Set")
|
|
58
|
+
return Array.from(n);
|
|
59
|
+
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))
|
|
60
|
+
return _array_like_to_array(o, minLen);
|
|
61
|
+
}
|
|
62
|
+
import { match, P } from "ts-pattern";
|
|
63
|
+
import { toJsonSchema } from "./toJsonSchema.js";
|
|
64
|
+
export function createSchemaData(ts) {
|
|
65
|
+
var options = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
|
|
66
|
+
var schema = toJsonSchema(ts);
|
|
67
|
+
return createJsonSchemaData(schema, options);
|
|
68
|
+
}
|
|
69
|
+
function createJsonSchemaData(schema, options) {
|
|
70
|
+
var skip = function (s, ctx) {
|
|
71
|
+
return Boolean(!ctx.required && s.nullable);
|
|
72
|
+
};
|
|
73
|
+
if (options.all) {
|
|
74
|
+
skip = function () {
|
|
75
|
+
return false;
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
if (options.skip) {
|
|
79
|
+
skip = options.skip;
|
|
80
|
+
}
|
|
81
|
+
return _create(schema, {
|
|
82
|
+
skip: skip
|
|
83
|
+
}, {
|
|
84
|
+
required: false
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
function _create(schema, options, ctx) {
|
|
88
|
+
var skip = options.skip;
|
|
89
|
+
if (skip(schema, ctx)) {
|
|
90
|
+
return schema.default;
|
|
91
|
+
}
|
|
92
|
+
if (schema.default !== undefined) {
|
|
93
|
+
return schema.default;
|
|
94
|
+
}
|
|
95
|
+
return match(schema).returnType().with({
|
|
96
|
+
default: P.select()
|
|
97
|
+
}, function (v) {
|
|
98
|
+
return v;
|
|
99
|
+
}).with({
|
|
100
|
+
const: P.nonNullable
|
|
101
|
+
}, function (v) {
|
|
102
|
+
return v.const;
|
|
103
|
+
}).with({
|
|
104
|
+
anyOf: P.nonNullable
|
|
105
|
+
}, function (schema) {
|
|
106
|
+
return _create(schema.anyOf[0], options, {
|
|
107
|
+
required: false
|
|
108
|
+
});
|
|
109
|
+
}).with({
|
|
110
|
+
oneOf: P.nonNullable
|
|
111
|
+
}, function (schema) {
|
|
112
|
+
return _create(schema.oneOf[0], options, {
|
|
113
|
+
required: false
|
|
114
|
+
});
|
|
115
|
+
}).with({
|
|
116
|
+
type: "string"
|
|
117
|
+
}, function (schema) {
|
|
118
|
+
return "";
|
|
119
|
+
}).with({
|
|
120
|
+
type: P.union("number", "integer")
|
|
121
|
+
}, function (schema) {
|
|
122
|
+
return 0;
|
|
123
|
+
}).with({
|
|
124
|
+
type: "object"
|
|
125
|
+
}, function () {
|
|
126
|
+
var out = {};
|
|
127
|
+
var required = schema.required || [];
|
|
128
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
129
|
+
try {
|
|
130
|
+
for (var _iterator = Object.entries(schema.properties || {})[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
|
131
|
+
var _step_value = _sliced_to_array(_step.value, 2), k = _step_value[0], v = _step_value[1];
|
|
132
|
+
var value = _create(v, options, {
|
|
133
|
+
required: required.includes(k)
|
|
134
|
+
});
|
|
135
|
+
if (value === undefined) {
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
out[k] = value;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
catch (err) {
|
|
142
|
+
_didIteratorError = true;
|
|
143
|
+
_iteratorError = err;
|
|
144
|
+
}
|
|
145
|
+
finally {
|
|
146
|
+
try {
|
|
147
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
148
|
+
_iterator.return();
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
finally {
|
|
152
|
+
if (_didIteratorError) {
|
|
153
|
+
throw _iteratorError;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return out;
|
|
158
|
+
}).with({
|
|
159
|
+
type: "null"
|
|
160
|
+
}, function () {
|
|
161
|
+
return null;
|
|
162
|
+
}).with({
|
|
163
|
+
type: "boolean"
|
|
164
|
+
}, function (schema) {
|
|
165
|
+
return false;
|
|
166
|
+
}).with({
|
|
167
|
+
type: "array"
|
|
168
|
+
}, function (schema) {
|
|
169
|
+
return [];
|
|
170
|
+
}).otherwise(function () {
|
|
171
|
+
return undefined;
|
|
172
|
+
});
|
|
173
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
function _type_of(obj) {
|
|
2
|
+
"@swc/helpers - typeof";
|
|
3
|
+
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
|
|
4
|
+
}
|
|
5
|
+
import { firstOfMaybeArray } from "@wener/utils";
|
|
6
|
+
import { toJsonSchema } from "./toJsonSchema.js";
|
|
7
|
+
export function findJsonSchemaByPath(schema, objectPath) {
|
|
8
|
+
schema = toJsonSchema(schema);
|
|
9
|
+
if (!objectPath || !schema) {
|
|
10
|
+
return undefined;
|
|
11
|
+
}
|
|
12
|
+
var segments = objectPath.split(".");
|
|
13
|
+
var currentSchema = schema;
|
|
14
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
15
|
+
try {
|
|
16
|
+
for (var _iterator = segments[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
|
17
|
+
var segment = _step.value;
|
|
18
|
+
// 检查当前 schema 是否是对象类型且有 properties
|
|
19
|
+
if (currentSchema && (typeof currentSchema === "undefined" ? "undefined" : _type_of(currentSchema)) === "object" && currentSchema.properties && currentSchema.properties[segment]) {
|
|
20
|
+
currentSchema = currentSchema.properties[segment];
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
if (/^\d$/.test(segment) || segment === "[]") {
|
|
24
|
+
if (currentSchema.items) {
|
|
25
|
+
currentSchema = firstOfMaybeArray(currentSchema.items);
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
catch (err) {
|
|
33
|
+
_didIteratorError = true;
|
|
34
|
+
_iteratorError = err;
|
|
35
|
+
}
|
|
36
|
+
finally {
|
|
37
|
+
try {
|
|
38
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
39
|
+
_iterator.return();
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
finally {
|
|
43
|
+
if (_didIteratorError) {
|
|
44
|
+
throw _iteratorError;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return currentSchema;
|
|
49
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
var _cache = new WeakMap();
|
|
2
|
+
export function getSchemaCache(obj, key, compute) {
|
|
3
|
+
var _c, _key;
|
|
4
|
+
var c = _cache.get(obj);
|
|
5
|
+
if (!c) {
|
|
6
|
+
c = {};
|
|
7
|
+
_cache.set(obj, c);
|
|
8
|
+
}
|
|
9
|
+
var _;
|
|
10
|
+
return (_ = (_c = c)[_key = key]) !== null && _ !== void 0 ? _ : _c[_key] = compute();
|
|
11
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { deepFreeze } from "@wener/utils";
|
|
2
|
+
import { getSchemaCache } from "./getSchemaCache.js";
|
|
3
|
+
import { toJsonSchema } from "./toJsonSchema.js";
|
|
4
|
+
export function getSchemaOptions(s) {
|
|
5
|
+
var js = toJsonSchema(s);
|
|
6
|
+
return getSchemaCache(s, "options", function () {
|
|
7
|
+
var _this;
|
|
8
|
+
var out = ((_this = js.anyOf || js.oneOf) === null || _this === void 0 ? void 0 : _this.map(function (v) {
|
|
9
|
+
var value = v.const;
|
|
10
|
+
return {
|
|
11
|
+
label: String(v.description || v.title || value),
|
|
12
|
+
value: value ? String(value) : ""
|
|
13
|
+
};
|
|
14
|
+
})) || [];
|
|
15
|
+
out = deepFreeze(out);
|
|
16
|
+
return out;
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
export function getSchemaOptionLabel(schema, value) {
|
|
20
|
+
var _getSchemaOptions_find;
|
|
21
|
+
return (_getSchemaOptions_find = getSchemaOptions(schema).find(function (v) {
|
|
22
|
+
return v.value === value;
|
|
23
|
+
})) === null || _getSchemaOptions_find === void 0 ? void 0 : _getSchemaOptions_find.label;
|
|
24
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { isJsonSchema, isZodSchema, isTypeBoxSchema, validate, parseData } from "./validate.js";
|
|
2
|
+
export { getSchemaOptions, getSchemaOptionLabel } from "./getSchemaOptions.js";
|
|
3
|
+
export { toJsonSchema } from "./toJsonSchema.js";
|
|
4
|
+
export { findJsonSchemaByPath } from "./findJsonSchemaByPath.js";
|
|
5
|
+
export { createSchemaData } from "./createSchemaData.js";
|