@nice2dev/ui-graphics 1.3.15 → 1.3.16
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/CHANGELOG.md +9 -0
- package/dist/cjs/animation/AnimatedPerson.js +470 -143
- package/dist/cjs/animation/AnimationEditor.js +289 -101
- package/dist/cjs/animation/Audience.js +83 -61
- package/dist/cjs/animation/BodyRenderer.js +69 -28
- package/dist/cjs/animation/animationHelper.js +281 -288
- package/dist/cjs/animation/characterTypes.js +200 -203
- package/dist/cjs/animation/choreoDSL.js +205 -219
- package/dist/cjs/animation/choreography.js +4 -32
- package/dist/cjs/animation/karaokeIntegration.js +68 -77
- package/dist/cjs/animation/lottieExport.js +108 -114
- package/dist/cjs/animation/motionPath.js +225 -248
- package/dist/cjs/animation/physics.js +227 -254
- package/dist/cjs/animation/rig/RigPlayer.js +53 -56
- package/dist/cjs/animation/rig/RiggedBody.js +71 -26
- package/dist/cjs/animation/rig/choreoRigBridge.js +72 -77
- package/dist/cjs/animation/rig/ik.js +13 -18
- package/dist/cjs/animation/rig/poseOps.js +34 -41
- package/dist/cjs/animation/rig/presets.js +64 -72
- package/dist/cjs/animation/rig/rigMath.js +25 -32
- package/dist/cjs/animation/rig/rigTypes.js +59 -63
- package/dist/cjs/animation/shapes/arms.js +31 -13
- package/dist/cjs/animation/shapes/eyes.js +129 -47
- package/dist/cjs/animation/shapes/hair.js +72 -45
- package/dist/cjs/animation/shapes/heads.js +131 -57
- package/dist/cjs/animation/shapes/headwear.js +47 -38
- package/dist/cjs/animation/shapes/legs.js +31 -13
- package/dist/cjs/animation/shapes/mouths.js +120 -44
- package/dist/cjs/animation/shapes/noses.js +80 -25
- package/dist/cjs/animation/shapes/outfits.js +101 -39
- package/dist/cjs/animation/shapes/torsos.js +15 -11
- package/dist/cjs/animation/spineExport.js +103 -121
- package/dist/cjs/core/LocalUI.js +108 -43
- package/dist/cjs/core/NiceBrushEngine.js +758 -552
- package/dist/cjs/core/collaboration.js +249 -251
- package/dist/cjs/core/colorBlindness.js +100 -115
- package/dist/cjs/core/dragDrop.js +155 -158
- package/dist/cjs/core/editorAsk.js +51 -41
- package/dist/cjs/core/fixes.js +140 -171
- package/dist/cjs/core/gridGuides.js +166 -201
- package/dist/cjs/core/historyVisual.js +156 -135
- package/dist/cjs/core/i18n.js +13 -14
- package/dist/cjs/core/integrations.js +238 -249
- package/dist/cjs/core/minimap.js +149 -151
- package/dist/cjs/core/plugins.js +98 -113
- package/dist/cjs/core/rtl.js +71 -83
- package/dist/cjs/core/shortcuts.js +196 -225
- package/dist/cjs/core/theme.js +142 -149
- package/dist/cjs/core/yjsCollaboration.js +232 -293
- package/dist/cjs/design/design-tools.js +12 -22
- package/dist/cjs/font/NiceFontEditor.js +877 -316
- package/dist/cjs/font/fontFormatUtils.js +388 -448
- package/dist/cjs/game/GameAsset2dEditor.js +1201 -392
- package/dist/cjs/game/GameAsset2dEditor.module.css.js +146 -5
- package/dist/cjs/game/gameAssetTypes.js +7 -16
- package/dist/cjs/game/gameAssetUtils.js +390 -413
- package/dist/cjs/game/useGameAssetEditor.js +820 -763
- package/dist/cjs/index.js +81 -84
- package/dist/cjs/nice2dev-ui-graphics.css +1 -1
- package/dist/cjs/node_modules/lib0/array.js +8 -70
- package/dist/cjs/node_modules/lib0/binary.js +3 -20
- package/dist/cjs/node_modules/lib0/broadcastchannel.js +24 -86
- package/dist/cjs/node_modules/lib0/buffer.js +15 -82
- package/dist/cjs/node_modules/lib0/conditions.js +3 -16
- package/dist/cjs/node_modules/lib0/decoding.js +108 -287
- package/dist/cjs/node_modules/lib0/dom.js +14 -44
- package/dist/cjs/node_modules/lib0/encoding.js +68 -418
- package/dist/cjs/node_modules/lib0/environment.js +26 -92
- package/dist/cjs/node_modules/lib0/error.js +5 -29
- package/dist/cjs/node_modules/lib0/function.js +27 -66
- package/dist/cjs/node_modules/lib0/iterator.js +8 -33
- package/dist/cjs/node_modules/lib0/logging.common.js +12 -22
- package/dist/cjs/node_modules/lib0/logging.js +32 -71
- package/dist/cjs/node_modules/lib0/map.js +16 -86
- package/dist/cjs/node_modules/lib0/math.js +3 -32
- package/dist/cjs/node_modules/lib0/number.js +4 -15
- package/dist/cjs/node_modules/lib0/object.js +14 -88
- package/dist/cjs/node_modules/lib0/observable.js +38 -78
- package/dist/cjs/node_modules/lib0/pair.js +3 -20
- package/dist/cjs/node_modules/lib0/prng.js +8 -77
- package/dist/cjs/node_modules/lib0/promise.js +6 -30
- package/dist/cjs/node_modules/lib0/random.js +8 -23
- package/dist/cjs/node_modules/lib0/schema.js +313 -687
- package/dist/cjs/node_modules/lib0/set.js +3 -10
- package/dist/cjs/node_modules/lib0/storage.js +20 -55
- package/dist/cjs/node_modules/lib0/string.js +17 -72
- package/dist/cjs/node_modules/lib0/symbol.js +2 -12
- package/dist/cjs/node_modules/lib0/time.js +2 -15
- package/dist/cjs/node_modules/lib0/trait/equality.js +3 -28
- package/dist/cjs/node_modules/lib0/url.js +4 -18
- package/dist/cjs/node_modules/lib0/webcrypto.js +2 -5
- package/dist/cjs/node_modules/y-protocols/auth.js +5 -19
- package/dist/cjs/node_modules/y-protocols/awareness.js +53 -116
- package/dist/cjs/node_modules/y-protocols/sync.js +16 -103
- package/dist/cjs/node_modules/y-websocket/src/y-websocket.js +77 -219
- package/dist/cjs/node_modules/yjs/dist/yjs.js +2128 -4868
- package/dist/cjs/photo/FilterThumb.js +37 -25
- package/dist/cjs/photo/NiceAITools.js +679 -467
- package/dist/cjs/photo/PhotoEditor.js +34 -20
- package/dist/cjs/photo/PhotoEditor.module.css.js +296 -5
- package/dist/cjs/photo/PhotoEditorCanvas.js +250 -91
- package/dist/cjs/photo/PhotoEditorLeftPanel.js +1086 -224
- package/dist/cjs/photo/PhotoEditorToolbar.js +135 -19
- package/dist/cjs/photo/photoEditorActions.js +89 -72
- package/dist/cjs/photo/photoEditorTypes.js +29 -32
- package/dist/cjs/photo/photoFilters.js +959 -985
- package/dist/cjs/photo/photoOverlays.js +899 -945
- package/dist/cjs/photo/photoSelection.js +427 -499
- package/dist/cjs/photo/usePhotoEditor.js +739 -741
- package/dist/cjs/pixel/HSVPicker.js +191 -175
- package/dist/cjs/pixel/PixelEditor.js +159 -48
- package/dist/cjs/pixel/PixelEditor.module.css.js +209 -5
- package/dist/cjs/pixel/PixelEditorCanvas.js +29 -16
- package/dist/cjs/pixel/PixelEditorMenuBar.js +160 -162
- package/dist/cjs/pixel/PixelEditorRightPanel.js +377 -45
- package/dist/cjs/pixel/PixelEditorStatusBar.js +47 -20
- package/dist/cjs/pixel/PixelEditorTimeline.js +106 -23
- package/dist/cjs/pixel/PixelEditorToolbar.js +99 -21
- package/dist/cjs/pixel/SpriteGeneratorPanel.js +483 -303
- package/dist/cjs/pixel/animalGeneratorPresets.js +776 -601
- package/dist/cjs/pixel/asepriteFormat.js +511 -560
- package/dist/cjs/pixel/buildingGeneratorPresets.js +693 -520
- package/dist/cjs/pixel/characterGeneratorPresets.js +644 -544
- package/dist/cjs/pixel/pixel-pro.js +52 -63
- package/dist/cjs/pixel/pixelEditorExports.js +388 -428
- package/dist/cjs/pixel/pixelEditorTypes.js +91 -108
- package/dist/cjs/pixel/pixelEditorUtils.js +519 -561
- package/dist/cjs/pixel/spriteGeneratorCore.js +206 -257
- package/dist/cjs/pixel/usePixelEditor.js +1321 -1343
- package/dist/cjs/texture/Nice3DTexturePainter.js +707 -440
- package/dist/cjs/ui/NiceUIDesigner.js +954 -527
- package/dist/cjs/vector/NiceSvgToCode.js +586 -446
- package/dist/cjs/vector/VectorEditor.js +290 -117
- package/dist/cjs/vector/VectorEditor.module.css.js +86 -5
- package/dist/cjs/vector/VectorEditorMenuBar.js +333 -275
- package/dist/cjs/vector/VectorEditorRightPanel.js +1094 -371
- package/dist/cjs/vector/VectorEditorShapeRenderer.js +198 -60
- package/dist/cjs/vector/VectorEditorStatusBar.js +39 -13
- package/dist/cjs/vector/useVectorEditor.js +848 -889
- package/dist/cjs/vector/vectorAnimation.js +110 -137
- package/dist/cjs/vector/vectorBooleanOps.js +398 -485
- package/dist/cjs/vector/vectorEditorExport.js +201 -203
- package/dist/cjs/vector/vectorEditorImport.js +173 -189
- package/dist/cjs/vector/vectorEditorTypes.js +42 -49
- package/dist/cjs/vector/vectorGradients.js +151 -174
- package/dist/cjs/vector/vectorIconExport.js +52 -59
- package/dist/cjs/vector/vectorIconImport.js +169 -219
- package/dist/esm/animation/AnimatedPerson.js +470 -141
- package/dist/esm/animation/AnimationEditor.js +290 -100
- package/dist/esm/animation/Audience.js +83 -59
- package/dist/esm/animation/BodyRenderer.js +71 -25
- package/dist/esm/animation/animationHelper.js +284 -287
- package/dist/esm/animation/characterTypes.js +204 -202
- package/dist/esm/animation/choreoDSL.js +212 -218
- package/dist/esm/animation/choreography.js +8 -31
- package/dist/esm/animation/karaokeIntegration.js +74 -76
- package/dist/esm/animation/lottieExport.js +112 -113
- package/dist/esm/animation/motionPath.js +235 -247
- package/dist/esm/animation/physics.js +235 -253
- package/dist/esm/animation/rig/RigPlayer.js +53 -54
- package/dist/esm/animation/rig/RiggedBody.js +71 -24
- package/dist/esm/animation/rig/choreoRigBridge.js +74 -76
- package/dist/esm/animation/rig/ik.js +14 -17
- package/dist/esm/animation/rig/poseOps.js +37 -40
- package/dist/esm/animation/rig/presets.js +70 -71
- package/dist/esm/animation/rig/rigMath.js +28 -31
- package/dist/esm/animation/rig/rigTypes.js +62 -62
- package/dist/esm/animation/shapes/arms.js +32 -12
- package/dist/esm/animation/shapes/eyes.js +135 -46
- package/dist/esm/animation/shapes/hair.js +80 -44
- package/dist/esm/animation/shapes/heads.js +140 -56
- package/dist/esm/animation/shapes/headwear.js +54 -37
- package/dist/esm/animation/shapes/legs.js +32 -12
- package/dist/esm/animation/shapes/mouths.js +127 -43
- package/dist/esm/animation/shapes/noses.js +87 -24
- package/dist/esm/animation/shapes/outfits.js +106 -38
- package/dist/esm/animation/shapes/torsos.js +16 -10
- package/dist/esm/animation/spineExport.js +106 -120
- package/dist/esm/core/LocalUI.js +111 -42
- package/dist/esm/core/NiceBrushEngine.js +761 -551
- package/dist/esm/core/collaboration.js +255 -250
- package/dist/esm/core/colorBlindness.js +106 -114
- package/dist/esm/core/dragDrop.js +157 -157
- package/dist/esm/core/editorAsk.js +53 -40
- package/dist/esm/core/fixes.js +145 -170
- package/dist/esm/core/gridGuides.js +178 -200
- package/dist/esm/core/historyVisual.js +158 -134
- package/dist/esm/core/i18n.js +15 -13
- package/dist/esm/core/integrations.js +244 -248
- package/dist/esm/core/minimap.js +151 -150
- package/dist/esm/core/plugins.js +102 -112
- package/dist/esm/core/rtl.js +77 -82
- package/dist/esm/core/shortcuts.js +200 -224
- package/dist/esm/core/theme.js +150 -148
- package/dist/esm/core/yjsCollaboration.js +240 -292
- package/dist/esm/design/design-tools.js +13 -21
- package/dist/esm/font/NiceFontEditor.js +880 -315
- package/dist/esm/font/fontFormatUtils.js +394 -447
- package/dist/esm/game/GameAsset2dEditor.js +1202 -391
- package/dist/esm/game/GameAsset2dEditor.module.css.js +146 -3
- package/dist/esm/game/gameAssetTypes.js +8 -15
- package/dist/esm/game/gameAssetUtils.js +408 -412
- package/dist/esm/game/useGameAssetEditor.js +821 -762
- package/dist/esm/index.js +332 -69
- package/dist/esm/nice2dev-ui-graphics.css +1 -1
- package/dist/esm/node_modules/lib0/array.js +15 -69
- package/dist/esm/node_modules/lib0/binary.js +14 -19
- package/dist/esm/node_modules/lib0/broadcastchannel.js +27 -85
- package/dist/esm/node_modules/lib0/buffer.js +20 -80
- package/dist/esm/node_modules/lib0/conditions.js +4 -15
- package/dist/esm/node_modules/lib0/decoding.js +128 -286
- package/dist/esm/node_modules/lib0/dom.js +20 -43
- package/dist/esm/node_modules/lib0/encoding.js +90 -415
- package/dist/esm/node_modules/lib0/environment.js +33 -91
- package/dist/esm/node_modules/lib0/error.js +8 -28
- package/dist/esm/node_modules/lib0/function.js +31 -65
- package/dist/esm/node_modules/lib0/iterator.js +11 -32
- package/dist/esm/node_modules/lib0/logging.common.js +22 -21
- package/dist/esm/node_modules/lib0/logging.js +44 -70
- package/dist/esm/node_modules/lib0/map.js +21 -85
- package/dist/esm/node_modules/lib0/math.js +9 -31
- package/dist/esm/node_modules/lib0/number.js +7 -14
- package/dist/esm/node_modules/lib0/object.js +26 -87
- package/dist/esm/node_modules/lib0/observable.js +40 -77
- package/dist/esm/node_modules/lib0/pair.js +5 -19
- package/dist/esm/node_modules/lib0/prng.js +15 -76
- package/dist/esm/node_modules/lib0/promise.js +7 -29
- package/dist/esm/node_modules/lib0/random.js +10 -22
- package/dist/esm/node_modules/lib0/schema.js +367 -686
- package/dist/esm/node_modules/lib0/set.js +4 -9
- package/dist/esm/node_modules/lib0/storage.js +23 -54
- package/dist/esm/node_modules/lib0/string.js +26 -71
- package/dist/esm/node_modules/lib0/symbol.js +3 -11
- package/dist/esm/node_modules/lib0/time.js +3 -14
- package/dist/esm/node_modules/lib0/trait/equality.js +5 -27
- package/dist/esm/node_modules/lib0/url.js +5 -17
- package/dist/esm/node_modules/lib0/webcrypto.js +3 -4
- package/dist/esm/node_modules/y-protocols/auth.js +7 -18
- package/dist/esm/node_modules/y-protocols/awareness.js +58 -115
- package/dist/esm/node_modules/y-protocols/sync.js +26 -102
- package/dist/esm/node_modules/y-websocket/src/y-websocket.js +82 -218
- package/dist/esm/node_modules/yjs/dist/yjs.js +2263 -4899
- package/dist/esm/photo/FilterThumb.js +37 -23
- package/dist/esm/photo/NiceAITools.js +685 -466
- package/dist/esm/photo/PhotoEditor.js +34 -18
- package/dist/esm/photo/PhotoEditor.module.css.js +296 -3
- package/dist/esm/photo/PhotoEditorCanvas.js +250 -89
- package/dist/esm/photo/PhotoEditorLeftPanel.js +1086 -222
- package/dist/esm/photo/PhotoEditorToolbar.js +135 -17
- package/dist/esm/photo/photoEditorActions.js +93 -71
- package/dist/esm/photo/photoEditorTypes.js +32 -31
- package/dist/esm/photo/photoFilters.js +967 -984
- package/dist/esm/photo/photoOverlays.js +913 -944
- package/dist/esm/photo/photoSelection.js +438 -498
- package/dist/esm/photo/usePhotoEditor.js +740 -740
- package/dist/esm/pixel/HSVPicker.js +192 -174
- package/dist/esm/pixel/PixelEditor.js +160 -47
- package/dist/esm/pixel/PixelEditor.module.css.js +209 -3
- package/dist/esm/pixel/PixelEditorCanvas.js +29 -14
- package/dist/esm/pixel/PixelEditorMenuBar.js +160 -160
- package/dist/esm/pixel/PixelEditorRightPanel.js +377 -43
- package/dist/esm/pixel/PixelEditorStatusBar.js +47 -18
- package/dist/esm/pixel/PixelEditorTimeline.js +106 -21
- package/dist/esm/pixel/PixelEditorToolbar.js +99 -19
- package/dist/esm/pixel/SpriteGeneratorPanel.js +483 -301
- package/dist/esm/pixel/animalGeneratorPresets.js +782 -600
- package/dist/esm/pixel/asepriteFormat.js +517 -559
- package/dist/esm/pixel/buildingGeneratorPresets.js +699 -519
- package/dist/esm/pixel/characterGeneratorPresets.js +650 -543
- package/dist/esm/pixel/pixel-pro.js +53 -62
- package/dist/esm/pixel/pixelEditorExports.js +397 -427
- package/dist/esm/pixel/pixelEditorTypes.js +103 -107
- package/dist/esm/pixel/pixelEditorUtils.js +551 -560
- package/dist/esm/pixel/spriteGeneratorCore.js +223 -256
- package/dist/esm/pixel/usePixelEditor.js +1322 -1342
- package/dist/esm/texture/Nice3DTexturePainter.js +709 -439
- package/dist/esm/ui/NiceUIDesigner.js +957 -526
- package/dist/esm/vector/NiceSvgToCode.js +589 -445
- package/dist/esm/vector/VectorEditor.js +291 -116
- package/dist/esm/vector/VectorEditor.module.css.js +86 -3
- package/dist/esm/vector/VectorEditorMenuBar.js +333 -273
- package/dist/esm/vector/VectorEditorRightPanel.js +1094 -369
- package/dist/esm/vector/VectorEditorShapeRenderer.js +199 -59
- package/dist/esm/vector/VectorEditorStatusBar.js +39 -11
- package/dist/esm/vector/useVectorEditor.js +849 -888
- package/dist/esm/vector/vectorAnimation.js +115 -136
- package/dist/esm/vector/vectorBooleanOps.js +401 -484
- package/dist/esm/vector/vectorEditorExport.js +207 -202
- package/dist/esm/vector/vectorEditorImport.js +174 -188
- package/dist/esm/vector/vectorEditorTypes.js +50 -48
- package/dist/esm/vector/vectorGradients.js +158 -173
- package/dist/esm/vector/vectorIconExport.js +53 -58
- package/dist/esm/vector/vectorIconImport.js +171 -218
- package/package.json +9 -15
|
@@ -1,167 +1,94 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var
|
|
4
|
-
var
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
* Simple & efficient schemas for your data.
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* @typedef {string|number|bigint|boolean|null|undefined|symbol} Primitive
|
|
22
|
-
*/
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* @typedef {{ [k:string|number|symbol]: any }} AnyObject
|
|
26
|
-
*/
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* @template T
|
|
30
|
-
* @typedef {T extends Schema<infer X> ? X : T} Unwrap
|
|
31
|
-
*/
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* @template T
|
|
35
|
-
* @typedef {T extends Schema<infer X> ? X : T} TypeOf
|
|
36
|
-
*/
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* @template {readonly unknown[]} T
|
|
40
|
-
* @typedef {T extends readonly [Schema<infer First>, ...infer Rest] ? [First, ...UnwrapArray<Rest>] : [] } UnwrapArray
|
|
41
|
-
*/
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* @template T
|
|
45
|
-
* @typedef {T extends Schema<infer S> ? Schema<S> : never} CastToSchema
|
|
46
|
-
*/
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* @template {unknown[]} Arr
|
|
50
|
-
* @typedef {Arr extends [...unknown[], infer L] ? L : never} TupleLast
|
|
51
|
-
*/
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* @template {unknown[]} Arr
|
|
55
|
-
* @typedef {Arr extends [...infer Fs, unknown] ? Fs : never} TuplePop
|
|
56
|
-
*/
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* @template {readonly unknown[]} T
|
|
60
|
-
* @typedef {T extends []
|
|
61
|
-
* ? {}
|
|
62
|
-
* : T extends [infer First]
|
|
63
|
-
* ? First
|
|
64
|
-
* : T extends [infer First, ...infer Rest]
|
|
65
|
-
* ? First & Intersect<Rest>
|
|
66
|
-
* : never
|
|
67
|
-
* } Intersect
|
|
68
|
-
*/
|
|
69
|
-
|
|
70
|
-
const schemaSymbol = Symbol('0schema');
|
|
71
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
4
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
5
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
6
|
+
const object = require("./object.js");
|
|
7
|
+
const array = require("./array.js");
|
|
8
|
+
const error = require("./error.js");
|
|
9
|
+
const environment = require("./environment.js");
|
|
10
|
+
const equality = require("./trait/equality.js");
|
|
11
|
+
const _function = require("./function.js");
|
|
12
|
+
const string = require("./string.js");
|
|
13
|
+
const prng = require("./prng.js");
|
|
14
|
+
const number = require("./number.js");
|
|
15
|
+
const schemaSymbol = Symbol("0schema");
|
|
72
16
|
class ValidationError {
|
|
73
|
-
constructor
|
|
74
|
-
/**
|
|
75
|
-
* Reverse errors
|
|
76
|
-
* @type {Array<{ path: string?, expected: string, has: string, message: string? }>}
|
|
77
|
-
*/
|
|
17
|
+
constructor() {
|
|
78
18
|
this._rerrs = [];
|
|
79
19
|
}
|
|
80
|
-
|
|
81
20
|
/**
|
|
82
21
|
* @param {string?} path
|
|
83
22
|
* @param {string} expected
|
|
84
23
|
* @param {string} has
|
|
85
24
|
* @param {string?} message
|
|
86
25
|
*/
|
|
87
|
-
extend
|
|
26
|
+
extend(path, expected, has, message = null) {
|
|
88
27
|
this._rerrs.push({ path, expected, has, message });
|
|
89
28
|
}
|
|
90
|
-
|
|
91
|
-
toString () {
|
|
29
|
+
toString() {
|
|
92
30
|
const s = [];
|
|
93
31
|
for (let i = this._rerrs.length - 1; i > 0; i--) {
|
|
94
32
|
const r = this._rerrs[i];
|
|
95
|
-
|
|
96
|
-
s.push(string.repeat(' ', (this._rerrs.length - i) * 2) + `${r.path != null ? `[${r.path}] ` : ''}${r.has} doesn't match ${r.expected}. ${r.message}`);
|
|
33
|
+
s.push(string.repeat(" ", (this._rerrs.length - i) * 2) + `${r.path != null ? `[${r.path}] ` : ""}${r.has} doesn't match ${r.expected}. ${r.message}`);
|
|
97
34
|
}
|
|
98
|
-
return s.join(
|
|
35
|
+
return s.join("\n");
|
|
99
36
|
}
|
|
100
37
|
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* @param {any} a
|
|
104
|
-
* @param {any} b
|
|
105
|
-
* @return {boolean}
|
|
106
|
-
*/
|
|
107
38
|
const shapeExtends = (a, b) => {
|
|
108
|
-
if (a === b) return true
|
|
109
|
-
if (a == null || b == null || a.constructor !== b.constructor) return false
|
|
110
|
-
if (a[equality.EqualityTraitSymbol]) return equality.equals(a, b)
|
|
39
|
+
if (a === b) return true;
|
|
40
|
+
if (a == null || b == null || a.constructor !== b.constructor) return false;
|
|
41
|
+
if (a[equality.EqualityTraitSymbol]) return equality.equals(a, b);
|
|
111
42
|
if (array.isArray(a)) {
|
|
112
|
-
return array.every(
|
|
113
|
-
|
|
114
|
-
|
|
43
|
+
return array.every(
|
|
44
|
+
a,
|
|
45
|
+
(aitem) => array.some(b, (bitem) => shapeExtends(aitem, bitem))
|
|
46
|
+
);
|
|
115
47
|
} else if (object.isObject(a)) {
|
|
116
|
-
return object.every(
|
|
117
|
-
|
|
118
|
-
|
|
48
|
+
return object.every(
|
|
49
|
+
a,
|
|
50
|
+
(aitem, akey) => shapeExtends(aitem, b[akey])
|
|
51
|
+
);
|
|
119
52
|
}
|
|
120
|
-
|
|
121
|
-
return false
|
|
53
|
+
return false;
|
|
122
54
|
};
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* @template T
|
|
126
|
-
* @implements {equalityTraits.EqualityTrait}
|
|
127
|
-
*/
|
|
128
55
|
class Schema {
|
|
129
|
-
// this.shape must not be defined on Schema. Otherwise typecheck on metatypes (e.g. $$object) won't work as expected anymore
|
|
130
|
-
/**
|
|
131
|
-
* If true, the more things are added to the shape the more objects this schema will accept (e.g.
|
|
132
|
-
* union). By default, the more objects are added, the the fewer objects this schema will accept.
|
|
133
|
-
* @protected
|
|
134
|
-
*/
|
|
135
|
-
static _dilutes = false
|
|
136
|
-
|
|
137
56
|
/**
|
|
138
57
|
* @param {Schema<any>} other
|
|
139
58
|
*/
|
|
140
|
-
extends
|
|
141
|
-
let [a, b] = [
|
|
142
|
-
|
|
143
|
-
|
|
59
|
+
extends(other) {
|
|
60
|
+
let [a, b] = [
|
|
61
|
+
/** @type {any} */
|
|
62
|
+
this.shape,
|
|
63
|
+
/** @type {any} */
|
|
64
|
+
other.shape
|
|
65
|
+
];
|
|
66
|
+
if (
|
|
67
|
+
/** @type {typeof Schema<any>} */
|
|
68
|
+
this.constructor._dilutes
|
|
69
|
+
) [b, a] = [a, b];
|
|
70
|
+
return shapeExtends(a, b);
|
|
144
71
|
}
|
|
145
|
-
|
|
146
72
|
/**
|
|
147
73
|
* Overwrite this when necessary. By default, we only check the `shape` property which every shape
|
|
148
74
|
* should have.
|
|
149
75
|
* @param {Schema<any>} other
|
|
150
76
|
*/
|
|
151
|
-
equals
|
|
152
|
-
|
|
153
|
-
|
|
77
|
+
equals(other) {
|
|
78
|
+
return this.constructor === other.constructor && _function.equalityDeep(this.shape, other.shape);
|
|
79
|
+
}
|
|
80
|
+
[schemaSymbol]() {
|
|
81
|
+
return true;
|
|
154
82
|
}
|
|
155
|
-
|
|
156
|
-
[schemaSymbol] () { return true }
|
|
157
|
-
|
|
158
83
|
/**
|
|
159
84
|
* @param {object} other
|
|
160
85
|
*/
|
|
161
|
-
[equality.EqualityTraitSymbol]
|
|
162
|
-
return this.equals(
|
|
86
|
+
[equality.EqualityTraitSymbol](other) {
|
|
87
|
+
return this.equals(
|
|
88
|
+
/** @type {any} */
|
|
89
|
+
other
|
|
90
|
+
);
|
|
163
91
|
}
|
|
164
|
-
|
|
165
92
|
/**
|
|
166
93
|
* Use `schema.validate(obj)` with a typed parameter that is already of typed to be an instance of
|
|
167
94
|
* Schema. Validate will check the structure of the parameter and return true iff the instance
|
|
@@ -170,10 +97,9 @@ class Schema {
|
|
|
170
97
|
* @param {T} o
|
|
171
98
|
* @return {boolean}
|
|
172
99
|
*/
|
|
173
|
-
validate
|
|
174
|
-
return this.check(o)
|
|
100
|
+
validate(o) {
|
|
101
|
+
return this.check(o);
|
|
175
102
|
}
|
|
176
|
-
|
|
177
103
|
/* c8 ignore start */
|
|
178
104
|
/**
|
|
179
105
|
* Similar to validate, but this method accepts untyped parameters.
|
|
@@ -182,26 +108,25 @@ class Schema {
|
|
|
182
108
|
* @param {ValidationError} [_err]
|
|
183
109
|
* @return {_o is T}
|
|
184
110
|
*/
|
|
185
|
-
check
|
|
111
|
+
check(_o, _err) {
|
|
186
112
|
error.methodUnimplemented();
|
|
187
113
|
}
|
|
188
114
|
/* c8 ignore stop */
|
|
189
|
-
|
|
190
115
|
/**
|
|
191
116
|
* @type {Schema<T?>}
|
|
192
117
|
*/
|
|
193
|
-
get nullable
|
|
194
|
-
|
|
195
|
-
return $union(this, $null)
|
|
118
|
+
get nullable() {
|
|
119
|
+
return $union(this, $null);
|
|
196
120
|
}
|
|
197
|
-
|
|
198
121
|
/**
|
|
199
122
|
* @type {$Optional<Schema<T>>}
|
|
200
123
|
*/
|
|
201
|
-
get optional
|
|
202
|
-
return new $Optional(
|
|
124
|
+
get optional() {
|
|
125
|
+
return new $Optional(
|
|
126
|
+
/** @type {Schema<T>} */
|
|
127
|
+
this
|
|
128
|
+
);
|
|
203
129
|
}
|
|
204
|
-
|
|
205
130
|
/**
|
|
206
131
|
* Cast a variable to a specific type. Returns the casted value, or throws an exception otherwise.
|
|
207
132
|
* Use this if you know that the type is of a specific type and you just want to convince the type
|
|
@@ -214,11 +139,13 @@ class Schema {
|
|
|
214
139
|
* @param {OO} o
|
|
215
140
|
* @return {Extract<OO, T> extends never ? T : (OO extends Array<never> ? T : Extract<OO,T>)}
|
|
216
141
|
*/
|
|
217
|
-
cast
|
|
142
|
+
cast(o) {
|
|
218
143
|
assert(o, this);
|
|
219
|
-
return
|
|
144
|
+
return (
|
|
145
|
+
/** @type {any} */
|
|
146
|
+
o
|
|
147
|
+
);
|
|
220
148
|
}
|
|
221
|
-
|
|
222
149
|
/**
|
|
223
150
|
* EXPECTO PATRONUM!! 🪄
|
|
224
151
|
* This function protects against type errors. Though it may not work in the real world.
|
|
@@ -238,758 +165,480 @@ class Schema {
|
|
|
238
165
|
* @param {T} o
|
|
239
166
|
* @return {o extends T ? T : never}
|
|
240
167
|
*/
|
|
241
|
-
expect
|
|
168
|
+
expect(o) {
|
|
242
169
|
assert(o, this);
|
|
243
|
-
return o
|
|
170
|
+
return o;
|
|
244
171
|
}
|
|
245
172
|
}
|
|
246
|
-
|
|
173
|
+
// this.shape must not be defined on Schema. Otherwise typecheck on metatypes (e.g. $$object) won't work as expected anymore
|
|
247
174
|
/**
|
|
248
|
-
*
|
|
249
|
-
*
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
/**
|
|
253
|
-
* @template {(new (...args:any[]) => any) | ((...args:any[]) => any)} C
|
|
254
|
-
* @extends {Schema<Instance<C>>}
|
|
175
|
+
* If true, the more things are added to the shape the more objects this schema will accept (e.g.
|
|
176
|
+
* union). By default, the more objects are added, the the fewer objects this schema will accept.
|
|
177
|
+
* @protected
|
|
255
178
|
*/
|
|
179
|
+
__publicField(Schema, "_dilutes", false);
|
|
256
180
|
class $ConstructedBy extends Schema {
|
|
257
181
|
/**
|
|
258
182
|
* @param {C} c
|
|
259
183
|
* @param {((o:Instance<C>)=>boolean)|null} check
|
|
260
184
|
*/
|
|
261
|
-
constructor
|
|
185
|
+
constructor(c, check) {
|
|
262
186
|
super();
|
|
263
187
|
this.shape = c;
|
|
264
188
|
this._c = check;
|
|
265
189
|
}
|
|
266
|
-
|
|
267
190
|
/**
|
|
268
191
|
* @param {any} o
|
|
269
192
|
* @param {ValidationError} [err]
|
|
270
193
|
* @return {o is C extends ((...args:any[]) => infer T) ? T : (C extends (new (...args:any[]) => any) ? InstanceType<C> : never)} o
|
|
271
194
|
*/
|
|
272
|
-
check
|
|
195
|
+
check(o, err = void 0) {
|
|
273
196
|
const c = o?.constructor === this.shape && (this._c == null || this._c(o));
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
return c
|
|
197
|
+
!c && err?.extend(null, this.shape.name, o?.constructor.name, o?.constructor !== this.shape ? "Constructor match failed" : "Check failed");
|
|
198
|
+
return c;
|
|
277
199
|
}
|
|
278
200
|
}
|
|
279
|
-
|
|
280
|
-
/**
|
|
281
|
-
* @template {(new (...args:any[]) => any) | ((...args:any[]) => any)} C
|
|
282
|
-
* @param {C} c
|
|
283
|
-
* @param {((o:Instance<C>) => boolean)|null} check
|
|
284
|
-
* @return {CastToSchema<$ConstructedBy<C>>}
|
|
285
|
-
*/
|
|
286
201
|
const $constructedBy = (c, check = null) => new $ConstructedBy(c, check);
|
|
287
202
|
$constructedBy($ConstructedBy);
|
|
288
|
-
|
|
289
|
-
/**
|
|
290
|
-
* Check custom properties on any object. You may want to overwrite the generated Schema<any>.
|
|
291
|
-
*
|
|
292
|
-
* @extends {Schema<any>}
|
|
293
|
-
*/
|
|
294
203
|
class $Custom extends Schema {
|
|
295
204
|
/**
|
|
296
205
|
* @param {(o:any) => boolean} check
|
|
297
206
|
*/
|
|
298
|
-
constructor
|
|
207
|
+
constructor(check) {
|
|
299
208
|
super();
|
|
300
|
-
/**
|
|
301
|
-
* @type {(o:any) => boolean}
|
|
302
|
-
*/
|
|
303
209
|
this.shape = check;
|
|
304
210
|
}
|
|
305
|
-
|
|
306
211
|
/**
|
|
307
212
|
* @param {any} o
|
|
308
213
|
* @param {ValidationError} err
|
|
309
214
|
* @return {o is any}
|
|
310
215
|
*/
|
|
311
|
-
check
|
|
216
|
+
check(o, err) {
|
|
312
217
|
const c = this.shape(o);
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
return c
|
|
218
|
+
!c && err?.extend(null, "custom prop", o?.constructor.name, "failed to check custom prop");
|
|
219
|
+
return c;
|
|
316
220
|
}
|
|
317
221
|
}
|
|
318
|
-
|
|
319
|
-
/**
|
|
320
|
-
* @param {(o:any) => boolean} check
|
|
321
|
-
* @return {Schema<any>}
|
|
322
|
-
*/
|
|
323
222
|
const $custom = (check) => new $Custom(check);
|
|
324
223
|
$constructedBy($Custom);
|
|
325
|
-
|
|
326
|
-
/**
|
|
327
|
-
* @template {Primitive} T
|
|
328
|
-
* @extends {Schema<T>}
|
|
329
|
-
*/
|
|
330
224
|
class $Literal extends Schema {
|
|
331
225
|
/**
|
|
332
226
|
* @param {Array<T>} literals
|
|
333
227
|
*/
|
|
334
|
-
constructor
|
|
228
|
+
constructor(literals) {
|
|
335
229
|
super();
|
|
336
230
|
this.shape = literals;
|
|
337
231
|
}
|
|
338
|
-
|
|
339
232
|
/**
|
|
340
233
|
*
|
|
341
234
|
* @param {any} o
|
|
342
235
|
* @param {ValidationError} [err]
|
|
343
236
|
* @return {o is T}
|
|
344
237
|
*/
|
|
345
|
-
check
|
|
346
|
-
const c = this.shape.some(a => a === o);
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
return c
|
|
238
|
+
check(o, err) {
|
|
239
|
+
const c = this.shape.some((a) => a === o);
|
|
240
|
+
!c && err?.extend(null, this.shape.join(" | "), o.toString());
|
|
241
|
+
return c;
|
|
350
242
|
}
|
|
351
243
|
}
|
|
352
|
-
|
|
353
|
-
/**
|
|
354
|
-
* @template {Primitive[]} T
|
|
355
|
-
* @param {T} literals
|
|
356
|
-
* @return {CastToSchema<$Literal<T[number]>>}
|
|
357
|
-
*/
|
|
358
244
|
const $literal = (...literals) => new $Literal(literals);
|
|
359
245
|
const $$literal = $constructedBy($Literal);
|
|
360
|
-
|
|
361
|
-
/**
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
*/
|
|
365
|
-
|
|
366
|
-
/**
|
|
367
|
-
* @param {string} str
|
|
368
|
-
* @return {string}
|
|
369
|
-
*/
|
|
370
|
-
const _regexEscape = /** @type {any} */ (RegExp).escape || /** @type {(str:string) => string} */ (str =>
|
|
371
|
-
str.replace(/[().|&,$^[\]]/g, s => '\\' + s)
|
|
246
|
+
const _regexEscape = (
|
|
247
|
+
/** @type {any} */
|
|
248
|
+
RegExp.escape || /** @type {(str:string) => string} */
|
|
249
|
+
((str) => str.replace(/[().|&,$^[\]]/g, (s) => "\\" + s))
|
|
372
250
|
);
|
|
373
|
-
|
|
374
|
-
/**
|
|
375
|
-
* @param {string|Schema<any>} s
|
|
376
|
-
* @return {string[]}
|
|
377
|
-
*/
|
|
378
|
-
const _schemaStringTemplateToRegex = s => {
|
|
251
|
+
const _schemaStringTemplateToRegex = (s) => {
|
|
379
252
|
if ($string.check(s)) {
|
|
380
|
-
return [_regexEscape(s)]
|
|
253
|
+
return [_regexEscape(s)];
|
|
381
254
|
}
|
|
382
255
|
if ($$literal.check(s)) {
|
|
383
|
-
return
|
|
256
|
+
return (
|
|
257
|
+
/** @type {Array<string|number>} */
|
|
258
|
+
s.shape.map((v) => v + "")
|
|
259
|
+
);
|
|
384
260
|
}
|
|
385
261
|
if ($$number.check(s)) {
|
|
386
|
-
return [
|
|
262
|
+
return ["[+-]?\\d+.?\\d*"];
|
|
387
263
|
}
|
|
388
264
|
if ($$string.check(s)) {
|
|
389
|
-
return [
|
|
265
|
+
return [".*"];
|
|
390
266
|
}
|
|
391
267
|
if ($$union.check(s)) {
|
|
392
|
-
return s.shape.map(_schemaStringTemplateToRegex).flat(1)
|
|
268
|
+
return s.shape.map(_schemaStringTemplateToRegex).flat(1);
|
|
393
269
|
}
|
|
394
|
-
/* c8 ignore next 2 */
|
|
395
|
-
// unexpected schema structure (only supports unions and string in literal types)
|
|
396
270
|
error.unexpectedCase();
|
|
397
271
|
};
|
|
398
|
-
|
|
399
|
-
/**
|
|
400
|
-
* @template {Array<string|Schema<string|number>>} T
|
|
401
|
-
* @extends {Schema<CastStringTemplateArgsToTemplate<T>>}
|
|
402
|
-
*/
|
|
403
272
|
class $StringTemplate extends Schema {
|
|
404
273
|
/**
|
|
405
274
|
* @param {T} shape
|
|
406
275
|
*/
|
|
407
|
-
constructor
|
|
276
|
+
constructor(shape) {
|
|
408
277
|
super();
|
|
409
278
|
this.shape = shape;
|
|
410
|
-
this._r = new RegExp(
|
|
279
|
+
this._r = new RegExp("^" + shape.map(_schemaStringTemplateToRegex).map((opts) => `(${opts.join("|")})`).join("") + "$");
|
|
411
280
|
}
|
|
412
|
-
|
|
413
281
|
/**
|
|
414
282
|
* @param {any} o
|
|
415
283
|
* @param {ValidationError} [err]
|
|
416
284
|
* @return {o is CastStringTemplateArgsToTemplate<T>}
|
|
417
285
|
*/
|
|
418
|
-
check
|
|
286
|
+
check(o, err) {
|
|
419
287
|
const c = this._r.exec(o) != null;
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
return c
|
|
288
|
+
!c && err?.extend(null, this._r.toString(), o.toString(), "String doesn't match string template.");
|
|
289
|
+
return c;
|
|
423
290
|
}
|
|
424
291
|
}
|
|
425
292
|
$constructedBy($StringTemplate);
|
|
426
|
-
|
|
427
|
-
const isOptionalSymbol = Symbol('optional');
|
|
428
|
-
/**
|
|
429
|
-
* @template {Schema<any>} S
|
|
430
|
-
* @extends Schema<Unwrap<S>|undefined>
|
|
431
|
-
*/
|
|
293
|
+
const isOptionalSymbol = Symbol("optional");
|
|
432
294
|
class $Optional extends Schema {
|
|
433
295
|
/**
|
|
434
296
|
* @param {S} shape
|
|
435
297
|
*/
|
|
436
|
-
constructor
|
|
298
|
+
constructor(shape) {
|
|
437
299
|
super();
|
|
438
300
|
this.shape = shape;
|
|
439
301
|
}
|
|
440
|
-
|
|
441
302
|
/**
|
|
442
303
|
* @param {any} o
|
|
443
304
|
* @param {ValidationError} [err]
|
|
444
305
|
* @return {o is (Unwrap<S>|undefined)}
|
|
445
306
|
*/
|
|
446
|
-
check
|
|
447
|
-
const c = o ===
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
307
|
+
check(o, err) {
|
|
308
|
+
const c = o === void 0 || this.shape.check(o);
|
|
309
|
+
!c && err?.extend(null, "undefined (optional)", "()");
|
|
310
|
+
return c;
|
|
311
|
+
}
|
|
312
|
+
get [isOptionalSymbol]() {
|
|
313
|
+
return true;
|
|
451
314
|
}
|
|
452
|
-
|
|
453
|
-
get [isOptionalSymbol] () { return true }
|
|
454
315
|
}
|
|
455
316
|
const $$optional = $constructedBy($Optional);
|
|
456
|
-
|
|
457
|
-
/**
|
|
458
|
-
* @extends Schema<never>
|
|
459
|
-
*/
|
|
460
317
|
class $Never extends Schema {
|
|
461
318
|
/**
|
|
462
319
|
* @param {any} _o
|
|
463
320
|
* @param {ValidationError} [err]
|
|
464
321
|
* @return {_o is never}
|
|
465
322
|
*/
|
|
466
|
-
check
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
return false
|
|
323
|
+
check(_o, err) {
|
|
324
|
+
err?.extend(null, "never", typeof _o);
|
|
325
|
+
return false;
|
|
470
326
|
}
|
|
471
327
|
}
|
|
472
328
|
$constructedBy($Never);
|
|
473
|
-
|
|
474
|
-
/**
|
|
475
|
-
* @template {{ [key: string|symbol|number]: Schema<any> }} S
|
|
476
|
-
* @typedef {{ [Key in keyof S as S[Key] extends $Optional<Schema<any>> ? Key : never]?: S[Key] extends $Optional<Schema<infer Type>> ? Type : never } & { [Key in keyof S as S[Key] extends $Optional<Schema<any>> ? never : Key]: S[Key] extends Schema<infer Type> ? Type : never }} $ObjectToType
|
|
477
|
-
*/
|
|
478
|
-
|
|
479
|
-
/**
|
|
480
|
-
* @template {{[key:string|symbol|number]: Schema<any>}} S
|
|
481
|
-
* @extends {Schema<$ObjectToType<S>>}
|
|
482
|
-
*/
|
|
483
|
-
class $Object extends Schema {
|
|
329
|
+
const _$Object = class _$Object extends Schema {
|
|
484
330
|
/**
|
|
485
331
|
* @param {S} shape
|
|
486
332
|
* @param {boolean} partial
|
|
487
333
|
*/
|
|
488
|
-
constructor
|
|
334
|
+
constructor(shape, partial = false) {
|
|
489
335
|
super();
|
|
490
|
-
/**
|
|
491
|
-
* @type {S}
|
|
492
|
-
*/
|
|
493
336
|
this.shape = shape;
|
|
494
337
|
this._isPartial = partial;
|
|
495
338
|
}
|
|
496
|
-
|
|
497
|
-
static _dilutes = true
|
|
498
|
-
|
|
499
339
|
/**
|
|
500
340
|
* @type {Schema<Partial<$ObjectToType<S>>>}
|
|
501
341
|
*/
|
|
502
|
-
get partial
|
|
503
|
-
return new $Object(this.shape, true)
|
|
342
|
+
get partial() {
|
|
343
|
+
return new _$Object(this.shape, true);
|
|
504
344
|
}
|
|
505
|
-
|
|
506
345
|
/**
|
|
507
346
|
* @param {any} o
|
|
508
347
|
* @param {ValidationError} err
|
|
509
348
|
* @return {o is $ObjectToType<S>}
|
|
510
349
|
*/
|
|
511
|
-
check
|
|
350
|
+
check(o, err) {
|
|
512
351
|
if (o == null) {
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
return false
|
|
352
|
+
err?.extend(null, "object", "null");
|
|
353
|
+
return false;
|
|
516
354
|
}
|
|
517
355
|
return object.every(this.shape, (vv, vk) => {
|
|
518
|
-
const c =
|
|
519
|
-
!c && err?.extend(vk.toString(), vv.toString(), typeof o[vk],
|
|
520
|
-
return c
|
|
521
|
-
})
|
|
356
|
+
const c = this._isPartial && !object.hasProperty(o, vk) || vv.check(o[vk], err);
|
|
357
|
+
!c && err?.extend(vk.toString(), vv.toString(), typeof o[vk], "Object property does not match");
|
|
358
|
+
return c;
|
|
359
|
+
});
|
|
522
360
|
}
|
|
523
|
-
}
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
// I used an explicit type annotation instead of $ObjectToType, so that the user doesn't see the
|
|
531
|
-
// weird type definitions when inspecting type definions.
|
|
532
|
-
/**
|
|
533
|
-
* @template {{ [key:string|symbol|number]: Schema<any> }} S
|
|
534
|
-
* @param {S} def
|
|
535
|
-
* @return {_ObjectDefToSchema<S> extends Schema<infer S> ? Schema<{ [K in keyof S]: S[K] }> : never}
|
|
536
|
-
*/
|
|
537
|
-
const $object = def => /** @type {any} */ (new $Object(def));
|
|
361
|
+
};
|
|
362
|
+
__publicField(_$Object, "_dilutes", true);
|
|
363
|
+
let $Object = _$Object;
|
|
364
|
+
const $object = (def) => (
|
|
365
|
+
/** @type {any} */
|
|
366
|
+
new $Object(def)
|
|
367
|
+
);
|
|
538
368
|
const $$object = $constructedBy($Object);
|
|
539
|
-
|
|
540
|
-
* @type {Schema<{[key:string]: any}>}
|
|
541
|
-
*/
|
|
542
|
-
const $objectAny = $custom(o => o != null && (o.constructor === Object || o.constructor == null));
|
|
543
|
-
|
|
544
|
-
/**
|
|
545
|
-
* @template {Schema<string|number|symbol>} Keys
|
|
546
|
-
* @template {Schema<any>} Values
|
|
547
|
-
* @extends {Schema<{ [key in Unwrap<Keys>]: Unwrap<Values> }>}
|
|
548
|
-
*/
|
|
369
|
+
const $objectAny = $custom((o) => o != null && (o.constructor === Object || o.constructor == null));
|
|
549
370
|
class $Record extends Schema {
|
|
550
371
|
/**
|
|
551
372
|
* @param {Keys} keys
|
|
552
373
|
* @param {Values} values
|
|
553
374
|
*/
|
|
554
|
-
constructor
|
|
375
|
+
constructor(keys, values) {
|
|
555
376
|
super();
|
|
556
377
|
this.shape = {
|
|
557
|
-
keys,
|
|
378
|
+
keys,
|
|
379
|
+
values
|
|
558
380
|
};
|
|
559
381
|
}
|
|
560
|
-
|
|
561
382
|
/**
|
|
562
383
|
* @param {any} o
|
|
563
384
|
* @param {ValidationError} err
|
|
564
385
|
* @return {o is { [key in Unwrap<Keys>]: Unwrap<Values> }}
|
|
565
386
|
*/
|
|
566
|
-
check
|
|
387
|
+
check(o, err) {
|
|
567
388
|
return o != null && object.every(o, (vv, vk) => {
|
|
568
389
|
const ck = this.shape.keys.check(vk, err);
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
})
|
|
390
|
+
!ck && err?.extend(vk + "", "Record", typeof o, ck ? "Key doesn't match schema" : "Value doesn't match value");
|
|
391
|
+
return ck && this.shape.values.check(vv, err);
|
|
392
|
+
});
|
|
573
393
|
}
|
|
574
394
|
}
|
|
575
|
-
|
|
576
|
-
/**
|
|
577
|
-
* @template {Schema<string|number|symbol>} Keys
|
|
578
|
-
* @template {Schema<any>} Values
|
|
579
|
-
* @param {Keys} keys
|
|
580
|
-
* @param {Values} values
|
|
581
|
-
* @return {CastToSchema<$Record<Keys,Values>>}
|
|
582
|
-
*/
|
|
583
395
|
const $record = (keys, values) => new $Record(keys, values);
|
|
584
396
|
const $$record = $constructedBy($Record);
|
|
585
|
-
|
|
586
|
-
/**
|
|
587
|
-
* @template {Schema<any>[]} S
|
|
588
|
-
* @extends {Schema<{ [Key in keyof S]: S[Key] extends Schema<infer Type> ? Type : never }>}
|
|
589
|
-
*/
|
|
590
397
|
class $Tuple extends Schema {
|
|
591
398
|
/**
|
|
592
399
|
* @param {S} shape
|
|
593
400
|
*/
|
|
594
|
-
constructor
|
|
401
|
+
constructor(shape) {
|
|
595
402
|
super();
|
|
596
403
|
this.shape = shape;
|
|
597
404
|
}
|
|
598
|
-
|
|
599
405
|
/**
|
|
600
406
|
* @param {any} o
|
|
601
407
|
* @param {ValidationError} err
|
|
602
408
|
* @return {o is { [K in keyof S]: S[K] extends Schema<infer Type> ? Type : never }}
|
|
603
409
|
*/
|
|
604
|
-
check
|
|
410
|
+
check(o, err) {
|
|
605
411
|
return o != null && object.every(this.shape, (vv, vk) => {
|
|
606
|
-
const c =
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
412
|
+
const c = (
|
|
413
|
+
/** @type {Schema<any>} */
|
|
414
|
+
vv.check(o[vk], err)
|
|
415
|
+
);
|
|
416
|
+
!c && err?.extend(vk.toString(), "Tuple", typeof vv);
|
|
417
|
+
return c;
|
|
418
|
+
});
|
|
611
419
|
}
|
|
612
420
|
}
|
|
613
|
-
|
|
614
|
-
/**
|
|
615
|
-
* @template {Array<Schema<any>>} T
|
|
616
|
-
* @param {T} def
|
|
617
|
-
* @return {CastToSchema<$Tuple<T>>}
|
|
618
|
-
*/
|
|
619
421
|
const $tuple = (...def) => new $Tuple(def);
|
|
620
422
|
$constructedBy($Tuple);
|
|
621
|
-
|
|
622
|
-
/**
|
|
623
|
-
* @template {Schema<any>} S
|
|
624
|
-
* @extends {Schema<Array<S extends Schema<infer T> ? T : never>>}
|
|
625
|
-
*/
|
|
626
423
|
class $Array extends Schema {
|
|
627
424
|
/**
|
|
628
425
|
* @param {Array<S>} v
|
|
629
426
|
*/
|
|
630
|
-
constructor
|
|
427
|
+
constructor(v) {
|
|
631
428
|
super();
|
|
632
|
-
/**
|
|
633
|
-
* @type {Schema<S extends Schema<infer T> ? T : never>}
|
|
634
|
-
*/
|
|
635
429
|
this.shape = v.length === 1 ? v[0] : new $Union(v);
|
|
636
430
|
}
|
|
637
|
-
|
|
638
431
|
/**
|
|
639
432
|
* @param {any} o
|
|
640
433
|
* @param {ValidationError} [err]
|
|
641
434
|
* @return {o is Array<S extends Schema<infer T> ? T : never>} o
|
|
642
435
|
*/
|
|
643
|
-
check
|
|
644
|
-
const c = array.isArray(o) && array.every(o, oi => this.shape.check(oi));
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
return c
|
|
436
|
+
check(o, err) {
|
|
437
|
+
const c = array.isArray(o) && array.every(o, (oi) => this.shape.check(oi));
|
|
438
|
+
!c && err?.extend(null, "Array", "");
|
|
439
|
+
return c;
|
|
648
440
|
}
|
|
649
441
|
}
|
|
650
|
-
|
|
651
|
-
/**
|
|
652
|
-
* @template {Array<Schema<any>>} T
|
|
653
|
-
* @param {T} def
|
|
654
|
-
* @return {Schema<Array<T extends Array<Schema<infer S>> ? S : never>>}
|
|
655
|
-
*/
|
|
656
442
|
const $array = (...def) => new $Array(def);
|
|
657
443
|
const $$array = $constructedBy($Array);
|
|
658
|
-
|
|
659
|
-
* @type {Schema<Array<any>>}
|
|
660
|
-
*/
|
|
661
|
-
const $arrayAny = $custom(o => array.isArray(o));
|
|
662
|
-
|
|
663
|
-
/**
|
|
664
|
-
* @template T
|
|
665
|
-
* @extends {Schema<T>}
|
|
666
|
-
*/
|
|
444
|
+
const $arrayAny = $custom((o) => array.isArray(o));
|
|
667
445
|
class $InstanceOf extends Schema {
|
|
668
446
|
/**
|
|
669
447
|
* @param {new (...args:any) => T} constructor
|
|
670
448
|
* @param {((o:T) => boolean)|null} check
|
|
671
449
|
*/
|
|
672
|
-
constructor
|
|
450
|
+
constructor(constructor, check) {
|
|
673
451
|
super();
|
|
674
452
|
this.shape = constructor;
|
|
675
453
|
this._c = check;
|
|
676
454
|
}
|
|
677
|
-
|
|
678
455
|
/**
|
|
679
456
|
* @param {any} o
|
|
680
457
|
* @param {ValidationError} err
|
|
681
458
|
* @return {o is T}
|
|
682
459
|
*/
|
|
683
|
-
check
|
|
460
|
+
check(o, err) {
|
|
684
461
|
const c = o instanceof this.shape && (this._c == null || this._c(o));
|
|
685
|
-
/* c8 ignore next */
|
|
686
462
|
!c && err?.extend(null, this.shape.name, o?.constructor.name);
|
|
687
|
-
return c
|
|
463
|
+
return c;
|
|
688
464
|
}
|
|
689
465
|
}
|
|
690
|
-
|
|
691
|
-
/**
|
|
692
|
-
* @template T
|
|
693
|
-
* @param {new (...args:any) => T} c
|
|
694
|
-
* @param {((o:T) => boolean)|null} check
|
|
695
|
-
* @return {Schema<T>}
|
|
696
|
-
*/
|
|
697
466
|
const $instanceOf = (c, check = null) => new $InstanceOf(c, check);
|
|
698
467
|
$constructedBy($InstanceOf);
|
|
699
|
-
|
|
700
468
|
const $$schema = $instanceOf(Schema);
|
|
701
|
-
|
|
702
|
-
/**
|
|
703
|
-
* @template {Schema<any>[]} Args
|
|
704
|
-
* @typedef {(...args:UnwrapArray<TuplePop<Args>>)=>Unwrap<TupleLast<Args>>} _LArgsToLambdaDef
|
|
705
|
-
*/
|
|
706
|
-
|
|
707
|
-
/**
|
|
708
|
-
* @template {Array<Schema<any>>} Args
|
|
709
|
-
* @extends {Schema<_LArgsToLambdaDef<Args>>}
|
|
710
|
-
*/
|
|
711
469
|
class $Lambda extends Schema {
|
|
712
470
|
/**
|
|
713
471
|
* @param {Args} args
|
|
714
472
|
*/
|
|
715
|
-
constructor
|
|
473
|
+
constructor(args) {
|
|
716
474
|
super();
|
|
717
475
|
this.len = args.length - 1;
|
|
718
476
|
this.args = $tuple(...args.slice(-1));
|
|
719
477
|
this.res = args[this.len];
|
|
720
478
|
}
|
|
721
|
-
|
|
722
479
|
/**
|
|
723
480
|
* @param {any} f
|
|
724
481
|
* @param {ValidationError} err
|
|
725
482
|
* @return {f is _LArgsToLambdaDef<Args>}
|
|
726
483
|
*/
|
|
727
|
-
check
|
|
484
|
+
check(f, err) {
|
|
728
485
|
const c = f.constructor === Function && f.length <= this.len;
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
return c
|
|
486
|
+
!c && err?.extend(null, "function", typeof f);
|
|
487
|
+
return c;
|
|
732
488
|
}
|
|
733
489
|
}
|
|
734
490
|
const $$lambda = $constructedBy($Lambda);
|
|
735
|
-
|
|
736
|
-
/**
|
|
737
|
-
* @type {Schema<Function>}
|
|
738
|
-
*/
|
|
739
|
-
const $function = $custom(o => typeof o === 'function');
|
|
740
|
-
|
|
741
|
-
/**
|
|
742
|
-
* @template {Array<Schema<any>>} T
|
|
743
|
-
* @extends {Schema<Intersect<UnwrapArray<T>>>}
|
|
744
|
-
*/
|
|
491
|
+
const $function = $custom((o) => typeof o === "function");
|
|
745
492
|
class $Intersection extends Schema {
|
|
746
493
|
/**
|
|
747
494
|
* @param {T} v
|
|
748
495
|
*/
|
|
749
|
-
constructor
|
|
496
|
+
constructor(v) {
|
|
750
497
|
super();
|
|
751
|
-
/**
|
|
752
|
-
* @type {T}
|
|
753
|
-
*/
|
|
754
498
|
this.shape = v;
|
|
755
499
|
}
|
|
756
|
-
|
|
757
500
|
/**
|
|
758
501
|
* @param {any} o
|
|
759
502
|
* @param {ValidationError} [err]
|
|
760
503
|
* @return {o is Intersect<UnwrapArray<T>>}
|
|
761
504
|
*/
|
|
762
|
-
check
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
!c && err?.extend(null, 'Intersectinon', typeof o);
|
|
767
|
-
return c
|
|
505
|
+
check(o, err) {
|
|
506
|
+
const c = array.every(this.shape, (check) => check.check(o, err));
|
|
507
|
+
!c && err?.extend(null, "Intersectinon", typeof o);
|
|
508
|
+
return c;
|
|
768
509
|
}
|
|
769
510
|
}
|
|
770
|
-
$constructedBy($Intersection, o => o.shape.length > 0);
|
|
771
|
-
|
|
772
|
-
/**
|
|
773
|
-
* @template S
|
|
774
|
-
* @extends {Schema<S>}
|
|
775
|
-
*/
|
|
511
|
+
$constructedBy($Intersection, (o) => o.shape.length > 0);
|
|
776
512
|
class $Union extends Schema {
|
|
777
|
-
static _dilutes = true
|
|
778
|
-
|
|
779
513
|
/**
|
|
780
514
|
* @param {Array<Schema<S>>} v
|
|
781
515
|
*/
|
|
782
|
-
constructor
|
|
516
|
+
constructor(v) {
|
|
783
517
|
super();
|
|
784
518
|
this.shape = v;
|
|
785
519
|
}
|
|
786
|
-
|
|
787
520
|
/**
|
|
788
521
|
* @param {any} o
|
|
789
522
|
* @param {ValidationError} [err]
|
|
790
523
|
* @return {o is S}
|
|
791
524
|
*/
|
|
792
|
-
check
|
|
525
|
+
check(o, err) {
|
|
793
526
|
const c = array.some(this.shape, (vv) => vv.check(o, err));
|
|
794
|
-
err?.extend(null,
|
|
795
|
-
return c
|
|
527
|
+
err?.extend(null, "Union", typeof o);
|
|
528
|
+
return c;
|
|
796
529
|
}
|
|
797
530
|
}
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
const $union = (...schemas) => schemas.findIndex($s => $$union.check($s)) >= 0
|
|
805
|
-
? $union(...schemas.map($s => $($s)).map($s => $$union.check($s) ? $s.shape : [$s]).flat(1))
|
|
806
|
-
: (schemas.length === 1
|
|
807
|
-
? schemas[0]
|
|
808
|
-
: new $Union(schemas));
|
|
809
|
-
const $$union = /** @type {Schema<$Union<any>>} */ ($constructedBy($Union));
|
|
810
|
-
|
|
531
|
+
__publicField($Union, "_dilutes", true);
|
|
532
|
+
const $union = (...schemas) => schemas.findIndex(($s) => $$union.check($s)) >= 0 ? $union(...schemas.map(($s) => $($s)).map(($s) => $$union.check($s) ? $s.shape : [$s]).flat(1)) : schemas.length === 1 ? schemas[0] : new $Union(schemas);
|
|
533
|
+
const $$union = (
|
|
534
|
+
/** @type {Schema<$Union<any>>} */
|
|
535
|
+
$constructedBy($Union)
|
|
536
|
+
);
|
|
811
537
|
const _t = () => true;
|
|
812
|
-
/**
|
|
813
|
-
* @type {Schema<any>}
|
|
814
|
-
*/
|
|
815
538
|
const $any = $custom(_t);
|
|
816
|
-
const $$any =
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
const
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
const $
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
const
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
const $boolean = $custom(o => typeof o === 'boolean');
|
|
846
|
-
const $$boolean = /** @type {Schema<Schema<Boolean>>} */ ($custom(o => o === $boolean));
|
|
847
|
-
|
|
848
|
-
/**
|
|
849
|
-
* @type {Schema<undefined>}
|
|
850
|
-
*/
|
|
851
|
-
const $undefined = $literal(undefined);
|
|
852
|
-
/** @type {Schema<Schema<undefined>>} */ ($constructedBy($Literal, o => o.shape.length === 1 && o.shape[0] === undefined));
|
|
853
|
-
|
|
854
|
-
/**
|
|
855
|
-
* @type {Schema<void>}
|
|
856
|
-
*/
|
|
857
|
-
$literal(undefined);
|
|
858
|
-
|
|
539
|
+
const $$any = (
|
|
540
|
+
/** @type {Schema<Schema<any>>} */
|
|
541
|
+
$constructedBy($Custom, (o) => o.shape === _t)
|
|
542
|
+
);
|
|
543
|
+
const $bigint = $custom((o) => typeof o === "bigint");
|
|
544
|
+
const $$bigint = (
|
|
545
|
+
/** @type {Schema<Schema<BigInt>>} */
|
|
546
|
+
$custom((o) => o === $bigint)
|
|
547
|
+
);
|
|
548
|
+
const $symbol = $custom((o) => typeof o === "symbol");
|
|
549
|
+
$custom((o) => o === $symbol);
|
|
550
|
+
const $number = $custom((o) => typeof o === "number");
|
|
551
|
+
const $$number = (
|
|
552
|
+
/** @type {Schema<Schema<number>>} */
|
|
553
|
+
$custom((o) => o === $number)
|
|
554
|
+
);
|
|
555
|
+
const $string = $custom((o) => typeof o === "string");
|
|
556
|
+
const $$string = (
|
|
557
|
+
/** @type {Schema<Schema<string>>} */
|
|
558
|
+
$custom((o) => o === $string)
|
|
559
|
+
);
|
|
560
|
+
const $boolean = $custom((o) => typeof o === "boolean");
|
|
561
|
+
const $$boolean = (
|
|
562
|
+
/** @type {Schema<Schema<Boolean>>} */
|
|
563
|
+
$custom((o) => o === $boolean)
|
|
564
|
+
);
|
|
565
|
+
const $undefined = $literal(void 0);
|
|
566
|
+
$constructedBy($Literal, (o) => o.shape.length === 1 && o.shape[0] === void 0);
|
|
567
|
+
$literal(void 0);
|
|
859
568
|
const $null = $literal(null);
|
|
860
|
-
const $$null =
|
|
861
|
-
|
|
569
|
+
const $$null = (
|
|
570
|
+
/** @type {Schema<Schema<null>>} */
|
|
571
|
+
$constructedBy($Literal, (o) => o.shape.length === 1 && o.shape[0] === null)
|
|
572
|
+
);
|
|
862
573
|
$constructedBy(Uint8Array);
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
/**
|
|
866
|
-
* @type {Schema<Primitive>}
|
|
867
|
-
*/
|
|
574
|
+
$constructedBy($ConstructedBy, (o) => o.shape === Uint8Array);
|
|
868
575
|
const $primitive = $union($number, $string, $null, $undefined, $bigint, $boolean, $symbol);
|
|
869
|
-
|
|
870
|
-
/**
|
|
871
|
-
* @typedef {JSON[]} JSONArray
|
|
872
|
-
*/
|
|
873
|
-
/**
|
|
874
|
-
* @typedef {Primitive|JSONArray|{ [key:string]:JSON }} JSON
|
|
875
|
-
*/
|
|
876
|
-
/**
|
|
877
|
-
* @type {Schema<null|number|string|boolean|JSON[]|{[key:string]:JSON}>}
|
|
878
|
-
*/
|
|
879
576
|
(() => {
|
|
880
|
-
const $jsonArr =
|
|
881
|
-
|
|
577
|
+
const $jsonArr = (
|
|
578
|
+
/** @type {$Array<$any>} */
|
|
579
|
+
$array($any)
|
|
580
|
+
);
|
|
581
|
+
const $jsonRecord = (
|
|
582
|
+
/** @type {$Record<$string,$any>} */
|
|
583
|
+
$record($string, $any)
|
|
584
|
+
);
|
|
882
585
|
const $json = $union($number, $string, $null, $boolean, $jsonArr, $jsonRecord);
|
|
883
586
|
$jsonArr.shape = $json;
|
|
884
587
|
$jsonRecord.shape.values = $json;
|
|
885
|
-
return $json
|
|
588
|
+
return $json;
|
|
886
589
|
})();
|
|
887
|
-
|
|
888
|
-
/**
|
|
889
|
-
* @template {any} IN
|
|
890
|
-
* @typedef {IN extends Schema<any> ? IN
|
|
891
|
-
* : (IN extends string|number|boolean|null ? Schema<IN>
|
|
892
|
-
* : (IN extends new (...args:any[])=>any ? Schema<InstanceType<IN>>
|
|
893
|
-
* : (IN extends any[] ? Schema<{ [K in keyof IN]: Unwrap<ReadSchema<IN[K]>> }[number]>
|
|
894
|
-
* : (IN extends object ? (_ObjectDefToSchema<{[K in keyof IN]:ReadSchema<IN[K]>}> extends Schema<infer S> ? Schema<{ [K in keyof S]: S[K] }> : never)
|
|
895
|
-
* : never)
|
|
896
|
-
* )
|
|
897
|
-
* )
|
|
898
|
-
* )
|
|
899
|
-
* } ReadSchemaOld
|
|
900
|
-
*/
|
|
901
|
-
|
|
902
|
-
/**
|
|
903
|
-
* @template {any} IN
|
|
904
|
-
* @typedef {[Extract<IN,Schema<any>>,Extract<IN,string|number|boolean|null>,Extract<IN,new (...args:any[])=>any>,Extract<IN,any[]>,Extract<Exclude<IN,Schema<any>|string|number|boolean|null|(new (...args:any[])=>any)|any[]>,object>] extends [infer Schemas, infer Primitives, infer Constructors, infer Arrs, infer Obj]
|
|
905
|
-
* ? Schema<
|
|
906
|
-
* (Schemas extends Schema<infer S> ? S : never)
|
|
907
|
-
* | Primitives
|
|
908
|
-
* | (Constructors extends new (...args:any[])=>any ? InstanceType<Constructors> : never)
|
|
909
|
-
* | (Arrs extends any[] ? { [K in keyof Arrs]: Unwrap<ReadSchema<Arrs[K]>> }[number] : never)
|
|
910
|
-
* | (Obj extends object ? Unwrap<(_ObjectDefToSchema<{[K in keyof Obj]:ReadSchema<Obj[K]>}> extends Schema<infer S> ? Schema<{ [K in keyof S]: S[K] }> : never)> : never)>
|
|
911
|
-
* : never
|
|
912
|
-
* } ReadSchema
|
|
913
|
-
*/
|
|
914
|
-
|
|
915
|
-
/**
|
|
916
|
-
* @typedef {ReadSchema<{x:42}|{y:99}|Schema<string>|[1,2,{}]>} Q
|
|
917
|
-
*/
|
|
918
|
-
|
|
919
|
-
/**
|
|
920
|
-
* @template IN
|
|
921
|
-
* @param {IN} o
|
|
922
|
-
* @return {ReadSchema<IN>}
|
|
923
|
-
*/
|
|
924
|
-
const $ = o => {
|
|
590
|
+
const $ = (o) => {
|
|
925
591
|
if ($$schema.check(o)) {
|
|
926
|
-
return
|
|
592
|
+
return (
|
|
593
|
+
/** @type {any} */
|
|
594
|
+
o
|
|
595
|
+
);
|
|
927
596
|
} else if ($objectAny.check(o)) {
|
|
928
|
-
/**
|
|
929
|
-
* @type {any}
|
|
930
|
-
*/
|
|
931
597
|
const o2 = {};
|
|
932
598
|
for (const k in o) {
|
|
933
599
|
o2[k] = $(o[k]);
|
|
934
600
|
}
|
|
935
|
-
return
|
|
601
|
+
return (
|
|
602
|
+
/** @type {any} */
|
|
603
|
+
$object(o2)
|
|
604
|
+
);
|
|
936
605
|
} else if ($arrayAny.check(o)) {
|
|
937
|
-
return
|
|
606
|
+
return (
|
|
607
|
+
/** @type {any} */
|
|
608
|
+
$union(...o.map($))
|
|
609
|
+
);
|
|
938
610
|
} else if ($primitive.check(o)) {
|
|
939
|
-
return
|
|
611
|
+
return (
|
|
612
|
+
/** @type {any} */
|
|
613
|
+
$literal(o)
|
|
614
|
+
);
|
|
940
615
|
} else if ($function.check(o)) {
|
|
941
|
-
return
|
|
616
|
+
return (
|
|
617
|
+
/** @type {any} */
|
|
618
|
+
$constructedBy(
|
|
619
|
+
/** @type {any} */
|
|
620
|
+
o
|
|
621
|
+
)
|
|
622
|
+
);
|
|
942
623
|
}
|
|
943
|
-
/* c8 ignore next */
|
|
944
624
|
error.unexpectedCase();
|
|
945
625
|
};
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
const assert = environment.production
|
|
955
|
-
? () => {}
|
|
956
|
-
: (o, schema) => {
|
|
957
|
-
const err = new ValidationError();
|
|
958
|
-
if (!schema.check(o, err)) {
|
|
959
|
-
throw error.create(`Expected value to be of type ${schema.constructor.name}.\n${err.toString()}`)
|
|
960
|
-
}
|
|
961
|
-
};
|
|
962
|
-
/* c8 ignore end */
|
|
963
|
-
|
|
964
|
-
/**
|
|
965
|
-
* @template In
|
|
966
|
-
* @template Out
|
|
967
|
-
* @typedef {{ if: Schema<In>, h: (o:In,state?:any)=>Out }} Pattern
|
|
968
|
-
*/
|
|
969
|
-
|
|
970
|
-
/**
|
|
971
|
-
* @template {Pattern<any,any>} P
|
|
972
|
-
* @template In
|
|
973
|
-
* @typedef {ReturnType<Extract<P,Pattern<In extends number ? number : (In extends string ? string : In),any>>['h']>} PatternMatchResult
|
|
974
|
-
*/
|
|
975
|
-
|
|
976
|
-
/**
|
|
977
|
-
* @todo move this to separate library
|
|
978
|
-
* @template {any} [State=undefined]
|
|
979
|
-
* @template {Pattern<any,any>} [Patterns=never]
|
|
980
|
-
*/
|
|
626
|
+
const assert = environment.production ? () => {
|
|
627
|
+
} : (o, schema) => {
|
|
628
|
+
const err = new ValidationError();
|
|
629
|
+
if (!schema.check(o, err)) {
|
|
630
|
+
throw error.create(`Expected value to be of type ${schema.constructor.name}.
|
|
631
|
+
${err.toString()}`);
|
|
632
|
+
}
|
|
633
|
+
};
|
|
981
634
|
class PatternMatcher {
|
|
982
635
|
/**
|
|
983
636
|
* @param {Schema<State>} [$state]
|
|
984
637
|
*/
|
|
985
|
-
constructor
|
|
986
|
-
/**
|
|
987
|
-
* @type {Array<Patterns>}
|
|
988
|
-
*/
|
|
638
|
+
constructor($state) {
|
|
989
639
|
this.patterns = [];
|
|
990
640
|
this.$state = $state;
|
|
991
641
|
}
|
|
992
|
-
|
|
993
642
|
/**
|
|
994
643
|
* @template P
|
|
995
644
|
* @template R
|
|
@@ -997,101 +646,83 @@ class PatternMatcher {
|
|
|
997
646
|
* @param {(o:NoInfer<Unwrap<ReadSchema<P>>>,s:State)=>R} handler
|
|
998
647
|
* @return {PatternMatcher<State,Patterns|Pattern<Unwrap<ReadSchema<P>>,R>>}
|
|
999
648
|
*/
|
|
1000
|
-
if
|
|
1001
|
-
// @ts-ignore
|
|
649
|
+
if(pattern, handler) {
|
|
1002
650
|
this.patterns.push({ if: $(pattern), h: handler });
|
|
1003
|
-
|
|
1004
|
-
return this
|
|
651
|
+
return this;
|
|
1005
652
|
}
|
|
1006
|
-
|
|
1007
653
|
/**
|
|
1008
654
|
* @template R
|
|
1009
655
|
* @param {(o:any,s:State)=>R} h
|
|
1010
656
|
*/
|
|
1011
|
-
else
|
|
1012
|
-
return this.if($any, h)
|
|
657
|
+
else(h) {
|
|
658
|
+
return this.if($any, h);
|
|
1013
659
|
}
|
|
1014
|
-
|
|
1015
660
|
/**
|
|
1016
661
|
* @return {State extends undefined
|
|
1017
662
|
* ? <In extends Unwrap<Patterns['if']>>(o:In,state?:undefined)=>PatternMatchResult<Patterns,In>
|
|
1018
663
|
* : <In extends Unwrap<Patterns['if']>>(o:In,state:State)=>PatternMatchResult<Patterns,In>}
|
|
1019
664
|
*/
|
|
1020
|
-
done
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
665
|
+
done() {
|
|
666
|
+
return (
|
|
667
|
+
/** @type {any} */
|
|
668
|
+
(o, s) => {
|
|
669
|
+
for (let i = 0; i < this.patterns.length; i++) {
|
|
670
|
+
const p = this.patterns[i];
|
|
671
|
+
if (p.if.check(o)) {
|
|
672
|
+
return p.h(o, s);
|
|
673
|
+
}
|
|
1028
674
|
}
|
|
675
|
+
throw error.create("Unhandled pattern");
|
|
1029
676
|
}
|
|
1030
|
-
|
|
1031
|
-
}
|
|
677
|
+
);
|
|
1032
678
|
}
|
|
1033
679
|
}
|
|
1034
|
-
|
|
1035
|
-
/**
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
*/
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
*
|
|
1045
|
-
* @type {<T>(o:T,gen:prng.PRNG)=>T}
|
|
1046
|
-
*/
|
|
1047
|
-
const _random = /** @type {any} */ (match(/** @type {Schema<prng.PRNG>} */ ($any))
|
|
1048
|
-
.if($$number, (_o, gen) => prng.int53(gen, number.MIN_SAFE_INTEGER, number.MAX_SAFE_INTEGER))
|
|
1049
|
-
.if($$string, (_o, gen) => prng.word(gen))
|
|
1050
|
-
.if($$boolean, (_o, gen) => prng.bool(gen))
|
|
1051
|
-
.if($$bigint, (_o, gen) => BigInt(prng.int53(gen, number.MIN_SAFE_INTEGER, number.MAX_SAFE_INTEGER)))
|
|
1052
|
-
.if($$union, (o, gen) => random(gen, prng.oneOf(gen, o.shape)))
|
|
1053
|
-
.if($$object, (o, gen) => {
|
|
1054
|
-
/**
|
|
1055
|
-
* @type {any}
|
|
1056
|
-
*/
|
|
680
|
+
const match = (state) => new PatternMatcher(
|
|
681
|
+
/** @type {any} */
|
|
682
|
+
state
|
|
683
|
+
);
|
|
684
|
+
const _random = (
|
|
685
|
+
/** @type {any} */
|
|
686
|
+
match(
|
|
687
|
+
/** @type {Schema<prng.PRNG>} */
|
|
688
|
+
$any
|
|
689
|
+
).if($$number, (_o, gen) => prng.int53(gen, number.MIN_SAFE_INTEGER, number.MAX_SAFE_INTEGER)).if($$string, (_o, gen) => prng.word(gen)).if($$boolean, (_o, gen) => prng.bool(gen)).if($$bigint, (_o, gen) => BigInt(prng.int53(gen, number.MIN_SAFE_INTEGER, number.MAX_SAFE_INTEGER))).if($$union, (o, gen) => random(gen, prng.oneOf(gen, o.shape))).if($$object, (o, gen) => {
|
|
1057
690
|
const res = {};
|
|
1058
691
|
for (const k in o.shape) {
|
|
1059
692
|
let prop = o.shape[k];
|
|
1060
693
|
if ($$optional.check(prop)) {
|
|
1061
|
-
if (prng.bool(gen)) {
|
|
694
|
+
if (prng.bool(gen)) {
|
|
695
|
+
continue;
|
|
696
|
+
}
|
|
1062
697
|
prop = prop.shape;
|
|
1063
698
|
}
|
|
1064
699
|
res[k] = _random(prop, gen);
|
|
1065
700
|
}
|
|
1066
|
-
return res
|
|
1067
|
-
})
|
|
1068
|
-
.if($$array, (o, gen) => {
|
|
701
|
+
return res;
|
|
702
|
+
}).if($$array, (o, gen) => {
|
|
1069
703
|
const arr = [];
|
|
1070
704
|
const n = prng.int32(gen, 0, 42);
|
|
1071
705
|
for (let i = 0; i < n; i++) {
|
|
1072
706
|
arr.push(random(gen, o.shape));
|
|
1073
707
|
}
|
|
1074
|
-
return arr
|
|
1075
|
-
})
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
.if($$
|
|
1080
|
-
return null
|
|
1081
|
-
})
|
|
1082
|
-
.if($$lambda, (o, gen) => {
|
|
708
|
+
return arr;
|
|
709
|
+
}).if($$literal, (o, gen) => {
|
|
710
|
+
return prng.oneOf(gen, o.shape);
|
|
711
|
+
}).if($$null, (o, gen) => {
|
|
712
|
+
return null;
|
|
713
|
+
}).if($$lambda, (o, gen) => {
|
|
1083
714
|
const res = random(gen, o.res);
|
|
1084
|
-
return () => res
|
|
1085
|
-
})
|
|
1086
|
-
|
|
1087
|
-
$
|
|
715
|
+
return () => res;
|
|
716
|
+
}).if($$any, (o, gen) => random(gen, prng.oneOf(gen, [
|
|
717
|
+
$number,
|
|
718
|
+
$string,
|
|
719
|
+
$null,
|
|
720
|
+
$undefined,
|
|
721
|
+
$bigint,
|
|
722
|
+
$boolean,
|
|
1088
723
|
$array($number),
|
|
1089
|
-
$record($union(
|
|
1090
|
-
])))
|
|
1091
|
-
.if($$record, (o, gen) => {
|
|
1092
|
-
/**
|
|
1093
|
-
* @type {any}
|
|
1094
|
-
*/
|
|
724
|
+
$record($union("a", "b", "c"), $number)
|
|
725
|
+
]))).if($$record, (o, gen) => {
|
|
1095
726
|
const res = {};
|
|
1096
727
|
const keysN = prng.int53(gen, 0, 3);
|
|
1097
728
|
for (let i = 0; i < keysN; i++) {
|
|
@@ -1099,18 +730,13 @@ const _random = /** @type {any} */ (match(/** @type {Schema<prng.PRNG>} */ ($any
|
|
|
1099
730
|
const val = random(gen, o.shape.values);
|
|
1100
731
|
res[key] = val;
|
|
1101
732
|
}
|
|
1102
|
-
return res
|
|
1103
|
-
})
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
/**
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
* @param {S} schema
|
|
1110
|
-
* @return {Unwrap<ReadSchema<S>>}
|
|
1111
|
-
*/
|
|
1112
|
-
const random = (gen, schema) => /** @type {any} */ (_random($(schema), gen));
|
|
1113
|
-
|
|
733
|
+
return res;
|
|
734
|
+
}).done()
|
|
735
|
+
);
|
|
736
|
+
const random = (gen, schema) => (
|
|
737
|
+
/** @type {any} */
|
|
738
|
+
_random($(schema), gen)
|
|
739
|
+
);
|
|
1114
740
|
exports.$ = $;
|
|
1115
741
|
exports.$$any = $$any;
|
|
1116
742
|
exports.$$array = $$array;
|