@es-joy/jsoe 0.16.0 → 0.17.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.
Files changed (208) hide show
  1. package/.ncurc.cjs +4 -5
  2. package/CHANGES.md +4 -0
  3. package/README.md +27 -3
  4. package/badges/licenses-badge.svg +1 -0
  5. package/dist/formatAndTypeChoices.d.ts +39 -19
  6. package/dist/formatAndTypeChoices.d.ts.map +1 -1
  7. package/dist/formats/indexedDBKey.d.ts +1 -1
  8. package/dist/formats/indexedDBKey.d.ts.map +1 -1
  9. package/dist/formats/json.d.ts +1 -1
  10. package/dist/formats/json.d.ts.map +1 -1
  11. package/dist/formats/schema.d.ts +11 -0
  12. package/dist/formats/schema.d.ts.map +1 -0
  13. package/dist/formats/structuredCloning.d.ts +11 -13
  14. package/dist/formats/structuredCloning.d.ts.map +1 -1
  15. package/dist/formats.d.ts +86 -25
  16. package/dist/formats.d.ts.map +1 -1
  17. package/dist/fundamentalTypes/BooleanObjectType.d.ts +1 -1
  18. package/dist/fundamentalTypes/BooleanObjectType.d.ts.map +1 -1
  19. package/dist/fundamentalTypes/NumberObjectType.d.ts +1 -1
  20. package/dist/fundamentalTypes/NumberObjectType.d.ts.map +1 -1
  21. package/dist/fundamentalTypes/StringObjectType.d.ts +1 -1
  22. package/dist/fundamentalTypes/StringObjectType.d.ts.map +1 -1
  23. package/dist/fundamentalTypes/arrayReferenceType.d.ts +1 -1
  24. package/dist/fundamentalTypes/arrayType.d.ts +3 -3
  25. package/dist/fundamentalTypes/arrayType.d.ts.map +1 -1
  26. package/dist/fundamentalTypes/bigintObjectType.d.ts +6 -0
  27. package/dist/fundamentalTypes/bigintObjectType.d.ts.map +1 -0
  28. package/dist/fundamentalTypes/bigintType.d.ts +1 -1
  29. package/dist/fundamentalTypes/bigintType.d.ts.map +1 -1
  30. package/dist/fundamentalTypes/blobType.d.ts +1 -1
  31. package/dist/fundamentalTypes/blobType.d.ts.map +1 -1
  32. package/dist/fundamentalTypes/booleanType.d.ts +8 -0
  33. package/dist/fundamentalTypes/booleanType.d.ts.map +1 -0
  34. package/dist/fundamentalTypes/catchType.d.ts +6 -0
  35. package/dist/fundamentalTypes/catchType.d.ts.map +1 -0
  36. package/dist/fundamentalTypes/dateType.d.ts +4 -4
  37. package/dist/fundamentalTypes/dateType.d.ts.map +1 -1
  38. package/dist/fundamentalTypes/domexceptionType.d.ts +1 -1
  39. package/dist/fundamentalTypes/domexceptionType.d.ts.map +1 -1
  40. package/dist/fundamentalTypes/enumType.d.ts +6 -0
  41. package/dist/fundamentalTypes/enumType.d.ts.map +1 -0
  42. package/dist/fundamentalTypes/errorType.d.ts +1 -1
  43. package/dist/fundamentalTypes/errorType.d.ts.map +1 -1
  44. package/dist/fundamentalTypes/fileType.d.ts +1 -1
  45. package/dist/fundamentalTypes/fileType.d.ts.map +1 -1
  46. package/dist/fundamentalTypes/filelistType.d.ts +1 -1
  47. package/dist/fundamentalTypes/filelistType.d.ts.map +1 -1
  48. package/dist/fundamentalTypes/functionType.d.ts +6 -0
  49. package/dist/fundamentalTypes/functionType.d.ts.map +1 -0
  50. package/dist/fundamentalTypes/literalType.d.ts +6 -0
  51. package/dist/fundamentalTypes/literalType.d.ts.map +1 -0
  52. package/dist/fundamentalTypes/mapType.d.ts +1 -1
  53. package/dist/fundamentalTypes/mapType.d.ts.map +1 -1
  54. package/dist/fundamentalTypes/nanType.d.ts +6 -0
  55. package/dist/fundamentalTypes/nanType.d.ts.map +1 -0
  56. package/dist/fundamentalTypes/nativeEnumType.d.ts +6 -0
  57. package/dist/fundamentalTypes/nativeEnumType.d.ts.map +1 -0
  58. package/dist/fundamentalTypes/neverType.d.ts +6 -0
  59. package/dist/fundamentalTypes/neverType.d.ts.map +1 -0
  60. package/dist/fundamentalTypes/noneditableType.d.ts +1 -1
  61. package/dist/fundamentalTypes/noneditableType.d.ts.map +1 -1
  62. package/dist/fundamentalTypes/nullType.d.ts +1 -1
  63. package/dist/fundamentalTypes/nullType.d.ts.map +1 -1
  64. package/dist/fundamentalTypes/numberType.d.ts +1 -1
  65. package/dist/fundamentalTypes/numberType.d.ts.map +1 -1
  66. package/dist/fundamentalTypes/objectReferenceType.d.ts +1 -1
  67. package/dist/fundamentalTypes/objectType.d.ts +1 -1
  68. package/dist/fundamentalTypes/objectType.d.ts.map +1 -1
  69. package/dist/fundamentalTypes/promiseType.d.ts +6 -0
  70. package/dist/fundamentalTypes/promiseType.d.ts.map +1 -0
  71. package/dist/fundamentalTypes/recordType.d.ts +6 -0
  72. package/dist/fundamentalTypes/recordType.d.ts.map +1 -0
  73. package/dist/fundamentalTypes/regexpType.d.ts +1 -1
  74. package/dist/fundamentalTypes/regexpType.d.ts.map +1 -1
  75. package/dist/fundamentalTypes/setType.d.ts +1 -1
  76. package/dist/fundamentalTypes/setType.d.ts.map +1 -1
  77. package/dist/fundamentalTypes/sparseUndefinedType.d.ts +1 -1
  78. package/dist/fundamentalTypes/sparseUndefinedType.d.ts.map +1 -1
  79. package/dist/fundamentalTypes/stringType.d.ts +1 -1
  80. package/dist/fundamentalTypes/stringType.d.ts.map +1 -1
  81. package/dist/fundamentalTypes/symbolType.d.ts +8 -0
  82. package/dist/fundamentalTypes/symbolType.d.ts.map +1 -0
  83. package/dist/fundamentalTypes/tupleType.d.ts +6 -0
  84. package/dist/fundamentalTypes/tupleType.d.ts.map +1 -0
  85. package/dist/fundamentalTypes/undefinedType.d.ts +1 -1
  86. package/dist/fundamentalTypes/undefinedType.d.ts.map +1 -1
  87. package/dist/fundamentalTypes/voidType.d.ts +6 -0
  88. package/dist/fundamentalTypes/voidType.d.ts.map +1 -0
  89. package/dist/index.d.ts +2 -2
  90. package/dist/index.d.ts.map +1 -1
  91. package/dist/index.js +9660 -1586
  92. package/dist/subTypes/blobHTMLType.d.ts.map +1 -1
  93. package/dist/subTypes/falseType.d.ts +1 -1
  94. package/dist/subTypes/trueType.d.ts +1 -1
  95. package/dist/superTypes/SpecialNumberType.d.ts +1 -1
  96. package/dist/superTypes/SpecialRealNumberType.d.ts +1 -1
  97. package/dist/superTypes/SpecialRealNumberType.d.ts.map +1 -1
  98. package/dist/superTypes/buffersourceType.d.ts +1 -1
  99. package/dist/superTypes/buffersourceType.d.ts.map +1 -1
  100. package/dist/superTypes/dommatrixType.d.ts +1 -1
  101. package/dist/superTypes/dommatrixType.d.ts.map +1 -1
  102. package/dist/superTypes/dompointType.d.ts +1 -1
  103. package/dist/superTypes/dompointType.d.ts.map +1 -1
  104. package/dist/superTypes/domrectType.d.ts +1 -1
  105. package/dist/superTypes/domrectType.d.ts.map +1 -1
  106. package/dist/superTypes/errorsSpecialType.d.ts +1 -1
  107. package/dist/superTypes/errorsSpecialType.d.ts.map +1 -1
  108. package/dist/typeChoices.d.ts +33 -16
  109. package/dist/typeChoices.d.ts.map +1 -1
  110. package/dist/types.d.ts +161 -157
  111. package/dist/types.d.ts.map +1 -1
  112. package/dist/utils/dialogs.d.ts +3 -3
  113. package/dist/utils/dialogs.d.ts.map +1 -1
  114. package/dist/utils/jsonPointer.d.ts.map +1 -1
  115. package/dist/utils/media.d.ts.map +1 -1
  116. package/dist/utils/objects.d.ts +16 -0
  117. package/dist/utils/objects.d.ts.map +1 -0
  118. package/dist/utils/types.d.ts +1 -1
  119. package/dist/utils/types.d.ts.map +1 -1
  120. package/dist/vendor-imports.d.ts +1 -1
  121. package/eslint.config.js +74 -0
  122. package/licenseInfo.json +4 -0
  123. package/package.json +33 -42
  124. package/rollup.config.js +15 -0
  125. package/src/formatAndTypeChoices.js +68 -54
  126. package/src/formats/indexedDBKey.js +9 -4
  127. package/src/formats/json.js +7 -4
  128. package/src/formats/schema.js +772 -0
  129. package/src/formats/structuredCloning.js +187 -86
  130. package/src/formats.js +61 -26
  131. package/src/fundamentalTypes/BooleanObjectType.js +18 -6
  132. package/src/fundamentalTypes/NumberObjectType.js +18 -5
  133. package/src/fundamentalTypes/StringObjectType.js +20 -8
  134. package/src/fundamentalTypes/arrayType.js +1241 -533
  135. package/src/fundamentalTypes/bigintObjectType.js +64 -0
  136. package/src/fundamentalTypes/bigintType.js +31 -5
  137. package/src/fundamentalTypes/blobType.js +201 -190
  138. package/src/fundamentalTypes/booleanType.js +63 -0
  139. package/src/fundamentalTypes/catchType.js +140 -0
  140. package/src/fundamentalTypes/dateType.js +57 -19
  141. package/src/fundamentalTypes/domexceptionType.js +61 -63
  142. package/src/fundamentalTypes/enumType.js +52 -0
  143. package/src/fundamentalTypes/errorType.js +20 -7
  144. package/src/fundamentalTypes/fileType.js +195 -189
  145. package/src/fundamentalTypes/filelistType.js +4 -0
  146. package/src/fundamentalTypes/functionType.js +150 -0
  147. package/src/fundamentalTypes/literalType.js +81 -0
  148. package/src/fundamentalTypes/mapType.js +4 -0
  149. package/src/fundamentalTypes/nanType.js +41 -0
  150. package/src/fundamentalTypes/nativeEnumType.js +155 -0
  151. package/src/fundamentalTypes/neverType.js +44 -0
  152. package/src/fundamentalTypes/noneditableType.js +19 -5
  153. package/src/fundamentalTypes/nullType.js +16 -6
  154. package/src/fundamentalTypes/numberType.js +49 -6
  155. package/src/fundamentalTypes/objectType.js +10 -2
  156. package/src/fundamentalTypes/promiseType.js +60 -0
  157. package/src/fundamentalTypes/recordType.js +42 -0
  158. package/src/fundamentalTypes/regexpType.js +51 -33
  159. package/src/fundamentalTypes/setType.js +4 -0
  160. package/src/fundamentalTypes/sparseUndefinedType.js +3 -0
  161. package/src/fundamentalTypes/stringType.js +254 -6
  162. package/src/fundamentalTypes/symbolType.js +81 -0
  163. package/src/fundamentalTypes/tupleType.js +39 -0
  164. package/src/fundamentalTypes/undefinedType.js +14 -4
  165. package/src/fundamentalTypes/voidType.js +48 -0
  166. package/src/jsoe.css +37 -3
  167. package/src/subTypes/blobHTMLType.js +33 -24
  168. package/src/superTypes/SpecialRealNumberType.js +22 -6
  169. package/src/superTypes/buffersourceType.js +86 -30
  170. package/src/superTypes/dommatrixType.js +24 -7
  171. package/src/superTypes/dompointType.js +27 -7
  172. package/src/superTypes/domrectType.js +33 -7
  173. package/src/superTypes/errorsSpecialType.js +30 -14
  174. package/src/typeChoices.js +115 -44
  175. package/src/types.js +129 -45
  176. package/src/utils/FileList.js +3 -3
  177. package/src/utils/dialogs.js +6 -8
  178. package/src/utils/objects.js +22 -0
  179. package/src/vendor-imports.js +1 -1
  180. package/tsconfig.json +2 -2
  181. package/vendor/jamilih/dist/jml-es.js +23 -6
  182. package/vendor/typeson-registry/dist/index.js +77 -47
  183. package/vendor/zod/lib/index.mjs +4236 -0
  184. package/vendor/zodex/dist/esm/dezerialize.d.ts +26 -0
  185. package/vendor/zodex/dist/esm/dezerialize.js +393 -0
  186. package/vendor/zodex/dist/esm/index.d.ts +9 -0
  187. package/vendor/zodex/dist/esm/index.js +4 -0
  188. package/vendor/zodex/dist/esm/infer.d.ts +29 -0
  189. package/vendor/zodex/dist/esm/infer.js +1 -0
  190. package/vendor/zodex/dist/esm/package.json +1 -0
  191. package/vendor/zodex/dist/esm/types.d.ts +201 -0
  192. package/vendor/zodex/dist/esm/types.js +13 -0
  193. package/vendor/zodex/dist/esm/ui.d.ts +23 -0
  194. package/vendor/zodex/dist/esm/ui.js +10 -0
  195. package/vendor/zodex/dist/esm/zerialize.d.ts +50 -0
  196. package/vendor/zodex/dist/esm/zerialize.js +417 -0
  197. package/vendor/zodex/dist/esm/zod-types.d.ts +11 -0
  198. package/vendor/zodex/dist/esm/zod-types.js +1 -0
  199. package/.eslintignore +0 -13
  200. package/.eslintrc.cjs +0 -145
  201. package/.nyc_output/out.json +0 -117370
  202. package/cypress.config.ts +0 -15
  203. package/demo/index-instrumented.html +0 -29
  204. package/demo/index-instrumented.js +0 -644
  205. package/demo/index.html +0 -29
  206. package/demo/index.js +0 -215
  207. package/src/formats/schemaAndArbitrary.js +0 -20
  208. package/src/formats/schemaOnly.js +0 -539
@@ -1,539 +0,0 @@
1
- /* eslint-disable
2
- no-constant-condition,
3
- sonarjs/no-empty-collection,
4
- sonarjs/no-identical-conditions,
5
- no-constant-binary-expression -- Just debugging */
6
- import {getTypeForFormatStateAndValue} from '../formats.js';
7
- import * as structuredCloning from './structuredCloning.js';
8
- import deepEqual from '../deepEqual.js';
9
-
10
- /**
11
- * @typedef {import('./formats.js').StructuredCloneValue} StructuredCloneValue
12
- */
13
- /**
14
- * @type {import('./structuredCloning.js').FormatIterator}
15
- */
16
- export const iterate = (records, stateObj) => {
17
- console.log('records', records, stateObj);
18
- const {schemaContent: schema} = stateObj;
19
- if (!schema || typeof schema !== 'object') {
20
- throw new TypeError(`Malformed schema provided`);
21
- }
22
- stateObj.format = 'schemaOnly';
23
- /*
24
- Before labeling ANY (unless only one match), we need to
25
- confirm the whole is a match
26
- */
27
- stateObj[''] = schema;
28
-
29
- const possibleSchemaObjects = [];
30
- possibleSchemaObjects.map((possibleSchemaObject) => {
31
- return flattenSchemas({possibleSchemaObject});
32
- });
33
-
34
- const resolveRef = (/* {schema, possibleSchemaObject} */) => {
35
- // Todo?
36
- };
37
-
38
- /**
39
- * @param {object} cfg
40
- * @param {StructuredCloneValue[]} cfg.list
41
- * @param {StructuredCloneValue} cfg.value
42
- * @returns {boolean}
43
- */
44
- const valueInEnumList = ({list, value}) => {
45
- return list.some((en) => deepEqual(en, value));
46
- };
47
- const getTypesForValues = ({enumList}) => enumList.map(
48
- (value) => getTypeForFormatStateAndValue({
49
- value,
50
- format: 'structuredCloning',
51
- state: 'arrayNonindexKeys'
52
- // state: 'sparseArrays'
53
- })
54
- );
55
- /**
56
- * @typedef {PlainObject} SchemaObject
57
- * @property {string} type
58
- */
59
- /**
60
- * Avoids duplicates for same type but additive for `enum`
61
- * @param {Array<any>} anyOf
62
- * @returns {SchemaObject[]}
63
- */
64
- const reduceAnyOf = (anyOf) => {
65
- return anyOf.reduce((arr, schemaObj, _i) => {
66
- const earlierSchemaObject = arr.find((schemaObject) => {
67
- return schemaObject.type === schemaObj.type;
68
- });
69
- if (!earlierSchemaObject) {
70
- arr.push(schemaObj);
71
- } else if (earlierSchemaObject.enum) {
72
- schemaObj.enum.forEach((value) => {
73
- if (!valueInEnumList({
74
- value,
75
- list: earlierSchemaObject.enum
76
- })) {
77
- earlierSchemaObject.enum.push(value);
78
- }
79
- });
80
- } else if ('enum' in schemaObj) {
81
- earlierSchemaObject.enum = schemaObj.enum;
82
- }
83
- return arr;
84
- }, []);
85
- };
86
- const reduceEnumList = ({enumList}) => {
87
- const newEnumTypes = getTypesForValues({enumList});
88
- return reduceAnyOf(newEnumTypes.map((type, i) => {
89
- return {
90
- type,
91
- enum: enumList[i]
92
- };
93
- }));
94
- };
95
- const addEnumConstraint = ({schema, enumList}) => {
96
- if (!enumList.length) {
97
- throw new TypeError(`Not possible to match empty enum`);
98
- }
99
-
100
- enumList = reduceEnumList({enumList});
101
-
102
- // eslint-disable-next-line unicorn/prefer-ternary -- Refactoring
103
- if (!schema.enum) { // Enforce new constraint allowances
104
- schema.enum = enumList;
105
- } else { // Remove any old allowances not part of new ones
106
- schema.enum = schema.enum.filter((value) => {
107
- return valueInEnumList({
108
- value, list: enumList
109
- });
110
- });
111
- }
112
- };
113
- /**
114
- * Assumes confining further--not expanding allowances.
115
- * @param {PlainObject} args
116
- * @param {SchemaObject} args.schema
117
- * @param {Array<any>} args.enumList
118
- * @returns {undefined}
119
- */
120
- const addEnumAnyOfConstraint = ({schema, enumList}) => {
121
- if (!schema.anyOf.length) {
122
- // Reduction is necessary for duplicates within new items
123
- schema.anyOf = reduceEnumList({enumList});
124
- return;
125
- }
126
- const newEnumTypes = getTypesForValues({enumList});
127
- schema.anyOf = schema.anyOf.filter((subschema) => {
128
- const matchingNewEnumListItems = newEnumTypes.reduce(
129
- (arr, enumType, i) => {
130
- // Filter out different types
131
- if (enumType === subschema.type &&
132
- // Filter out enum values duplicated within new list
133
- !valueInEnumList({
134
- value: enumList[i],
135
- list: arr
136
- })
137
- ) {
138
- arr.push(enumList[i]);
139
- }
140
- return arr;
141
- },
142
- []
143
- );
144
- const hasMatchingType = matchingNewEnumListItems.length;
145
- if (hasMatchingType) {
146
- addEnumConstraint({
147
- schema: subschema, enumList: matchingNewEnumListItems
148
- });
149
- }
150
- return hasMatchingType;
151
- });
152
- };
153
- /**
154
- * Assumes confining further--not expanding allowances.
155
- * @param {PlainObject} args
156
- * @param {SchemaObject} args.schema
157
- * @param {Array<any>} args.newAnyOf
158
- * -- param {boolean} args.oneOf
159
- * @returns {undefined}
160
- */
161
- const addTypeConstraints = ({
162
- schema, newAnyOf // , oneOf = false
163
- }) => {
164
- newAnyOf = reduceAnyOf(newAnyOf);
165
- if (!schema.anyOf.length) {
166
- // Reduction is necessary for duplicates within new items
167
- schema.anyOf = newAnyOf;
168
- return;
169
- }
170
- schema.anyOf = schema.anyOf
171
- ? schema.anyOf.filter((preexistingSchema) => {
172
- const {type} = preexistingSchema;
173
- return newAnyOf.some((schemaObj) => {
174
- const addOrFilterEnumList = ({enumList}) => {
175
- // eslint-disable-next-line unicorn/prefer-ternary
176
- if (!preexistingSchema.enum) {
177
- preexistingSchema.enum = reduceEnumList({enumList});
178
- } else {
179
- preexistingSchema.enum = preexistingSchema.enum.filter(
180
- (value) => {
181
- return valueInEnumList({
182
- value, list: enumList
183
- });
184
- }
185
- );
186
- }
187
- };
188
- const {
189
- type: newSchemaObjectType,
190
- enum: enumList,
191
- const: cnst
192
- } = schemaObj;
193
- const hasEnumList = Boolean(enumList);
194
- const hasConst = 'const' in schemaObj;
195
- const hasMatchingEnumType = hasEnumList &&
196
- getTypesForValues({enumList}).includes(type);
197
- const hasMatchingConstType = hasConst &&
198
- getTypesForValues({enumList: [cnst]}).includes(type);
199
- if (hasMatchingEnumType) {
200
- addOrFilterEnumList({enumList});
201
- }
202
- if (hasMatchingConstType) {
203
- addOrFilterEnumList({enumList: [cnst]});
204
- }
205
- return (!newSchemaObjectType || newSchemaObjectType === type) &&
206
- (!hasEnumList || hasMatchingEnumType) &&
207
- (!hasConst || hasMatchingConstType);
208
- // Todo (readme): Handle type-specific conditions
209
- // Todo (readme): Recurse on `anyOf`, etc. children
210
- // Todo (readme): Use `oneOf` argument (to add `not`?)
211
- });
212
- })
213
- : newAnyOf;
214
- };
215
- const confineSchemaType = ({targetSchema, newSchema}) => {
216
- if (newSchema.type) {
217
- if (targetSchema.type) {
218
- if (Array.isArray(targetSchema.type)) {
219
- if (Array.isArray(newSchema.type)) {
220
- targetSchema.type = targetSchema.type.filter((type) => {
221
- return newSchema.type.includes(type);
222
- });
223
- } else if (targetSchema.type.includes(newSchema.type)) {
224
- targetSchema.type = newSchema.type;
225
- } else {
226
- throw new TypeError(
227
- `The constraints for ${newSchema.type} and ` +
228
- `${targetSchema.type} cannot be met.`
229
- );
230
- }
231
- // Todo: This does nothing; why was this ocndition here?
232
- // eslint-disable-next-line no-dupe-else-if
233
- } else if (0 && Array.isArray(targetSchema.type)) {
234
- if (targetSchema.type.includes(newSchema.type)) {
235
- targetSchema.type = newSchema.type;
236
- } else {
237
- throw new TypeError(
238
- `The constraints for ${newSchema.type} and ` +
239
- `${targetSchema.type} cannot be met.`
240
- );
241
- }
242
- } else if (targetSchema.type !== newSchema.type) {
243
- throw new TypeError(
244
- `The constraints for ${newSchema.type} and ` +
245
- `${targetSchema.type} cannot be met.`
246
- );
247
- }
248
- } else {
249
- targetSchema.type = newSchema.type;
250
- }
251
- }
252
- };
253
- const consolidateSchemas = (schemas) => {
254
- const retSchema = {};
255
- schemas.forEach((schema) => {
256
- confineSchemaType({targetSchema: retSchema, newSchema: schema});
257
- if (schema.enum) {
258
- addEnumConstraint({schema: retSchema, enumList: schema.enum});
259
- }
260
- if ('const' in schema) {
261
- addEnumConstraint({schema: retSchema, enumList: [schema.const]});
262
- }
263
- if ('multipleOf' in schema) {
264
- if ('multipleOf' in retSchema) {
265
- if (retSchema.multipleOf % schema.multipleOf &&
266
- schema.multipleOf % retSchema.multipleOf
267
- ) {
268
- retSchema.multipleOf *= schema.multipleOf;
269
- }
270
- } else {
271
- retSchema.multipleOf = schema.multipleOf;
272
- }
273
- }
274
- if ('maximum' in schema) {
275
- if ('maximum' in retSchema) {
276
- if (schema.maximum < retSchema.maximum) {
277
- retSchema.maximum = schema.maximum;
278
- }
279
- } else {
280
- retSchema.maximum = schema.maximum;
281
- }
282
- }
283
- if ('minimum' in schema) {
284
- if ('minimum' in retSchema) {
285
- if (schema.minimum > retSchema.minimum) {
286
- retSchema.minimum = schema.minimum;
287
- }
288
- } else {
289
- retSchema.minimum = schema.minimum;
290
- }
291
- }
292
- if ('maxLength' in schema) {
293
- if ('maxLength' in retSchema) {
294
- if (schema.maxLength < retSchema.maxLength) {
295
- retSchema.maxLength = schema.maxLength;
296
- }
297
- } else {
298
- retSchema.maxLength = schema.maxLength;
299
- }
300
- }
301
- if ('minLength' in schema) {
302
- if ('minLength' in retSchema) {
303
- if (schema.minLength > retSchema.minLength) {
304
- retSchema.minLength = schema.minLength;
305
- }
306
- } else {
307
- retSchema.minLength = schema.minLength;
308
- }
309
- }
310
- if ('pattern' in schema) {
311
- if ('allOf' in retSchema) {
312
- if (!retSchema.allOf.some(
313
- (item) => item.pattern === schema.pattern
314
- )) {
315
- retSchema.allOf.push({pattern: schema.pattern});
316
- }
317
- } else { /* if ('pattern' in retSchema) {
318
- retSchema.allOf = [
319
- {pattern: retSchema.pattern},
320
- {pattern: schema.pattern}
321
- ];
322
- delete retSchema.pattern;
323
- } else { */
324
- // retSchema.pattern = schema.pattern;
325
- retSchema.allOf = [{pattern: schema.pattern}];
326
- }
327
- }
328
- if ('maxItems' in schema) {
329
- if ('maxItems' in retSchema) {
330
- if (schema.maxItems < retSchema.maxItems) {
331
- retSchema.maxItems = schema.maxItems;
332
- }
333
- } else {
334
- retSchema.maxItems = schema.maxItems;
335
- }
336
- }
337
- if ('minItems' in schema) {
338
- if ('minItems' in retSchema) {
339
- if (schema.minItems > retSchema.minItems) {
340
- retSchema.minItems = schema.minItems;
341
- }
342
- } else {
343
- retSchema.minItems = schema.minItems;
344
- }
345
- }
346
- // Todo (low): Add `format` to `allOf`?
347
- /*
348
- Todo (readme): Merge other properties
349
- 6.4. Validation Keywords for Arrays
350
- 6.4.1. items
351
- 6.4.2. additionalItems
352
- 6.4.5. uniqueItems
353
- 6.4.6. contains
354
- 6.5. Validation Keywords for Objects
355
- 6.5.3. required
356
- 6.5.4. properties
357
- 6.5.5. patternProperties
358
- 6.5.6. additionalProperties
359
- 6.5.7. dependencies
360
- 6.5.8. propertyNames
361
- 6.6. Keywords for Applying Subschemas Conditionally
362
- 6.6.1. if
363
- 6.6.2. then
364
- 6.6.3. else
365
- 6.7. Keywords for Applying Subschemas With Boolean Logic
366
- 6.7.1. allOf
367
- 6.7.2. anyOf
368
- 6.7.3. oneOf
369
- 6.7.4. not
370
- */
371
- if ('maxProperties' in schema) {
372
- if ('maxProperties' in retSchema) {
373
- if (schema.maxProperties < retSchema.maxProperties) {
374
- retSchema.maxProperties = schema.maxProperties;
375
- }
376
- } else {
377
- retSchema.maxProperties = schema.maxProperties;
378
- }
379
- }
380
- if ('minProperties' in schema) {
381
- if ('minProperties' in retSchema) {
382
- if (schema.minProperties > retSchema.minProperties) {
383
- retSchema.minProperties = schema.minProperties;
384
- }
385
- } else {
386
- retSchema.minProperties = schema.minProperties;
387
- }
388
- }
389
- });
390
- // Todo (low): We could remove properties if type doesn't support
391
- if ('maximum' in retSchema && `exclusiveMaximum` in retSchema) {
392
- if (retSchema.maximum >= retSchema.exclusiveMaximum) {
393
- delete retSchema.maximum;
394
- } else {
395
- delete retSchema.exclusiveMaximum;
396
- }
397
- }
398
- if ('minimum' in retSchema && `exclusiveMinimum` in retSchema) {
399
- if (retSchema.minimum <= retSchema.exclusiveMinimum) {
400
- delete retSchema.minimum;
401
- } else {
402
- delete retSchema.exclusiveMinimum;
403
- }
404
- }
405
- if ('maximum' in retSchema && `minimum` in retSchema &&
406
- retSchema.maximum < retSchema.minimum
407
- ) {
408
- throw new TypeError(`Max under minimum`);
409
- }
410
- if ('maximum' in retSchema && `exclusiveMinimum` in retSchema &&
411
- retSchema.maximum <= retSchema.exclusiveMinimum
412
- ) {
413
- throw new TypeError(`Max under or equal to exclusive minimum`);
414
- }
415
- if ('exclusiveMaximum' in retSchema &&
416
- `exclusiveMinimum` in retSchema &&
417
- retSchema.exclusiveMaximum < retSchema.minimum
418
- ) {
419
- throw new TypeError(`Exclusive max under exclusive minimum`);
420
- }
421
- if ('exclusiveMaximum' in retSchema && `minimum` in retSchema &&
422
- retSchema.exclusiveMaximum <= retSchema.minimum
423
- ) {
424
- throw new TypeError(`Exclusive max under or equal to minimum`);
425
- }
426
- if ('maxLength' in retSchema && `minLength` in retSchema &&
427
- retSchema.maxLength < retSchema.minLength
428
- ) {
429
- throw new TypeError('Max length under min length');
430
- }
431
- // Todo (low): We might parse `pattern` to see if it necessitates
432
- // a `minLength` (or a `maxLength` if `^` and `$` are used?)
433
- return retSchema;
434
- };
435
- const flattenSchemas = ({schema, possibleSchemaObject: pso}) => {
436
- if (!schema) {
437
- schema = {};
438
- }
439
- // Todo (readme): resolve refs
440
- resolveRef({schema, pso});
441
-
442
- // type -> [type] -> anyOf
443
- const newAnyOf = 'type' in pso
444
- ? (Array.isArray(pso.type) ? pso.type : [pso.type]).map(
445
- (type) => ({type})
446
- )
447
- : [];
448
- addTypeConstraints({schema, newAnyOf});
449
-
450
- // enum -> enum
451
- // const -> enum
452
- if ('const' in pso) {
453
- addEnumAnyOfConstraint({schema, enumList: [pso.const]});
454
- }
455
- if ('enum' in pso) {
456
- addEnumAnyOfConstraint({schema, enumList: pso.enum});
457
- }
458
-
459
- if (pso.anyOf) {
460
- addTypeConstraints({schema, newAnyOf: pso.anyOf});
461
- }
462
- if (pso.oneOf) {
463
- addTypeConstraints({schema, newAnyOf: pso.oneOf, oneOf: true});
464
- }
465
- if (pso.allOf) {
466
- addTypeConstraints({schema, newAnyOf: [
467
- consolidateSchemas(pso.allOf)
468
- ]});
469
- }
470
- if (pso.not) {
471
- // Todo
472
- }
473
- if (pso.if) {
474
- // pso.then
475
- // pso.else
476
- }
477
- // flattenSchemas(pso.anyOf))
478
- if (!schema.anyOf.length) {
479
- // Todo: Should allow anything
480
- }
481
-
482
- // Todo: Handle `$ref` and aggregate oneOf/allOf/anyOf
483
- // (including `type: []`)/not; if/then/else
484
- // to `anyOf` with each constrained with any `allOf` for
485
- // required (and any `not` (including `oneOf`) for prohibited if
486
- // they can't be expressed as allOf),
487
- // and any `if` conditions applied?
488
- // Todo: Keep going until find all `type` (object/array/etc.)
489
- // and extract out
490
- return []; // Todo: Add possible schema objects
491
- };
492
- // Todo: Descend into arrays/objects (changing path)
493
- const resolvePropertyInSchemas = (/* {
494
- possibleSchemaObjects, arrayOrObjectPropertyName
495
- } */) => {
496
- // Todo?
497
- };
498
- stateObj.getPossibleSchemasForPathAndType = function ({
499
- keypath, parentPath, valueType, arrayOrObjectPropertyName
500
- }) {
501
- const possibleSchemaObjects = flattenSchemas({
502
- possibleSchemaObject: stateObj[parentPath]
503
- });
504
-
505
- // Todo: We'll probably need a separate encapsulateObserver
506
- // run-through to ensure we exclude all non-viable
507
- // schemas along the way
508
- // Todo: Check type/enum
509
- if (parentPath) {
510
- resolvePropertyInSchemas({
511
- possibleSchemaObjects, arrayOrObjectPropertyName
512
- });
513
- }
514
-
515
- if (!stateObj[keypath]) {
516
- stateObj[keypath] = [];
517
- }
518
- stateObj[keypath].push(
519
- ...possibleSchemaObjects.filter((possibleSchemaObject) => {
520
- return 'type' in possibleSchemaObject &&
521
- possibleSchemaObject.type === valueType;
522
- })
523
- );
524
-
525
- // apparently need this or else no return type to assign
526
- return stateObj;
527
- };
528
- return structuredCloning.iterate(records, stateObj);
529
- };
530
-
531
- /**
532
- * @param {string} state
533
- * @returns {string[]}
534
- */
535
- export const getTypesForState = (state) => {
536
- return structuredCloning.getTypesForState.call(
537
- this, types, state
538
- );
539
- };