@wener/common 2.0.5 → 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.
Files changed (206) hide show
  1. package/lib/ai/qwen3vl/index.js +1 -1
  2. package/lib/ai/qwen3vl/utils.js +15 -15
  3. package/lib/ai/vision/DocLayoutElementTypeSchema.js +22 -22
  4. package/lib/ai/vision/ImageAnnotationSchema.js +63 -47
  5. package/lib/ai/vision/index.js +2 -2
  6. package/lib/ai/vision/resolveImageAnnotation.js +81 -95
  7. package/lib/cn/ChineseResidentIdNo.js +55 -41
  8. package/lib/cn/ChineseResidentIdNo.mod.js +6 -1
  9. package/lib/cn/ChineseResidentIdNo.test.js +22 -21
  10. package/lib/cn/DivisionCode.js +220 -235
  11. package/lib/cn/DivisionCode.mod.js +6 -1
  12. package/lib/cn/DivisionCode.test.js +92 -121
  13. package/lib/cn/Mod11.js +18 -37
  14. package/lib/cn/Mod31.js +23 -41
  15. package/lib/cn/UnifiedSocialCreditCode.js +143 -137
  16. package/lib/cn/UnifiedSocialCreditCode.mod.js +6 -1
  17. package/lib/cn/UnifiedSocialCreditCode.test.js +21 -15
  18. package/lib/cn/formatChineseAmount.js +46 -71
  19. package/lib/cn/index.js +6 -6
  20. package/lib/cn/mod.js +5 -3
  21. package/lib/cn/parseChineseNumber.js +81 -85
  22. package/lib/cn/parseChineseNumber.test.js +183 -261
  23. package/lib/cn/pinyin/cartesianProduct.js +19 -19
  24. package/lib/cn/pinyin/cartesianProduct.test.js +78 -178
  25. package/lib/cn/pinyin/loader.js +13 -11
  26. package/lib/cn/pinyin/preload.js +2 -1
  27. package/lib/cn/pinyin/toPinyin.test.js +149 -161
  28. package/lib/cn/pinyin/toPinyinPure.js +28 -23
  29. package/lib/cn/pinyin/transform.js +11 -11
  30. package/lib/cn/types.d.js +2 -2
  31. package/lib/consola/createStandardConsolaReporter.js +14 -15
  32. package/lib/consola/formatLogObject.js +149 -133
  33. package/lib/consola/formatLogObject.test.js +167 -178
  34. package/lib/consola/index.js +2 -2
  35. package/lib/data/formatSort.js +14 -12
  36. package/lib/data/formatSort.test.js +33 -33
  37. package/lib/data/index.js +3 -3
  38. package/lib/data/maybeNumber.js +23 -23
  39. package/lib/data/parseSort.js +75 -68
  40. package/lib/data/parseSort.test.js +196 -187
  41. package/lib/data/resolvePagination.js +38 -39
  42. package/lib/data/resolvePagination.test.js +228 -218
  43. package/lib/data/types.d.js +2 -2
  44. package/lib/dayjs/dayjs.js +20 -20
  45. package/lib/dayjs/formatDuration.js +56 -56
  46. package/lib/dayjs/formatDuration.test.js +63 -77
  47. package/lib/dayjs/index.js +4 -4
  48. package/lib/dayjs/parseDuration.js +21 -26
  49. package/lib/dayjs/parseRelativeTime.js +65 -66
  50. package/lib/dayjs/parseRelativeTime.test.js +227 -243
  51. package/lib/dayjs/resolveRelativeTime.js +73 -72
  52. package/lib/dayjs/resolveRelativeTime.test.js +296 -307
  53. package/lib/decimal/index.js +1 -1
  54. package/lib/decimal/parseDecimal.js +12 -12
  55. package/lib/drain3/Drain.js +303 -338
  56. package/lib/drain3/LogCluster.js +25 -25
  57. package/lib/drain3/Node.js +24 -24
  58. package/lib/drain3/TemplateMiner.js +197 -196
  59. package/lib/drain3/index.js +5 -5
  60. package/lib/drain3/persistence/FilePersistence.js +19 -19
  61. package/lib/drain3/persistence/MemoryPersistence.js +8 -8
  62. package/lib/drain3/persistence/PersistenceHandler.js +2 -2
  63. package/lib/drain3/types.js +2 -2
  64. package/lib/emittery/emitter.js +7 -7
  65. package/lib/emittery/index.js +1 -1
  66. package/lib/foundation/schema/SexType.js +15 -12
  67. package/lib/foundation/schema/index.js +1 -1
  68. package/lib/foundation/schema/parseSexType.js +15 -16
  69. package/lib/foundation/schema/types.js +8 -6
  70. package/lib/fs/FileSystemError.js +18 -18
  71. package/lib/fs/IFileSystem.d.js +2 -2
  72. package/lib/fs/MemoryFileSystem.test.js +172 -181
  73. package/lib/fs/createBrowserFileSystem.js +222 -235
  74. package/lib/fs/createMemoryFileSystem.js +472 -510
  75. package/lib/fs/createSandboxFileSystem.js +102 -101
  76. package/lib/fs/createWebDavFileSystem.js +162 -149
  77. package/lib/fs/createWebFileSystem.js +197 -220
  78. package/lib/fs/findMimeType.js +14 -14
  79. package/lib/fs/index.js +7 -7
  80. package/lib/fs/minio/createMinioFileSystem.js +959 -956
  81. package/lib/fs/minio/index.js +1 -1
  82. package/lib/fs/orpc/FileSystemContract.js +57 -57
  83. package/lib/fs/orpc/createContractClientFileSystem.js +88 -88
  84. package/lib/fs/orpc/index.js +2 -2
  85. package/lib/fs/orpc/server/createFileSystemContractImpl.js +62 -60
  86. package/lib/fs/orpc/server/index.js +1 -1
  87. package/lib/fs/s3/createS3MiniFileSystem.js +756 -737
  88. package/lib/fs/s3/index.js +1 -1
  89. package/lib/fs/s3/s3mini.test.js +524 -553
  90. package/lib/fs/scandir.js +56 -56
  91. package/lib/fs/server/createDatabaseFileSystem.js +834 -741
  92. package/lib/fs/server/createNodeFileSystem.js +407 -405
  93. package/lib/fs/server/dbfs.test.js +201 -214
  94. package/lib/fs/server/index.js +1 -1
  95. package/lib/fs/server/loadTestDatabase.js +40 -43
  96. package/lib/fs/tests/runFileSystemTest.js +352 -316
  97. package/lib/fs/types.js +17 -20
  98. package/lib/fs/utils/getFileUrl.js +24 -30
  99. package/lib/fs/utils.js +17 -17
  100. package/lib/fs/webdav/index.js +1 -1
  101. package/lib/index.js +2 -2
  102. package/lib/jsonschema/JsonSchema.js +216 -155
  103. package/lib/jsonschema/JsonSchema.test.js +123 -124
  104. package/lib/jsonschema/forEachJsonSchema.js +41 -41
  105. package/lib/jsonschema/index.js +2 -2
  106. package/lib/jsonschema/types.d.js +2 -2
  107. package/lib/meta/defineFileType.js +32 -38
  108. package/lib/meta/defineInit.js +39 -35
  109. package/lib/meta/defineMetadata.js +37 -34
  110. package/lib/meta/defineMetadata.test.js +13 -12
  111. package/lib/meta/index.js +3 -3
  112. package/lib/orpc/createOpenApiContractClient.js +26 -24
  113. package/lib/orpc/createRpcContractClient.js +37 -31
  114. package/lib/orpc/index.js +2 -2
  115. package/lib/orpc/resolveLinkPlugins.js +25 -25
  116. package/lib/password/PHC.js +187 -189
  117. package/lib/password/PHC.test.js +517 -535
  118. package/lib/password/Password.js +85 -80
  119. package/lib/password/Password.test.js +330 -364
  120. package/lib/password/createArgon2PasswordAlgorithm.js +50 -51
  121. package/lib/password/createBase64PasswordAlgorithm.js +11 -11
  122. package/lib/password/createBcryptPasswordAlgorithm.js +20 -18
  123. package/lib/password/createPBKDF2PasswordAlgorithm.js +65 -52
  124. package/lib/password/createScryptPasswordAlgorithm.js +74 -63
  125. package/lib/password/index.js +5 -5
  126. package/lib/password/server/index.js +1 -1
  127. package/lib/resource/Identifiable.js +2 -2
  128. package/lib/resource/ListQuery.js +42 -42
  129. package/lib/resource/getTitleOfResource.js +5 -5
  130. package/lib/resource/index.js +2 -2
  131. package/lib/resource/schema/AnyResourceSchema.js +91 -89
  132. package/lib/resource/schema/BaseResourceSchema.js +26 -26
  133. package/lib/resource/schema/ResourceActionType.js +117 -115
  134. package/lib/resource/schema/ResourceStatus.js +94 -92
  135. package/lib/resource/schema/ResourceType.js +25 -23
  136. package/lib/resource/schema/index.js +5 -5
  137. package/lib/resource/schema/types.js +86 -55
  138. package/lib/resource/schema/types.test.js +16 -13
  139. package/lib/s3/formatS3Url.js +60 -60
  140. package/lib/s3/formatS3Url.test.js +238 -261
  141. package/lib/s3/index.js +2 -2
  142. package/lib/s3/parseS3Url.js +61 -60
  143. package/lib/s3/parseS3Url.test.js +270 -269
  144. package/lib/schema/SchemaRegistry.js +41 -42
  145. package/lib/schema/SchemaRegistry.mod.js +1 -1
  146. package/lib/schema/TypeSchema.d.js +2 -2
  147. package/lib/schema/createSchemaData.js +113 -67
  148. package/lib/schema/findJsonSchemaByPath.js +28 -23
  149. package/lib/schema/formatZodError.js +112 -131
  150. package/lib/schema/formatZodError.test.js +192 -195
  151. package/lib/schema/getSchemaCache.js +7 -7
  152. package/lib/schema/getSchemaOptions.js +17 -16
  153. package/lib/schema/index.js +6 -6
  154. package/lib/schema/toJsonSchema.js +195 -189
  155. package/lib/schema/toJsonSchema.test.js +34 -26
  156. package/lib/schema/validate.js +105 -96
  157. package/lib/tools/generateSchema.js +40 -40
  158. package/lib/tools/renderJsonSchemaToMarkdownDoc.js +74 -74
  159. package/lib/utils/buildBaseUrl.js +8 -8
  160. package/lib/utils/buildRedactorFormSchema.js +54 -53
  161. package/lib/utils/getEstimateProcessTime.js +24 -19
  162. package/lib/utils/index.js +3 -3
  163. package/lib/utils/resolveFeatureOptions.js +9 -9
  164. package/package.json +14 -14
  165. package/src/ai/vision/index.ts +2 -2
  166. package/src/cn/index.ts +1 -2
  167. package/src/consola/index.ts +1 -1
  168. package/src/data/index.ts +3 -4
  169. package/src/data/resolvePagination.ts +2 -2
  170. package/src/dayjs/formatDuration.ts +8 -9
  171. package/src/dayjs/index.ts +1 -1
  172. package/src/dayjs/parseRelativeTime.ts +1 -1
  173. package/src/dayjs/resolveRelativeTime.ts +1 -1
  174. package/src/drain3/Drain.test.ts +2 -2
  175. package/src/drain3/index.ts +2 -4
  176. package/src/fs/createWebDavFileSystem.ts +2 -7
  177. package/src/fs/createWebFileSystem.ts +1 -1
  178. package/src/fs/index.ts +4 -4
  179. package/src/fs/minio/createMinioFileSystem.ts +2 -2
  180. package/src/fs/minio/index.ts +1 -1
  181. package/src/fs/s3/createS3MiniFileSystem.ts +1 -1
  182. package/src/fs/server/createDatabaseFileSystem.ts +84 -120
  183. package/src/fs/server/dbfs.test.ts +14 -10
  184. package/src/fs/server/index.ts +1 -0
  185. package/src/fs/server/loadTestDatabase.ts +8 -119
  186. package/src/jsonschema/index.ts +1 -1
  187. package/src/meta/index.ts +2 -3
  188. package/src/orm/createSqliteDialect.ts +17 -0
  189. package/src/orm/index.ts +1 -0
  190. package/src/orpc/createOpenApiContractClient.ts +1 -1
  191. package/src/orpc/index.ts +1 -1
  192. package/src/password/createArgon2PasswordAlgorithm.ts +1 -1
  193. package/src/password/index.ts +2 -2
  194. package/src/resource/index.ts +3 -3
  195. package/src/resource/schema/index.ts +4 -4
  196. package/src/s3/index.ts +1 -1
  197. package/src/schema/SchemaRegistry.ts +1 -1
  198. package/src/schema/createSchemaData.ts +1 -1
  199. package/src/schema/findJsonSchemaByPath.ts +1 -1
  200. package/src/schema/index.ts +5 -5
  201. package/src/schema/validate.ts +1 -1
  202. package/src/utils/buildRedactorFormSchema.ts +1 -1
  203. package/src/utils/formatNumber.ts +18 -0
  204. package/src/utils/formatPercent.ts +17 -0
  205. package/src/utils/index.ts +3 -3
  206. package/src/utils/resolveFeatureOptions.ts +1 -1
package/lib/fs/types.js CHANGED
@@ -1,27 +1,24 @@
1
- import { z } from "zod/v4";
1
+ import { z } from 'zod/v4';
2
2
  export const FileKind = Object.freeze({
3
- __proto__: null,
4
- directory: 'directory',
5
- file: 'file'
3
+ __proto__: null,
4
+ directory: 'directory',
5
+ file: 'file',
6
6
  });
7
- export const FileKindSchema = z.enum([
8
- 'directory',
9
- 'file'
10
- ]);
7
+ export const FileKindSchema = z.enum(['directory', 'file']);
11
8
  export const ReaddirOptionsSchema = z.object({
12
- recursive: z.boolean().optional(),
13
- depth: z.number().optional(),
14
- glob: z.string().optional(),
15
- kind: FileKindSchema.optional(),
16
- cursor: z.string().optional(),
17
- hidden: z.boolean().default(false)
9
+ recursive: z.boolean().optional(),
10
+ depth: z.number().optional(),
11
+ glob: z.string().optional(),
12
+ kind: FileKindSchema.optional(),
13
+ cursor: z.string().optional(),
14
+ hidden: z.boolean().default(false),
18
15
  });
19
16
  export const FileUrlOptionsSchema = z.object({
20
- size: z.coerce.number().optional(),
21
- width: z.number().optional(),
22
- height: z.number().optional(),
23
- quality: z.number().optional(),
24
- thumbnail: z.boolean().optional()
17
+ size: z.coerce.number().optional(),
18
+ width: z.number().optional(),
19
+ height: z.number().optional(),
20
+ quality: z.number().optional(),
21
+ thumbnail: z.boolean().optional(),
25
22
  });
26
23
 
27
- //# sourceMappingURL=types.js.map
24
+ //# sourceMappingURL=types.js.map
@@ -1,35 +1,29 @@
1
1
  export function getFileUrl(fs, needle, { ...options } = {}) {
2
- const [file, path] = resolveFilePath(needle);
3
- if (!path || !needle) return;
4
- let out;
5
- if (fs?.getUrl) {
6
- try {
7
- out = fs.getUrl(needle, options);
8
- } catch (e) {
9
- console.error(`failed to get file url`, needle, e);
10
- }
11
- }
12
- if (!out && file) {
13
- out = file.meta?.url || file.url;
14
- }
15
- return out;
2
+ const [file, path] = resolveFilePath(needle);
3
+ if (!path || !needle) return;
4
+ let out;
5
+ if (fs?.getUrl) {
6
+ try {
7
+ out = fs.getUrl(needle, options);
8
+ } catch (e) {
9
+ console.error(`failed to get file url`, needle, e);
10
+ }
11
+ }
12
+ if (!out && file) {
13
+ out = file.meta?.url || file.url;
14
+ }
15
+ return out;
16
16
  }
17
17
  export function resolveFilePath(needle) {
18
- if (!needle) {
19
- return [
20
- undefined,
21
- undefined
22
- ];
23
- }
24
- let file = typeof needle === 'string' ? undefined : needle;
25
- let path = typeof needle === 'string' ? needle : needle.path;
26
- if (file && !path) {
27
- path = file.path;
28
- }
29
- return [
30
- file,
31
- path
32
- ];
18
+ if (!needle) {
19
+ return [undefined, undefined];
20
+ }
21
+ let file = typeof needle === 'string' ? undefined : needle;
22
+ let path = typeof needle === 'string' ? needle : needle.path;
23
+ if (file && !path) {
24
+ path = file.path;
25
+ }
26
+ return [file, path];
33
27
  }
34
28
 
35
- //# sourceMappingURL=getFileUrl.js.map
29
+ //# sourceMappingURL=getFileUrl.js.map
package/lib/fs/utils.js CHANGED
@@ -1,22 +1,22 @@
1
- import { ArrayBuffers, classOf } from "@wener/utils";
1
+ import { ArrayBuffers, classOf } from '@wener/utils';
2
2
  export function resolveData(data) {
3
- let buf;
4
- if (typeof data === 'string') {
5
- buf = ArrayBuffers.toUint8Array(ArrayBuffers.from(data, 'utf8'));
6
- } else if (data instanceof ArrayBuffer) {
7
- buf = new Uint8Array(data);
8
- } else if (data instanceof Uint8Array) {
9
- buf = data;
10
- } else {
11
- throw new Error(`unable to resolve data: ${typeof data} ${classOf(data)}`);
12
- }
13
- return buf;
3
+ let buf;
4
+ if (typeof data === 'string') {
5
+ buf = ArrayBuffers.toUint8Array(ArrayBuffers.from(data, 'utf8'));
6
+ } else if (data instanceof ArrayBuffer) {
7
+ buf = new Uint8Array(data);
8
+ } else if (data instanceof Uint8Array) {
9
+ buf = data;
10
+ } else {
11
+ throw new Error(`unable to resolve data: ${typeof data} ${classOf(data)}`);
12
+ }
13
+ return buf;
14
14
  }
15
15
  export function getPath(f) {
16
- if (typeof f === 'string') {
17
- return f;
18
- }
19
- return f.path;
16
+ if (typeof f === 'string') {
17
+ return f;
18
+ }
19
+ return f.path;
20
20
  }
21
21
 
22
- //# sourceMappingURL=utils.js.map
22
+ //# sourceMappingURL=utils.js.map
@@ -1,2 +1,2 @@
1
- export { createWebDavFileSystem } from "../createWebDavFileSystem.js";
1
+ export { createWebDavFileSystem } from '../createWebDavFileSystem.js';
2
2
  //# sourceMappingURL=index.js.map
package/lib/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @deprecated
3
- */ export { parseSort } from "./data/parseSort.js";
3
+ */ export { parseSort } from './data/parseSort.js';
4
4
  /**
5
5
  * @deprecated
6
- */ export { resolvePagination as normalizePagination } from "./data/resolvePagination.js";
6
+ */ export { resolvePagination as normalizePagination } from './data/resolvePagination.js';
7
7
  //# sourceMappingURL=index.js.map
@@ -1,180 +1,241 @@
1
- import { isNil } from "@wener/utils";
2
- import Ajv from "ajv";
3
- import addFormats from "ajv-formats";
4
- import addKeywords from "ajv-keywords";
5
- import { match, P } from "ts-pattern";
1
+ import { isNil } from '@wener/utils';
2
+ import Ajv from 'ajv';
3
+ import addFormats from 'ajv-formats';
4
+ import addKeywords from 'ajv-keywords';
5
+ import { match, P } from 'ts-pattern';
6
+
6
7
  function _createValidator(opt) {
7
- const ajv = new Ajv(opt);
8
- addKeywords(ajv);
9
- addFormats(ajv);
10
- return ajv;
8
+ const ajv = new Ajv(opt);
9
+ addKeywords(ajv);
10
+ addFormats(ajv);
11
+ return ajv;
11
12
  }
12
13
  function validate({ schema, data, mutate, clone, validator }) {
13
- let opts = {
14
- // strict: 'log',
15
- strict: true,
16
- strictSchema: 'log'
17
- };
18
- if (mutate) {
19
- Object.assign(opts, {
20
- removeAdditional: true,
21
- useDefaults: true,
22
- coerceTypes: true,
23
- allErrors: true
24
- });
25
- }
26
- if (clone) {
27
- data = structuredClone(data);
28
- }
29
- if (!validator) {
30
- validator = JsonSchema.createValidator(opts);
31
- }
32
- const validate = validator.compile(schema);
33
- // consider reusing validate instance
34
- const valid = validate(data);
35
- const errors = validate.errors;
36
- // localize(errors);
37
- return {
38
- data,
39
- success: valid,
40
- message: validator.errorsText(errors),
41
- errors: errors
42
- };
14
+ let opts = {
15
+ // strict: 'log',
16
+ strict: true,
17
+ strictSchema: 'log',
18
+ };
19
+ if (mutate) {
20
+ Object.assign(opts, {
21
+ removeAdditional: true,
22
+ useDefaults: true,
23
+ coerceTypes: true,
24
+ allErrors: true,
25
+ });
26
+ }
27
+ if (clone) {
28
+ data = structuredClone(data);
29
+ }
30
+ if (!validator) {
31
+ validator = JsonSchema.createValidator(opts);
32
+ }
33
+ const validate = validator.compile(schema);
34
+ // consider reusing validate instance
35
+ const valid = validate(data);
36
+ const errors = validate.errors;
37
+ // localize(errors);
38
+ return {
39
+ data,
40
+ success: valid,
41
+ message: validator.errorsText(errors),
42
+ errors: errors,
43
+ };
43
44
  }
44
45
  let schemas = [];
45
46
  let createValidator = _createValidator;
46
47
  function addSchema(schema, { onConflict = 'throw' } = {}) {
47
- if (!schema.$id) throw new Error('Schema must have $id');
48
- switch(onConflict){
49
- case 'ignore':
50
- onConflict = (old)=>old;
51
- break;
52
- case 'replace':
53
- onConflict = (_, neo)=>neo;
54
- break;
55
- case 'throw':
56
- onConflict = (_old, neo)=>{
57
- throw new Error(`Schema ${neo.$id} already exists`);
58
- };
59
- break;
60
- }
61
- let idx = schemas.findIndex((s)=>s.$id === schema.$id);
62
- if (idx >= 0) {
63
- schemas[idx] = onConflict(schemas[idx], schema);
64
- } else {
65
- schemas.push(schema);
66
- }
48
+ if (!schema.$id) throw new Error('Schema must have $id');
49
+ switch (onConflict) {
50
+ case 'ignore':
51
+ onConflict = (old) => old;
52
+ break;
53
+ case 'replace':
54
+ onConflict = (_, neo) => neo;
55
+ break;
56
+ case 'throw':
57
+ onConflict = (_old, neo) => {
58
+ throw new Error(`Schema ${neo.$id} already exists`);
59
+ };
60
+ break;
61
+ }
62
+ let idx = schemas.findIndex((s) => s.$id === schema.$id);
63
+ if (idx >= 0) {
64
+ schemas[idx] = onConflict(schemas[idx], schema);
65
+ } else {
66
+ schemas.push(schema);
67
+ }
67
68
  }
68
69
  /**
69
70
  * Check data is valid, will not use default
70
71
  */ function check(schema, data) {
71
- return validate({
72
- schema,
73
- data,
74
- mutate: false,
75
- clone: true
76
- });
72
+ return validate({
73
+ schema,
74
+ data,
75
+ mutate: false,
76
+ clone: true,
77
+ });
77
78
  }
78
79
  /**
79
80
  * Parse data with default value and coerceTypes
80
81
  */ function safeParse(schema, data) {
81
- return validate({
82
- schema,
83
- data,
84
- mutate: true,
85
- clone: true
86
- });
82
+ return validate({
83
+ schema,
84
+ data,
85
+ mutate: true,
86
+ clone: true,
87
+ });
87
88
  }
88
89
  function parse(schema, data) {
89
- const { data: out, message, errors } = validate({
90
- schema,
91
- data,
92
- mutate: true,
93
- clone: true
94
- });
95
- if (errors) {
96
- throw new Error(message);
97
- }
98
- return out;
90
+ const {
91
+ data: out,
92
+ message,
93
+ errors,
94
+ } = validate({
95
+ schema,
96
+ data,
97
+ mutate: true,
98
+ clone: true,
99
+ });
100
+ if (errors) {
101
+ throw new Error(message);
102
+ }
103
+ return out;
99
104
  }
100
105
  function create(schema, data) {
101
- // will not ensure value match the rule
102
- return match(schema).returnType().with({
103
- const: P.nonNullable
104
- }, (v)=>v).with({
105
- default: P.select()
106
- }, (v)=>v).with({
107
- anyOf: P.nonNullable
108
- }, (schema)=>{
109
- return create(schema.anyOf[0]);
110
- }).with({
111
- oneOf: P.nonNullable
112
- }, (schema)=>{
113
- return create(schema.oneOf[0]);
114
- }).with({
115
- type: 'string'
116
- }, (_schema)=>'').with({
117
- type: P.union('number', 'integer')
118
- }, (_schema)=>0).with({
119
- type: 'object'
120
- }, (schema)=>{
121
- let out = validate({
122
- schema,
123
- data: data ?? {},
124
- mutate: true
125
- });
126
- if (!out.success) {
127
- // fallback
128
- let obj = data || {};
129
- schema.required?.forEach((key)=>{
130
- if (!(key in obj)) {
131
- let last = obj[key];
132
- let prop = schema.properties?.[key];
133
- if (prop && isNil(last)) obj[key] = JsonSchema.create(prop, last);
134
- }
135
- });
136
- out = validate({
137
- schema,
138
- data: obj,
139
- mutate: true
140
- });
141
- if (!out.success) {
142
- console.warn(`Failed to create object with schema: ${out.message}`);
143
- }
144
- }
145
- return out.data;
146
- }).with({
147
- type: 'null'
148
- }, ()=>null).with({
149
- type: 'boolean'
150
- }, (_schema)=>false).with({
151
- type: 'array'
152
- }, (_schema)=>[]).otherwise(()=>{
153
- return undefined;
154
- });
106
+ // will not ensure value match the rule
107
+ return match(schema)
108
+ .returnType()
109
+ .with(
110
+ {
111
+ const: P.nonNullable,
112
+ },
113
+ (v) => v,
114
+ )
115
+ .with(
116
+ {
117
+ default: P.select(),
118
+ },
119
+ (v) => v,
120
+ )
121
+ .with(
122
+ {
123
+ anyOf: P.nonNullable,
124
+ },
125
+ (schema) => {
126
+ return create(schema.anyOf[0]);
127
+ },
128
+ )
129
+ .with(
130
+ {
131
+ oneOf: P.nonNullable,
132
+ },
133
+ (schema) => {
134
+ return create(schema.oneOf[0]);
135
+ },
136
+ )
137
+ .with(
138
+ {
139
+ type: 'string',
140
+ },
141
+ (_schema) => '',
142
+ )
143
+ .with(
144
+ {
145
+ type: P.union('number', 'integer'),
146
+ },
147
+ (_schema) => 0,
148
+ )
149
+ .with(
150
+ {
151
+ type: 'object',
152
+ },
153
+ (schema) => {
154
+ let out = validate({
155
+ schema,
156
+ data: data ?? {},
157
+ mutate: true,
158
+ });
159
+ if (!out.success) {
160
+ // fallback
161
+ let obj = data || {};
162
+ schema.required?.forEach((key) => {
163
+ if (!(key in obj)) {
164
+ let last = obj[key];
165
+ let prop = schema.properties?.[key];
166
+ if (prop && isNil(last)) obj[key] = JsonSchema.create(prop, last);
167
+ }
168
+ });
169
+ out = validate({
170
+ schema,
171
+ data: obj,
172
+ mutate: true,
173
+ });
174
+ if (!out.success) {
175
+ console.warn(`Failed to create object with schema: ${out.message}`);
176
+ }
177
+ }
178
+ return out.data;
179
+ },
180
+ )
181
+ .with(
182
+ {
183
+ type: 'null',
184
+ },
185
+ () => null,
186
+ )
187
+ .with(
188
+ {
189
+ type: 'boolean',
190
+ },
191
+ (_schema) => false,
192
+ )
193
+ .with(
194
+ {
195
+ type: 'array',
196
+ },
197
+ (_schema) => [],
198
+ )
199
+ .otherwise(() => {
200
+ return undefined;
201
+ });
155
202
  }
156
203
  function isPrimitiveType(schema) {
157
- return match(schema).returnType().with({
158
- type: P.union('number', 'integer', 'string', 'boolean')
159
- }, ()=>true).with({
160
- anyOf: P.nonNullable
161
- }, (schema)=>{
162
- return isPrimitiveType(schema.anyOf[0]);
163
- }).with({
164
- oneOf: P.nonNullable
165
- }, (schema)=>{
166
- return isPrimitiveType(schema.oneOf[0]);
167
- }).otherwise(()=>false);
204
+ return match(schema)
205
+ .returnType()
206
+ .with(
207
+ {
208
+ type: P.union('number', 'integer', 'string', 'boolean'),
209
+ },
210
+ () => true,
211
+ )
212
+ .with(
213
+ {
214
+ anyOf: P.nonNullable,
215
+ },
216
+ (schema) => {
217
+ return isPrimitiveType(schema.anyOf[0]);
218
+ },
219
+ )
220
+ .with(
221
+ {
222
+ oneOf: P.nonNullable,
223
+ },
224
+ (schema) => {
225
+ return isPrimitiveType(schema.oneOf[0]);
226
+ },
227
+ )
228
+ .otherwise(() => false);
168
229
  }
169
230
  export const JsonSchema = {
170
- createValidator,
171
- addSchema,
172
- schemas,
173
- check,
174
- safeParse,
175
- parse,
176
- create,
177
- isPrimitiveType
231
+ createValidator,
232
+ addSchema,
233
+ schemas,
234
+ check,
235
+ safeParse,
236
+ parse,
237
+ create,
238
+ isPrimitiveType,
178
239
  };
179
240
 
180
- //# sourceMappingURL=JsonSchema.js.map
241
+ //# sourceMappingURL=JsonSchema.js.map