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