@wener/common 1.0.5 → 2.0.2
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/ChineseResidentIdNo.js +41 -0
- package/lib/cn/ChineseResidentIdNo.mod.js +1 -0
- package/lib/cn/ChineseResidentIdNo.test.js +22 -0
- package/lib/cn/DivisionCode.js +214 -280
- package/lib/cn/DivisionCode.mod.js +1 -0
- package/lib/cn/DivisionCode.test.js +134 -0
- package/lib/cn/Mod11.js +86 -0
- package/lib/cn/Mod31.js +98 -0
- package/lib/cn/UnifiedSocialCreditCode.js +130 -109
- package/lib/cn/UnifiedSocialCreditCode.mod.js +1 -0
- package/lib/cn/UnifiedSocialCreditCode.test.js +16 -0
- package/lib/cn/index.js +1 -4
- package/lib/cn/mod.js +6 -0
- package/lib/cn/types.d.js +0 -2
- package/lib/consola/createStandardConsolaReporter.js +6 -6
- package/lib/consola/formatLogObject.js +147 -34
- package/lib/consola/index.js +0 -1
- 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/dayjs/dayjs.js +38 -0
- package/lib/dayjs/formatDuration.js +58 -0
- package/lib/dayjs/formatDuration.test.js +90 -0
- package/lib/dayjs/index.js +3 -0
- package/lib/dayjs/parseDuration.js +32 -0
- package/lib/decimal/index.js +1 -0
- package/lib/decimal/parseDecimal.js +13 -0
- package/lib/foundation/schema/SexType.js +14 -0
- package/lib/foundation/schema/index.js +1 -0
- package/lib/foundation/schema/parseSexType.js +18 -0
- package/lib/foundation/schema/types.js +5 -0
- package/lib/index.js +0 -1
- package/lib/jsonschema/JsonSchema.js +78 -52
- 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 +295 -30
- 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/ListQuery.js +119 -0
- package/lib/resource/getTitleOfResource.js +10 -0
- package/lib/resource/index.js +2 -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/index.js +5 -0
- package/lib/resource/schema/types.js +89 -0
- package/lib/resource/schema/types.test.js +14 -0
- package/lib/schema/SchemaRegistry.js +45 -0
- package/lib/schema/SchemaRegistry.mod.js +2 -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 +6 -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/tools/generateSchema.js +197 -39
- package/lib/tools/renderJsonSchemaToMarkdownDoc.js +143 -55
- package/lib/utils/getEstimateProcessTime.js +20 -0
- package/lib/utils/index.js +1 -0
- package/package.json +38 -17
- package/src/cn/ChineseResidentIdNo.mod.ts +7 -0
- package/src/cn/ChineseResidentIdNo.test.ts +18 -0
- package/src/cn/ChineseResidentIdNo.ts +66 -0
- package/src/cn/DivisionCode.mod.ts +7 -0
- package/src/cn/DivisionCode.test.ts +3 -13
- package/src/cn/DivisionCode.ts +132 -195
- package/src/cn/{Mod11Checksum.ts → Mod11.ts} +3 -1
- package/src/cn/{Mod31Checksum.ts → Mod31.ts} +2 -0
- package/src/cn/UnifiedSocialCreditCode.mod.ts +7 -0
- package/src/cn/UnifiedSocialCreditCode.test.ts +2 -2
- package/src/cn/UnifiedSocialCreditCode.ts +105 -125
- package/src/cn/__snapshots__/ChineseResidentIdNo.test.ts.snap +14 -0
- package/src/cn/__snapshots__/UnifiedSocialCreditCode.test.ts.snap +18 -12
- package/src/cn/index.ts +1 -3
- package/src/cn/mod.ts +3 -0
- package/src/consola/formatLogObject.ts +12 -4
- 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/dayjs/dayjs.ts +40 -0
- package/src/dayjs/formatDuration.test.ts +14 -0
- package/src/dayjs/formatDuration.ts +86 -0
- package/src/dayjs/index.ts +3 -0
- package/src/dayjs/parseDuration.ts +40 -0
- package/src/decimal/index.ts +1 -0
- package/src/decimal/parseDecimal.ts +16 -0
- package/src/foundation/schema/SexType.ts +21 -0
- package/src/foundation/schema/index.ts +1 -0
- package/src/foundation/schema/parseSexType.ts +19 -0
- package/src/foundation/schema/types.ts +8 -0
- package/src/jsonschema/JsonSchema.test.ts +17 -0
- package/src/jsonschema/JsonSchema.ts +2 -2
- package/src/jsonschema/types.d.ts +63 -12
- package/src/password/Password.ts +2 -2
- package/src/resource/Identifiable.ts +3 -0
- package/src/resource/ListQuery.ts +53 -0
- package/src/resource/getTitleOfResource.tsx +6 -0
- package/src/resource/index.ts +4 -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/index.ts +5 -0
- package/src/resource/schema/types.test.ts +18 -0
- package/src/resource/schema/types.ts +105 -0
- package/src/schema/SchemaRegistry.mod.ts +1 -0
- package/src/schema/SchemaRegistry.ts +46 -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 +7 -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 +28 -28
- package/src/utils/getEstimateProcessTime.ts +36 -0
- package/src/utils/index.ts +1 -0
- package/lib/cn/DivisionCode.js.map +0 -1
- package/lib/cn/Mod11Checksum.js +0 -42
- package/lib/cn/Mod11Checksum.js.map +0 -1
- package/lib/cn/Mod31Checksum.js +0 -48
- package/lib/cn/Mod31Checksum.js.map +0 -1
- package/lib/cn/ResidentIdentityCardNumber.js +0 -50
- package/lib/cn/ResidentIdentityCardNumber.js.map +0 -1
- package/lib/cn/UnifiedSocialCreditCode.js.map +0 -1
- package/lib/cn/formatDate.js +0 -15
- package/lib/cn/formatDate.js.map +0 -1
- package/lib/cn/index.js.map +0 -1
- package/lib/cn/parseSex.js +0 -22
- package/lib/cn/parseSex.js.map +0 -1
- package/lib/cn/types.d.js.map +0 -1
- package/lib/consola/createStandardConsolaReporter.js.map +0 -1
- package/lib/consola/formatLogObject.js.map +0 -1
- package/lib/consola/index.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 +0 -10
- package/lib/search/AdvanceSearch.js.map +0 -1
- package/lib/search/formatAdvanceSearch.js +0 -64
- package/lib/search/formatAdvanceSearch.js.map +0 -1
- package/lib/search/index.js +0 -2
- package/lib/search/index.js.map +0 -1
- package/lib/search/optimizeAdvanceSearch.js +0 -89
- package/lib/search/optimizeAdvanceSearch.js.map +0 -1
- package/lib/search/parseAdvanceSearch.js +0 -20
- package/lib/search/parseAdvanceSearch.js.map +0 -1
- package/lib/search/parser.d.js +0 -3
- package/lib/search/parser.d.js.map +0 -1
- package/lib/search/parser.js +0 -3065
- package/lib/search/parser.js.map +0 -1
- package/lib/search/types.d.js +0 -3
- package/lib/search/types.d.js.map +0 -1
- package/lib/tools/generateSchema.js.map +0 -1
- package/lib/tools/renderJsonSchemaToMarkdownDoc.js.map +0 -1
- package/src/cn/ResidentIdentityCardNumber.test.ts +0 -17
- package/src/cn/ResidentIdentityCardNumber.ts +0 -96
- package/src/cn/__snapshots__/ResidentIdentityCardNumber.test.ts.snap +0 -15
- package/src/cn/formatDate.ts +0 -12
- package/src/cn/parseSex.ts +0 -13
- package/src/search/AdvanceSearch.test.ts +0 -149
- package/src/search/AdvanceSearch.ts +0 -14
- package/src/search/Makefile +0 -2
- package/src/search/__snapshots__/AdvanceSearch.test.ts.snap +0 -675
- package/src/search/formatAdvanceSearch.ts +0 -52
- package/src/search/index.ts +0 -1
- package/src/search/optimizeAdvanceSearch.ts +0 -77
- package/src/search/parseAdvanceSearch.ts +0 -23
- package/src/search/parser.d.ts +0 -8
- package/src/search/parser.js +0 -2794
- package/src/search/parser.peggy +0 -237
- package/src/search/types.d.ts +0 -45
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { z } from 'zod/v4';
|
|
2
|
+
export var ResourceStatus = Object.freeze({
|
|
3
|
+
__proto__: null,
|
|
4
|
+
Active: 'Active',
|
|
5
|
+
Approved: 'Approved',
|
|
6
|
+
Cancelled: 'Cancelled',
|
|
7
|
+
Closed: 'Closed',
|
|
8
|
+
Completed: 'Completed',
|
|
9
|
+
Confirmed: 'Confirmed',
|
|
10
|
+
Contacted: 'Contacted',
|
|
11
|
+
Contacting: 'Contacting',
|
|
12
|
+
Deleted: 'Deleted',
|
|
13
|
+
Denied: 'Denied',
|
|
14
|
+
Disabled: 'Disabled',
|
|
15
|
+
Disqualified: 'Disqualified',
|
|
16
|
+
Draft: 'Draft',
|
|
17
|
+
Enabled: 'Enabled',
|
|
18
|
+
Expired: 'Expired',
|
|
19
|
+
Failed: 'Failed',
|
|
20
|
+
InProgress: 'InProgress',
|
|
21
|
+
Inactive: 'Inactive',
|
|
22
|
+
Initial: 'Initial',
|
|
23
|
+
Lost: 'Lost',
|
|
24
|
+
New: 'New',
|
|
25
|
+
Nurturing: 'Nurturing',
|
|
26
|
+
OnHold: 'OnHold',
|
|
27
|
+
Open: 'Open',
|
|
28
|
+
Overdue: 'Overdue',
|
|
29
|
+
Paid: 'Paid',
|
|
30
|
+
PartiallyPaid: 'PartiallyPaid',
|
|
31
|
+
Pending: 'Pending',
|
|
32
|
+
PendingAcceptance: 'PendingAcceptance',
|
|
33
|
+
PendingApproval: 'PendingApproval',
|
|
34
|
+
PendingConfirmation: 'PendingConfirmation',
|
|
35
|
+
PendingResubmission: 'PendingResubmission',
|
|
36
|
+
Posted: 'Posted',
|
|
37
|
+
Published: 'Published',
|
|
38
|
+
Qualified: 'Qualified',
|
|
39
|
+
Rejected: 'Rejected',
|
|
40
|
+
Submitted: 'Submitted',
|
|
41
|
+
Suspended: 'Suspended',
|
|
42
|
+
Terminated: 'Terminated',
|
|
43
|
+
Unqualified: 'Unqualified',
|
|
44
|
+
Void: 'Void',
|
|
45
|
+
Won: 'Won'
|
|
46
|
+
});
|
|
47
|
+
export var ResourceStatusSchema = z.union([
|
|
48
|
+
z.literal(ResourceStatus.Active).describe('活跃'),
|
|
49
|
+
z.literal(ResourceStatus.Approved).describe('已通过'),
|
|
50
|
+
z.literal(ResourceStatus.Cancelled).describe('已取消'),
|
|
51
|
+
z.literal(ResourceStatus.Closed).describe('已关闭'),
|
|
52
|
+
z.literal(ResourceStatus.Completed).describe('已完成'),
|
|
53
|
+
z.literal(ResourceStatus.Confirmed).describe('已确认'),
|
|
54
|
+
z.literal(ResourceStatus.Contacted).describe('已联系'),
|
|
55
|
+
z.literal(ResourceStatus.Contacting).describe('联系中'),
|
|
56
|
+
z.literal(ResourceStatus.Deleted).describe('已删除'),
|
|
57
|
+
z.literal(ResourceStatus.Denied).describe('已拒绝'),
|
|
58
|
+
z.literal(ResourceStatus.Disabled).describe('已禁用'),
|
|
59
|
+
z.literal(ResourceStatus.Disqualified).describe('不合格'),
|
|
60
|
+
z.literal(ResourceStatus.Draft).describe('草稿'),
|
|
61
|
+
z.literal(ResourceStatus.Enabled).describe('已启用'),
|
|
62
|
+
z.literal(ResourceStatus.Expired).describe('已过期'),
|
|
63
|
+
z.literal(ResourceStatus.InProgress).describe('进行中'),
|
|
64
|
+
z.literal(ResourceStatus.Inactive).describe('停用'),
|
|
65
|
+
z.literal(ResourceStatus.Initial).describe('初始'),
|
|
66
|
+
z.literal(ResourceStatus.Lost).describe('已失去'),
|
|
67
|
+
z.literal(ResourceStatus.New).describe('新建'),
|
|
68
|
+
z.literal(ResourceStatus.Nurturing).describe('培育中'),
|
|
69
|
+
z.literal(ResourceStatus.OnHold).describe('搁置'),
|
|
70
|
+
z.literal(ResourceStatus.Open).describe('开放'),
|
|
71
|
+
z.literal(ResourceStatus.Overdue).describe('逾期'),
|
|
72
|
+
z.literal(ResourceStatus.Paid).describe('已支付'),
|
|
73
|
+
z.literal(ResourceStatus.PartiallyPaid).describe('部分支付'),
|
|
74
|
+
z.literal(ResourceStatus.Pending).describe('待审核'),
|
|
75
|
+
z.literal(ResourceStatus.PendingAcceptance).describe('待接受'),
|
|
76
|
+
z.literal(ResourceStatus.PendingApproval).describe('待批准'),
|
|
77
|
+
z.literal(ResourceStatus.PendingConfirmation).describe('待确认'),
|
|
78
|
+
z.literal(ResourceStatus.PendingResubmission).describe('待重新提交'),
|
|
79
|
+
z.literal(ResourceStatus.Posted).describe('已过账'),
|
|
80
|
+
z.literal(ResourceStatus.Published).describe('已发布'),
|
|
81
|
+
z.literal(ResourceStatus.Qualified).describe('已合格'),
|
|
82
|
+
z.literal(ResourceStatus.Rejected).describe('已驳回'),
|
|
83
|
+
z.literal(ResourceStatus.Submitted).describe('已提交'),
|
|
84
|
+
z.literal(ResourceStatus.Suspended).describe('暂停'),
|
|
85
|
+
z.literal(ResourceStatus.Terminated).describe('终止'),
|
|
86
|
+
z.literal(ResourceStatus.Unqualified).describe('未达标'),
|
|
87
|
+
z.literal(ResourceStatus.Void).describe('作废'),
|
|
88
|
+
z.literal(ResourceStatus.Won).describe('已赢得')
|
|
89
|
+
]).meta({
|
|
90
|
+
title: 'ResourceStatus',
|
|
91
|
+
description: '资源状态',
|
|
92
|
+
type: 'string'
|
|
93
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { z } from 'zod/v4';
|
|
2
|
+
export var ResourceType = Object.freeze({
|
|
3
|
+
__proto__: null,
|
|
4
|
+
User: 'User',
|
|
5
|
+
Customer: 'Customer',
|
|
6
|
+
Contact: 'Contact',
|
|
7
|
+
Account: 'Account',
|
|
8
|
+
Order: 'Order',
|
|
9
|
+
Lead: 'Lead',
|
|
10
|
+
Opportunity: 'Opportunity'
|
|
11
|
+
});
|
|
12
|
+
export var ResourceTypeSchema = z.union([
|
|
13
|
+
z.literal(ResourceType.User).describe('用户'),
|
|
14
|
+
z.literal(ResourceType.Customer).describe('客户'),
|
|
15
|
+
z.literal(ResourceType.Contact).describe('联系人'),
|
|
16
|
+
z.literal(ResourceType.Account).describe('账户'),
|
|
17
|
+
z.literal(ResourceType.Order).describe('订单'),
|
|
18
|
+
z.literal(ResourceType.Lead).describe('线索'),
|
|
19
|
+
z.literal(ResourceType.Opportunity).describe('商机')
|
|
20
|
+
]).meta({
|
|
21
|
+
title: 'ResourceType',
|
|
22
|
+
description: '资源类型',
|
|
23
|
+
type: 'string'
|
|
24
|
+
});
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { ResourceStatus, ResourceStatusSchema } from "./ResourceStatus.js";
|
|
2
|
+
export { ResourceActionType, ResourceActionTypeSchema, ResourceActionDataSchema } from "./ResourceActionType.js";
|
|
3
|
+
export { AnyResourceSchema } from "./AnyResourceSchema.js";
|
|
4
|
+
export { rz } from "./types.js";
|
|
5
|
+
export { BaseResourceSchema } from "./BaseResourceSchema.js";
|
|
@@ -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,45 @@
|
|
|
1
|
+
function _type_of(obj) {
|
|
2
|
+
"@swc/helpers - typeof";
|
|
3
|
+
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
|
|
4
|
+
}
|
|
5
|
+
import { getGlobalStates } from "@wener/utils";
|
|
6
|
+
import { toJsonSchema } from "./index.js";
|
|
7
|
+
var types = getGlobalStates("@wener/common/resource/schema/SchemaRegistry", function () {
|
|
8
|
+
return new Map();
|
|
9
|
+
});
|
|
10
|
+
export function get(needle) {
|
|
11
|
+
var key = getKey(needle);
|
|
12
|
+
var found = types.get(key);
|
|
13
|
+
if (found) {
|
|
14
|
+
return found;
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
if (needle && typeof needle !== "string") {
|
|
18
|
+
return toJsonSchema(needle);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
throw new Error("Schema not found: ".concat(key));
|
|
22
|
+
}
|
|
23
|
+
function getKey(s) {
|
|
24
|
+
var key;
|
|
25
|
+
if (typeof s === "string") {
|
|
26
|
+
key = s;
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
var js = toJsonSchema(s);
|
|
30
|
+
key = js.$id || js.title;
|
|
31
|
+
}
|
|
32
|
+
if (!key) {
|
|
33
|
+
throw new Error("Schema must have $id or title");
|
|
34
|
+
}
|
|
35
|
+
return key;
|
|
36
|
+
}
|
|
37
|
+
export function set(key, def) {
|
|
38
|
+
if (!def) {
|
|
39
|
+
def = key;
|
|
40
|
+
if (!def || (typeof def === "undefined" ? "undefined" : _type_of(def)) !== "object") {
|
|
41
|
+
throw new Error("Invalid schema definition for: ".concat(getKey(key)));
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
types.set(getKey(key), toJsonSchema(def));
|
|
45
|
+
}
|
|
@@ -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 = Array.isArray(schema.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 { SchemaRegistry } from "./SchemaRegistry.mod.js";
|
|
4
|
+
export function getSchemaOptions(s) {
|
|
5
|
+
var js = SchemaRegistry.get(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,6 @@
|
|
|
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";
|
|
6
|
+
export { SchemaRegistry } from "./SchemaRegistry.mod.js";
|