@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.
- package/lib/ai/qwen3vl/index.js +1 -1
- package/lib/ai/qwen3vl/utils.js +15 -15
- 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.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/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.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/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/dayjs/dayjs.js +20 -20
- package/lib/dayjs/formatDuration.js +56 -56
- 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 +73 -72
- 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 +303 -338
- package/lib/drain3/LogCluster.js +25 -25
- package/lib/drain3/Node.js +24 -24
- package/lib/drain3/TemplateMiner.js +197 -196
- package/lib/drain3/index.js +5 -5
- package/lib/drain3/persistence/FilePersistence.js +19 -19
- package/lib/drain3/persistence/MemoryPersistence.js +8 -8
- package/lib/drain3/persistence/PersistenceHandler.js +2 -2
- package/lib/drain3/types.js +2 -2
- 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/MemoryFileSystem.test.js +172 -181
- package/lib/fs/createBrowserFileSystem.js +222 -235
- package/lib/fs/createMemoryFileSystem.js +472 -510
- package/lib/fs/createSandboxFileSystem.js +102 -101
- package/lib/fs/createWebDavFileSystem.js +162 -149
- package/lib/fs/createWebFileSystem.js +197 -220
- package/lib/fs/findMimeType.js +14 -14
- package/lib/fs/index.js +7 -7
- package/lib/fs/minio/createMinioFileSystem.js +959 -956
- package/lib/fs/minio/index.js +1 -1
- package/lib/fs/orpc/FileSystemContract.js +57 -57
- package/lib/fs/orpc/createContractClientFileSystem.js +88 -88
- package/lib/fs/orpc/index.js +2 -2
- package/lib/fs/orpc/server/createFileSystemContractImpl.js +62 -60
- package/lib/fs/orpc/server/index.js +1 -1
- package/lib/fs/s3/createS3MiniFileSystem.js +756 -737
- 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/createNodeFileSystem.js +407 -405
- 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 -316
- 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/webdav/index.js +1 -1
- package/lib/index.js +2 -2
- package/lib/jsonschema/JsonSchema.js +216 -155
- package/lib/jsonschema/JsonSchema.test.js +123 -124
- package/lib/jsonschema/forEachJsonSchema.js +41 -41
- package/lib/jsonschema/index.js +2 -2
- package/lib/jsonschema/types.d.js +2 -2
- 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.test.js +13 -12
- package/lib/meta/index.js +3 -3
- package/lib/orpc/createOpenApiContractClient.js +26 -24
- 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.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/createBase64PasswordAlgorithm.js +11 -11
- package/lib/password/createBcryptPasswordAlgorithm.js +20 -18
- package/lib/password/createPBKDF2PasswordAlgorithm.js +65 -52
- package/lib/password/createScryptPasswordAlgorithm.js +74 -63
- 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/getTitleOfResource.js +5 -5
- package/lib/resource/index.js +2 -2
- 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.test.js +238 -261
- package/lib/s3/index.js +2 -2
- package/lib/s3/parseS3Url.js +61 -60
- package/lib/s3/parseS3Url.test.js +270 -269
- package/lib/schema/SchemaRegistry.js +41 -42
- package/lib/schema/SchemaRegistry.mod.js +1 -1
- package/lib/schema/TypeSchema.d.js +2 -2
- package/lib/schema/createSchemaData.js +113 -67
- package/lib/schema/findJsonSchemaByPath.js +28 -23
- package/lib/schema/formatZodError.js +112 -131
- 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 +195 -189
- package/lib/schema/toJsonSchema.test.js +34 -26
- package/lib/schema/validate.js +105 -96
- 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 +54 -53
- package/lib/utils/getEstimateProcessTime.js +24 -19
- package/lib/utils/index.js +3 -3
- package/lib/utils/resolveFeatureOptions.js +9 -9
- package/package.json +14 -14
- package/src/ai/vision/index.ts +2 -2
- package/src/cn/index.ts +1 -2
- package/src/consola/index.ts +1 -1
- package/src/data/index.ts +3 -4
- package/src/data/resolvePagination.ts +2 -2
- package/src/dayjs/formatDuration.ts +8 -9
- package/src/dayjs/index.ts +1 -1
- package/src/dayjs/parseRelativeTime.ts +1 -1
- package/src/dayjs/resolveRelativeTime.ts +1 -1
- package/src/drain3/Drain.test.ts +2 -2
- package/src/drain3/index.ts +2 -4
- package/src/fs/createWebDavFileSystem.ts +2 -7
- package/src/fs/createWebFileSystem.ts +1 -1
- package/src/fs/index.ts +4 -4
- package/src/fs/minio/createMinioFileSystem.ts +2 -2
- package/src/fs/minio/index.ts +1 -1
- package/src/fs/s3/createS3MiniFileSystem.ts +1 -1
- package/src/fs/server/createDatabaseFileSystem.ts +84 -120
- package/src/fs/server/dbfs.test.ts +14 -10
- package/src/fs/server/index.ts +1 -0
- package/src/fs/server/loadTestDatabase.ts +8 -119
- package/src/jsonschema/index.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 +1 -1
- package/src/orpc/index.ts +1 -1
- package/src/password/createArgon2PasswordAlgorithm.ts +1 -1
- package/src/password/index.ts +2 -2
- package/src/resource/index.ts +3 -3
- package/src/resource/schema/index.ts +4 -4
- package/src/s3/index.ts +1 -1
- package/src/schema/SchemaRegistry.ts +1 -1
- package/src/schema/createSchemaData.ts +1 -1
- package/src/schema/findJsonSchemaByPath.ts +1 -1
- package/src/schema/index.ts +5 -5
- package/src/schema/validate.ts +1 -1
- package/src/utils/buildRedactorFormSchema.ts +1 -1
- 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/fs/types.js
CHANGED
|
@@ -1,27 +1,24 @@
|
|
|
1
|
-
import { z } from
|
|
1
|
+
import { z } from 'zod/v4';
|
|
2
2
|
export const FileKind = Object.freeze({
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
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
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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
|
|
1
|
+
import { ArrayBuffers, classOf } from '@wener/utils';
|
|
2
2
|
export function resolveData(data) {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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
|
package/lib/fs/webdav/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { createWebDavFileSystem } from
|
|
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
|
|
3
|
+
*/ export { parseSort } from './data/parseSort.js';
|
|
4
4
|
/**
|
|
5
5
|
* @deprecated
|
|
6
|
-
*/ export { resolvePagination as normalizePagination } from
|
|
6
|
+
*/ export { resolvePagination as normalizePagination } from './data/resolvePagination.js';
|
|
7
7
|
//# sourceMappingURL=index.js.map
|
|
@@ -1,180 +1,241 @@
|
|
|
1
|
-
import { isNil } from
|
|
2
|
-
import Ajv from
|
|
3
|
-
import addFormats from
|
|
4
|
-
import addKeywords from
|
|
5
|
-
import { match, P } from
|
|
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
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
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
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
82
|
+
return validate({
|
|
83
|
+
schema,
|
|
84
|
+
data,
|
|
85
|
+
mutate: true,
|
|
86
|
+
clone: true,
|
|
87
|
+
});
|
|
87
88
|
}
|
|
88
89
|
function parse(schema, data) {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
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
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
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
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
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
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
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
|