@es-joy/jsoe 0.16.0 → 0.18.0
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/.ncurc.cjs +4 -5
- package/CHANGES.md +12 -0
- package/README.md +27 -3
- package/badges/licenses-badge.svg +1 -0
- package/dist/formatAndTypeChoices.d.ts +39 -19
- package/dist/formatAndTypeChoices.d.ts.map +1 -1
- package/dist/formats/indexedDBKey.d.ts +1 -1
- package/dist/formats/indexedDBKey.d.ts.map +1 -1
- package/dist/formats/json.d.ts +1 -1
- package/dist/formats/json.d.ts.map +1 -1
- package/dist/formats/schema.d.ts +11 -0
- package/dist/formats/schema.d.ts.map +1 -0
- package/dist/formats/structuredCloning.d.ts +11 -13
- package/dist/formats/structuredCloning.d.ts.map +1 -1
- package/dist/formats.d.ts +86 -25
- package/dist/formats.d.ts.map +1 -1
- package/dist/fundamentalTypes/BooleanObjectType.d.ts +1 -1
- package/dist/fundamentalTypes/BooleanObjectType.d.ts.map +1 -1
- package/dist/fundamentalTypes/NumberObjectType.d.ts +1 -1
- package/dist/fundamentalTypes/NumberObjectType.d.ts.map +1 -1
- package/dist/fundamentalTypes/StringObjectType.d.ts +1 -1
- package/dist/fundamentalTypes/StringObjectType.d.ts.map +1 -1
- package/dist/fundamentalTypes/arrayReferenceType.d.ts +1 -1
- package/dist/fundamentalTypes/arrayType.d.ts +3 -3
- package/dist/fundamentalTypes/arrayType.d.ts.map +1 -1
- package/dist/fundamentalTypes/bigintObjectType.d.ts +6 -0
- package/dist/fundamentalTypes/bigintObjectType.d.ts.map +1 -0
- package/dist/fundamentalTypes/bigintType.d.ts +1 -1
- package/dist/fundamentalTypes/bigintType.d.ts.map +1 -1
- package/dist/fundamentalTypes/blobType.d.ts +1 -1
- package/dist/fundamentalTypes/blobType.d.ts.map +1 -1
- package/dist/fundamentalTypes/booleanType.d.ts +8 -0
- package/dist/fundamentalTypes/booleanType.d.ts.map +1 -0
- package/dist/fundamentalTypes/catchType.d.ts +6 -0
- package/dist/fundamentalTypes/catchType.d.ts.map +1 -0
- package/dist/fundamentalTypes/dateType.d.ts +4 -4
- package/dist/fundamentalTypes/dateType.d.ts.map +1 -1
- package/dist/fundamentalTypes/domexceptionType.d.ts +1 -1
- package/dist/fundamentalTypes/domexceptionType.d.ts.map +1 -1
- package/dist/fundamentalTypes/enumType.d.ts +6 -0
- package/dist/fundamentalTypes/enumType.d.ts.map +1 -0
- package/dist/fundamentalTypes/errorType.d.ts +1 -1
- package/dist/fundamentalTypes/errorType.d.ts.map +1 -1
- package/dist/fundamentalTypes/fileType.d.ts +1 -1
- package/dist/fundamentalTypes/fileType.d.ts.map +1 -1
- package/dist/fundamentalTypes/filelistType.d.ts +1 -1
- package/dist/fundamentalTypes/filelistType.d.ts.map +1 -1
- package/dist/fundamentalTypes/functionType.d.ts +6 -0
- package/dist/fundamentalTypes/functionType.d.ts.map +1 -0
- package/dist/fundamentalTypes/literalType.d.ts +6 -0
- package/dist/fundamentalTypes/literalType.d.ts.map +1 -0
- package/dist/fundamentalTypes/mapType.d.ts +1 -1
- package/dist/fundamentalTypes/mapType.d.ts.map +1 -1
- package/dist/fundamentalTypes/nanType.d.ts +6 -0
- package/dist/fundamentalTypes/nanType.d.ts.map +1 -0
- package/dist/fundamentalTypes/nativeEnumType.d.ts +6 -0
- package/dist/fundamentalTypes/nativeEnumType.d.ts.map +1 -0
- package/dist/fundamentalTypes/neverType.d.ts +6 -0
- package/dist/fundamentalTypes/neverType.d.ts.map +1 -0
- package/dist/fundamentalTypes/noneditableType.d.ts +1 -1
- package/dist/fundamentalTypes/noneditableType.d.ts.map +1 -1
- package/dist/fundamentalTypes/nullType.d.ts +1 -1
- package/dist/fundamentalTypes/nullType.d.ts.map +1 -1
- package/dist/fundamentalTypes/numberType.d.ts +1 -1
- package/dist/fundamentalTypes/numberType.d.ts.map +1 -1
- package/dist/fundamentalTypes/objectReferenceType.d.ts +1 -1
- package/dist/fundamentalTypes/objectType.d.ts +1 -1
- package/dist/fundamentalTypes/objectType.d.ts.map +1 -1
- package/dist/fundamentalTypes/promiseType.d.ts +6 -0
- package/dist/fundamentalTypes/promiseType.d.ts.map +1 -0
- package/dist/fundamentalTypes/recordType.d.ts +6 -0
- package/dist/fundamentalTypes/recordType.d.ts.map +1 -0
- package/dist/fundamentalTypes/regexpType.d.ts +1 -1
- package/dist/fundamentalTypes/regexpType.d.ts.map +1 -1
- package/dist/fundamentalTypes/setType.d.ts +1 -1
- package/dist/fundamentalTypes/setType.d.ts.map +1 -1
- package/dist/fundamentalTypes/sparseUndefinedType.d.ts +1 -1
- package/dist/fundamentalTypes/sparseUndefinedType.d.ts.map +1 -1
- package/dist/fundamentalTypes/stringType.d.ts +1 -1
- package/dist/fundamentalTypes/stringType.d.ts.map +1 -1
- package/dist/fundamentalTypes/symbolType.d.ts +8 -0
- package/dist/fundamentalTypes/symbolType.d.ts.map +1 -0
- package/dist/fundamentalTypes/tupleType.d.ts +6 -0
- package/dist/fundamentalTypes/tupleType.d.ts.map +1 -0
- package/dist/fundamentalTypes/undefinedType.d.ts +1 -1
- package/dist/fundamentalTypes/undefinedType.d.ts.map +1 -1
- package/dist/fundamentalTypes/voidType.d.ts +6 -0
- package/dist/fundamentalTypes/voidType.d.ts.map +1 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9663 -1586
- package/dist/subTypes/blobHTMLType.d.ts.map +1 -1
- package/dist/subTypes/falseType.d.ts +1 -1
- package/dist/subTypes/trueType.d.ts +1 -1
- package/dist/superTypes/SpecialNumberType.d.ts +1 -1
- package/dist/superTypes/SpecialRealNumberType.d.ts +1 -1
- package/dist/superTypes/SpecialRealNumberType.d.ts.map +1 -1
- package/dist/superTypes/buffersourceType.d.ts +1 -1
- package/dist/superTypes/buffersourceType.d.ts.map +1 -1
- package/dist/superTypes/dommatrixType.d.ts +1 -1
- package/dist/superTypes/dommatrixType.d.ts.map +1 -1
- package/dist/superTypes/dompointType.d.ts +1 -1
- package/dist/superTypes/dompointType.d.ts.map +1 -1
- package/dist/superTypes/domrectType.d.ts +1 -1
- package/dist/superTypes/domrectType.d.ts.map +1 -1
- package/dist/superTypes/errorsSpecialType.d.ts +1 -1
- package/dist/superTypes/errorsSpecialType.d.ts.map +1 -1
- package/dist/typeChoices.d.ts +33 -16
- package/dist/typeChoices.d.ts.map +1 -1
- package/dist/types.d.ts +161 -157
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/dialogs.d.ts +3 -3
- package/dist/utils/dialogs.d.ts.map +1 -1
- package/dist/utils/jsonPointer.d.ts.map +1 -1
- package/dist/utils/media.d.ts.map +1 -1
- package/dist/utils/objects.d.ts +16 -0
- package/dist/utils/objects.d.ts.map +1 -0
- package/dist/utils/types.d.ts +1 -1
- package/dist/utils/types.d.ts.map +1 -1
- package/dist/vendor-imports.d.ts +1 -1
- package/eslint.config.js +74 -0
- package/licenseInfo.json +4 -0
- package/package.json +33 -42
- package/rollup.config.js +15 -0
- package/src/formatAndTypeChoices.js +71 -54
- package/src/formats/indexedDBKey.js +9 -4
- package/src/formats/json.js +7 -4
- package/src/formats/schema.js +772 -0
- package/src/formats/structuredCloning.js +187 -86
- package/src/formats.js +61 -26
- package/src/fundamentalTypes/BooleanObjectType.js +18 -6
- package/src/fundamentalTypes/NumberObjectType.js +18 -5
- package/src/fundamentalTypes/StringObjectType.js +20 -8
- package/src/fundamentalTypes/arrayType.js +1241 -533
- package/src/fundamentalTypes/bigintObjectType.js +64 -0
- package/src/fundamentalTypes/bigintType.js +31 -5
- package/src/fundamentalTypes/blobType.js +201 -190
- package/src/fundamentalTypes/booleanType.js +63 -0
- package/src/fundamentalTypes/catchType.js +140 -0
- package/src/fundamentalTypes/dateType.js +57 -19
- package/src/fundamentalTypes/domexceptionType.js +61 -63
- package/src/fundamentalTypes/enumType.js +52 -0
- package/src/fundamentalTypes/errorType.js +20 -7
- package/src/fundamentalTypes/fileType.js +195 -189
- package/src/fundamentalTypes/filelistType.js +4 -0
- package/src/fundamentalTypes/functionType.js +150 -0
- package/src/fundamentalTypes/literalType.js +81 -0
- package/src/fundamentalTypes/mapType.js +4 -0
- package/src/fundamentalTypes/nanType.js +41 -0
- package/src/fundamentalTypes/nativeEnumType.js +155 -0
- package/src/fundamentalTypes/neverType.js +44 -0
- package/src/fundamentalTypes/noneditableType.js +19 -5
- package/src/fundamentalTypes/nullType.js +16 -6
- package/src/fundamentalTypes/numberType.js +49 -6
- package/src/fundamentalTypes/objectType.js +10 -2
- package/src/fundamentalTypes/promiseType.js +60 -0
- package/src/fundamentalTypes/recordType.js +42 -0
- package/src/fundamentalTypes/regexpType.js +51 -33
- package/src/fundamentalTypes/setType.js +4 -0
- package/src/fundamentalTypes/sparseUndefinedType.js +3 -0
- package/src/fundamentalTypes/stringType.js +254 -6
- package/src/fundamentalTypes/symbolType.js +81 -0
- package/src/fundamentalTypes/tupleType.js +39 -0
- package/src/fundamentalTypes/undefinedType.js +14 -4
- package/src/fundamentalTypes/voidType.js +48 -0
- package/src/jsoe.css +37 -3
- package/src/subTypes/blobHTMLType.js +33 -24
- package/src/superTypes/SpecialRealNumberType.js +22 -6
- package/src/superTypes/buffersourceType.js +86 -30
- package/src/superTypes/dommatrixType.js +24 -7
- package/src/superTypes/dompointType.js +27 -7
- package/src/superTypes/domrectType.js +33 -7
- package/src/superTypes/errorsSpecialType.js +30 -14
- package/src/typeChoices.js +115 -44
- package/src/types.js +129 -45
- package/src/utils/FileList.js +3 -3
- package/src/utils/dialogs.js +6 -8
- package/src/utils/objects.js +22 -0
- package/src/vendor-imports.js +1 -1
- package/tsconfig.json +2 -2
- package/vendor/jamilih/dist/jml-es.js +23 -6
- package/vendor/typeson-registry/dist/index.js +77 -47
- package/vendor/zod/lib/index.mjs +4236 -0
- package/vendor/zodex/dist/esm/dezerialize.d.ts +26 -0
- package/vendor/zodex/dist/esm/dezerialize.js +393 -0
- package/vendor/zodex/dist/esm/index.d.ts +9 -0
- package/vendor/zodex/dist/esm/index.js +4 -0
- package/vendor/zodex/dist/esm/infer.d.ts +29 -0
- package/vendor/zodex/dist/esm/infer.js +1 -0
- package/vendor/zodex/dist/esm/package.json +1 -0
- package/vendor/zodex/dist/esm/types.d.ts +201 -0
- package/vendor/zodex/dist/esm/types.js +13 -0
- package/vendor/zodex/dist/esm/ui.d.ts +23 -0
- package/vendor/zodex/dist/esm/ui.js +10 -0
- package/vendor/zodex/dist/esm/zerialize.d.ts +50 -0
- package/vendor/zodex/dist/esm/zerialize.js +417 -0
- package/vendor/zodex/dist/esm/zod-types.d.ts +11 -0
- package/vendor/zodex/dist/esm/zod-types.js +1 -0
- package/.eslintignore +0 -13
- package/.eslintrc.cjs +0 -145
- package/.nyc_output/out.json +0 -117370
- package/cypress.config.ts +0 -15
- package/demo/index-instrumented.html +0 -29
- package/demo/index-instrumented.js +0 -644
- package/demo/index.html +0 -29
- package/demo/index.js +0 -215
- package/src/formats/schemaAndArbitrary.js +0 -20
- package/src/formats/schemaOnly.js +0 -539
|
@@ -0,0 +1,772 @@
|
|
|
1
|
+
// import {z} from 'zod';
|
|
2
|
+
import {dezerialize} from 'zodex';
|
|
3
|
+
|
|
4
|
+
import structuredCloning from './structuredCloning.js';
|
|
5
|
+
|
|
6
|
+
import {resolveJSONPointer} from '../utils/jsonPointer.js';
|
|
7
|
+
import {copyObject} from '../utils/objects.js';
|
|
8
|
+
|
|
9
|
+
/* eslint-disable jsdoc/valid-types -- https://github.com/jsdoc-type-pratt-parser/jsdoc-type-pratt-parser/issues/147 */
|
|
10
|
+
/**
|
|
11
|
+
* @typedef {T[keyof T]} ValueOf<T>
|
|
12
|
+
* @template T
|
|
13
|
+
*/
|
|
14
|
+
/* eslint-enable jsdoc/valid-types -- https://github.com/jsdoc-type-pratt-parser/jsdoc-type-pratt-parser/issues/147 */
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @typedef {ValueOf<
|
|
18
|
+
* Pick<import('zodex').SzType, "type">
|
|
19
|
+
* >} AvailableZodexType
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
/** @type {Map<AvailableZodexType, import('../types.js').AvailableType>} */
|
|
23
|
+
const zodexToStructuredCloningTypeMap = new Map([
|
|
24
|
+
['boolean', 'boolean'],
|
|
25
|
+
['number', 'number'],
|
|
26
|
+
['nan', 'nan'],
|
|
27
|
+
['bigInt', 'bigint'],
|
|
28
|
+
['string', 'string'],
|
|
29
|
+
['date', 'date'],
|
|
30
|
+
['undefined', 'undef'],
|
|
31
|
+
['void', 'void'],
|
|
32
|
+
['null', 'null'],
|
|
33
|
+
|
|
34
|
+
// ['array', 'array'],
|
|
35
|
+
['array', 'arrayNonindexKeys'],
|
|
36
|
+
|
|
37
|
+
['object', 'object'],
|
|
38
|
+
['enum', 'enum'],
|
|
39
|
+
['literal', 'literal'],
|
|
40
|
+
|
|
41
|
+
['tuple', 'tuple'],
|
|
42
|
+
['record', 'record'],
|
|
43
|
+
['map', 'map'],
|
|
44
|
+
['set', 'set'],
|
|
45
|
+
|
|
46
|
+
['symbol', 'symbol'],
|
|
47
|
+
|
|
48
|
+
['never', 'never'],
|
|
49
|
+
|
|
50
|
+
// Todo: Allow non-cloning version to return these too, but filter out
|
|
51
|
+
// otherwise
|
|
52
|
+
['function', 'function'],
|
|
53
|
+
['promise', 'promise'],
|
|
54
|
+
|
|
55
|
+
['catch', 'catch'],
|
|
56
|
+
['nativeEnum', 'nativeEnum']
|
|
57
|
+
]);
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* @typedef {import('zodex').SzType} ZodexSchema
|
|
61
|
+
*/
|
|
62
|
+
/**
|
|
63
|
+
* @typedef {import('../utils/objects.js').NestedObject} NestedObject
|
|
64
|
+
*/
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* @param {ZodexSchema} leftItem
|
|
68
|
+
* @param {ZodexSchema} rightItem
|
|
69
|
+
* @throws {Error}
|
|
70
|
+
* @returns {ZodexSchema}
|
|
71
|
+
*/
|
|
72
|
+
function mergeSchema (leftItem, rightItem) {
|
|
73
|
+
if (leftItem.type !== 'object') {
|
|
74
|
+
console.log('leftItem', leftItem);
|
|
75
|
+
throw new Error('Unexpected leftItem of type ' + leftItem.type);
|
|
76
|
+
}
|
|
77
|
+
if (rightItem.type !== 'object') {
|
|
78
|
+
console.log('rightItem', rightItem);
|
|
79
|
+
throw new Error('Unexpected rightItem of type ' + rightItem.type);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const newLeftObj = copyObject(leftItem);
|
|
83
|
+
|
|
84
|
+
for (const [prop, val] of Object.entries(rightItem)) {
|
|
85
|
+
if (prop !== 'type' && prop !== 'properties') {
|
|
86
|
+
if (prop === 'description') {
|
|
87
|
+
if (val !== 'Modifiers') { // A bit cleaner
|
|
88
|
+
newLeftObj[prop] = newLeftObj[prop]
|
|
89
|
+
? newLeftObj[prop] + ' and ' + val
|
|
90
|
+
: val;
|
|
91
|
+
}
|
|
92
|
+
} else { // catchall, unknownKeys
|
|
93
|
+
if (prop in newLeftObj) {
|
|
94
|
+
throw new Error(
|
|
95
|
+
'Duplicate property ' + prop + ' of value ' +
|
|
96
|
+
JSON.stringify(val) + ' and ' +
|
|
97
|
+
JSON.stringify(newLeftObj[prop])
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
newLeftObj[prop] = val && typeof val === 'object'
|
|
102
|
+
? copyObject(val)
|
|
103
|
+
: val;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
for (const [prop, val] of Object.entries(rightItem.properties)) {
|
|
109
|
+
if (typeof newLeftObj.properties !== 'string' &&
|
|
110
|
+
prop in newLeftObj.properties) {
|
|
111
|
+
throw new Error(
|
|
112
|
+
'Duplicate property ' + prop + ' of value ' +
|
|
113
|
+
JSON.stringify(val) + ' and ' +
|
|
114
|
+
JSON.stringify(newLeftObj.properties[prop])
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
/** @type {NestedObject} */ (
|
|
118
|
+
newLeftObj.properties
|
|
119
|
+
)[prop] = val && typeof val === 'object'
|
|
120
|
+
? copyObject(val)
|
|
121
|
+
: val;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return newLeftObj;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* @param {Set<ZodexSchema>} left
|
|
129
|
+
* @param {Set<ZodexSchema>} right
|
|
130
|
+
* @returns {ZodexSchema[]}
|
|
131
|
+
*/
|
|
132
|
+
function flattenIntersection (left, right) {
|
|
133
|
+
const leftArray = [...left];
|
|
134
|
+
const rightArray = [...right];
|
|
135
|
+
|
|
136
|
+
const items = [];
|
|
137
|
+
for (const leftItem of leftArray) {
|
|
138
|
+
for (const rightItem of rightArray) {
|
|
139
|
+
items.push(mergeSchema(leftItem, rightItem));
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return items;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
let unionGroupID = 0;
|
|
147
|
+
/**
|
|
148
|
+
* @param {ZodexSchema} schemaObject
|
|
149
|
+
* @param {(ZodexSchema & {
|
|
150
|
+
* $unionGroupID?: number, $defaultValue?: any, $readonlyParent?: any
|
|
151
|
+
* })[]} set
|
|
152
|
+
* @returns {void}
|
|
153
|
+
*/
|
|
154
|
+
function addModifiers (schemaObject, set) {
|
|
155
|
+
if ('defaultValue' in schemaObject) {
|
|
156
|
+
unionGroupID++;
|
|
157
|
+
for (const obj of set) {
|
|
158
|
+
// Todo: Validate that `defaultValue` is possible and allow for
|
|
159
|
+
// selection of the first schema to validate the `defaultValue`;
|
|
160
|
+
// also validate things like impossible max/min combos
|
|
161
|
+
obj.$unionGroupID = unionGroupID;
|
|
162
|
+
obj.$defaultValue = schemaObject.defaultValue;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (schemaObject.isNullable) {
|
|
167
|
+
set.push({
|
|
168
|
+
type: 'null'
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (schemaObject.isOptional) {
|
|
173
|
+
for (const obj of set) {
|
|
174
|
+
if (!('isOptional' in obj)) {
|
|
175
|
+
obj.isOptional = schemaObject.isOptional;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
if (schemaObject.readonly) {
|
|
180
|
+
for (const obj of set) {
|
|
181
|
+
if (!('readonly' in obj)) {
|
|
182
|
+
obj.$readonlyParent = schemaObject.readonly;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (schemaObject.description) {
|
|
188
|
+
for (const obj of set) {
|
|
189
|
+
obj.description = obj.description
|
|
190
|
+
? obj.description + ' and ' + schemaObject.description
|
|
191
|
+
: schemaObject.description;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* @param {ZodexSchema} schemaObject
|
|
198
|
+
* @param {ZodexSchema} originalJSON
|
|
199
|
+
* @returns {Set<ZodexSchema>}
|
|
200
|
+
*/
|
|
201
|
+
function getTypesForSchema (schemaObject, originalJSON) {
|
|
202
|
+
switch (schemaObject.type) {
|
|
203
|
+
case 'never':
|
|
204
|
+
return new Set();
|
|
205
|
+
case 'object': {
|
|
206
|
+
const set = new Set();
|
|
207
|
+
// const {properties} = schemaObject;
|
|
208
|
+
// if (
|
|
209
|
+
// 'type' in properties && properties.type.type === 'enum' &&
|
|
210
|
+
// properties.type.values.length === 1
|
|
211
|
+
// ) {
|
|
212
|
+
// set.add(schemaObject);
|
|
213
|
+
// set.add(properties.type.defaultValue);
|
|
214
|
+
// }
|
|
215
|
+
set.add(schemaObject);
|
|
216
|
+
return set;
|
|
217
|
+
}
|
|
218
|
+
case 'discriminatedUnion':
|
|
219
|
+
case 'union': {
|
|
220
|
+
/** @type {(ZodexSchema & {$discriminator?: string})[]} */
|
|
221
|
+
let set = [];
|
|
222
|
+
for (const option of schemaObject.options) {
|
|
223
|
+
set = [...set, ...getTypesForSchema(option, originalJSON)];
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (schemaObject.type === 'discriminatedUnion') {
|
|
227
|
+
for (const obj of set) {
|
|
228
|
+
// Todo: Use to confirm the object has the discriminator
|
|
229
|
+
obj.$discriminator = schemaObject.discriminator;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
addModifiers(schemaObject, set);
|
|
234
|
+
return new Set(set);
|
|
235
|
+
} case 'intersection': {
|
|
236
|
+
const left = getTypesForSchema(schemaObject.left, originalJSON);
|
|
237
|
+
const right = getTypesForSchema(schemaObject.right, originalJSON);
|
|
238
|
+
|
|
239
|
+
const set = flattenIntersection(left, right);
|
|
240
|
+
addModifiers(schemaObject, set);
|
|
241
|
+
return new Set(set);
|
|
242
|
+
} case 'any': case 'unknown':
|
|
243
|
+
return new Set([
|
|
244
|
+
{
|
|
245
|
+
type: 'boolean'
|
|
246
|
+
},
|
|
247
|
+
{
|
|
248
|
+
type: 'number'
|
|
249
|
+
},
|
|
250
|
+
{
|
|
251
|
+
type: 'nan'
|
|
252
|
+
},
|
|
253
|
+
{
|
|
254
|
+
type: 'bigInt'
|
|
255
|
+
},
|
|
256
|
+
{
|
|
257
|
+
type: 'string'
|
|
258
|
+
},
|
|
259
|
+
{
|
|
260
|
+
description: 'Email',
|
|
261
|
+
type: 'string',
|
|
262
|
+
kind: 'email'
|
|
263
|
+
},
|
|
264
|
+
{
|
|
265
|
+
description: 'URL',
|
|
266
|
+
type: 'string',
|
|
267
|
+
kind: 'url'
|
|
268
|
+
},
|
|
269
|
+
{
|
|
270
|
+
description: 'Date',
|
|
271
|
+
type: 'string',
|
|
272
|
+
kind: 'date'
|
|
273
|
+
},
|
|
274
|
+
{
|
|
275
|
+
type: 'date'
|
|
276
|
+
},
|
|
277
|
+
// {
|
|
278
|
+
// type: 'void'
|
|
279
|
+
// },
|
|
280
|
+
{
|
|
281
|
+
type: 'undefined'
|
|
282
|
+
},
|
|
283
|
+
{
|
|
284
|
+
type: 'null'
|
|
285
|
+
},
|
|
286
|
+
{
|
|
287
|
+
type: 'object',
|
|
288
|
+
properties: {},
|
|
289
|
+
unknownKeys: 'passthrough'
|
|
290
|
+
},
|
|
291
|
+
|
|
292
|
+
// Todo: support these types separately
|
|
293
|
+
// {
|
|
294
|
+
// type: 'symbol'
|
|
295
|
+
// },
|
|
296
|
+
// {
|
|
297
|
+
// type: 'promise',
|
|
298
|
+
// value: {
|
|
299
|
+
// type: 'any'
|
|
300
|
+
// }
|
|
301
|
+
// },
|
|
302
|
+
// {
|
|
303
|
+
// type: 'function',
|
|
304
|
+
// args: {
|
|
305
|
+
// type: 'tuple',
|
|
306
|
+
// items: [],
|
|
307
|
+
// rest: {
|
|
308
|
+
// type: 'any'
|
|
309
|
+
// }
|
|
310
|
+
// },
|
|
311
|
+
// returns: {
|
|
312
|
+
// type: 'any'
|
|
313
|
+
// }
|
|
314
|
+
// },
|
|
315
|
+
{
|
|
316
|
+
type: 'nativeEnum',
|
|
317
|
+
values: {
|
|
318
|
+
type: 'union',
|
|
319
|
+
options: [
|
|
320
|
+
{
|
|
321
|
+
description: 'Numeric',
|
|
322
|
+
type: 'record',
|
|
323
|
+
key: {
|
|
324
|
+
type: 'number'
|
|
325
|
+
},
|
|
326
|
+
value: {
|
|
327
|
+
type: 'string'
|
|
328
|
+
}
|
|
329
|
+
},
|
|
330
|
+
{
|
|
331
|
+
description: 'String',
|
|
332
|
+
type: 'record',
|
|
333
|
+
key: {
|
|
334
|
+
type: 'string'
|
|
335
|
+
},
|
|
336
|
+
value: {
|
|
337
|
+
type: 'union',
|
|
338
|
+
options: [
|
|
339
|
+
{
|
|
340
|
+
type: 'string'
|
|
341
|
+
},
|
|
342
|
+
{
|
|
343
|
+
type: 'number'
|
|
344
|
+
}
|
|
345
|
+
]
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
]
|
|
349
|
+
}
|
|
350
|
+
},
|
|
351
|
+
{
|
|
352
|
+
type: 'array',
|
|
353
|
+
element: {
|
|
354
|
+
type: 'any'
|
|
355
|
+
}
|
|
356
|
+
},
|
|
357
|
+
{
|
|
358
|
+
type: 'set',
|
|
359
|
+
value: {
|
|
360
|
+
type: 'any'
|
|
361
|
+
}
|
|
362
|
+
},
|
|
363
|
+
{
|
|
364
|
+
type: 'map',
|
|
365
|
+
key: {
|
|
366
|
+
type: 'any'
|
|
367
|
+
},
|
|
368
|
+
value: {
|
|
369
|
+
type: 'any'
|
|
370
|
+
}
|
|
371
|
+
},
|
|
372
|
+
{
|
|
373
|
+
type: 'never'
|
|
374
|
+
},
|
|
375
|
+
{
|
|
376
|
+
type: 'effect',
|
|
377
|
+
effects: [
|
|
378
|
+
{
|
|
379
|
+
name: 'regexp',
|
|
380
|
+
type: 'refinement'
|
|
381
|
+
}
|
|
382
|
+
],
|
|
383
|
+
inner: {type: 'any'}
|
|
384
|
+
},
|
|
385
|
+
{
|
|
386
|
+
type: 'effect',
|
|
387
|
+
effects: [
|
|
388
|
+
{
|
|
389
|
+
name: 'blob',
|
|
390
|
+
type: 'refinement'
|
|
391
|
+
}
|
|
392
|
+
],
|
|
393
|
+
inner: {type: 'any'}
|
|
394
|
+
},
|
|
395
|
+
{
|
|
396
|
+
type: 'effect',
|
|
397
|
+
effects: [
|
|
398
|
+
{
|
|
399
|
+
name: 'BooleanObject',
|
|
400
|
+
type: 'refinement'
|
|
401
|
+
}
|
|
402
|
+
],
|
|
403
|
+
inner: {type: 'any'}
|
|
404
|
+
},
|
|
405
|
+
{
|
|
406
|
+
type: 'effect',
|
|
407
|
+
effects: [
|
|
408
|
+
{
|
|
409
|
+
name: 'NumberObject',
|
|
410
|
+
type: 'refinement'
|
|
411
|
+
}
|
|
412
|
+
],
|
|
413
|
+
inner: {type: 'any'}
|
|
414
|
+
},
|
|
415
|
+
{
|
|
416
|
+
type: 'effect',
|
|
417
|
+
effects: [
|
|
418
|
+
{
|
|
419
|
+
name: 'StringObject',
|
|
420
|
+
type: 'refinement'
|
|
421
|
+
}
|
|
422
|
+
],
|
|
423
|
+
inner: {type: 'any'}
|
|
424
|
+
},
|
|
425
|
+
{
|
|
426
|
+
type: 'effect',
|
|
427
|
+
effects: [
|
|
428
|
+
{
|
|
429
|
+
name: 'SpecialRealNumber',
|
|
430
|
+
type: 'refinement'
|
|
431
|
+
}
|
|
432
|
+
],
|
|
433
|
+
inner: {type: 'any'}
|
|
434
|
+
},
|
|
435
|
+
{
|
|
436
|
+
type: 'effect',
|
|
437
|
+
effects: [
|
|
438
|
+
{
|
|
439
|
+
name: 'domexception',
|
|
440
|
+
type: 'refinement'
|
|
441
|
+
}
|
|
442
|
+
],
|
|
443
|
+
inner: {type: 'any'}
|
|
444
|
+
},
|
|
445
|
+
{
|
|
446
|
+
type: 'effect',
|
|
447
|
+
effects: [
|
|
448
|
+
{
|
|
449
|
+
name: 'error',
|
|
450
|
+
type: 'refinement'
|
|
451
|
+
}
|
|
452
|
+
],
|
|
453
|
+
inner: {type: 'any'}
|
|
454
|
+
},
|
|
455
|
+
{
|
|
456
|
+
type: 'effect',
|
|
457
|
+
effects: [
|
|
458
|
+
{
|
|
459
|
+
name: 'filelist',
|
|
460
|
+
type: 'refinement'
|
|
461
|
+
}
|
|
462
|
+
],
|
|
463
|
+
inner: {type: 'any'}
|
|
464
|
+
},
|
|
465
|
+
{
|
|
466
|
+
type: 'effect',
|
|
467
|
+
effects: [
|
|
468
|
+
{
|
|
469
|
+
name: 'file',
|
|
470
|
+
type: 'refinement'
|
|
471
|
+
}
|
|
472
|
+
],
|
|
473
|
+
inner: {type: 'any'}
|
|
474
|
+
},
|
|
475
|
+
{
|
|
476
|
+
type: 'effect',
|
|
477
|
+
effects: [
|
|
478
|
+
{
|
|
479
|
+
name: 'resurrectable', // noneditable
|
|
480
|
+
type: 'refinement'
|
|
481
|
+
}
|
|
482
|
+
],
|
|
483
|
+
inner: {type: 'any'}
|
|
484
|
+
},
|
|
485
|
+
{
|
|
486
|
+
type: 'effect',
|
|
487
|
+
effects: [
|
|
488
|
+
{
|
|
489
|
+
name: 'blobHTML',
|
|
490
|
+
type: 'refinement'
|
|
491
|
+
}
|
|
492
|
+
],
|
|
493
|
+
inner: {type: 'any'}
|
|
494
|
+
},
|
|
495
|
+
{
|
|
496
|
+
type: 'effect',
|
|
497
|
+
effects: [
|
|
498
|
+
{
|
|
499
|
+
name: 'buffersource',
|
|
500
|
+
type: 'refinement'
|
|
501
|
+
}
|
|
502
|
+
],
|
|
503
|
+
inner: {type: 'any'}
|
|
504
|
+
},
|
|
505
|
+
{
|
|
506
|
+
type: 'effect',
|
|
507
|
+
effects: [
|
|
508
|
+
{
|
|
509
|
+
name: 'dommatrix',
|
|
510
|
+
type: 'refinement'
|
|
511
|
+
}
|
|
512
|
+
],
|
|
513
|
+
inner: {type: 'any'}
|
|
514
|
+
},
|
|
515
|
+
{
|
|
516
|
+
type: 'effect',
|
|
517
|
+
effects: [
|
|
518
|
+
{
|
|
519
|
+
name: 'dompoint',
|
|
520
|
+
type: 'refinement'
|
|
521
|
+
}
|
|
522
|
+
],
|
|
523
|
+
inner: {type: 'any'}
|
|
524
|
+
},
|
|
525
|
+
{
|
|
526
|
+
type: 'effect',
|
|
527
|
+
effects: [
|
|
528
|
+
{
|
|
529
|
+
name: 'domrect',
|
|
530
|
+
type: 'refinement'
|
|
531
|
+
}
|
|
532
|
+
],
|
|
533
|
+
inner: {type: 'any'}
|
|
534
|
+
},
|
|
535
|
+
{
|
|
536
|
+
type: 'effect',
|
|
537
|
+
effects: [
|
|
538
|
+
{
|
|
539
|
+
name: 'errors',
|
|
540
|
+
type: 'refinement'
|
|
541
|
+
}
|
|
542
|
+
],
|
|
543
|
+
inner: {type: 'any'}
|
|
544
|
+
},
|
|
545
|
+
// Todo: Adapt into a widget to drag to point back to another object
|
|
546
|
+
{
|
|
547
|
+
description: 'JSON Reference',
|
|
548
|
+
type: 'object',
|
|
549
|
+
properties: {
|
|
550
|
+
$ref: {
|
|
551
|
+
type: 'string'
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
]);
|
|
556
|
+
default: {
|
|
557
|
+
if ('$ref' in schemaObject) {
|
|
558
|
+
const refObj = resolveJSONPointer({
|
|
559
|
+
obj: originalJSON,
|
|
560
|
+
path: /** @type {import('zodex').SzRef} */ (schemaObject).$ref
|
|
561
|
+
});
|
|
562
|
+
return getTypesForSchema(refObj, originalJSON);
|
|
563
|
+
}
|
|
564
|
+
return new Set([schemaObject]);
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
/** @type {import('../formats.js').Format} */
|
|
570
|
+
const schema = {
|
|
571
|
+
iterate (records, stateObj) {
|
|
572
|
+
// console.log('records', records, stateObj);
|
|
573
|
+
stateObj.format = 'schema';
|
|
574
|
+
return structuredCloning.iterate(records, stateObj);
|
|
575
|
+
},
|
|
576
|
+
|
|
577
|
+
convertFromTypeson (
|
|
578
|
+
typesonType, types, v, arrayOrObjectPropertyName, parentSchemaInfo, stateObj
|
|
579
|
+
) {
|
|
580
|
+
// eslint-disable-next-line prefer-const -- Convenient
|
|
581
|
+
let [parentSchema, parentSchemaIdx] = parentSchemaInfo ?? [];
|
|
582
|
+
if (!stateObj) {
|
|
583
|
+
throw new Error('State object expected for schema');
|
|
584
|
+
}
|
|
585
|
+
let currentSchema = stateObj.schemaContent;
|
|
586
|
+
let mustBeOptional = false;
|
|
587
|
+
// console.log('currentSchema', currentSchema);
|
|
588
|
+
// console.log(
|
|
589
|
+
// 'parentSchema', parentSchemaIdx, parentSchema, '::',
|
|
590
|
+
// arrayOrObjectPropertyName
|
|
591
|
+
// );
|
|
592
|
+
|
|
593
|
+
// We shouldn't have to reprocess intersections, etc., as this is our own
|
|
594
|
+
// union
|
|
595
|
+
if (typeof parentSchemaIdx === 'number' && parentSchema?.type === 'union') {
|
|
596
|
+
parentSchema = parentSchema.options[parentSchemaIdx];
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
switch (parentSchema?.type) {
|
|
600
|
+
case 'object':
|
|
601
|
+
currentSchema = /** @type {import('zodex').SzObject} */ (
|
|
602
|
+
parentSchema
|
|
603
|
+
).properties[
|
|
604
|
+
/** @type {string} */ (arrayOrObjectPropertyName)
|
|
605
|
+
];
|
|
606
|
+
if (!currentSchema) {
|
|
607
|
+
currentSchema = /** @type {import('zodex').SzObject} */ (
|
|
608
|
+
parentSchema
|
|
609
|
+
).catchall;
|
|
610
|
+
mustBeOptional = true;
|
|
611
|
+
}
|
|
612
|
+
break;
|
|
613
|
+
case 'array':
|
|
614
|
+
currentSchema = /** @type {import('zodex').SzArray} */ (
|
|
615
|
+
parentSchema
|
|
616
|
+
).element;
|
|
617
|
+
break;
|
|
618
|
+
case 'set':
|
|
619
|
+
currentSchema = /** @type {import('zodex').SzSet} */ (
|
|
620
|
+
parentSchema
|
|
621
|
+
).value;
|
|
622
|
+
break;
|
|
623
|
+
case 'effect':
|
|
624
|
+
currentSchema = /** @type {import('zodex').SzEffect} */ (
|
|
625
|
+
parentSchema
|
|
626
|
+
).inner;
|
|
627
|
+
break;
|
|
628
|
+
// eslint-disable-next-line sonarjs/no-duplicated-branches -- Maintenance
|
|
629
|
+
case 'promise':
|
|
630
|
+
currentSchema = /** @type {import('zodex').SzPromise} */ (
|
|
631
|
+
parentSchema
|
|
632
|
+
).value;
|
|
633
|
+
break;
|
|
634
|
+
case 'tuple':
|
|
635
|
+
currentSchema = /** @type {import('zodex').SzTuple} */ (
|
|
636
|
+
parentSchema
|
|
637
|
+
).items[Number(arrayOrObjectPropertyName)] ??
|
|
638
|
+
/** @type {import('zodex').SzTuple} */ (
|
|
639
|
+
parentSchema
|
|
640
|
+
).rest;
|
|
641
|
+
break;
|
|
642
|
+
// Todo:
|
|
643
|
+
// 'record': key, value
|
|
644
|
+
// 'map': key, value
|
|
645
|
+
// 'function': args, returns
|
|
646
|
+
default:
|
|
647
|
+
break;
|
|
648
|
+
}
|
|
649
|
+
if (!currentSchema) {
|
|
650
|
+
return {type: typesonType};
|
|
651
|
+
}
|
|
652
|
+
const schemaObjects = [...getTypesForSchema(
|
|
653
|
+
/** @type {import('zodex').SzType} */ (currentSchema),
|
|
654
|
+
/** @type {import('zodex').SzType} */ (currentSchema)
|
|
655
|
+
)];
|
|
656
|
+
// console.log(
|
|
657
|
+
// 'vvv', v, currentSchema,
|
|
658
|
+
// arrayOrObjectPropertyName, parentSchema, schemaObjects
|
|
659
|
+
// );
|
|
660
|
+
// console.log('schemaObjects', schemaObjects);
|
|
661
|
+
for (const [schemaIdx, schema] of schemaObjects.entries()) {
|
|
662
|
+
let unknownKeys;
|
|
663
|
+
if (schema.type === 'object') {
|
|
664
|
+
({unknownKeys} = schema);
|
|
665
|
+
// We don't want to eagerly match, e.g., if there are other objects
|
|
666
|
+
// which include the optional properties; this could cause a problem,
|
|
667
|
+
// however, if the tested object has extra non-standard properties
|
|
668
|
+
schema.unknownKeys = 'strict';
|
|
669
|
+
}
|
|
670
|
+
const dezSchema = dezerialize(schema);
|
|
671
|
+
const parsed = dezSchema.safeParse(v);
|
|
672
|
+
|
|
673
|
+
if (schema.type === 'object') {
|
|
674
|
+
schema.unknownKeys = unknownKeys;
|
|
675
|
+
}
|
|
676
|
+
// console.log('parsed', parsed.success, v, schema);
|
|
677
|
+
if (parsed.success) {
|
|
678
|
+
if (currentSchema.type === 'any' && schema.description) {
|
|
679
|
+
schema.description += ' (any)';
|
|
680
|
+
}
|
|
681
|
+
let type = zodexToStructuredCloningTypeMap.get(schema.type);
|
|
682
|
+
if (!type && schema.type === 'effect') {
|
|
683
|
+
type = /** @type {import('../types.js').AvailableType} */ (
|
|
684
|
+
schema.effects[0].name
|
|
685
|
+
);
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
const typeObject =
|
|
689
|
+
/** @type {Required<import('../types.js').TypeObject>} */ (
|
|
690
|
+
types.getTypeObject(
|
|
691
|
+
/** @type {import('../types.js').AvailableType} */ (type)
|
|
692
|
+
)
|
|
693
|
+
);
|
|
694
|
+
|
|
695
|
+
if (typeObject.valueMatch && typeObject.valueMatch(v)) {
|
|
696
|
+
// console.log(
|
|
697
|
+
// 'matched', v, v?.length, type, schema, schemaIdx, schemaObjects
|
|
698
|
+
// );
|
|
699
|
+
// console.log('ssss', !stateObj.rootUI ||
|
|
700
|
+
// (stateObj.readonly || schemaObjects.length === 1),
|
|
701
|
+
// schema, schemaObjects);
|
|
702
|
+
return {
|
|
703
|
+
type,
|
|
704
|
+
schemaIdx,
|
|
705
|
+
// For `readonly`, we just want to show the current type (no
|
|
706
|
+
// pull-down)
|
|
707
|
+
schema: !stateObj.rootUI ||
|
|
708
|
+
(stateObj.readonly || schemaObjects.length === 1)
|
|
709
|
+
? schema
|
|
710
|
+
: {
|
|
711
|
+
type: 'union',
|
|
712
|
+
options: schemaObjects
|
|
713
|
+
},
|
|
714
|
+
mustBeOptional
|
|
715
|
+
};
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
return {type: typesonType};
|
|
720
|
+
},
|
|
721
|
+
|
|
722
|
+
types () {
|
|
723
|
+
return structuredCloning.types();
|
|
724
|
+
},
|
|
725
|
+
|
|
726
|
+
getTypesAndSchemasForState (types, state, schemaObject, schemaOriginal) {
|
|
727
|
+
if (!schemaObject) {
|
|
728
|
+
throw new Error('Missing schema object');
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
// We don't care about the current schema, as these are inner types
|
|
732
|
+
if (state === 'array') {
|
|
733
|
+
return structuredCloning.getTypesAndSchemasForState(
|
|
734
|
+
types, state, schemaObject, schemaOriginal
|
|
735
|
+
);
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
// alert(JSON.stringify(schemaObject));
|
|
739
|
+
const schemaObjects = [...getTypesForSchema(
|
|
740
|
+
schemaObject,
|
|
741
|
+
/** @type {import('zodex').SzType} */ (schemaOriginal) ?? schemaObject
|
|
742
|
+
)];
|
|
743
|
+
|
|
744
|
+
// Note: Zod does not support array/object references
|
|
745
|
+
|
|
746
|
+
// Todo: implement schema restrictions like tuple on array, record on object
|
|
747
|
+
// Todo: Fix `iterate` for schemas (e.g., inject a value method in demo)
|
|
748
|
+
|
|
749
|
+
/** @type {AvailableZodexType[]} */
|
|
750
|
+
const typeArray = schemaObjects.map(({type}) => {
|
|
751
|
+
return type;
|
|
752
|
+
});
|
|
753
|
+
|
|
754
|
+
return {
|
|
755
|
+
schemaObjects,
|
|
756
|
+
types: typeArray.map((item, idx) => {
|
|
757
|
+
if (item === 'effect') {
|
|
758
|
+
return /** @type {import('../types.js').AvailableType} */ (
|
|
759
|
+
/** @type {import('zodex').SzEffect} */ (
|
|
760
|
+
schemaObjects[idx]
|
|
761
|
+
).effects[0].name
|
|
762
|
+
);
|
|
763
|
+
}
|
|
764
|
+
return /** @type {import('../types.js').AvailableType} */ (
|
|
765
|
+
zodexToStructuredCloningTypeMap.get(item)
|
|
766
|
+
);
|
|
767
|
+
})
|
|
768
|
+
};
|
|
769
|
+
}
|
|
770
|
+
};
|
|
771
|
+
|
|
772
|
+
export default schema;
|