@valbuild/core 0.15.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.
- package/ROADMAP.md +106 -0
- package/dist/declarations/src/ValApi.d.ts +26 -0
- package/dist/declarations/src/expr/eval.d.ts +1 -1
- package/dist/declarations/src/future/fetchVal.d.ts +5 -0
- package/dist/declarations/src/index.d.ts +38 -9
- package/dist/declarations/src/initSchema.d.ts +2 -10
- package/dist/declarations/src/initVal.d.ts +3 -21
- package/dist/declarations/src/module.d.ts +2 -14
- package/dist/declarations/src/patch/index.d.ts +1 -1
- package/dist/declarations/src/patch/util.d.ts +2 -0
- package/dist/declarations/src/schema/array.d.ts +3 -2
- package/dist/declarations/src/schema/boolean.d.ts +3 -2
- package/dist/declarations/src/schema/image.d.ts +4 -3
- package/dist/declarations/src/schema/index.d.ts +8 -7
- package/dist/declarations/src/schema/keyOf.d.ts +23 -0
- package/dist/declarations/src/schema/literal.d.ts +3 -2
- package/dist/declarations/src/schema/number.d.ts +3 -2
- package/dist/declarations/src/schema/object.d.ts +3 -2
- package/dist/declarations/src/schema/record.d.ts +19 -0
- package/dist/declarations/src/schema/richtext.d.ts +3 -2
- package/dist/declarations/src/schema/string.d.ts +3 -2
- package/dist/declarations/src/schema/union.d.ts +4 -2
- package/dist/declarations/src/schema/validation/ValidationError.d.ts +14 -0
- package/dist/declarations/src/schema/validation/ValidationFix.d.ts +2 -0
- package/dist/declarations/src/selector/SelectorProxy.d.ts +7 -0
- package/dist/declarations/src/selector/array.d.ts +2 -13
- package/dist/declarations/src/selector/file.d.ts +1 -4
- package/dist/declarations/src/selector/future/array.d.ts +17 -0
- package/dist/declarations/src/selector/future/boolean.d.ts +2 -0
- package/dist/declarations/src/selector/future/file.d.ts +9 -0
- package/dist/declarations/src/selector/{i18n.d.ts → future/i18n.d.ts} +2 -2
- package/dist/declarations/src/selector/future/index.d.ts +81 -0
- package/dist/declarations/src/selector/future/number.d.ts +2 -0
- package/dist/declarations/src/selector/future/object.d.ts +10 -0
- package/dist/declarations/src/selector/future/primitive.d.ts +9 -0
- package/dist/declarations/src/selector/{remote.d.ts → future/remote.d.ts} +1 -1
- package/dist/declarations/src/selector/future/string.d.ts +2 -0
- package/dist/declarations/src/selector/index.d.ts +5 -32
- package/dist/declarations/src/selector/object.d.ts +2 -9
- package/dist/declarations/src/selector/primitive.d.ts +3 -9
- package/dist/declarations/src/source/{i18n.d.ts → future/i18n.d.ts} +2 -2
- package/dist/declarations/src/source/{remote.d.ts → future/remote.d.ts} +3 -3
- package/dist/declarations/src/source/index.d.ts +4 -3
- package/dist/declarations/src/val/index.d.ts +2 -2
- package/dist/index-369caccf.esm.js +550 -0
- package/dist/{index-06df0a5b.esm.js → index-3e3e839e.esm.js} +190 -555
- package/dist/{index-9663f28a.cjs.dev.js → index-486c7fbf.cjs.dev.js} +224 -617
- package/dist/{index-b2270f8f.cjs.prod.js → index-601a7d73.cjs.prod.js} +224 -617
- package/dist/index-8706c87e.cjs.prod.js +582 -0
- package/dist/index-a6e642dd.cjs.dev.js +582 -0
- package/dist/ops-0d09f8ee.cjs.prod.js +684 -0
- package/dist/ops-23a5abb2.esm.js +671 -0
- package/dist/ops-f3015423.cjs.dev.js +684 -0
- package/dist/valbuild-core.cjs.dev.js +640 -633
- package/dist/valbuild-core.cjs.prod.js +640 -633
- package/dist/valbuild-core.esm.js +578 -572
- package/expr/dist/valbuild-core-expr.cjs.dev.js +8 -8
- package/expr/dist/valbuild-core-expr.cjs.prod.js +8 -8
- package/expr/dist/valbuild-core-expr.esm.js +2 -2
- package/package.json +2 -2
- package/patch/dist/valbuild-core-patch.cjs.dev.js +32 -23
- package/patch/dist/valbuild-core-patch.cjs.prod.js +32 -23
- package/patch/dist/valbuild-core-patch.esm.js +12 -4
- package/src/ValApi.ts +85 -0
- package/src/expr/eval.test.ts +2 -2
- package/src/expr/eval.ts +2 -2
- package/src/expr/repl.ts +2 -2
- package/src/{fetchVal.test.ts → future/fetchVal.test.ts} +57 -57
- package/src/{fetchVal.ts → future/fetchVal.ts} +17 -22
- package/src/index.ts +51 -14
- package/src/initSchema.ts +11 -12
- package/src/initVal.ts +42 -52
- package/src/module.test.ts +40 -40
- package/src/module.ts +53 -43
- package/src/patch/deref.test.ts +1 -1
- package/src/patch/deref.ts +1 -1
- package/src/patch/index.ts +1 -0
- package/src/patch/json.test.ts +0 -1
- package/src/patch/util.ts +7 -0
- package/src/schema/array.ts +45 -4
- package/src/schema/boolean.ts +14 -3
- package/src/schema/{i18n.ts → future/i18n.ts} +15 -11
- package/src/schema/{oneOf.ts → future/oneOf.ts} +21 -18
- package/src/schema/image.ts +66 -6
- package/src/schema/index.ts +37 -13
- package/src/schema/keyOf.ts +167 -0
- package/src/schema/literal.ts +24 -3
- package/src/schema/number.ts +14 -3
- package/src/schema/object.ts +50 -7
- package/src/schema/record.ts +103 -0
- package/src/schema/richtext.ts +63 -3
- package/src/schema/string.ts +14 -3
- package/src/schema/union.ts +4 -3
- package/src/schema/validation/ValidationError.ts +16 -0
- package/src/schema/validation/ValidationFix.ts +6 -0
- package/src/schema/validation.test.ts +291 -0
- package/src/selector/SelectorProxy.ts +16 -16
- package/src/selector/array.ts +2 -26
- package/src/selector/file.ts +1 -9
- package/src/selector/{ExprProxy.test.ts → future/ExprProxy.test.ts} +2 -2
- package/src/selector/{ExprProxy.ts → future/ExprProxy.ts} +9 -2
- package/src/selector/{SelectorProxy.test.ts → future/SelectorProxy.test.ts} +4 -4
- package/src/selector/future/SelectorProxy.ts +238 -0
- package/src/selector/future/array.ts +37 -0
- package/src/selector/future/boolean.ts +4 -0
- package/src/selector/future/file.ts +14 -0
- package/src/selector/{i18n.ts → future/i18n.ts} +2 -2
- package/src/selector/future/index.ts +165 -0
- package/src/selector/future/number.ts +4 -0
- package/src/selector/future/object.ts +22 -0
- package/src/selector/future/primitive.ts +17 -0
- package/src/selector/{remote.ts → future/remote.ts} +1 -1
- package/src/selector/{selector.test.ts → future/selector.test.ts} +8 -28
- package/src/selector/{selectorOf.ts → future/selectorOf.ts} +1 -1
- package/src/selector/future/string.ts +4 -0
- package/src/selector/index.ts +4 -46
- package/src/selector/object.ts +2 -19
- package/src/selector/primitive.ts +3 -16
- package/src/source/{i18n.ts → future/i18n.ts} +2 -2
- package/src/source/{remote.ts → future/remote.ts} +3 -3
- package/src/source/index.ts +3 -2
- package/src/val/array.ts +1 -1
- package/src/val/index.ts +2 -2
- package/src/val/object.ts +1 -1
- package/dist/createClass-012eebbf.esm.js +0 -109
- package/dist/createClass-a436dbfe.cjs.dev.js +0 -116
- package/dist/createClass-de7426aa.cjs.prod.js +0 -116
- package/dist/declarations/src/fetchVal.d.ts +0 -5
- package/dist/declarations/src/schema/i18n.d.ts +0 -21
- package/dist/declarations/src/schema/oneOf.d.ts +0 -22
- package/dist/ops-6fae92a1.esm.js +0 -12
- package/dist/ops-87cdbafc.cjs.dev.js +0 -14
- package/dist/ops-ae4d1bc2.cjs.prod.js +0 -14
@@ -0,0 +1,684 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
var index = require('./index-a6e642dd.cjs.dev.js');
|
4
|
+
var result = require('./result-48320acd.cjs.dev.js');
|
5
|
+
|
6
|
+
function hasOwn(obj, prop) {
|
7
|
+
return Object.prototype.hasOwnProperty.call(obj, prop);
|
8
|
+
}
|
9
|
+
function _andThen(f, source, path) {
|
10
|
+
if (source) {
|
11
|
+
return newSelectorProxy(f(newSelectorProxy(source, path)));
|
12
|
+
}
|
13
|
+
return newSelectorProxy(source, path);
|
14
|
+
}
|
15
|
+
function isSelector(source) {
|
16
|
+
return index._typeof(source) === "object" && source !== null && (index.GetSource in source || index.Path in source);
|
17
|
+
}
|
18
|
+
function newSelectorProxy(source, path, moduleSchema) {
|
19
|
+
var _ref;
|
20
|
+
if (index._typeof(source) === "object") {
|
21
|
+
if (isSelector(source)) {
|
22
|
+
return source;
|
23
|
+
} else if (index.isSerializedVal(source)) {
|
24
|
+
return newSelectorProxy(source.val, source.valPath);
|
25
|
+
}
|
26
|
+
}
|
27
|
+
if (source && source[index.FILE_REF_PROP] && source[index.VAL_EXTENSION] === "file") {
|
28
|
+
var fileRef = source[index.FILE_REF_PROP];
|
29
|
+
if (typeof fileRef !== "string") {
|
30
|
+
throw Error("Invalid file ref: " + fileRef);
|
31
|
+
}
|
32
|
+
return newSelectorProxy(index.convertFileSource(source), path, moduleSchema);
|
33
|
+
}
|
34
|
+
switch (index._typeof(source)) {
|
35
|
+
case "function":
|
36
|
+
case "symbol":
|
37
|
+
throw Error("Invalid selector type: ".concat(index._typeof(source), ": ").concat(source));
|
38
|
+
case "object":
|
39
|
+
// Handles both objects and arrays!
|
40
|
+
if (source !== null) {
|
41
|
+
return new Proxy(source, {
|
42
|
+
// TODO: see proxy docs if we want more traps
|
43
|
+
has: function has(target, prop) {
|
44
|
+
if (prop === index.GetSource) {
|
45
|
+
return true;
|
46
|
+
}
|
47
|
+
if (prop === index.Path) {
|
48
|
+
return true;
|
49
|
+
}
|
50
|
+
if (prop === "andThen") {
|
51
|
+
return true;
|
52
|
+
}
|
53
|
+
if (prop === index.GetSchema) {
|
54
|
+
return true;
|
55
|
+
}
|
56
|
+
return prop in target;
|
57
|
+
},
|
58
|
+
get: function get(target, prop) {
|
59
|
+
if (prop === index.GetSource) {
|
60
|
+
return source;
|
61
|
+
}
|
62
|
+
if (prop === index.Path) {
|
63
|
+
return path;
|
64
|
+
}
|
65
|
+
if (prop === index.GetSchema) {
|
66
|
+
return moduleSchema;
|
67
|
+
}
|
68
|
+
if (prop === "andThen") {
|
69
|
+
return function (f) {
|
70
|
+
return _andThen(f, source, path);
|
71
|
+
};
|
72
|
+
}
|
73
|
+
if (Array.isArray(target)) {
|
74
|
+
if (prop === "filter") {
|
75
|
+
return function (f) {
|
76
|
+
var filtered = target.map(function (a, i) {
|
77
|
+
return newSelectorProxy(a, createValPathOfItem(path, i), moduleSchema === null || moduleSchema === void 0 ? void 0 : moduleSchema.item);
|
78
|
+
}).filter(function (a) {
|
79
|
+
if (f && f instanceof index.Schema) {
|
80
|
+
return f.assert(unValify(a));
|
81
|
+
} else {
|
82
|
+
return unValify(f(a));
|
83
|
+
}
|
84
|
+
});
|
85
|
+
return newSelectorProxy(filtered, path, moduleSchema);
|
86
|
+
};
|
87
|
+
} else if (prop === "map") {
|
88
|
+
return function (f) {
|
89
|
+
var filtered = target.map(function (a, i) {
|
90
|
+
var valueOrSelector = f(newSelectorProxy(a, createValPathOfItem(path, i), moduleSchema === null || moduleSchema === void 0 ? void 0 : moduleSchema.item), newSelectorProxy(i));
|
91
|
+
if (isSelector(valueOrSelector)) {
|
92
|
+
return valueOrSelector;
|
93
|
+
}
|
94
|
+
return newSelectorProxy(valueOrSelector);
|
95
|
+
});
|
96
|
+
return newSelectorProxy(filtered, path, moduleSchema);
|
97
|
+
};
|
98
|
+
}
|
99
|
+
}
|
100
|
+
if (Array.isArray(target) && prop === "length") {
|
101
|
+
return newSelectorProxy(target.length);
|
102
|
+
}
|
103
|
+
var reflectedValue = Reflect.get(target, prop);
|
104
|
+
if (hasOwn(source, prop)) {
|
105
|
+
if (!Number.isNaN(Number(prop))) {
|
106
|
+
return newSelectorProxy(reflectedValue, createValPathOfItem(path, Number(prop)), moduleSchema === null || moduleSchema === void 0 ? void 0 : moduleSchema.item);
|
107
|
+
}
|
108
|
+
return newSelectorProxy(reflectedValue, createValPathOfItem(path, prop), moduleSchema === null || moduleSchema === void 0 ? void 0 : moduleSchema.items[prop]);
|
109
|
+
}
|
110
|
+
return reflectedValue;
|
111
|
+
}
|
112
|
+
});
|
113
|
+
}
|
114
|
+
// intentional fallthrough
|
115
|
+
// eslint-disable-next-line no-fallthrough
|
116
|
+
default:
|
117
|
+
return _ref = {
|
118
|
+
eq: function eq(other) {
|
119
|
+
var otherValue = other;
|
120
|
+
if (isSelector(other)) {
|
121
|
+
otherValue = other[index.GetSource];
|
122
|
+
if (otherValue instanceof index.Expr) {
|
123
|
+
throw Error("TODO: Cannot evaluate equality with an Expr");
|
124
|
+
}
|
125
|
+
}
|
126
|
+
return newSelectorProxy(source === otherValue, undefined);
|
127
|
+
},
|
128
|
+
andThen: function andThen(f) {
|
129
|
+
return _andThen(f, source === undefined ? null : source, path);
|
130
|
+
}
|
131
|
+
}, index._defineProperty(_ref, index.GetSource, source === undefined ? null : source), index._defineProperty(_ref, index.Path, path), index._defineProperty(_ref, index.GetSchema, moduleSchema), _ref;
|
132
|
+
}
|
133
|
+
}
|
134
|
+
function createValPathOfItem(arrayPath, prop) {
|
135
|
+
if (index._typeof(prop) === "symbol") {
|
136
|
+
throw Error("Cannot create val path of array item with symbol prop: ".concat(prop.toString()));
|
137
|
+
}
|
138
|
+
return arrayPath && "".concat(arrayPath, ".").concat(JSON.stringify(prop));
|
139
|
+
}
|
140
|
+
|
141
|
+
// TODO: could we do .val on the objects instead?
|
142
|
+
function unValify(valueOrSelector) {
|
143
|
+
if (index._typeof(valueOrSelector) === "object" && (index.GetSource in valueOrSelector || index.Path in valueOrSelector)) {
|
144
|
+
var selectorValue = valueOrSelector[index.GetSource];
|
145
|
+
return selectorValue;
|
146
|
+
}
|
147
|
+
return valueOrSelector;
|
148
|
+
}
|
149
|
+
|
150
|
+
var ObjectSchema = /*#__PURE__*/function (_Schema) {
|
151
|
+
index._inherits(ObjectSchema, _Schema);
|
152
|
+
var _super = index._createSuper(ObjectSchema);
|
153
|
+
function ObjectSchema(items) {
|
154
|
+
var _this;
|
155
|
+
var opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
156
|
+
index._classCallCheck(this, ObjectSchema);
|
157
|
+
_this = _super.call(this);
|
158
|
+
_this.items = items;
|
159
|
+
_this.opt = opt;
|
160
|
+
return _this;
|
161
|
+
}
|
162
|
+
index._createClass(ObjectSchema, [{
|
163
|
+
key: "validate",
|
164
|
+
value: function validate(path, src) {
|
165
|
+
var _this2 = this;
|
166
|
+
var error = false;
|
167
|
+
if (this.opt && (src === null || src === undefined)) {
|
168
|
+
return false;
|
169
|
+
}
|
170
|
+
if (index._typeof(src) !== "object") {
|
171
|
+
return index._defineProperty({}, path, [{
|
172
|
+
message: "Expected 'object', got '".concat(index._typeof(src), "'")
|
173
|
+
}]);
|
174
|
+
} else if (Array.isArray(src)) {
|
175
|
+
return index._defineProperty({}, path, [{
|
176
|
+
message: "Expected 'object', got 'array'"
|
177
|
+
}]);
|
178
|
+
}
|
179
|
+
Object.entries(this.items).forEach(function (_ref3) {
|
180
|
+
var _ref4 = index._slicedToArray(_ref3, 2),
|
181
|
+
key = _ref4[0],
|
182
|
+
schema = _ref4[1];
|
183
|
+
var subPath = createValPathOfItem(path, key);
|
184
|
+
if (!subPath) {
|
185
|
+
error = _this2.appendValidationError(error, path, "Internal error: could not create path at ".concat(!path && typeof path === "string" ? "<empty string>" : path, " at key ").concat(key),
|
186
|
+
// Should! never happen
|
187
|
+
src);
|
188
|
+
} else {
|
189
|
+
var subError = schema.validate(subPath, src[key]);
|
190
|
+
if (subError && error) {
|
191
|
+
error = index._objectSpread2(index._objectSpread2({}, subError), error);
|
192
|
+
} else if (subError) {
|
193
|
+
error = subError;
|
194
|
+
}
|
195
|
+
}
|
196
|
+
});
|
197
|
+
return error;
|
198
|
+
}
|
199
|
+
}, {
|
200
|
+
key: "assert",
|
201
|
+
value: function assert(src) {
|
202
|
+
if (this.opt && (src === null || src === undefined)) {
|
203
|
+
return true;
|
204
|
+
}
|
205
|
+
if (!src) {
|
206
|
+
return false;
|
207
|
+
}
|
208
|
+
for (var _i = 0, _Object$entries = Object.entries(this.items); _i < _Object$entries.length; _i++) {
|
209
|
+
var _Object$entries$_i = index._slicedToArray(_Object$entries[_i], 2),
|
210
|
+
_key = _Object$entries$_i[0],
|
211
|
+
schema = _Object$entries$_i[1];
|
212
|
+
if (!schema.assert(src[_key])) {
|
213
|
+
return false;
|
214
|
+
}
|
215
|
+
}
|
216
|
+
return index._typeof(src) === "object" && !Array.isArray(src);
|
217
|
+
}
|
218
|
+
}, {
|
219
|
+
key: "optional",
|
220
|
+
value: function optional() {
|
221
|
+
return new ObjectSchema(this.items, true);
|
222
|
+
}
|
223
|
+
}, {
|
224
|
+
key: "serialize",
|
225
|
+
value: function serialize() {
|
226
|
+
return {
|
227
|
+
type: "object",
|
228
|
+
items: Object.fromEntries(Object.entries(this.items).map(function (_ref5) {
|
229
|
+
var _ref6 = index._slicedToArray(_ref5, 2),
|
230
|
+
key = _ref6[0],
|
231
|
+
schema = _ref6[1];
|
232
|
+
return [key, schema.serialize()];
|
233
|
+
})),
|
234
|
+
opt: this.opt
|
235
|
+
};
|
236
|
+
}
|
237
|
+
}]);
|
238
|
+
return ObjectSchema;
|
239
|
+
}(index.Schema);
|
240
|
+
var object = function object(schema) {
|
241
|
+
return new ObjectSchema(schema);
|
242
|
+
};
|
243
|
+
|
244
|
+
var ArraySchema = /*#__PURE__*/function (_Schema) {
|
245
|
+
index._inherits(ArraySchema, _Schema);
|
246
|
+
var _super = index._createSuper(ArraySchema);
|
247
|
+
function ArraySchema(item) {
|
248
|
+
var _this;
|
249
|
+
var opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
250
|
+
index._classCallCheck(this, ArraySchema);
|
251
|
+
_this = _super.call(this);
|
252
|
+
_this.item = item;
|
253
|
+
_this.opt = opt;
|
254
|
+
return _this;
|
255
|
+
}
|
256
|
+
index._createClass(ArraySchema, [{
|
257
|
+
key: "validate",
|
258
|
+
value: function validate(path, src) {
|
259
|
+
var _this2 = this;
|
260
|
+
var error = false;
|
261
|
+
if (this.opt && (src === null || src === undefined)) {
|
262
|
+
return false;
|
263
|
+
}
|
264
|
+
if (index._typeof(src) !== "object" || !Array.isArray(src)) {
|
265
|
+
return index._defineProperty({}, path, [{
|
266
|
+
message: "Expected 'array', got '".concat(index._typeof(src), "'")
|
267
|
+
}]);
|
268
|
+
}
|
269
|
+
src.forEach(function (i, idx) {
|
270
|
+
var subPath = createValPathOfItem(path, idx);
|
271
|
+
if (!subPath) {
|
272
|
+
error = _this2.appendValidationError(error, path, "Internal error: could not create path at ".concat(!path && typeof path === "string" ? "<empty string>" : path, " at index ").concat(idx),
|
273
|
+
// Should! never happen
|
274
|
+
src);
|
275
|
+
} else {
|
276
|
+
var subError = _this2.item.validate(subPath, i);
|
277
|
+
if (subError && error) {
|
278
|
+
error = index._objectSpread2(index._objectSpread2({}, subError), error);
|
279
|
+
} else if (subError) {
|
280
|
+
error = subError;
|
281
|
+
}
|
282
|
+
}
|
283
|
+
});
|
284
|
+
return error;
|
285
|
+
}
|
286
|
+
}, {
|
287
|
+
key: "assert",
|
288
|
+
value: function assert(src) {
|
289
|
+
if (this.opt && (src === null || src === undefined)) {
|
290
|
+
return true;
|
291
|
+
}
|
292
|
+
if (!src) {
|
293
|
+
return false;
|
294
|
+
}
|
295
|
+
var _iterator = result._createForOfIteratorHelper(src),
|
296
|
+
_step;
|
297
|
+
try {
|
298
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
299
|
+
var _item = _step.value;
|
300
|
+
if (!this.item.assert(_item)) {
|
301
|
+
return false;
|
302
|
+
}
|
303
|
+
}
|
304
|
+
} catch (err) {
|
305
|
+
_iterator.e(err);
|
306
|
+
} finally {
|
307
|
+
_iterator.f();
|
308
|
+
}
|
309
|
+
return index._typeof(src) === "object" && Array.isArray(src);
|
310
|
+
}
|
311
|
+
}, {
|
312
|
+
key: "optional",
|
313
|
+
value: function optional() {
|
314
|
+
return new ArraySchema(this.item, true);
|
315
|
+
}
|
316
|
+
}, {
|
317
|
+
key: "serialize",
|
318
|
+
value: function serialize() {
|
319
|
+
return {
|
320
|
+
type: "array",
|
321
|
+
item: this.item.serialize(),
|
322
|
+
opt: this.opt
|
323
|
+
};
|
324
|
+
}
|
325
|
+
}]);
|
326
|
+
return ArraySchema;
|
327
|
+
}(index.Schema);
|
328
|
+
var array = function array(schema) {
|
329
|
+
return new ArraySchema(schema);
|
330
|
+
};
|
331
|
+
|
332
|
+
var UnionSchema = /*#__PURE__*/function (_Schema) {
|
333
|
+
index._inherits(UnionSchema, _Schema);
|
334
|
+
var _super = index._createSuper(UnionSchema);
|
335
|
+
function UnionSchema(key, items) {
|
336
|
+
var _this;
|
337
|
+
var opt = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
338
|
+
index._classCallCheck(this, UnionSchema);
|
339
|
+
_this = _super.call(this);
|
340
|
+
_this.key = key;
|
341
|
+
_this.items = items;
|
342
|
+
_this.opt = opt;
|
343
|
+
return _this;
|
344
|
+
}
|
345
|
+
index._createClass(UnionSchema, [{
|
346
|
+
key: "validate",
|
347
|
+
value: function validate(path, src) {
|
348
|
+
throw new Error("Method not implemented.");
|
349
|
+
}
|
350
|
+
}, {
|
351
|
+
key: "assert",
|
352
|
+
value: function assert(src) {
|
353
|
+
throw new Error("Method not implemented.");
|
354
|
+
}
|
355
|
+
}, {
|
356
|
+
key: "optional",
|
357
|
+
value: function optional() {
|
358
|
+
throw new Error("Method not implemented.");
|
359
|
+
}
|
360
|
+
}, {
|
361
|
+
key: "serialize",
|
362
|
+
value: function serialize() {
|
363
|
+
return {
|
364
|
+
type: "union",
|
365
|
+
key: this.key,
|
366
|
+
items: this.items.map(function (o) {
|
367
|
+
return o.serialize();
|
368
|
+
}),
|
369
|
+
opt: this.opt
|
370
|
+
};
|
371
|
+
}
|
372
|
+
}]);
|
373
|
+
return UnionSchema;
|
374
|
+
}(index.Schema);
|
375
|
+
var union = function union(key) {
|
376
|
+
for (var _len = arguments.length, objects = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
377
|
+
objects[_key - 1] = arguments[_key];
|
378
|
+
}
|
379
|
+
return new UnionSchema(key, objects);
|
380
|
+
};
|
381
|
+
|
382
|
+
var RichTextSchema = /*#__PURE__*/function (_Schema) {
|
383
|
+
index._inherits(RichTextSchema, _Schema);
|
384
|
+
var _super = index._createSuper(RichTextSchema);
|
385
|
+
function RichTextSchema() {
|
386
|
+
var _this;
|
387
|
+
var opt = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
|
388
|
+
index._classCallCheck(this, RichTextSchema);
|
389
|
+
_this = _super.call(this);
|
390
|
+
_this.opt = opt;
|
391
|
+
return _this;
|
392
|
+
}
|
393
|
+
index._createClass(RichTextSchema, [{
|
394
|
+
key: "validate",
|
395
|
+
value: function validate(path, src) {
|
396
|
+
if (this.opt && (src === null || src === undefined)) {
|
397
|
+
return false;
|
398
|
+
}
|
399
|
+
if (src === null || src === undefined) {
|
400
|
+
return index._defineProperty({}, path, [{
|
401
|
+
message: "Expected non-nullable got '".concat(src, "'")
|
402
|
+
}]);
|
403
|
+
}
|
404
|
+
if (index._typeof(src) !== "object" && !Array.isArray(src)) {
|
405
|
+
return index._defineProperty({}, path, [{
|
406
|
+
message: "Expected 'object' (that is not of an array) or 'string', got '".concat(index._typeof(src), "'"),
|
407
|
+
value: src
|
408
|
+
}]);
|
409
|
+
}
|
410
|
+
if (src[index.VAL_EXTENSION] !== "richtext") {
|
411
|
+
return index._defineProperty({}, path, [{
|
412
|
+
message: "Expected _type key with value 'richtext' got '".concat(src[index.VAL_EXTENSION], "'"),
|
413
|
+
value: src
|
414
|
+
}]);
|
415
|
+
}
|
416
|
+
if (src.type !== "root") {
|
417
|
+
return index._defineProperty({}, path, [{
|
418
|
+
message: "Expected type key with value 'root' got '".concat(src.type, "'"),
|
419
|
+
value: src
|
420
|
+
}]);
|
421
|
+
}
|
422
|
+
if (index._typeof(src.children) !== "object" && !Array.isArray(src.children)) {
|
423
|
+
return index._defineProperty({}, path, [{
|
424
|
+
message: "Expected children to be an array, but got '".concat(src.type, "'"),
|
425
|
+
value: src
|
426
|
+
}]);
|
427
|
+
}
|
428
|
+
return false;
|
429
|
+
}
|
430
|
+
}, {
|
431
|
+
key: "assert",
|
432
|
+
value: function assert(src) {
|
433
|
+
// TODO:
|
434
|
+
return true;
|
435
|
+
}
|
436
|
+
}, {
|
437
|
+
key: "optional",
|
438
|
+
value: function optional() {
|
439
|
+
return new RichTextSchema(true);
|
440
|
+
}
|
441
|
+
}, {
|
442
|
+
key: "serialize",
|
443
|
+
value: function serialize() {
|
444
|
+
return {
|
445
|
+
type: "richtext",
|
446
|
+
opt: this.opt
|
447
|
+
};
|
448
|
+
}
|
449
|
+
}]);
|
450
|
+
return RichTextSchema;
|
451
|
+
}(index.Schema);
|
452
|
+
var richtext = function richtext() {
|
453
|
+
return new RichTextSchema();
|
454
|
+
};
|
455
|
+
|
456
|
+
function content(
|
457
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
458
|
+
id,
|
459
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
460
|
+
schema,
|
461
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
462
|
+
source) {
|
463
|
+
var _ref;
|
464
|
+
return _ref = {}, index._defineProperty(_ref, index.GetSource, source), index._defineProperty(_ref, index.GetSchema, schema), index._defineProperty(_ref, index.Path, id), _ref;
|
465
|
+
}
|
466
|
+
function getSource(valModule) {
|
467
|
+
var sourceOrExpr = valModule[index.GetSource];
|
468
|
+
if (sourceOrExpr instanceof index.Expr) {
|
469
|
+
throw Error("Cannot get raw source of an Expr");
|
470
|
+
}
|
471
|
+
var source = sourceOrExpr;
|
472
|
+
return source;
|
473
|
+
}
|
474
|
+
function splitModuleIdAndModulePath(path) {
|
475
|
+
return [path.slice(0, path.indexOf(".")), path.slice(path.indexOf(".") + 1)];
|
476
|
+
}
|
477
|
+
function isObjectSchema(schema) {
|
478
|
+
return schema instanceof ObjectSchema || index._typeof(schema) === "object" && "type" in schema && schema.type === "object";
|
479
|
+
}
|
480
|
+
function isArraySchema(schema) {
|
481
|
+
return schema instanceof ArraySchema || index._typeof(schema) === "object" && "type" in schema && schema.type === "array";
|
482
|
+
}
|
483
|
+
|
484
|
+
// function isI18nSchema(
|
485
|
+
// schema: Schema<SelectorSource> | SerializedSchema
|
486
|
+
// ): schema is I18nSchema<readonly string[]> | SerializedI18nSchema {
|
487
|
+
// return (
|
488
|
+
// schema instanceof I18nSchema ||
|
489
|
+
// (typeof schema === "object" && "type" in schema && schema.type === "i18n")
|
490
|
+
// );
|
491
|
+
// }
|
492
|
+
|
493
|
+
function isUnionSchema(schema) {
|
494
|
+
return schema instanceof UnionSchema || index._typeof(schema) === "object" && "type" in schema && schema.type === "union";
|
495
|
+
}
|
496
|
+
|
497
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
498
|
+
function isRichTextSchema(schema) {
|
499
|
+
return schema instanceof RichTextSchema || index._typeof(schema) === "object" && "type" in schema && schema.type === "richtext";
|
500
|
+
}
|
501
|
+
function isImageSchema(schema) {
|
502
|
+
return schema instanceof index.ImageSchema || index._typeof(schema) === "object" && "type" in schema && schema.type === "image";
|
503
|
+
}
|
504
|
+
|
505
|
+
// function isOneOfSchema(
|
506
|
+
// schema: Schema<SelectorSource> | SerializedSchema
|
507
|
+
// ): schema is OneOfSchema<GenericSelector<SourceArray>> | SerializedOneOfSchema {
|
508
|
+
// return (
|
509
|
+
// schema instanceof OneOfSchema ||
|
510
|
+
// (typeof schema === "object" && "type" in schema && schema.type === "oneOf")
|
511
|
+
// );
|
512
|
+
// }
|
513
|
+
|
514
|
+
function resolvePath(path, valModule, schema) {
|
515
|
+
var parts = parsePath(path);
|
516
|
+
var origParts = index._toConsumableArray(parts);
|
517
|
+
var resolvedSchema = schema;
|
518
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
519
|
+
var resolvedSource = valModule;
|
520
|
+
var _loop = function _loop() {
|
521
|
+
var part = parts.shift();
|
522
|
+
if (part === undefined) {
|
523
|
+
throw Error("Unexpected undefined part");
|
524
|
+
}
|
525
|
+
if (isArraySchema(resolvedSchema)) {
|
526
|
+
if (Number.isNaN(Number(part))) {
|
527
|
+
throw Error("Invalid path: array schema ".concat(resolvedSchema, " must have ").concat(part, " a number as path"));
|
528
|
+
}
|
529
|
+
if (index._typeof(resolvedSource) !== "object" && !Array.isArray(resolvedSource)) {
|
530
|
+
throw Error("Schema type error: expected source to be type of array, but got ".concat(index._typeof(resolvedSource)));
|
531
|
+
}
|
532
|
+
if (!resolvedSource[part]) {
|
533
|
+
var _resolvedSource;
|
534
|
+
throw Error("Invalid path: array source (length: ".concat((_resolvedSource = resolvedSource) === null || _resolvedSource === void 0 ? void 0 : _resolvedSource.length, ") did not have index ").concat(part, " from path: ").concat(path));
|
535
|
+
}
|
536
|
+
resolvedSource = resolvedSource[part];
|
537
|
+
resolvedSchema = resolvedSchema.item;
|
538
|
+
} else if (isObjectSchema(resolvedSchema)) {
|
539
|
+
if (index._typeof(resolvedSource) !== "object") {
|
540
|
+
throw Error("Schema type error: expected source to be type of object, but got ".concat(index._typeof(resolvedSource)));
|
541
|
+
}
|
542
|
+
if (!resolvedSource[part]) {
|
543
|
+
throw Error("Invalid path: object source did not have key ".concat(part, " from path: ").concat(path));
|
544
|
+
}
|
545
|
+
resolvedSource = resolvedSource[part];
|
546
|
+
resolvedSchema = resolvedSchema.items[part];
|
547
|
+
// } else if (isI18nSchema(resolvedSchema)) {
|
548
|
+
// if (!resolvedSchema.locales.includes(part)) {
|
549
|
+
// throw Error(
|
550
|
+
// `Invalid path: i18n schema ${resolvedSchema} supports locales ${resolvedSchema.locales.join(
|
551
|
+
// ", "
|
552
|
+
// )}, but found: ${part}`
|
553
|
+
// );
|
554
|
+
// }
|
555
|
+
// if (!Object.keys(resolvedSource).includes(part)) {
|
556
|
+
// throw Error(
|
557
|
+
// `Schema type error: expected source to be type of i18n with locale ${part}, but got ${JSON.stringify(
|
558
|
+
// Object.keys(resolvedSource)
|
559
|
+
// )}`
|
560
|
+
// );
|
561
|
+
// }
|
562
|
+
// resolvedSource = resolvedSource[part];
|
563
|
+
// resolvedSchema = resolvedSchema.item;
|
564
|
+
} else if (isImageSchema(resolvedSchema)) {
|
565
|
+
return {
|
566
|
+
v: {
|
567
|
+
path: origParts.slice(0, origParts.length - parts.length - 1).map(function (p) {
|
568
|
+
return JSON.stringify(p);
|
569
|
+
}).join("."),
|
570
|
+
// TODO: create a function generate path from parts (not sure if this always works)
|
571
|
+
schema: resolvedSchema,
|
572
|
+
source: resolvedSource
|
573
|
+
}
|
574
|
+
};
|
575
|
+
} else if (isUnionSchema(resolvedSchema)) {
|
576
|
+
var _key = resolvedSchema.key;
|
577
|
+
var keyValue = resolvedSource[_key];
|
578
|
+
if (!keyValue) {
|
579
|
+
throw Error("Invalid path: union source ".concat(resolvedSchema, " did not have required key ").concat(_key, " in path: ").concat(path));
|
580
|
+
}
|
581
|
+
var schemaOfUnionKey = resolvedSchema.items.find(
|
582
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
583
|
+
function (child) {
|
584
|
+
var _child$items, _child$items$_key;
|
585
|
+
return (child === null || child === void 0 ? void 0 : (_child$items = child.items) === null || _child$items === void 0 ? void 0 : (_child$items$_key = _child$items[_key]) === null || _child$items$_key === void 0 ? void 0 : _child$items$_key.value) === keyValue;
|
586
|
+
});
|
587
|
+
if (!schemaOfUnionKey) {
|
588
|
+
throw Error("Invalid path: union schema ".concat(resolvedSchema, " did not have a child object with ").concat(_key, " of value ").concat(keyValue, " in path: ").concat(path));
|
589
|
+
}
|
590
|
+
resolvedSchema = schemaOfUnionKey.items[part];
|
591
|
+
resolvedSource = resolvedSource[part];
|
592
|
+
} else if (isRichTextSchema(resolvedSchema)) {
|
593
|
+
return {
|
594
|
+
v: {
|
595
|
+
path: origParts.slice(0, origParts.length - parts.length - 1).map(function (p) {
|
596
|
+
return JSON.stringify(p);
|
597
|
+
}).join("."),
|
598
|
+
// TODO: create a function generate path from parts (not sure if this always works)
|
599
|
+
schema: resolvedSchema,
|
600
|
+
source: resolvedSource
|
601
|
+
}
|
602
|
+
};
|
603
|
+
} else {
|
604
|
+
throw Error("Invalid path: ".concat(part, " resolved to an unexpected schema ").concat(JSON.stringify(resolvedSchema)));
|
605
|
+
}
|
606
|
+
};
|
607
|
+
while (parts.length > 0) {
|
608
|
+
var _ret = _loop();
|
609
|
+
if (index._typeof(_ret) === "object") return _ret.v;
|
610
|
+
}
|
611
|
+
if (parts.length > 0) {
|
612
|
+
throw Error("Invalid path: ".concat(parts.join("."), " is not a valid path"));
|
613
|
+
}
|
614
|
+
return {
|
615
|
+
path: origParts.map(function (p) {
|
616
|
+
if (!Number.isNaN(Number(p))) {
|
617
|
+
return p;
|
618
|
+
} else {
|
619
|
+
return JSON.stringify(p);
|
620
|
+
}
|
621
|
+
}).join("."),
|
622
|
+
// TODO: create a function generate path from parts (not sure if this always works)
|
623
|
+
schema: resolvedSchema,
|
624
|
+
source: resolvedSource
|
625
|
+
};
|
626
|
+
}
|
627
|
+
function parsePath(input) {
|
628
|
+
var result = [];
|
629
|
+
var i = 0;
|
630
|
+
while (i < input.length) {
|
631
|
+
var part = "";
|
632
|
+
if (input[i] === '"') {
|
633
|
+
// Parse a quoted string
|
634
|
+
i++;
|
635
|
+
while (i < input.length && input[i] !== '"') {
|
636
|
+
if (input[i] === "\\" && input[i + 1] === '"') {
|
637
|
+
// Handle escaped double quotes
|
638
|
+
part += '"';
|
639
|
+
i++;
|
640
|
+
} else {
|
641
|
+
part += input[i];
|
642
|
+
}
|
643
|
+
i++;
|
644
|
+
}
|
645
|
+
if (input[i] !== '"') {
|
646
|
+
var _input$i;
|
647
|
+
throw new Error("Invalid input (".concat(JSON.stringify(input), "): Missing closing double quote: ").concat((_input$i = input[i]) !== null && _input$i !== void 0 ? _input$i : "at end of string", " (char: ").concat(i, "; length: ").concat(input.length, ")"));
|
648
|
+
}
|
649
|
+
} else {
|
650
|
+
// Parse a regular string
|
651
|
+
while (i < input.length && input[i] !== ".") {
|
652
|
+
part += input[i];
|
653
|
+
i++;
|
654
|
+
}
|
655
|
+
}
|
656
|
+
if (part !== "") {
|
657
|
+
result.push(part);
|
658
|
+
}
|
659
|
+
i++;
|
660
|
+
}
|
661
|
+
return result;
|
662
|
+
}
|
663
|
+
|
664
|
+
var PatchError = /*#__PURE__*/index._createClass(function PatchError(message) {
|
665
|
+
index._classCallCheck(this, PatchError);
|
666
|
+
this.message = message;
|
667
|
+
});
|
668
|
+
|
669
|
+
/**
|
670
|
+
* NOTE: MAY mutate the input document.
|
671
|
+
*/
|
672
|
+
|
673
|
+
exports.PatchError = PatchError;
|
674
|
+
exports.array = array;
|
675
|
+
exports.content = content;
|
676
|
+
exports.createValPathOfItem = createValPathOfItem;
|
677
|
+
exports.getSource = getSource;
|
678
|
+
exports.isSelector = isSelector;
|
679
|
+
exports.newSelectorProxy = newSelectorProxy;
|
680
|
+
exports.object = object;
|
681
|
+
exports.resolvePath = resolvePath;
|
682
|
+
exports.richtext = richtext;
|
683
|
+
exports.splitModuleIdAndModulePath = splitModuleIdAndModulePath;
|
684
|
+
exports.union = union;
|