@wener/common 2.0.2 → 2.0.3
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 +2 -0
- package/lib/ai/qwen3vl/index.js.map +1 -0
- package/lib/ai/qwen3vl/utils.js +31 -0
- package/lib/ai/qwen3vl/utils.js.map +1 -0
- package/lib/ai/vision/DocLayoutElementTypeSchema.js +28 -0
- package/lib/ai/vision/DocLayoutElementTypeSchema.js.map +1 -0
- package/lib/ai/vision/ImageAnnotationSchema.js +50 -0
- package/lib/ai/vision/ImageAnnotationSchema.js.map +1 -0
- package/lib/ai/vision/index.js +3 -0
- package/lib/ai/vision/index.js.map +1 -0
- package/lib/ai/vision/resolveImageAnnotation.js +105 -0
- package/lib/ai/vision/resolveImageAnnotation.js.map +1 -0
- package/lib/cn/ChineseResidentIdNo.js +21 -14
- package/lib/cn/ChineseResidentIdNo.js.map +1 -0
- package/lib/cn/ChineseResidentIdNo.test.js +1 -1
- package/lib/cn/DivisionCode.js +30 -25
- package/lib/cn/DivisionCode.js.map +1 -0
- package/lib/cn/DivisionCode.test.js +1 -1
- package/lib/cn/Mod11.js +38 -81
- package/lib/cn/Mod11.js.map +1 -0
- package/lib/cn/Mod31.js +41 -90
- package/lib/cn/Mod31.js.map +1 -0
- package/lib/cn/UnifiedSocialCreditCode.js +43 -34
- package/lib/cn/UnifiedSocialCreditCode.js.map +1 -0
- package/lib/cn/UnifiedSocialCreditCode.test.js +1 -1
- package/lib/cn/formatChineseAmount.js +77 -0
- package/lib/cn/formatChineseAmount.js.map +1 -0
- package/lib/cn/index.js +7 -1
- package/lib/cn/index.js.map +1 -0
- package/lib/cn/parseChineseNumber.js +94 -0
- package/lib/cn/parseChineseNumber.js.map +1 -0
- package/lib/cn/parseChineseNumber.test.js +278 -0
- package/lib/cn/pinyin/cartesianProduct.js +22 -0
- package/lib/cn/pinyin/cartesianProduct.js.map +1 -0
- package/lib/cn/pinyin/cartesianProduct.test.js +179 -0
- package/lib/cn/pinyin/data.json +23573 -0
- package/lib/cn/pinyin/loader.js +14 -0
- package/lib/cn/pinyin/loader.js.map +1 -0
- package/lib/cn/pinyin/preload.js +3 -0
- package/lib/cn/pinyin/preload.js.map +1 -0
- package/lib/cn/pinyin/toPinyin.test.js +167 -0
- package/lib/cn/pinyin/toPinyinPure.js +33 -0
- package/lib/cn/pinyin/toPinyinPure.js.map +1 -0
- package/lib/cn/pinyin/transform.js +14 -0
- package/lib/cn/pinyin/transform.js.map +1 -0
- package/lib/cn/types.d.js +2 -0
- package/lib/cn/types.d.js.map +1 -0
- package/lib/consola/createStandardConsolaReporter.js +6 -6
- package/lib/consola/createStandardConsolaReporter.js.map +1 -0
- package/lib/consola/formatLogObject.js +65 -145
- package/lib/consola/formatLogObject.js.map +1 -0
- package/lib/consola/formatLogObject.test.js +184 -0
- package/lib/consola/index.js +1 -0
- package/lib/consola/index.js.map +1 -0
- package/lib/data/formatSort.js +6 -5
- package/lib/data/formatSort.js.map +1 -0
- package/lib/data/index.js +1 -0
- package/lib/data/index.js.map +1 -0
- package/lib/data/maybeNumber.js +5 -7
- package/lib/data/maybeNumber.js.map +1 -0
- package/lib/data/parseSort.js +22 -28
- package/lib/data/parseSort.js.map +1 -0
- package/lib/data/resolvePagination.js +13 -17
- package/lib/data/resolvePagination.js.map +1 -0
- package/lib/data/types.d.js +2 -0
- package/lib/data/types.d.js.map +1 -0
- package/lib/dayjs/dayjs.js +21 -19
- package/lib/dayjs/dayjs.js.map +1 -0
- package/lib/dayjs/formatDuration.js +15 -14
- package/lib/dayjs/formatDuration.js.map +1 -0
- package/lib/dayjs/index.js +2 -0
- package/lib/dayjs/index.js.map +1 -0
- package/lib/dayjs/parseDuration.js +5 -8
- package/lib/dayjs/parseDuration.js.map +1 -0
- package/lib/dayjs/parseRelativeTime.js +90 -0
- package/lib/dayjs/parseRelativeTime.js.map +1 -0
- package/lib/dayjs/parseRelativeTime.test.js +247 -0
- package/lib/dayjs/resolveRelativeTime.js +158 -0
- package/lib/dayjs/resolveRelativeTime.js.map +1 -0
- package/lib/dayjs/resolveRelativeTime.test.js +310 -0
- package/lib/decimal/index.js +1 -0
- package/lib/decimal/index.js.map +1 -0
- package/lib/decimal/parseDecimal.js +3 -1
- package/lib/decimal/parseDecimal.js.map +1 -0
- package/lib/emittery/emitter.js +10 -0
- package/lib/emittery/emitter.js.map +1 -0
- package/lib/emittery/index.js +2 -0
- package/lib/emittery/index.js.map +1 -0
- package/lib/foundation/schema/SexType.js +5 -3
- package/lib/foundation/schema/SexType.js.map +1 -0
- package/lib/foundation/schema/index.js +1 -0
- package/lib/foundation/schema/index.js.map +1 -0
- package/lib/foundation/schema/parseSexType.js +1 -0
- package/lib/foundation/schema/parseSexType.js.map +1 -0
- package/lib/foundation/schema/types.js +4 -2
- package/lib/foundation/schema/types.js.map +1 -0
- package/lib/fs/FileSystemError.js +23 -0
- package/lib/fs/FileSystemError.js.map +1 -0
- package/lib/fs/IFileSystem.d.js +3 -0
- package/lib/fs/IFileSystem.d.js.map +1 -0
- package/lib/fs/MemoryFileSystem.test.js +188 -0
- package/lib/fs/createBrowserFileSystem.js +248 -0
- package/lib/fs/createBrowserFileSystem.js.map +1 -0
- package/lib/fs/createMemoryFileSystem.js +516 -0
- package/lib/fs/createMemoryFileSystem.js.map +1 -0
- package/lib/fs/createSandboxFileSystem.js +108 -0
- package/lib/fs/createSandboxFileSystem.js.map +1 -0
- package/lib/fs/createWebDavFileSystem.js +137 -0
- package/lib/fs/createWebDavFileSystem.js.map +1 -0
- package/lib/fs/findMimeType.js +17 -0
- package/lib/fs/findMimeType.js.map +1 -0
- package/lib/fs/index.js +8 -0
- package/lib/fs/index.js.map +1 -0
- package/lib/fs/orpc/FileSystemContract.js +93 -0
- package/lib/fs/orpc/FileSystemContract.js.map +1 -0
- package/lib/fs/orpc/createContractClientFileSystem.js +93 -0
- package/lib/fs/orpc/createContractClientFileSystem.js.map +1 -0
- package/lib/fs/orpc/index.js +3 -0
- package/lib/fs/orpc/index.js.map +1 -0
- package/lib/fs/orpc/server/createFileSystemContractImpl.js +63 -0
- package/lib/fs/orpc/server/createFileSystemContractImpl.js.map +1 -0
- package/lib/fs/orpc/server/index.js +2 -0
- package/lib/fs/orpc/server/index.js.map +1 -0
- package/lib/fs/s3/createS3MiniFileSystem.js +705 -0
- package/lib/fs/s3/createS3MiniFileSystem.js.map +1 -0
- package/lib/fs/s3/index.js +2 -0
- package/lib/fs/s3/index.js.map +1 -0
- package/lib/fs/s3/s3mini.test.js +584 -0
- package/lib/fs/scandir.js +59 -0
- package/lib/fs/scandir.js.map +1 -0
- package/lib/fs/server/createDatabaseFileSystem.js +750 -0
- package/lib/fs/server/createDatabaseFileSystem.js.map +1 -0
- package/lib/fs/server/createNodeFileSystem.js +401 -0
- package/lib/fs/server/createNodeFileSystem.js.map +1 -0
- package/lib/fs/server/dbfs.test.js +221 -0
- package/lib/fs/server/index.js +2 -0
- package/lib/fs/server/index.js.map +1 -0
- package/lib/fs/server/loadTestDatabase.js +127 -0
- package/lib/fs/server/loadTestDatabase.js.map +1 -0
- package/lib/fs/tests/runFileSystemTest.js +318 -0
- package/lib/fs/tests/runFileSystemTest.js.map +1 -0
- package/lib/fs/types.js +27 -0
- package/lib/fs/types.js.map +1 -0
- package/lib/fs/utils/getFileUrl.js +35 -0
- package/lib/fs/utils/getFileUrl.js.map +1 -0
- package/lib/fs/utils.js +22 -0
- package/lib/fs/utils.js.map +1 -0
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -0
- package/lib/jsonschema/JsonSchema.js +146 -172
- package/lib/jsonschema/JsonSchema.js.map +1 -0
- package/lib/jsonschema/forEachJsonSchema.js +44 -0
- package/lib/jsonschema/forEachJsonSchema.js.map +1 -0
- package/lib/jsonschema/index.js +2 -0
- package/lib/jsonschema/index.js.map +1 -0
- package/lib/jsonschema/types.d.js +2 -0
- package/lib/jsonschema/types.d.js.map +1 -0
- package/lib/meta/defineFileType.js +20 -103
- package/lib/meta/defineFileType.js.map +1 -0
- package/lib/meta/defineInit.js +31 -250
- package/lib/meta/defineInit.js.map +1 -0
- package/lib/meta/defineMetadata.js +24 -140
- package/lib/meta/defineMetadata.js.map +1 -0
- package/lib/meta/index.js +1 -0
- package/lib/meta/index.js.map +1 -0
- package/lib/orpc/createOpenApiContractClient.js +27 -0
- package/lib/orpc/createOpenApiContractClient.js.map +1 -0
- package/lib/orpc/createRpcContractClient.js +34 -0
- package/lib/orpc/createRpcContractClient.js.map +1 -0
- package/lib/orpc/index.js +3 -0
- package/lib/orpc/index.js.map +1 -0
- package/lib/orpc/resolveLinkPlugins.js +28 -0
- package/lib/orpc/resolveLinkPlugins.js.map +1 -0
- package/lib/password/PHC.js +63 -87
- package/lib/password/PHC.js.map +1 -0
- package/lib/password/PHC.test.js +11 -3
- package/lib/password/Password.js +29 -294
- package/lib/password/Password.js.map +1 -0
- package/lib/password/Password.test.js +35 -22
- package/lib/password/createArgon2PasswordAlgorithm.js +35 -191
- package/lib/password/createArgon2PasswordAlgorithm.js.map +1 -0
- package/lib/password/createBase64PasswordAlgorithm.js +8 -141
- package/lib/password/createBase64PasswordAlgorithm.js.map +1 -0
- package/lib/password/createBcryptPasswordAlgorithm.js +13 -168
- package/lib/password/createBcryptPasswordAlgorithm.js.map +1 -0
- package/lib/password/createPBKDF2PasswordAlgorithm.js +46 -228
- package/lib/password/createPBKDF2PasswordAlgorithm.js.map +1 -0
- package/lib/password/createScryptPasswordAlgorithm.js +55 -211
- package/lib/password/createScryptPasswordAlgorithm.js.map +1 -0
- package/lib/password/index.js +1 -0
- package/lib/password/index.js.map +1 -0
- package/lib/password/server/index.js +1 -0
- package/lib/password/server/index.js.map +1 -0
- package/lib/resource/Identifiable.js +2 -0
- package/lib/resource/Identifiable.js.map +1 -0
- package/lib/resource/ListQuery.js +21 -93
- package/lib/resource/ListQuery.js.map +1 -0
- package/lib/resource/getTitleOfResource.js +3 -5
- package/lib/resource/getTitleOfResource.js.map +1 -0
- package/lib/resource/index.js +1 -0
- package/lib/resource/index.js.map +1 -0
- package/lib/resource/schema/AnyResourceSchema.js +2 -1
- package/lib/resource/schema/AnyResourceSchema.js.map +1 -0
- package/lib/resource/schema/BaseResourceSchema.js +2 -1
- package/lib/resource/schema/BaseResourceSchema.js.map +1 -0
- package/lib/resource/schema/ResourceActionType.js +6 -4
- package/lib/resource/schema/ResourceActionType.js.map +1 -0
- package/lib/resource/schema/ResourceStatus.js +5 -3
- package/lib/resource/schema/ResourceStatus.js.map +1 -0
- package/lib/resource/schema/ResourceType.js +5 -3
- package/lib/resource/schema/ResourceType.js.map +1 -0
- package/lib/resource/schema/index.js +1 -0
- package/lib/resource/schema/index.js.map +1 -0
- package/lib/resource/schema/types.js +16 -20
- package/lib/resource/schema/types.js.map +1 -0
- package/lib/s3/formatS3Url.js +65 -0
- package/lib/s3/formatS3Url.js.map +1 -0
- package/lib/s3/formatS3Url.test.js +262 -0
- package/lib/s3/index.js +3 -0
- package/lib/s3/index.js.map +1 -0
- package/lib/s3/parseS3Url.js +65 -0
- package/lib/s3/parseS3Url.js.map +1 -0
- package/lib/s3/parseS3Url.test.js +270 -0
- package/lib/schema/SchemaRegistry.js +38 -38
- package/lib/schema/SchemaRegistry.js.map +1 -0
- package/lib/schema/TypeSchema.d.js +2 -0
- package/lib/schema/TypeSchema.d.js.map +1 -0
- package/lib/schema/createSchemaData.js +26 -125
- package/lib/schema/createSchemaData.js.map +1 -0
- package/lib/schema/findJsonSchemaByPath.js +13 -36
- package/lib/schema/findJsonSchemaByPath.js.map +1 -0
- package/lib/schema/formatZodError.js +140 -0
- package/lib/schema/formatZodError.js.map +1 -0
- package/lib/schema/formatZodError.test.js +196 -0
- package/lib/schema/getSchemaCache.js +5 -5
- package/lib/schema/getSchemaCache.js.map +1 -0
- package/lib/schema/getSchemaOptions.js +8 -11
- package/lib/schema/getSchemaOptions.js.map +1 -0
- package/lib/schema/index.js +2 -1
- package/lib/schema/index.js.map +1 -0
- package/lib/schema/toJsonSchema.js +47 -290
- package/lib/schema/toJsonSchema.js.map +1 -0
- package/lib/schema/validate.js +33 -45
- package/lib/schema/validate.js.map +1 -0
- package/lib/tools/generateSchema.js +39 -197
- package/lib/tools/generateSchema.js.map +1 -0
- package/lib/tools/renderJsonSchemaToMarkdownDoc.js +55 -143
- package/lib/tools/renderJsonSchemaToMarkdownDoc.js.map +1 -0
- package/lib/utils/buildBaseUrl.js +13 -0
- package/lib/utils/buildBaseUrl.js.map +1 -0
- package/lib/utils/buildRedactorFormSchema.js +59 -0
- package/lib/utils/buildRedactorFormSchema.js.map +1 -0
- package/lib/utils/getEstimateProcessTime.js +12 -11
- package/lib/utils/getEstimateProcessTime.js.map +1 -0
- package/lib/utils/index.js +3 -0
- package/lib/utils/index.js.map +1 -0
- package/lib/utils/resolveFeatureOptions.js +12 -0
- package/lib/utils/resolveFeatureOptions.js.map +1 -0
- package/package.json +61 -13
- package/src/ai/qwen3vl/index.ts +1 -0
- package/src/ai/qwen3vl/utils.ts +36 -0
- package/src/ai/vision/DocLayoutElementTypeSchema.ts +30 -0
- package/src/ai/vision/ImageAnnotationSchema.ts +60 -0
- package/src/ai/vision/index.ts +2 -0
- package/src/ai/vision/resolveImageAnnotation.ts +135 -0
- package/src/cn/ChineseResidentIdNo.test.ts +1 -1
- package/src/cn/ChineseResidentIdNo.ts +8 -0
- package/src/cn/DivisionCode.test.ts +1 -1
- package/src/cn/DivisionCode.ts +8 -0
- package/src/cn/UnifiedSocialCreditCode.test.ts +1 -1
- package/src/cn/UnifiedSocialCreditCode.ts +15 -0
- package/src/cn/__snapshots__/UnifiedSocialCreditCode.test.ts.snap +23 -0
- package/src/cn/formatChineseAmount.ts +61 -0
- package/src/cn/index.ts +7 -1
- package/src/cn/parseChineseNumber.test.ts +159 -0
- package/src/cn/parseChineseNumber.ts +97 -0
- package/src/cn/pinyin/cartesianProduct.test.ts +64 -0
- package/src/cn/pinyin/cartesianProduct.ts +24 -0
- package/src/cn/pinyin/data.json +23573 -0
- package/src/cn/pinyin/loader.ts +12 -0
- package/src/cn/pinyin/preload.ts +3 -0
- package/src/cn/pinyin/toPinyin.test.ts +12 -0
- package/src/cn/pinyin/toPinyinPure.ts +43 -0
- package/src/cn/pinyin/transform.ts +12 -0
- package/src/consola/formatLogObject.test.ts +27 -0
- package/src/consola/formatLogObject.ts +34 -6
- package/src/dayjs/dayjs.ts +18 -18
- package/src/dayjs/index.ts +3 -1
- package/src/dayjs/parseRelativeTime.test.ts +185 -0
- package/src/dayjs/parseRelativeTime.ts +115 -0
- package/src/dayjs/resolveRelativeTime.test.ts +357 -0
- package/src/dayjs/resolveRelativeTime.ts +167 -0
- package/src/emittery/emitter.ts +9 -0
- package/src/emittery/index.ts +1 -0
- package/src/fs/FileSystemError.ts +26 -0
- package/src/fs/IFileSystem.d.ts +102 -0
- package/src/fs/MemoryFileSystem.test.ts +37 -0
- package/src/fs/createBrowserFileSystem.ts +291 -0
- package/src/fs/createMemoryFileSystem.ts +604 -0
- package/src/fs/createSandboxFileSystem.ts +136 -0
- package/src/fs/createWebDavFileSystem.ts +172 -0
- package/src/fs/findMimeType.ts +23 -0
- package/src/fs/index.ts +8 -0
- package/src/fs/orpc/FileSystemContract.ts +92 -0
- package/src/fs/orpc/createContractClientFileSystem.ts +115 -0
- package/src/fs/orpc/index.ts +2 -0
- package/src/fs/orpc/server/createFileSystemContractImpl.ts +64 -0
- package/src/fs/orpc/server/index.ts +1 -0
- package/src/fs/s3/createS3MiniFileSystem.ts +830 -0
- package/src/fs/s3/index.ts +1 -0
- package/src/fs/s3/s3mini.test.ts +264 -0
- package/src/fs/scandir.ts +75 -0
- package/src/fs/server/createDatabaseFileSystem.ts +668 -0
- package/src/fs/server/createNodeFileSystem.ts +499 -0
- package/src/fs/server/dbfs.test.ts +47 -0
- package/src/fs/server/index.ts +1 -0
- package/src/fs/server/loadTestDatabase.ts +131 -0
- package/src/fs/tests/runFileSystemTest.ts +288 -0
- package/src/fs/types.ts +29 -0
- package/src/fs/utils/getFileUrl.ts +44 -0
- package/src/fs/utils.ts +23 -0
- package/src/jsonschema/JsonSchema.ts +118 -110
- package/src/jsonschema/forEachJsonSchema.ts +50 -0
- package/src/jsonschema/index.ts +1 -0
- package/src/orpc/createOpenApiContractClient.ts +52 -0
- package/src/orpc/createRpcContractClient.ts +50 -0
- package/src/orpc/index.ts +2 -0
- package/src/orpc/resolveLinkPlugins.ts +29 -0
- package/src/password/PHC.ts +3 -3
- package/src/password/Password.test.ts +1 -1
- package/src/password/createPBKDF2PasswordAlgorithm.ts +2 -2
- package/src/resource/schema/AnyResourceSchema.ts +16 -2
- package/src/s3/formatS3Url.test.ts +254 -0
- package/src/s3/formatS3Url.ts +84 -0
- package/src/s3/index.ts +2 -0
- package/src/s3/parseS3Url.test.ts +258 -0
- package/src/s3/parseS3Url.ts +88 -0
- package/src/schema/SchemaRegistry.ts +35 -33
- package/src/schema/formatZodError.test.ts +196 -0
- package/src/schema/formatZodError.ts +151 -0
- package/src/schema/getSchemaOptions.ts +2 -2
- package/src/schema/index.ts +1 -1
- package/src/utils/buildBaseUrl.ts +12 -0
- package/src/utils/buildRedactorFormSchema.ts +85 -0
- package/src/utils/index.ts +4 -0
- package/src/utils/resolveFeatureOptions.ts +14 -0
- package/src/cn/ChineseResidentIdNo.mod.ts +0 -7
- package/src/cn/DivisionCode.mod.ts +0 -7
- package/src/cn/UnifiedSocialCreditCode.mod.ts +0 -7
- package/src/cn/mod.ts +0 -3
- package/src/schema/SchemaRegistry.mod.ts +0 -1
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { z } from "zod/v4";
|
|
3
|
+
import { formatZodError } from "./formatZodError.js";
|
|
4
|
+
describe("formatZodError", function () {
|
|
5
|
+
it("should format invalid_type error", function () {
|
|
6
|
+
var schema = z.object({
|
|
7
|
+
name: z.string(),
|
|
8
|
+
age: z.number()
|
|
9
|
+
});
|
|
10
|
+
var result = schema.safeParse({
|
|
11
|
+
name: 123,
|
|
12
|
+
age: "not-a-number"
|
|
13
|
+
});
|
|
14
|
+
expect(result.success).toBe(false);
|
|
15
|
+
if (!result.success) {
|
|
16
|
+
var formatted = formatZodError(result.error);
|
|
17
|
+
expect(formatted).toContain("name");
|
|
18
|
+
expect(formatted).toContain("age");
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
it("should format required field error", function () {
|
|
22
|
+
var schema = z.object({
|
|
23
|
+
name: z.string(),
|
|
24
|
+
email: z.string().email()
|
|
25
|
+
});
|
|
26
|
+
var result = schema.safeParse({});
|
|
27
|
+
expect(result.success).toBe(false);
|
|
28
|
+
if (!result.success) {
|
|
29
|
+
var formatted = formatZodError(result.error);
|
|
30
|
+
expect(formatted).toContain("\u5FC5\u586B");
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
it("should format email validation error", function () {
|
|
34
|
+
var schema = z.object({
|
|
35
|
+
email: z.string().email()
|
|
36
|
+
});
|
|
37
|
+
var result = schema.safeParse({
|
|
38
|
+
email: "invalid-email"
|
|
39
|
+
});
|
|
40
|
+
expect(result.success).toBe(false);
|
|
41
|
+
if (!result.success) {
|
|
42
|
+
var formatted = formatZodError(result.error);
|
|
43
|
+
expect(formatted).toContain("\u8BF7\u8F93\u5165\u6709\u6548\u7684\u90AE\u7BB1\u5730\u5740");
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
it("should format URL validation error", function () {
|
|
47
|
+
var schema = z.object({
|
|
48
|
+
url: z.string().url()
|
|
49
|
+
});
|
|
50
|
+
var result = schema.safeParse({
|
|
51
|
+
url: "not-a-url"
|
|
52
|
+
});
|
|
53
|
+
expect(result.success).toBe(false);
|
|
54
|
+
if (!result.success) {
|
|
55
|
+
var formatted = formatZodError(result.error);
|
|
56
|
+
expect(formatted).toContain("\u8BF7\u8F93\u5165\u6709\u6548\u7684 URL");
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
it("should format too_small error for string", function () {
|
|
60
|
+
var schema = z.object({
|
|
61
|
+
name: z.string().min(5)
|
|
62
|
+
});
|
|
63
|
+
var result = schema.safeParse({
|
|
64
|
+
name: "abc"
|
|
65
|
+
});
|
|
66
|
+
expect(result.success).toBe(false);
|
|
67
|
+
if (!result.success) {
|
|
68
|
+
var formatted = formatZodError(result.error);
|
|
69
|
+
expect(formatted).toContain("\u81F3\u5C11\u9700\u8981");
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
it("should format too_small error for number", function () {
|
|
73
|
+
var schema = z.object({
|
|
74
|
+
age: z.number().min(18)
|
|
75
|
+
});
|
|
76
|
+
var result = schema.safeParse({
|
|
77
|
+
age: 16
|
|
78
|
+
});
|
|
79
|
+
expect(result.success).toBe(false);
|
|
80
|
+
if (!result.success) {
|
|
81
|
+
var formatted = formatZodError(result.error);
|
|
82
|
+
expect(formatted).toContain("\u81F3\u5C11\u9700\u8981 18");
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
it("should format too_big error for string", function () {
|
|
86
|
+
var schema = z.object({
|
|
87
|
+
name: z.string().max(10)
|
|
88
|
+
});
|
|
89
|
+
var result = schema.safeParse({
|
|
90
|
+
name: "this is too long"
|
|
91
|
+
});
|
|
92
|
+
expect(result.success).toBe(false);
|
|
93
|
+
if (!result.success) {
|
|
94
|
+
var formatted = formatZodError(result.error);
|
|
95
|
+
expect(formatted).toContain("\u6700\u591A 10 \u4E2A\u5B57\u7B26");
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
it("should format invalid_enum_value error", function () {
|
|
99
|
+
var schema = z.object({
|
|
100
|
+
status: z.enum([
|
|
101
|
+
"active",
|
|
102
|
+
"inactive",
|
|
103
|
+
"pending"
|
|
104
|
+
])
|
|
105
|
+
});
|
|
106
|
+
var result = schema.safeParse({
|
|
107
|
+
status: "invalid"
|
|
108
|
+
});
|
|
109
|
+
expect(result.success).toBe(false);
|
|
110
|
+
if (!result.success) {
|
|
111
|
+
var formatted = formatZodError(result.error);
|
|
112
|
+
expect(formatted).toContain("\u5FC5\u987B\u662F\u4EE5\u4E0B\u503C\u4E4B\u4E00");
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
it("should format invalid_literal error", function () {
|
|
116
|
+
var schema = z.object({
|
|
117
|
+
type: z.literal("user")
|
|
118
|
+
});
|
|
119
|
+
var result = schema.safeParse({
|
|
120
|
+
type: "admin"
|
|
121
|
+
});
|
|
122
|
+
expect(result.success).toBe(false);
|
|
123
|
+
if (!result.success) {
|
|
124
|
+
var formatted = formatZodError(result.error);
|
|
125
|
+
expect(formatted).toContain("\u5FC5\u987B\u662F user");
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
it("should format nested object errors", function () {
|
|
129
|
+
var schema = z.object({
|
|
130
|
+
user: z.object({
|
|
131
|
+
name: z.string(),
|
|
132
|
+
email: z.string().email()
|
|
133
|
+
})
|
|
134
|
+
});
|
|
135
|
+
var result = schema.safeParse({
|
|
136
|
+
user: {
|
|
137
|
+
name: "",
|
|
138
|
+
email: "invalid"
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
expect(result.success).toBe(false);
|
|
142
|
+
if (!result.success) {
|
|
143
|
+
var formatted = formatZodError(result.error);
|
|
144
|
+
expect(formatted).toContain("user");
|
|
145
|
+
expect(formatted).toContain("email");
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
it("should format array errors", function () {
|
|
149
|
+
var schema = z.object({
|
|
150
|
+
tags: z.array(z.string()).min(1)
|
|
151
|
+
});
|
|
152
|
+
var result = schema.safeParse({
|
|
153
|
+
tags: []
|
|
154
|
+
});
|
|
155
|
+
expect(result.success).toBe(false);
|
|
156
|
+
if (!result.success) {
|
|
157
|
+
var formatted = formatZodError(result.error);
|
|
158
|
+
expect(formatted).toContain("\u81F3\u5C11\u9700\u8981 1 \u9879");
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
it("should format multiple errors", function () {
|
|
162
|
+
var schema = z.object({
|
|
163
|
+
name: z.string().min(1),
|
|
164
|
+
email: z.string().email(),
|
|
165
|
+
age: z.number().min(18)
|
|
166
|
+
});
|
|
167
|
+
var result = schema.safeParse({
|
|
168
|
+
name: "",
|
|
169
|
+
email: "invalid",
|
|
170
|
+
age: 15
|
|
171
|
+
});
|
|
172
|
+
expect(result.success).toBe(false);
|
|
173
|
+
if (!result.success) {
|
|
174
|
+
var formatted = formatZodError(result.error);
|
|
175
|
+
// Should contain multiple error messages separated by ';'
|
|
176
|
+
var parts = formatted.split("\uFF1B");
|
|
177
|
+
expect(parts.length).toBeGreaterThan(1);
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
it("should use schema description when available", function () {
|
|
181
|
+
var schema = z.object({
|
|
182
|
+
name: z.string().describe("\u59D3\u540D"),
|
|
183
|
+
email: z.string().email().describe("\u90AE\u7BB1\u5730\u5740")
|
|
184
|
+
});
|
|
185
|
+
var result = schema.safeParse({
|
|
186
|
+
name: "",
|
|
187
|
+
email: "invalid"
|
|
188
|
+
});
|
|
189
|
+
expect(result.success).toBe(false);
|
|
190
|
+
if (!result.success) {
|
|
191
|
+
var formatted = formatZodError(result.error, schema);
|
|
192
|
+
expect(formatted).toContain("\u59D3\u540D");
|
|
193
|
+
expect(formatted).toContain("\u90AE\u7BB1\u5730\u5740");
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
});
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
const _cache = new WeakMap();
|
|
2
2
|
export function getSchemaCache(obj, key, compute) {
|
|
3
|
-
|
|
4
|
-
var c = _cache.get(obj);
|
|
3
|
+
let c = _cache.get(obj);
|
|
5
4
|
if (!c) {
|
|
6
5
|
c = {};
|
|
7
6
|
_cache.set(obj, c);
|
|
8
7
|
}
|
|
9
|
-
|
|
10
|
-
return (_ = (_c = c)[_key = key]) !== null && _ !== void 0 ? _ : _c[_key] = compute();
|
|
8
|
+
return c[key] ??= compute();
|
|
11
9
|
}
|
|
10
|
+
|
|
11
|
+
//# sourceMappingURL=getSchemaCache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/schema/getSchemaCache.ts"],"sourcesContent":["import type { JsonSchemaDef } from '../jsonschema';\nimport type { TypeSchema } from './TypeSchema';\n\ntype SchemaCache = {\n\tjsonschema?: JsonSchemaDef;\n\toptions?: Array<{ value: string; label: string }>;\n};\nconst _cache = new WeakMap<any, SchemaCache>();\n\nexport function getSchemaCache<K extends keyof SchemaCache>(\n\tobj: TypeSchema,\n\tkey: K,\n\tcompute: () => NonNullable<SchemaCache[K]>,\n): NonNullable<SchemaCache[K]> {\n\tlet c = _cache.get(obj);\n\tif (!c) {\n\t\tc = {};\n\t\t_cache.set(obj, c);\n\t}\n\treturn (c[key] ??= compute());\n}\n"],"names":["_cache","WeakMap","getSchemaCache","obj","key","compute","c","get","set"],"mappings":"AAOA,MAAMA,SAAS,IAAIC;AAEnB,OAAO,SAASC,eACfC,GAAe,EACfC,GAAM,EACNC,OAA0C;IAE1C,IAAIC,IAAIN,OAAOO,GAAG,CAACJ;IACnB,IAAI,CAACG,GAAG;QACPA,IAAI,CAAC;QACLN,OAAOQ,GAAG,CAACL,KAAKG;IACjB;IACA,OAAQA,CAAC,CAACF,IAAI,KAAKC;AACpB"}
|
|
@@ -1,24 +1,21 @@
|
|
|
1
1
|
import { deepFreeze } from "@wener/utils";
|
|
2
2
|
import { getSchemaCache } from "./getSchemaCache.js";
|
|
3
|
-
import { SchemaRegistry } from "./SchemaRegistry.
|
|
3
|
+
import { SchemaRegistry } from "./SchemaRegistry.js";
|
|
4
4
|
export function getSchemaOptions(s) {
|
|
5
|
-
|
|
6
|
-
return getSchemaCache(s, "options",
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
var value = v.const;
|
|
5
|
+
let js = SchemaRegistry.get(s);
|
|
6
|
+
return getSchemaCache(s, "options", () => {
|
|
7
|
+
let out = (js.anyOf || js.oneOf)?.map((v) => {
|
|
8
|
+
let value = v.const;
|
|
10
9
|
return {
|
|
11
10
|
label: String(v.description || v.title || value),
|
|
12
11
|
value: value ? String(value) : ""
|
|
13
12
|
};
|
|
14
|
-
})
|
|
13
|
+
}) || [];
|
|
15
14
|
out = deepFreeze(out);
|
|
16
15
|
return out;
|
|
17
16
|
});
|
|
18
17
|
}
|
|
19
18
|
export function getSchemaOptionLabel(schema, value) {
|
|
20
|
-
|
|
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;
|
|
19
|
+
return getSchemaOptions(schema).find((v) => v.value === value)?.label;
|
|
24
20
|
}
|
|
21
|
+
//# sourceMappingURL=getSchemaOptions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/schema/getSchemaOptions.ts"],"sourcesContent":["import { deepFreeze } from '@wener/utils';\nimport { getSchemaCache } from './getSchemaCache';\nimport { SchemaRegistry } from './SchemaRegistry';\nimport type { TypeSchema } from './TypeSchema';\n\nexport function getSchemaOptions(s: TypeSchema): Array<{ value: string; label: string }> {\n\tlet js = SchemaRegistry.get(s);\n\treturn getSchemaCache(s, 'options', () => {\n\t\tlet out =\n\t\t\t(js.anyOf || js.oneOf)?.map((v: any) => {\n\t\t\t\tlet value = v.const;\n\t\t\t\treturn {\n\t\t\t\t\tlabel: String(v.description || v.title || value),\n\t\t\t\t\tvalue: value ? String(value) : '',\n\t\t\t\t};\n\t\t\t}) || [];\n\t\tout = deepFreeze(out);\n\t\treturn out;\n\t});\n}\n\nexport function getSchemaOptionLabel(schema: TypeSchema, value: string | undefined | null): string | undefined {\n\treturn getSchemaOptions(schema).find((v) => v.value === value)?.label;\n}\n"],"names":["deepFreeze","getSchemaCache","SchemaRegistry","getSchemaOptions","s","js","get","out","anyOf","oneOf","map","v","value","const","label","String","description","title","getSchemaOptionLabel","schema","find"],"mappings":"AAAA,SAASA,UAAU,QAAQ,eAAe;AAC1C,SAASC,cAAc,QAAQ,mBAAmB;AAClD,SAASC,cAAc,QAAQ,mBAAmB;AAGlD,OAAO,SAASC,iBAAiBC,CAAa;IAC7C,IAAIC,KAAKH,eAAeI,GAAG,CAACF;IAC5B,OAAOH,eAAeG,GAAG,WAAW;QACnC,IAAIG,MACH,AAACF,CAAAA,GAAGG,KAAK,IAAIH,GAAGI,KAAK,AAAD,GAAIC,IAAI,CAACC;YAC5B,IAAIC,QAAQD,EAAEE,KAAK;YACnB,OAAO;gBACNC,OAAOC,OAAOJ,EAAEK,WAAW,IAAIL,EAAEM,KAAK,IAAIL;gBAC1CA,OAAOA,QAAQG,OAAOH,SAAS;YAChC;QACD,MAAM,EAAE;QACTL,MAAMP,WAAWO;QACjB,OAAOA;IACR;AACD;AAEA,OAAO,SAASW,qBAAqBC,MAAkB,EAAEP,KAAgC;IACxF,OAAOT,iBAAiBgB,QAAQC,IAAI,CAAC,CAACT,IAAMA,EAAEC,KAAK,KAAKA,QAAQE;AACjE"}
|
package/lib/schema/index.js
CHANGED
|
@@ -3,4 +3,5 @@ export { getSchemaOptions, getSchemaOptionLabel } from "./getSchemaOptions.js";
|
|
|
3
3
|
export { toJsonSchema } from "./toJsonSchema.js";
|
|
4
4
|
export { findJsonSchemaByPath } from "./findJsonSchemaByPath.js";
|
|
5
5
|
export { createSchemaData } from "./createSchemaData.js";
|
|
6
|
-
export { SchemaRegistry } from "./SchemaRegistry.
|
|
6
|
+
export { SchemaRegistry } from "./SchemaRegistry.js";
|
|
7
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/schema/index.ts"],"sourcesContent":["export type * from './TypeSchema';\nexport { isJsonSchema, isZodSchema, isTypeBoxSchema, validate, parseData, type ValidationResult } from './validate';\nexport { getSchemaOptions, getSchemaOptionLabel } from './getSchemaOptions';\nexport { toJsonSchema } from './toJsonSchema';\nexport { findJsonSchemaByPath } from './findJsonSchemaByPath';\nexport { createSchemaData } from './createSchemaData';\nexport { SchemaRegistry } from './SchemaRegistry';\n"],"names":["isJsonSchema","isZodSchema","isTypeBoxSchema","validate","parseData","getSchemaOptions","getSchemaOptionLabel","toJsonSchema","findJsonSchemaByPath","createSchemaData","SchemaRegistry"],"mappings":"AACA,SAASA,YAAY,EAAEC,WAAW,EAAEC,eAAe,EAAEC,QAAQ,EAAEC,SAAS,QAA+B,aAAa;AACpH,SAASC,gBAAgB,EAAEC,oBAAoB,QAAQ,qBAAqB;AAC5E,SAASC,YAAY,QAAQ,iBAAiB;AAC9C,SAASC,oBAAoB,QAAQ,yBAAyB;AAC9D,SAASC,gBAAgB,QAAQ,qBAAqB;AACtD,SAASC,cAAc,QAAQ,mBAAmB"}
|