@wener/common 2.0.3 → 2.0.6
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/ai/qwen3vl/index.js +1 -1
- package/lib/ai/qwen3vl/utils.js +15 -15
- package/lib/ai/qwen3vl/utils.js.map +1 -1
- package/lib/ai/vision/DocLayoutElementTypeSchema.js +22 -22
- package/lib/ai/vision/ImageAnnotationSchema.js +63 -47
- package/lib/ai/vision/index.js +2 -2
- package/lib/ai/vision/resolveImageAnnotation.js +81 -95
- package/lib/cn/ChineseResidentIdNo.js +55 -41
- package/lib/cn/ChineseResidentIdNo.js.map +1 -1
- package/lib/cn/ChineseResidentIdNo.mod.js +6 -1
- package/lib/cn/ChineseResidentIdNo.test.js +22 -21
- package/lib/cn/DivisionCode.js +220 -235
- package/lib/cn/DivisionCode.mod.js +6 -1
- package/lib/cn/DivisionCode.test.js +92 -121
- package/lib/cn/Mod11.js +18 -37
- package/lib/cn/Mod11.js.map +1 -1
- package/lib/cn/Mod31.js +23 -41
- package/lib/cn/UnifiedSocialCreditCode.js +143 -137
- package/lib/cn/UnifiedSocialCreditCode.mod.js +6 -1
- package/lib/cn/UnifiedSocialCreditCode.test.js +21 -15
- package/lib/cn/formatChineseAmount.js +46 -71
- package/lib/cn/index.js +6 -6
- package/lib/cn/mod.js +5 -3
- package/lib/cn/parseChineseNumber.js +81 -85
- package/lib/cn/parseChineseNumber.test.js +183 -261
- package/lib/cn/pinyin/cartesianProduct.js +19 -19
- package/lib/cn/pinyin/cartesianProduct.test.js +78 -178
- package/lib/cn/pinyin/loader.js +13 -11
- package/lib/cn/pinyin/preload.js +2 -1
- package/lib/cn/pinyin/toPinyin.test.js +149 -161
- package/lib/cn/pinyin/toPinyinPure.js +28 -23
- package/lib/cn/pinyin/transform.js +11 -11
- package/lib/cn/types.d.js +2 -2
- package/lib/consola/createStandardConsolaReporter.js +14 -15
- package/lib/consola/formatLogObject.js +149 -133
- package/lib/consola/formatLogObject.js.map +1 -1
- package/lib/consola/formatLogObject.test.js +167 -178
- package/lib/consola/index.js +2 -2
- package/lib/data/formatSort.js +14 -12
- package/lib/data/formatSort.test.js +33 -33
- package/lib/data/index.js +3 -3
- package/lib/data/maybeNumber.js +23 -23
- package/lib/data/maybeNumber.js.map +1 -1
- package/lib/data/parseSort.js +75 -68
- package/lib/data/parseSort.test.js +196 -187
- package/lib/data/resolvePagination.js +38 -39
- package/lib/data/resolvePagination.test.js +228 -218
- package/lib/data/types.d.js +2 -2
- package/lib/data/types.d.js.map +1 -1
- package/lib/dayjs/dayjs.js +20 -20
- package/lib/dayjs/formatDuration.js +56 -56
- package/lib/dayjs/formatDuration.js.map +1 -1
- package/lib/dayjs/formatDuration.test.js +63 -77
- package/lib/dayjs/index.js +4 -4
- package/lib/dayjs/parseDuration.js +21 -26
- package/lib/dayjs/parseRelativeTime.js +65 -66
- package/lib/dayjs/parseRelativeTime.test.js +227 -243
- package/lib/dayjs/resolveRelativeTime.js +74 -144
- package/lib/dayjs/resolveRelativeTime.js.map +1 -1
- package/lib/dayjs/resolveRelativeTime.test.js +296 -307
- package/lib/decimal/index.js +1 -1
- package/lib/decimal/parseDecimal.js +12 -12
- package/lib/drain3/Drain.js +321 -0
- package/lib/drain3/Drain.js.map +1 -0
- package/lib/drain3/LogCluster.js +38 -0
- package/lib/drain3/LogCluster.js.map +1 -0
- package/lib/drain3/Node.js +39 -0
- package/lib/drain3/Node.js.map +1 -0
- package/lib/drain3/TemplateMiner.js +205 -0
- package/lib/drain3/TemplateMiner.js.map +1 -0
- package/lib/drain3/index.js +31 -0
- package/lib/drain3/index.js.map +1 -0
- package/lib/drain3/persistence/FilePersistence.js +24 -0
- package/lib/drain3/persistence/FilePersistence.js.map +1 -0
- package/lib/drain3/persistence/MemoryPersistence.js +18 -0
- package/lib/drain3/persistence/MemoryPersistence.js.map +1 -0
- package/lib/drain3/persistence/PersistenceHandler.js +5 -0
- package/lib/drain3/persistence/PersistenceHandler.js.map +1 -0
- package/lib/drain3/types.js +7 -0
- package/lib/drain3/types.js.map +1 -0
- package/lib/emittery/emitter.js +7 -7
- package/lib/emittery/index.js +1 -1
- package/lib/foundation/schema/SexType.js +15 -12
- package/lib/foundation/schema/index.js +1 -1
- package/lib/foundation/schema/parseSexType.js +15 -16
- package/lib/foundation/schema/types.js +8 -6
- package/lib/fs/FileSystemError.js +18 -18
- package/lib/fs/IFileSystem.d.js +2 -2
- package/lib/fs/IFileSystem.d.js.map +1 -1
- package/lib/fs/MemoryFileSystem.test.js +172 -181
- package/lib/fs/createBrowserFileSystem.js +222 -233
- package/lib/fs/createBrowserFileSystem.js.map +1 -1
- package/lib/fs/createMemoryFileSystem.js +473 -510
- package/lib/fs/createMemoryFileSystem.js.map +1 -1
- package/lib/fs/createSandboxFileSystem.js +102 -101
- package/lib/fs/createSandboxFileSystem.js.map +1 -1
- package/lib/fs/createWebDavFileSystem.js +162 -132
- package/lib/fs/createWebDavFileSystem.js.map +1 -1
- package/lib/fs/createWebFileSystem.js +202 -0
- package/lib/fs/createWebFileSystem.js.map +1 -0
- package/lib/fs/findMimeType.js +14 -14
- package/lib/fs/findMimeType.js.map +1 -1
- package/lib/fs/index.js +7 -7
- package/lib/fs/index.js.map +1 -1
- package/lib/fs/minio/createMinioFileSystem.js +977 -0
- package/lib/fs/minio/createMinioFileSystem.js.map +1 -0
- package/lib/fs/minio/index.js +2 -0
- package/lib/fs/minio/index.js.map +1 -0
- package/lib/fs/orpc/FileSystemContract.js +57 -57
- package/lib/fs/orpc/createContractClientFileSystem.js +88 -88
- package/lib/fs/orpc/createContractClientFileSystem.js.map +1 -1
- package/lib/fs/orpc/index.js +2 -2
- package/lib/fs/orpc/server/createFileSystemContractImpl.js +62 -60
- package/lib/fs/orpc/server/createFileSystemContractImpl.js.map +1 -1
- package/lib/fs/orpc/server/index.js +1 -1
- package/lib/fs/s3/createS3MiniFileSystem.js +756 -689
- package/lib/fs/s3/createS3MiniFileSystem.js.map +1 -1
- package/lib/fs/s3/index.js +1 -1
- package/lib/fs/s3/s3mini.test.js +524 -553
- package/lib/fs/scandir.js +56 -56
- package/lib/fs/server/createDatabaseFileSystem.js +834 -741
- package/lib/fs/server/createDatabaseFileSystem.js.map +1 -1
- package/lib/fs/server/createNodeFileSystem.js +407 -380
- package/lib/fs/server/createNodeFileSystem.js.map +1 -1
- package/lib/fs/server/dbfs.test.js +201 -214
- package/lib/fs/server/index.js +1 -1
- package/lib/fs/server/loadTestDatabase.js +40 -43
- package/lib/fs/tests/runFileSystemTest.js +352 -315
- package/lib/fs/tests/runFileSystemTest.js.map +1 -1
- package/lib/fs/types.js +17 -20
- package/lib/fs/utils/getFileUrl.js +24 -30
- package/lib/fs/utils.js +17 -17
- package/lib/fs/utils.js.map +1 -1
- package/lib/fs/webdav/index.js +2 -0
- package/lib/fs/webdav/index.js.map +1 -0
- package/lib/index.js +2 -2
- package/lib/jsonschema/JsonSchema.js +216 -155
- package/lib/jsonschema/JsonSchema.js.map +1 -1
- package/lib/jsonschema/JsonSchema.test.js +123 -124
- package/lib/jsonschema/forEachJsonSchema.js +41 -41
- package/lib/jsonschema/forEachJsonSchema.js.map +1 -1
- package/lib/jsonschema/index.js +2 -2
- package/lib/jsonschema/types.d.js +2 -2
- package/lib/jsonschema/types.d.js.map +1 -1
- package/lib/meta/defineFileType.js +32 -38
- package/lib/meta/defineInit.js +39 -35
- package/lib/meta/defineMetadata.js +37 -34
- package/lib/meta/defineMetadata.js.map +1 -1
- package/lib/meta/defineMetadata.test.js +13 -12
- package/lib/meta/index.js +3 -3
- package/lib/orpc/createOpenApiContractClient.js +26 -24
- package/lib/orpc/createOpenApiContractClient.js.map +1 -1
- package/lib/orpc/createRpcContractClient.js +37 -31
- package/lib/orpc/index.js +2 -2
- package/lib/orpc/resolveLinkPlugins.js +25 -25
- package/lib/password/PHC.js +187 -189
- package/lib/password/PHC.js.map +1 -1
- package/lib/password/PHC.test.js +517 -535
- package/lib/password/Password.js +85 -80
- package/lib/password/Password.test.js +330 -364
- package/lib/password/createArgon2PasswordAlgorithm.js +50 -51
- package/lib/password/createArgon2PasswordAlgorithm.js.map +1 -1
- package/lib/password/createBase64PasswordAlgorithm.js +11 -11
- package/lib/password/createBase64PasswordAlgorithm.js.map +1 -1
- package/lib/password/createBcryptPasswordAlgorithm.js +20 -18
- package/lib/password/createBcryptPasswordAlgorithm.js.map +1 -1
- package/lib/password/createPBKDF2PasswordAlgorithm.js +65 -52
- package/lib/password/createPBKDF2PasswordAlgorithm.js.map +1 -1
- package/lib/password/createScryptPasswordAlgorithm.js +74 -63
- package/lib/password/createScryptPasswordAlgorithm.js.map +1 -1
- package/lib/password/index.js +5 -5
- package/lib/password/server/index.js +1 -1
- package/lib/resource/Identifiable.js +2 -2
- package/lib/resource/ListQuery.js +42 -42
- package/lib/resource/ListQuery.js.map +1 -1
- package/lib/resource/getTitleOfResource.js +5 -5
- package/lib/resource/index.js +2 -2
- package/lib/resource/index.js.map +1 -1
- package/lib/resource/schema/AnyResourceSchema.js +91 -89
- package/lib/resource/schema/BaseResourceSchema.js +26 -26
- package/lib/resource/schema/ResourceActionType.js +117 -115
- package/lib/resource/schema/ResourceStatus.js +94 -92
- package/lib/resource/schema/ResourceType.js +25 -23
- package/lib/resource/schema/index.js +5 -5
- package/lib/resource/schema/types.js +86 -55
- package/lib/resource/schema/types.test.js +16 -13
- package/lib/s3/formatS3Url.js +60 -60
- package/lib/s3/formatS3Url.js.map +1 -1
- package/lib/s3/formatS3Url.test.js +238 -261
- package/lib/s3/index.js +2 -2
- package/lib/s3/parseS3Url.js +61 -60
- package/lib/s3/parseS3Url.js.map +1 -1
- package/lib/s3/parseS3Url.test.js +270 -269
- package/lib/schema/SchemaRegistry.js +41 -42
- package/lib/schema/SchemaRegistry.js.map +1 -1
- package/lib/schema/SchemaRegistry.mod.js +1 -1
- package/lib/schema/TypeSchema.d.js +2 -2
- package/lib/schema/TypeSchema.d.js.map +1 -1
- package/lib/schema/createSchemaData.js +113 -67
- package/lib/schema/createSchemaData.js.map +1 -1
- package/lib/schema/findJsonSchemaByPath.js +28 -23
- package/lib/schema/findJsonSchemaByPath.js.map +1 -1
- package/lib/schema/formatZodError.js +113 -134
- package/lib/schema/formatZodError.js.map +1 -1
- package/lib/schema/formatZodError.test.js +192 -195
- package/lib/schema/getSchemaCache.js +7 -7
- package/lib/schema/getSchemaOptions.js +17 -16
- package/lib/schema/index.js +6 -6
- package/lib/schema/toJsonSchema.js +196 -190
- package/lib/schema/toJsonSchema.js.map +1 -1
- package/lib/schema/toJsonSchema.test.js +34 -26
- package/lib/schema/validate.js +106 -97
- package/lib/schema/validate.js.map +1 -1
- package/lib/tools/generateSchema.js +40 -40
- package/lib/tools/renderJsonSchemaToMarkdownDoc.js +74 -74
- package/lib/utils/buildBaseUrl.js +8 -8
- package/lib/utils/buildRedactorFormSchema.js +55 -54
- package/lib/utils/buildRedactorFormSchema.js.map +1 -1
- package/lib/utils/getEstimateProcessTime.js +24 -19
- package/lib/utils/index.js +3 -3
- package/lib/utils/resolveFeatureOptions.js +9 -9
- package/package.json +37 -18
- package/src/ai/qwen3vl/utils.ts +1 -1
- package/src/ai/vision/index.ts +2 -2
- package/src/cn/ChineseResidentIdNo.ts +1 -1
- package/src/cn/Mod11.ts +1 -1
- package/src/cn/__snapshots__/ChineseResidentIdNo.test.ts.snap +1 -1
- package/src/cn/__snapshots__/UnifiedSocialCreditCode.test.ts.snap +0 -23
- package/src/cn/index.ts +1 -2
- package/src/cn/parseChineseNumber.test.ts +4 -4
- package/src/consola/formatLogObject.ts +6 -6
- package/src/consola/index.ts +1 -1
- package/src/data/index.ts +3 -4
- package/src/data/maybeNumber.ts +1 -1
- package/src/data/parseSort.test.ts +0 -1
- package/src/data/resolvePagination.ts +2 -2
- package/src/data/types.d.ts +2 -2
- package/src/dayjs/formatDuration.ts +10 -11
- package/src/dayjs/index.ts +1 -1
- package/src/dayjs/parseRelativeTime.ts +1 -1
- package/src/dayjs/resolveRelativeTime.ts +11 -14
- package/src/drain3/Drain.test.ts +378 -0
- package/src/drain3/Drain.ts +394 -0
- package/src/drain3/LogCluster.ts +46 -0
- package/src/drain3/Node.ts +53 -0
- package/src/drain3/TemplateMiner.ts +246 -0
- package/src/drain3/index.ts +34 -0
- package/src/drain3/persistence/FilePersistence.ts +24 -0
- package/src/drain3/persistence/MemoryPersistence.ts +23 -0
- package/src/drain3/persistence/PersistenceHandler.ts +19 -0
- package/src/drain3/types.ts +75 -0
- package/src/fs/IFileSystem.d.ts +1 -2
- package/src/fs/createBrowserFileSystem.ts +7 -5
- package/src/fs/createMemoryFileSystem.ts +9 -13
- package/src/fs/createSandboxFileSystem.ts +1 -1
- package/src/fs/createWebDavFileSystem.ts +30 -17
- package/src/fs/createWebFileSystem.ts +242 -0
- package/src/fs/findMimeType.ts +1 -4
- package/src/fs/index.ts +5 -5
- package/src/fs/minio/createMinioFileSystem.ts +1148 -0
- package/src/fs/minio/index.ts +1 -0
- package/src/fs/orpc/createContractClientFileSystem.ts +5 -5
- package/src/fs/orpc/server/createFileSystemContractImpl.ts +1 -1
- package/src/fs/s3/createS3MiniFileSystem.ts +120 -79
- package/src/fs/s3/s3fs.test.ts +441 -0
- package/src/fs/s3/s3mini.test.ts +2 -2
- package/src/fs/server/createDatabaseFileSystem.ts +78 -114
- package/src/fs/server/createNodeFileSystem.ts +32 -13
- package/src/fs/server/dbfs.test.ts +13 -8
- package/src/fs/server/index.ts +1 -0
- package/src/fs/server/loadTestDatabase.ts +8 -119
- package/src/fs/tests/runFileSystemTest.ts +29 -28
- package/src/fs/utils.ts +1 -1
- package/src/fs/webdav/index.ts +1 -0
- package/src/jsonschema/JsonSchema.ts +5 -5
- package/src/jsonschema/forEachJsonSchema.ts +1 -1
- package/src/jsonschema/index.ts +1 -1
- package/src/jsonschema/types.d.ts +1 -1
- package/src/meta/defineMetadata.ts +1 -1
- package/src/meta/index.ts +2 -3
- package/src/orm/createSqliteDialect.ts +17 -0
- package/src/orm/index.ts +1 -0
- package/src/orpc/createOpenApiContractClient.ts +3 -3
- package/src/orpc/index.ts +1 -1
- package/src/password/PHC.ts +3 -3
- package/src/password/createArgon2PasswordAlgorithm.ts +2 -2
- package/src/password/createBase64PasswordAlgorithm.ts +2 -2
- package/src/password/createBcryptPasswordAlgorithm.ts +4 -2
- package/src/password/createPBKDF2PasswordAlgorithm.ts +2 -2
- package/src/password/createScryptPasswordAlgorithm.ts +4 -4
- package/src/password/index.ts +2 -2
- package/src/resource/ListQuery.ts +4 -1
- package/src/resource/index.ts +3 -3
- package/src/resource/schema/index.ts +4 -4
- package/src/s3/formatS3Url.test.ts +1 -1
- package/src/s3/formatS3Url.ts +2 -2
- package/src/s3/index.ts +1 -1
- package/src/s3/parseS3Url.ts +1 -1
- package/src/schema/SchemaRegistry.ts +2 -2
- package/src/schema/TypeSchema.d.ts +6 -6
- package/src/schema/createSchemaData.ts +5 -5
- package/src/schema/findJsonSchemaByPath.ts +5 -5
- package/src/schema/formatZodError.test.ts +2 -1
- package/src/schema/formatZodError.ts +50 -62
- package/src/schema/index.ts +5 -5
- package/src/schema/toJsonSchema.ts +6 -6
- package/src/schema/validate.ts +2 -2
- package/src/utils/buildRedactorFormSchema.ts +4 -4
- package/src/utils/formatNumber.ts +18 -0
- package/src/utils/formatPercent.ts +17 -0
- package/src/utils/index.ts +3 -3
- package/src/utils/resolveFeatureOptions.ts +1 -1
package/lib/schema/validate.js
CHANGED
|
@@ -1,112 +1,121 @@
|
|
|
1
|
-
import { Kind as TypeBoxKind } from
|
|
2
|
-
import
|
|
3
|
-
import { TypeCompiler } from
|
|
4
|
-
import { ifPresent } from
|
|
5
|
-
import { Ajv } from
|
|
6
|
-
import addFormats from
|
|
1
|
+
import { Kind as TypeBoxKind } from '@sinclair/typebox';
|
|
2
|
+
import '@sinclair/typebox';
|
|
3
|
+
import { TypeCompiler } from '@sinclair/typebox/compiler';
|
|
4
|
+
import { ifPresent } from '@wener/utils';
|
|
5
|
+
import { Ajv } from 'ajv';
|
|
6
|
+
import addFormats from 'ajv-formats';
|
|
7
7
|
/*
|
|
8
8
|
https://github.com/react-hook-form/resolvers
|
|
9
9
|
https://github.com/decs/typeschema/tree/main/packages
|
|
10
10
|
*/ export function validate(schema, data) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
11
|
+
if (isZodSchema(schema)) {
|
|
12
|
+
const result = schema.safeParse(data);
|
|
13
|
+
if (result.success) {
|
|
14
|
+
return {
|
|
15
|
+
success: true,
|
|
16
|
+
data: result.data,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
return {
|
|
20
|
+
issues: result.error.issues.map(({ message, path, code }) => ({
|
|
21
|
+
message,
|
|
22
|
+
path,
|
|
23
|
+
code,
|
|
24
|
+
})),
|
|
25
|
+
// message: z.prettifyError(result.error),
|
|
26
|
+
success: false,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
if (isTypeBoxSchema(schema)) {
|
|
30
|
+
const checker = TypeCompiler.Compile(schema);
|
|
31
|
+
if (checker.Check(data)) {
|
|
32
|
+
return {
|
|
33
|
+
success: true,
|
|
34
|
+
data: data,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
issues: Array.from(checker.Errors(data)).map(({ message, path }) => {
|
|
39
|
+
return {
|
|
40
|
+
message,
|
|
41
|
+
path: [path],
|
|
42
|
+
};
|
|
43
|
+
}),
|
|
44
|
+
// message: '',
|
|
45
|
+
success: false,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
if (isJsonSchema(schema)) {
|
|
49
|
+
const ajv = new Ajv({
|
|
50
|
+
allErrors: true,
|
|
51
|
+
validateSchema: true,
|
|
52
|
+
});
|
|
53
|
+
addFormats(ajv);
|
|
54
|
+
const validator = ajv.compile(schema);
|
|
55
|
+
if (validator(data)) {
|
|
56
|
+
return {
|
|
57
|
+
data: data,
|
|
58
|
+
success: true,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
const issues =
|
|
62
|
+
validator.errors?.map((error) => {
|
|
63
|
+
const message = error.message || 'Unknown error';
|
|
64
|
+
const path = error.instancePath
|
|
65
|
+
.split('/')
|
|
66
|
+
.filter(Boolean)
|
|
67
|
+
.map((p) => p);
|
|
68
|
+
return {
|
|
69
|
+
message,
|
|
70
|
+
path,
|
|
71
|
+
};
|
|
72
|
+
}) || [];
|
|
73
|
+
return {
|
|
74
|
+
success: false,
|
|
75
|
+
// message: 'Invalid data',
|
|
76
|
+
issues,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
return {
|
|
80
|
+
success: false,
|
|
81
|
+
// message: 'Invalid schema',
|
|
82
|
+
issues: [
|
|
83
|
+
{
|
|
84
|
+
message: 'Unknown schema type',
|
|
85
|
+
},
|
|
86
|
+
],
|
|
87
|
+
};
|
|
86
88
|
}
|
|
87
89
|
export function parseData(schema, data) {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
90
|
+
let result = validate(schema, data);
|
|
91
|
+
if (result.success) {
|
|
92
|
+
return result.data;
|
|
93
|
+
}
|
|
94
|
+
throw Object.assign(
|
|
95
|
+
new Error(
|
|
96
|
+
result.issues
|
|
97
|
+
.map((v) => {
|
|
98
|
+
return `${ifPresent(v.path?.join(), (v) => `[${v}]`)}: ${v.message}`;
|
|
99
|
+
})
|
|
100
|
+
.join('; '),
|
|
101
|
+
),
|
|
102
|
+
{
|
|
103
|
+
issues: result.issues,
|
|
104
|
+
},
|
|
105
|
+
);
|
|
97
106
|
}
|
|
98
|
-
function
|
|
99
|
-
|
|
107
|
+
function _formatIssues(_schema, _issues) {
|
|
108
|
+
return '';
|
|
100
109
|
}
|
|
101
110
|
export function isZodSchema(schema) {
|
|
102
|
-
|
|
111
|
+
return typeof schema === 'object' && 'parse' in schema;
|
|
103
112
|
}
|
|
104
113
|
export function isTypeBoxSchema(schema) {
|
|
105
|
-
|
|
114
|
+
return typeof schema === 'object' && TypeBoxKind in schema;
|
|
106
115
|
}
|
|
107
116
|
export function isJsonSchema(schema) {
|
|
108
|
-
|
|
109
|
-
|
|
117
|
+
let sc = schema;
|
|
118
|
+
return typeof schema === 'object' && Boolean(sc.type || sc.properties || sc.anyOf || sc.oneOf || sc.$ref || sc.items);
|
|
110
119
|
}
|
|
111
120
|
|
|
112
|
-
//# sourceMappingURL=validate.js.map
|
|
121
|
+
//# sourceMappingURL=validate.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/schema/validate.ts"],"sourcesContent":["import { Kind as TypeBoxKind, type TSchema } from '@sinclair/typebox';\nimport '@sinclair/typebox';\nimport { TypeCompiler } from '@sinclair/typebox/compiler';\nimport { ifPresent } from '@wener/utils';\nimport { Ajv } from 'ajv';\nimport addFormats from 'ajv-formats';\nimport type { ZodType } from 'zod/v4';\nimport type { JsonSchemaDef } from '../jsonschema';\nimport type { SchemaOutput, TypeSchema } from './TypeSchema';\n\n/*\nhttps://github.com/react-hook-form/resolvers\nhttps://github.com/decs/typeschema/tree/main/packages\n*/\n\nexport function validate<S extends TypeSchema>(schema: S, data: unknown): ValidationResult<SchemaOutput<S>> {\n\tif (isZodSchema(schema)) {\n\t\tconst result = schema.safeParse(data);\n\t\tif (result.success) {\n\t\t\treturn {\n\t\t\t\tsuccess: true,\n\t\t\t\tdata: result.data as any,\n\t\t\t};\n\t\t}\n\n\t\treturn {\n\t\t\tissues: result.error.issues.map(({ message, path, code }) => ({ message, path, code })),\n\t\t\t// message: z.prettifyError(result.error),\n\t\t\tsuccess: false,\n\t\t};\n\t}\n\n\tif (isTypeBoxSchema(schema)) {\n\t\tconst checker = TypeCompiler.Compile(schema);\n\t\tif (checker.Check(data)) {\n\t\t\treturn {\n\t\t\t\tsuccess: true,\n\t\t\t\tdata: data as any,\n\t\t\t};\n\t\t}\n\t\treturn {\n\t\t\tissues: Array.from(checker.Errors(data)).map(({ message, path }) => {\n\t\t\t\treturn { message, path: [path] };\n\t\t\t}),\n\t\t\t// message: '',\n\t\t\tsuccess: false,\n\t\t};\n\t}\n\n\tif (isJsonSchema(schema)) {\n\t\tconst ajv = new Ajv({\n\t\t\tallErrors: true,\n\t\t\tvalidateSchema: true,\n\t\t});\n\t\taddFormats(ajv);\n\t\tconst validator = ajv.compile(schema);\n\t\tif (validator(data)) {\n\t\t\treturn {\n\t\t\t\tdata: data as any,\n\t\t\t\tsuccess: true,\n\t\t\t};\n\t\t}\n\n\t\tconst issues =\n\t\t\tvalidator.errors?.map((error) => {\n\t\t\t\tconst message = error.message || 'Unknown error';\n\t\t\t\tconst path = error.instancePath\n\t\t\t\t\t.split('/')\n\t\t\t\t\t.filter(Boolean)\n\t\t\t\t\t.map((p) => p as PropertyKey);\n\t\t\t\treturn { message, path };\n\t\t\t}) || [];\n\t\treturn {\n\t\t\tsuccess: false,\n\t\t\t// message: 'Invalid data',\n\t\t\tissues,\n\t\t};\n\t}\n\n\treturn {\n\t\tsuccess: false,\n\t\t// message: 'Invalid schema',\n\t\tissues: [\n\t\t\t{\n\t\t\t\tmessage: 'Unknown schema type',\n\t\t\t},\n\t\t],\n\t};\n}\n\nexport function parseData<S extends TypeSchema>(schema: S, data: unknown): SchemaOutput<S> {\n\tlet result = validate(schema, data);\n\tif (result.success) {\n\t\treturn result.data;\n\t}\n\tthrow Object.assign(\n\t\tnew Error(\n\t\t\tresult.issues\n\t\t\t\t.map((v) => {\n\t\t\t\t\treturn `${ifPresent(v.path?.join(), (v) => `[${v}]`)}: ${v.message}`;\n\t\t\t\t})\n\t\t\t\t.join('; '),\n\t\t),\n\t\t{\n\t\t\tissues: result.issues,\n\t\t},\n\t);\n}\n\nfunction
|
|
1
|
+
{"version":3,"sources":["../../src/schema/validate.ts"],"sourcesContent":["import { Kind as TypeBoxKind, type TSchema } from '@sinclair/typebox';\nimport '@sinclair/typebox';\nimport { TypeCompiler } from '@sinclair/typebox/compiler';\nimport { ifPresent } from '@wener/utils';\nimport { Ajv } from 'ajv';\nimport addFormats from 'ajv-formats';\nimport type { ZodType } from 'zod/v4';\nimport type { JsonSchemaDef } from '../jsonschema';\nimport type { SchemaOutput, TypeSchema } from './TypeSchema';\n\n/*\nhttps://github.com/react-hook-form/resolvers\nhttps://github.com/decs/typeschema/tree/main/packages\n*/\n\nexport function validate<S extends TypeSchema>(schema: S, data: unknown): ValidationResult<SchemaOutput<S>> {\n\tif (isZodSchema(schema)) {\n\t\tconst result = schema.safeParse(data);\n\t\tif (result.success) {\n\t\t\treturn {\n\t\t\t\tsuccess: true,\n\t\t\t\tdata: result.data as any,\n\t\t\t};\n\t\t}\n\n\t\treturn {\n\t\t\tissues: result.error.issues.map(({ message, path, code }) => ({ message, path, code })),\n\t\t\t// message: z.prettifyError(result.error),\n\t\t\tsuccess: false,\n\t\t};\n\t}\n\n\tif (isTypeBoxSchema(schema)) {\n\t\tconst checker = TypeCompiler.Compile(schema);\n\t\tif (checker.Check(data)) {\n\t\t\treturn {\n\t\t\t\tsuccess: true,\n\t\t\t\tdata: data as any,\n\t\t\t};\n\t\t}\n\t\treturn {\n\t\t\tissues: Array.from(checker.Errors(data)).map(({ message, path }) => {\n\t\t\t\treturn { message, path: [path] };\n\t\t\t}),\n\t\t\t// message: '',\n\t\t\tsuccess: false,\n\t\t};\n\t}\n\n\tif (isJsonSchema(schema)) {\n\t\tconst ajv = new Ajv({\n\t\t\tallErrors: true,\n\t\t\tvalidateSchema: true,\n\t\t});\n\t\taddFormats(ajv);\n\t\tconst validator = ajv.compile(schema);\n\t\tif (validator(data)) {\n\t\t\treturn {\n\t\t\t\tdata: data as any,\n\t\t\t\tsuccess: true,\n\t\t\t};\n\t\t}\n\n\t\tconst issues =\n\t\t\tvalidator.errors?.map((error) => {\n\t\t\t\tconst message = error.message || 'Unknown error';\n\t\t\t\tconst path = error.instancePath\n\t\t\t\t\t.split('/')\n\t\t\t\t\t.filter(Boolean)\n\t\t\t\t\t.map((p) => p as PropertyKey);\n\t\t\t\treturn { message, path };\n\t\t\t}) || [];\n\t\treturn {\n\t\t\tsuccess: false,\n\t\t\t// message: 'Invalid data',\n\t\t\tissues,\n\t\t};\n\t}\n\n\treturn {\n\t\tsuccess: false,\n\t\t// message: 'Invalid schema',\n\t\tissues: [\n\t\t\t{\n\t\t\t\tmessage: 'Unknown schema type',\n\t\t\t},\n\t\t],\n\t};\n}\n\nexport function parseData<S extends TypeSchema>(schema: S, data: unknown): SchemaOutput<S> {\n\tlet result = validate(schema, data);\n\tif (result.success) {\n\t\treturn result.data;\n\t}\n\tthrow Object.assign(\n\t\tnew Error(\n\t\t\tresult.issues\n\t\t\t\t.map((v) => {\n\t\t\t\t\treturn `${ifPresent(v.path?.join(), (v) => `[${v}]`)}: ${v.message}`;\n\t\t\t\t})\n\t\t\t\t.join('; '),\n\t\t),\n\t\t{\n\t\t\tissues: result.issues,\n\t\t},\n\t);\n}\n\nfunction _formatIssues(_schema: TypeSchema, _issues: Array<ValidationIssue>): string {\n\treturn '';\n}\n\nexport function isZodSchema<I = any, O = any>(schema: any | TypeSchema<I, O>): schema is ZodType<O, I> {\n\treturn typeof schema === 'object' && 'parse' in schema;\n}\n\nexport function isTypeBoxSchema(schema: any): schema is TSchema {\n\treturn typeof schema === 'object' && TypeBoxKind in schema;\n}\n\nexport function isJsonSchema(schema: any): schema is JsonSchemaDef {\n\tlet sc: JsonSchemaDef = schema;\n\treturn typeof schema === 'object' && Boolean(sc.type || sc.properties || sc.anyOf || sc.oneOf || sc.$ref || sc.items);\n}\n\nexport type ValidationIssue = {\n\tmessage: string;\n\tpath?: Array<PropertyKey>;\n\tcode?: string;\n};\n\nexport type ValidationResult<TOutput> =\n\t| { success: true; data: TOutput }\n\t| { success: false; issues: Array<ValidationIssue> };\n"],"names":["Kind","TypeBoxKind","TypeCompiler","ifPresent","Ajv","addFormats","validate","schema","data","isZodSchema","result","safeParse","success","issues","error","map","message","path","code","isTypeBoxSchema","checker","Compile","Check","Array","from","Errors","isJsonSchema","ajv","allErrors","validateSchema","validator","compile","errors","instancePath","split","filter","Boolean","p","parseData","Object","assign","Error","v","join","_formatIssues","_schema","_issues","sc","type","properties","anyOf","oneOf","$ref","items"],"mappings":"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"}
|
|
@@ -1,43 +1,43 @@
|
|
|
1
|
-
import { exec } from
|
|
2
|
-
import fs from
|
|
3
|
-
import path from
|
|
4
|
-
import * as Codegen from
|
|
1
|
+
import { exec } from 'node:child_process';
|
|
2
|
+
import fs from 'node:fs/promises';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import * as Codegen from '@sinclair/typebox-codegen';
|
|
5
5
|
export async function generateSchema({ file, dir = path.dirname(file) }) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
6
|
+
const fn = path.basename(file).replace(/\.d\.ts$/, '.ts');
|
|
7
|
+
const types = await fs.readFile(file, 'utf-8');
|
|
8
|
+
const typeboxDir = path.join(dir, 'typebox');
|
|
9
|
+
const zodDir = path.join(dir, 'zod');
|
|
10
|
+
await fs.mkdir(typeboxDir, {
|
|
11
|
+
recursive: true,
|
|
12
|
+
});
|
|
13
|
+
await fs.mkdir(zodDir, {
|
|
14
|
+
recursive: true,
|
|
15
|
+
});
|
|
16
|
+
const typeBoxFile = path.join(typeboxDir, fn);
|
|
17
|
+
const zodFile = path.join(zodDir, fn);
|
|
18
|
+
{
|
|
19
|
+
// avoid import type error
|
|
20
|
+
let out = Codegen.TypeScriptToTypeBox.Generate(types);
|
|
21
|
+
out = out.replace(/^import \{ Type, Static\b/, `import { Type, type Static`);
|
|
22
|
+
await fs.writeFile(typeBoxFile, out);
|
|
23
|
+
}
|
|
24
|
+
const model = Codegen.TypeScriptToModel.Generate(types);
|
|
25
|
+
await fs.writeFile(zodFile, Codegen.ModelToZod.Generate(model));
|
|
26
|
+
await new Promise((resolve, reject) => {
|
|
27
|
+
exec(`pnpm prettier --write "${dir}/{typebox,zod}/*.ts"`, (error, stdout, stderr) => {
|
|
28
|
+
if (error) {
|
|
29
|
+
console.error(`exec error: ${error}`);
|
|
30
|
+
reject(error);
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
resolve({
|
|
34
|
+
stderr,
|
|
35
|
+
stdout,
|
|
36
|
+
});
|
|
37
|
+
stdout && console.log(`prettier:stdout: ${stdout}`);
|
|
38
|
+
stderr && console.error(`prettier:stderr: ${stderr}`);
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
//# sourceMappingURL=generateSchema.js.map
|
|
43
|
+
//# sourceMappingURL=generateSchema.js.map
|
|
@@ -1,85 +1,85 @@
|
|
|
1
1
|
export function renderJsonSchemaToMarkdownDoc(rootSchema) {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
2
|
+
// markdown
|
|
3
|
+
const out = [];
|
|
4
|
+
const all = new Set();
|
|
5
|
+
const pending = [];
|
|
6
|
+
const addObject = (o) => {
|
|
7
|
+
if (all.has(o.$id)) {
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
all.add(o.$id);
|
|
11
|
+
pending.push(o);
|
|
12
|
+
};
|
|
13
|
+
const writeObjectProps = (T, { prefix = '' } = {}) => {
|
|
14
|
+
if (!T?.properties) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
for (const [name, schema] of Object.entries(T.properties)) {
|
|
18
|
+
let typ = schema.$id || schema.type;
|
|
19
|
+
if (typ === 'array') {
|
|
20
|
+
typ = `${schema.items.$id || schema.items.type}[]`;
|
|
21
|
+
if (schema.items.$id) {
|
|
22
|
+
addObject(schema.items);
|
|
23
|
+
}
|
|
24
|
+
} else if (schema.$id) {
|
|
25
|
+
addObject(schema);
|
|
26
|
+
}
|
|
27
|
+
if (!typ) {
|
|
28
|
+
if ('anyOf' in schema) {
|
|
29
|
+
typ = 'enum';
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
out.push(`| ${prefix}${name} | ${typ} | ${schema.title || schema.description || ''} |`);
|
|
33
|
+
if (typ === 'object') {
|
|
34
|
+
writeObjectProps(schema, {
|
|
35
|
+
prefix: `${prefix}${name}.`,
|
|
36
|
+
});
|
|
37
|
+
} else if (schema.type === 'array') {
|
|
38
|
+
if (schema.items.type === 'object' && !schema.items.$id) {
|
|
39
|
+
writeObjectProps(schema.items, {
|
|
40
|
+
prefix: `${prefix}${name}[].`,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
const writeObject = (T) => {
|
|
47
|
+
out.push(`### ${T.title || T.$id}`);
|
|
48
|
+
out.push(`
|
|
49
49
|
| $id | 名字 |
|
|
50
50
|
| --- | --- |
|
|
51
51
|
| ${T.$id || ''} | ${T.title || ''} |
|
|
52
52
|
`);
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
53
|
+
if (T.description) {
|
|
54
|
+
out.push('');
|
|
55
|
+
out.push(`> ${T.description}`);
|
|
56
|
+
out.push('');
|
|
57
|
+
}
|
|
58
|
+
out.push(`| 名字 | 类型 | 说明 |`);
|
|
59
|
+
out.push(`| --- | --- | --- |`);
|
|
60
|
+
writeObjectProps(T);
|
|
61
|
+
out.push('');
|
|
62
|
+
};
|
|
63
|
+
writeObject(rootSchema);
|
|
64
|
+
for (const schema of pending) {
|
|
65
|
+
if (schema.type === 'object') {
|
|
66
|
+
writeObject(schema);
|
|
67
|
+
} else if ('anyOf' in schema) {
|
|
68
|
+
out.push(`### ${schema.$id || schema.title}`);
|
|
69
|
+
out.push(`
|
|
70
70
|
| $id | 名字 |
|
|
71
71
|
| --- | --- |
|
|
72
72
|
| ${schema.$id || ''} | ${schema.title || ''} |
|
|
73
73
|
`);
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
74
|
+
out.push(`| 值 | 说明 |`);
|
|
75
|
+
out.push(`| --- | --- |`);
|
|
76
|
+
for (const item of schema.anyOf) {
|
|
77
|
+
out.push(`| ${item.const} | ${item.title || item.description || ''} |`);
|
|
78
|
+
}
|
|
79
|
+
out.push('');
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return out.join('\n');
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
-
//# sourceMappingURL=renderJsonSchemaToMarkdownDoc.js.map
|
|
85
|
+
//# sourceMappingURL=renderJsonSchemaToMarkdownDoc.js.map
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
function joinUrl(baseUrl, maybeUrl) {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
const sanitizedBase = baseUrl.endsWith('/') ? baseUrl.slice(0, -1) : baseUrl;
|
|
3
|
+
const sanitizedEndpoint = maybeUrl.startsWith('/') ? maybeUrl.slice(1) : maybeUrl;
|
|
4
|
+
return `${sanitizedBase}/${sanitizedEndpoint}`;
|
|
5
5
|
}
|
|
6
6
|
export function buildBaseUrl(url, baseUrl) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
if (!baseUrl || /^https?:/.test(url)) {
|
|
8
|
+
return url;
|
|
9
|
+
}
|
|
10
|
+
return joinUrl(baseUrl, url);
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
//# sourceMappingURL=buildBaseUrl.js.map
|
|
13
|
+
//# sourceMappingURL=buildBaseUrl.js.map
|
|
@@ -1,59 +1,60 @@
|
|
|
1
|
-
import { forEachJsonSchema } from
|
|
2
|
-
import { toJsonSchema } from
|
|
3
|
-
export const RedactedText =
|
|
4
|
-
export function buildRedactorFormSchema(
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
1
|
+
import { forEachJsonSchema } from '../jsonschema/index.js';
|
|
2
|
+
import { toJsonSchema } from '../schema/index.js';
|
|
3
|
+
export const RedactedText = '[redacted]';
|
|
4
|
+
export function buildRedactorFormSchema(
|
|
5
|
+
ts,
|
|
6
|
+
{ shouldRedact = (schema) => schema['x-sensitive'] === true, replacer = () => RedactedText },
|
|
7
|
+
) {
|
|
8
|
+
let js = toJsonSchema(ts);
|
|
9
|
+
let paths = [];
|
|
10
|
+
forEachJsonSchema(js, (js, { path }) => {
|
|
11
|
+
if (shouldRedact(js)) {
|
|
12
|
+
paths.push(path);
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
const vis = (root, path) => {
|
|
16
|
+
switch (root) {
|
|
17
|
+
case null:
|
|
18
|
+
case undefined:
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
let c = root;
|
|
22
|
+
for (let i = 0; i < path.length; i++) {
|
|
23
|
+
let k = path[i];
|
|
24
|
+
if (c && typeof c === 'object' && k in c) {
|
|
25
|
+
let v = c[k];
|
|
26
|
+
if (path.length === 1) {
|
|
27
|
+
c[k] = replacer(k, v);
|
|
28
|
+
} else if (Array.isArray(v)) {
|
|
29
|
+
for (let vv of v) {
|
|
30
|
+
vis(vv, path.slice(i + 1));
|
|
31
|
+
}
|
|
32
|
+
} else {
|
|
33
|
+
vis(v, path.slice(i + 1));
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
return (root) => {
|
|
39
|
+
if (!paths.length) {
|
|
40
|
+
return root;
|
|
41
|
+
}
|
|
42
|
+
paths.forEach((path) => {
|
|
43
|
+
vis(root, path);
|
|
44
|
+
});
|
|
45
|
+
return root;
|
|
46
|
+
};
|
|
46
47
|
}
|
|
47
|
-
function
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
48
|
+
function _buildReversibleRedactText({ id, type: _type, key: _key, timestamp } = {}) {
|
|
49
|
+
if (!id) {
|
|
50
|
+
return RedactedText;
|
|
51
|
+
}
|
|
52
|
+
if (timestamp === true) {
|
|
53
|
+
timestamp = Date.now();
|
|
54
|
+
}
|
|
55
|
+
return `[redacted ${id}]`;
|
|
55
56
|
}
|
|
56
57
|
export function isRedactedText(v) {
|
|
57
|
-
|
|
58
|
+
return v === RedactedText || (typeof v === 'string' && v.startsWith('[redacted ') && v.endsWith(']'));
|
|
58
59
|
}
|
|
59
60
|
//# sourceMappingURL=buildRedactorFormSchema.js.map
|