@valbuild/core 0.14.0 → 0.16.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 (66) hide show
  1. package/dist/SelectorProxy-2af1b2b8.cjs.prod.js +756 -0
  2. package/dist/SelectorProxy-63c2d0e2.esm.js +722 -0
  3. package/dist/SelectorProxy-873782a5.cjs.dev.js +756 -0
  4. package/dist/declarations/src/index.d.ts +2 -0
  5. package/dist/declarations/src/initVal.d.ts +1 -1
  6. package/dist/declarations/src/patch/index.d.ts +1 -1
  7. package/dist/declarations/src/patch/util.d.ts +2 -0
  8. package/dist/declarations/src/schema/array.d.ts +3 -2
  9. package/dist/declarations/src/schema/boolean.d.ts +3 -2
  10. package/dist/declarations/src/schema/i18n.d.ts +3 -2
  11. package/dist/declarations/src/schema/image.d.ts +3 -2
  12. package/dist/declarations/src/schema/index.d.ts +5 -2
  13. package/dist/declarations/src/schema/literal.d.ts +3 -2
  14. package/dist/declarations/src/schema/number.d.ts +3 -2
  15. package/dist/declarations/src/schema/object.d.ts +3 -2
  16. package/dist/declarations/src/schema/oneOf.d.ts +3 -2
  17. package/dist/declarations/src/schema/richtext.d.ts +3 -2
  18. package/dist/declarations/src/schema/string.d.ts +3 -2
  19. package/dist/declarations/src/schema/union.d.ts +3 -2
  20. package/dist/declarations/src/schema/validation/ValidationError.d.ts +14 -0
  21. package/dist/declarations/src/schema/validation/ValidationFix.d.ts +2 -0
  22. package/dist/index-2fff5ca8.cjs.dev.js +456 -0
  23. package/dist/{index-06df0a5b.esm.js → index-af761363.esm.js} +2 -555
  24. package/dist/index-cac9ecbd.cjs.prod.js +456 -0
  25. package/dist/ops-1b6e0e35.cjs.prod.js +552 -0
  26. package/dist/ops-74661336.esm.js +541 -0
  27. package/dist/ops-ea4827fc.cjs.dev.js +552 -0
  28. package/dist/valbuild-core.cjs.dev.js +151 -531
  29. package/dist/valbuild-core.cjs.prod.js +151 -531
  30. package/dist/valbuild-core.esm.js +65 -445
  31. package/expr/dist/valbuild-core-expr.cjs.dev.js +8 -8
  32. package/expr/dist/valbuild-core-expr.cjs.prod.js +8 -8
  33. package/expr/dist/valbuild-core-expr.esm.js +2 -2
  34. package/package.json +2 -1
  35. package/patch/dist/valbuild-core-patch.cjs.dev.js +30 -21
  36. package/patch/dist/valbuild-core-patch.cjs.prod.js +30 -21
  37. package/patch/dist/valbuild-core-patch.esm.js +12 -4
  38. package/src/expr/repl.ts +2 -2
  39. package/src/index.ts +5 -0
  40. package/src/initVal.ts +1 -1
  41. package/src/patch/index.ts +1 -0
  42. package/src/patch/util.ts +7 -0
  43. package/src/schema/array.ts +45 -4
  44. package/src/schema/boolean.ts +14 -3
  45. package/src/schema/i18n.ts +4 -2
  46. package/src/schema/image.ts +65 -5
  47. package/src/schema/index.ts +23 -2
  48. package/src/schema/literal.ts +24 -3
  49. package/src/schema/number.ts +14 -3
  50. package/src/schema/object.ts +50 -7
  51. package/src/schema/oneOf.ts +3 -2
  52. package/src/schema/richtext.ts +63 -3
  53. package/src/schema/string.ts +14 -3
  54. package/src/schema/union.ts +3 -2
  55. package/src/schema/validation/ValidationError.ts +16 -0
  56. package/src/schema/validation/ValidationFix.ts +6 -0
  57. package/src/schema/validation.test.ts +226 -0
  58. package/src/selector/SelectorProxy.ts +1 -1
  59. package/dist/createClass-012eebbf.esm.js +0 -109
  60. package/dist/createClass-a436dbfe.cjs.dev.js +0 -116
  61. package/dist/createClass-de7426aa.cjs.prod.js +0 -116
  62. package/dist/index-9663f28a.cjs.dev.js +0 -1037
  63. package/dist/index-b2270f8f.cjs.prod.js +0 -1037
  64. package/dist/ops-6fae92a1.esm.js +0 -12
  65. package/dist/ops-87cdbafc.cjs.dev.js +0 -14
  66. package/dist/ops-ae4d1bc2.cjs.prod.js +0 -14
@@ -0,0 +1,756 @@
1
+ 'use strict';
2
+
3
+ var result = require('./result-26f67b40.cjs.prod.js');
4
+
5
+ function _toPrimitive(input, hint) {
6
+ if (typeof input !== "object" || input === null) return input;
7
+ var prim = input[Symbol.toPrimitive];
8
+ if (prim !== undefined) {
9
+ var res = prim.call(input, hint || "default");
10
+ if (typeof res !== "object") return res;
11
+ throw new TypeError("@@toPrimitive must return a primitive value.");
12
+ }
13
+ return (hint === "string" ? String : Number)(input);
14
+ }
15
+
16
+ function _toPropertyKey(arg) {
17
+ var key = _toPrimitive(arg, "string");
18
+ return typeof key === "symbol" ? key : String(key);
19
+ }
20
+
21
+ function _defineProperty(obj, key, value) {
22
+ key = _toPropertyKey(key);
23
+ if (key in obj) {
24
+ Object.defineProperty(obj, key, {
25
+ value: value,
26
+ enumerable: true,
27
+ configurable: true,
28
+ writable: true
29
+ });
30
+ } else {
31
+ obj[key] = value;
32
+ }
33
+ return obj;
34
+ }
35
+
36
+ function ownKeys(object, enumerableOnly) {
37
+ var keys = Object.keys(object);
38
+ if (Object.getOwnPropertySymbols) {
39
+ var symbols = Object.getOwnPropertySymbols(object);
40
+ enumerableOnly && (symbols = symbols.filter(function (sym) {
41
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable;
42
+ })), keys.push.apply(keys, symbols);
43
+ }
44
+ return keys;
45
+ }
46
+ function _objectSpread2(target) {
47
+ for (var i = 1; i < arguments.length; i++) {
48
+ var source = null != arguments[i] ? arguments[i] : {};
49
+ i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
50
+ _defineProperty(target, key, source[key]);
51
+ }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
52
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
53
+ });
54
+ }
55
+ return target;
56
+ }
57
+
58
+ function _arrayWithoutHoles(arr) {
59
+ if (Array.isArray(arr)) return result._arrayLikeToArray(arr);
60
+ }
61
+
62
+ function _iterableToArray(iter) {
63
+ if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
64
+ }
65
+
66
+ function _nonIterableSpread() {
67
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
68
+ }
69
+
70
+ function _toConsumableArray(arr) {
71
+ return _arrayWithoutHoles(arr) || _iterableToArray(arr) || result._unsupportedIterableToArray(arr) || _nonIterableSpread();
72
+ }
73
+
74
+ function _typeof(obj) {
75
+ "@babel/helpers - typeof";
76
+
77
+ return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) {
78
+ return typeof obj;
79
+ } : function (obj) {
80
+ return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
81
+ }, _typeof(obj);
82
+ }
83
+
84
+ function _arrayWithHoles(arr) {
85
+ if (Array.isArray(arr)) return arr;
86
+ }
87
+
88
+ function _iterableToArrayLimit(arr, i) {
89
+ var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"];
90
+ if (null != _i) {
91
+ var _s,
92
+ _e,
93
+ _x,
94
+ _r,
95
+ _arr = [],
96
+ _n = !0,
97
+ _d = !1;
98
+ try {
99
+ if (_x = (_i = _i.call(arr)).next, 0 === i) {
100
+ if (Object(_i) !== _i) return;
101
+ _n = !1;
102
+ } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0);
103
+ } catch (err) {
104
+ _d = !0, _e = err;
105
+ } finally {
106
+ try {
107
+ if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return;
108
+ } finally {
109
+ if (_d) throw _e;
110
+ }
111
+ }
112
+ return _arr;
113
+ }
114
+ }
115
+
116
+ function _nonIterableRest() {
117
+ throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
118
+ }
119
+
120
+ function _slicedToArray(arr, i) {
121
+ return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || result._unsupportedIterableToArray(arr, i) || _nonIterableRest();
122
+ }
123
+
124
+ function _classCallCheck(instance, Constructor) {
125
+ if (!(instance instanceof Constructor)) {
126
+ throw new TypeError("Cannot call a class as a function");
127
+ }
128
+ }
129
+
130
+ function _defineProperties(target, props) {
131
+ for (var i = 0; i < props.length; i++) {
132
+ var descriptor = props[i];
133
+ descriptor.enumerable = descriptor.enumerable || false;
134
+ descriptor.configurable = true;
135
+ if ("value" in descriptor) descriptor.writable = true;
136
+ Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor);
137
+ }
138
+ }
139
+ function _createClass(Constructor, protoProps, staticProps) {
140
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
141
+ if (staticProps) _defineProperties(Constructor, staticProps);
142
+ Object.defineProperty(Constructor, "prototype", {
143
+ writable: false
144
+ });
145
+ return Constructor;
146
+ }
147
+
148
+ function _setPrototypeOf(o, p) {
149
+ _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) {
150
+ o.__proto__ = p;
151
+ return o;
152
+ };
153
+ return _setPrototypeOf(o, p);
154
+ }
155
+
156
+ function _inherits(subClass, superClass) {
157
+ if (typeof superClass !== "function" && superClass !== null) {
158
+ throw new TypeError("Super expression must either be null or a function");
159
+ }
160
+ subClass.prototype = Object.create(superClass && superClass.prototype, {
161
+ constructor: {
162
+ value: subClass,
163
+ writable: true,
164
+ configurable: true
165
+ }
166
+ });
167
+ Object.defineProperty(subClass, "prototype", {
168
+ writable: false
169
+ });
170
+ if (superClass) _setPrototypeOf(subClass, superClass);
171
+ }
172
+
173
+ function _getPrototypeOf(o) {
174
+ _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) {
175
+ return o.__proto__ || Object.getPrototypeOf(o);
176
+ };
177
+ return _getPrototypeOf(o);
178
+ }
179
+
180
+ function _isNativeReflectConstruct() {
181
+ if (typeof Reflect === "undefined" || !Reflect.construct) return false;
182
+ if (Reflect.construct.sham) return false;
183
+ if (typeof Proxy === "function") return true;
184
+ try {
185
+ Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
186
+ return true;
187
+ } catch (e) {
188
+ return false;
189
+ }
190
+ }
191
+
192
+ function _assertThisInitialized(self) {
193
+ if (self === void 0) {
194
+ throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
195
+ }
196
+ return self;
197
+ }
198
+
199
+ function _possibleConstructorReturn(self, call) {
200
+ if (call && (typeof call === "object" || typeof call === "function")) {
201
+ return call;
202
+ } else if (call !== void 0) {
203
+ throw new TypeError("Derived constructors may only return object or undefined");
204
+ }
205
+ return _assertThisInitialized(self);
206
+ }
207
+
208
+ function _createSuper(Derived) {
209
+ var hasNativeReflectConstruct = _isNativeReflectConstruct();
210
+ return function _createSuperInternal() {
211
+ var Super = _getPrototypeOf(Derived),
212
+ result;
213
+ if (hasNativeReflectConstruct) {
214
+ var NewTarget = _getPrototypeOf(this).constructor;
215
+ result = Reflect.construct(Super, arguments, NewTarget);
216
+ } else {
217
+ result = Super.apply(this, arguments);
218
+ }
219
+ return _possibleConstructorReturn(this, result);
220
+ };
221
+ }
222
+
223
+ var Schema = /*#__PURE__*/function () {
224
+ function Schema() {
225
+ _classCallCheck(this, Schema);
226
+ }
227
+ _createClass(Schema, [{
228
+ key: "remote",
229
+ value: function remote() {
230
+ // TODO: Schema<never, "Cannot create remote schema from non-remote source.">
231
+ throw new Error("You need Val Ultra to use .remote()");
232
+ }
233
+
234
+ /** MUTATES! since internal and perf sensitive */
235
+ }, {
236
+ key: "appendValidationError",
237
+ value: function appendValidationError(current, path, message, value) {
238
+ if (current) {
239
+ if (current[path]) {
240
+ current[path].push({
241
+ message: message,
242
+ value: value
243
+ });
244
+ } else {
245
+ current[path] = [{
246
+ message: message,
247
+ value: value
248
+ }];
249
+ }
250
+ return current;
251
+ } else {
252
+ return _defineProperty({}, path, [{
253
+ message: message,
254
+ value: value
255
+ }]);
256
+ }
257
+ }
258
+ }]);
259
+ return Schema;
260
+ }();
261
+
262
+ // TODO: SourceError<"Could not determine type of Schema">
263
+
264
+ /* eslint-disable @typescript-eslint/no-unused-vars */
265
+
266
+ /**
267
+ * Selectors can be used to select parts of a Val module.
268
+ * Unlike queries, joins, aggregates etc is and will not be supported.
269
+ *
270
+ * They are designed to be be used as if they were "normal" JSON data,
271
+ * though some concessions had to be made because of TypeScript limitations.
272
+ *
273
+ * Selectors works equally on source content, defined in code, and remote content.
274
+ *
275
+ * @example
276
+ * // Select the title of a document
277
+ * const titles = useVal(docsVal.map((doc) => doc.title));
278
+ *
279
+ * @example
280
+ * // Match on a union type
281
+ * const titles = useVal(docsVal.map((doc) => doc.fold("type")({
282
+ * newsletter: (newsletter) => newsletter.title,
283
+ * email: (email) => email.subject,
284
+ * }));
285
+ *
286
+ */
287
+
288
+ /**
289
+ * @internal
290
+ */
291
+ var GetSchema = Symbol("GetSchema");
292
+ /**
293
+ /**
294
+ * @internal
295
+ */
296
+ var Path = Symbol("Path");
297
+ /**
298
+ * @internal
299
+ */
300
+ var SourceOrExpr = Symbol("SourceOrExpr");
301
+ /**
302
+ * @internal
303
+ */
304
+ var ValError = Symbol("ValError");
305
+ var GenericSelector = /*#__PURE__*/function () {
306
+ function GenericSelector(valOrExpr, path, schema, error) {
307
+ _classCallCheck(this, GenericSelector);
308
+ this[Path] = path;
309
+ this[SourceOrExpr] = valOrExpr;
310
+ this[ValError] = error;
311
+ this[GetSchema] = schema;
312
+ }
313
+ _createClass(GenericSelector, [{
314
+ key: "assert",
315
+ value: function assert(schema, other) {
316
+ throw new Error("Not implemented");
317
+ }
318
+ }]);
319
+ return GenericSelector;
320
+ }();
321
+
322
+ /**
323
+ * Use this type to convert types that accepts both Source and Selectors
324
+ *
325
+ * An example would be where literals are supported like in most higher order functions (e.g. map in array)
326
+ **/
327
+
328
+ function getSchema(selector) {
329
+ return selector[GetSchema];
330
+ }
331
+
332
+ /* eslint-disable @typescript-eslint/no-unused-vars */
333
+
334
+ var Expr = /*#__PURE__*/_createClass(function Expr(span) {
335
+ _classCallCheck(this, Expr);
336
+ this.span = span;
337
+ });
338
+ var StringLiteral = /*#__PURE__*/function (_Expr) {
339
+ _inherits(StringLiteral, _Expr);
340
+ var _super = _createSuper(StringLiteral);
341
+ function StringLiteral(value, span) {
342
+ var _this;
343
+ _classCallCheck(this, StringLiteral);
344
+ _this = _super.call(this, span);
345
+ _defineProperty(_assertThisInitialized(_this), "type", "StringLiteral");
346
+ _this.value = value;
347
+ return _this;
348
+ }
349
+ _createClass(StringLiteral, [{
350
+ key: "transpile",
351
+ value: function transpile() {
352
+ return "'".concat(this.value, "'");
353
+ }
354
+ }]);
355
+ return StringLiteral;
356
+ }(Expr);
357
+ var Sym = /*#__PURE__*/function (_Expr2) {
358
+ _inherits(Sym, _Expr2);
359
+ var _super2 = _createSuper(Sym);
360
+ function Sym(value, span) {
361
+ var _this2;
362
+ _classCallCheck(this, Sym);
363
+ _this2 = _super2.call(this, span);
364
+ _defineProperty(_assertThisInitialized(_this2), "type", "Sym");
365
+ _this2.value = value;
366
+ return _this2;
367
+ }
368
+ _createClass(Sym, [{
369
+ key: "transpile",
370
+ value: function transpile() {
371
+ return this.value;
372
+ }
373
+ }]);
374
+ return Sym;
375
+ }(Expr);
376
+ var NilSym = new Sym("()");
377
+ var StringTemplate = /*#__PURE__*/function (_Expr3) {
378
+ _inherits(StringTemplate, _Expr3);
379
+ var _super3 = _createSuper(StringTemplate);
380
+ function StringTemplate(children, span) {
381
+ var _this3;
382
+ _classCallCheck(this, StringTemplate);
383
+ _this3 = _super3.call(this, span);
384
+ _defineProperty(_assertThisInitialized(_this3), "type", "StringTemplate");
385
+ _this3.children = children;
386
+ return _this3;
387
+ }
388
+ _createClass(StringTemplate, [{
389
+ key: "transpile",
390
+ value: function transpile() {
391
+ return "'".concat(this.children.map(function (child) {
392
+ if (child instanceof StringLiteral) {
393
+ return child.value;
394
+ } else {
395
+ return "${".concat(child.transpile(), "}");
396
+ }
397
+ }).join(""), "'");
398
+ }
399
+ }]);
400
+ return StringTemplate;
401
+ }(Expr);
402
+ var Call = /*#__PURE__*/function (_Expr4) {
403
+ _inherits(Call, _Expr4);
404
+ var _super4 = _createSuper(Call);
405
+ function Call(children, isAnon, span) {
406
+ var _this4;
407
+ _classCallCheck(this, Call);
408
+ _this4 = _super4.call(this, span);
409
+ _defineProperty(_assertThisInitialized(_this4), "type", "Call");
410
+ _this4.children = children;
411
+ _this4.isAnon = isAnon;
412
+ return _this4;
413
+ }
414
+ _createClass(Call, [{
415
+ key: "transpile",
416
+ value: function transpile() {
417
+ if (this.isAnon) {
418
+ return "!(".concat(this.children.map(function (child) {
419
+ return child.transpile();
420
+ }).join(" "), ")");
421
+ }
422
+ return "(".concat(this.children.map(function (child) {
423
+ return child.transpile();
424
+ }).join(" "), ")");
425
+ }
426
+ }]);
427
+ return Call;
428
+ }(Expr);
429
+
430
+ /* Branded extension types: file, remote, i18n */
431
+ var VAL_EXTENSION = "_type";
432
+
433
+ /**
434
+ * A phantom type parameter is one that doesn't show up at runtime, but is checked statically (and only) at compile time.
435
+ *
436
+ * An example where this is useful is remote types, where the type of the remote source is known at compile time,
437
+ * but the value is not there before it is fetched.
438
+ *
439
+ * @example
440
+ * type Example<T> = string & PhantomType<T>;
441
+ *
442
+ **/
443
+
444
+ var FILE_REF_PROP = "_ref";
445
+
446
+ /**
447
+ * A file source represents the path to a (local) file.
448
+ *
449
+ * It will be resolved into a Asset object.
450
+ *
451
+ */
452
+
453
+ function file(ref, metadata) {
454
+ var _ref;
455
+ return _ref = {}, _defineProperty(_ref, FILE_REF_PROP, ref), _defineProperty(_ref, VAL_EXTENSION, "file"), _defineProperty(_ref, "metadata", metadata), _ref;
456
+ }
457
+ function isFile(obj) {
458
+ return _typeof(obj) === "object" && obj !== null && VAL_EXTENSION in obj && obj[VAL_EXTENSION] === "file" && FILE_REF_PROP in obj && typeof obj[FILE_REF_PROP] === "string";
459
+ }
460
+
461
+ var ImageSchema = /*#__PURE__*/function (_Schema) {
462
+ _inherits(ImageSchema, _Schema);
463
+ var _super = _createSuper(ImageSchema);
464
+ function ImageSchema(options) {
465
+ var _this;
466
+ var opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
467
+ _classCallCheck(this, ImageSchema);
468
+ _this = _super.call(this);
469
+ _this.options = options;
470
+ _this.opt = opt;
471
+ return _this;
472
+ }
473
+ _createClass(ImageSchema, [{
474
+ key: "validate",
475
+ value: function validate(path, src) {
476
+ if (this.opt && (src === null || src === undefined)) {
477
+ return false;
478
+ }
479
+ if (src === null || src === undefined) {
480
+ return _defineProperty({}, path, [{
481
+ message: "Non-optional image was null or undefined.",
482
+ value: src
483
+ }]);
484
+ }
485
+ if (typeof src[FILE_REF_PROP] !== "string") {
486
+ return _defineProperty({}, path, [{
487
+ message: "Image did not have a file reference string. Got: ".concat(_typeof(src[FILE_REF_PROP])),
488
+ value: src
489
+ }]);
490
+ }
491
+ if (src[VAL_EXTENSION] !== "file") {
492
+ return _defineProperty({}, path, [{
493
+ message: "Image did not have the valid file extension type. Got: ".concat(src[VAL_EXTENSION]),
494
+ value: src
495
+ }]);
496
+ }
497
+ if (src.metadata) {
498
+ return _defineProperty({}, path, [{
499
+ message: "Found metadata, but it could not be validated. Image metadata must be an object with the required props: width (positive number), height (positive number) and sha256 (string of length 64 of the base16 hash).",
500
+ // These validation errors will have to be picked up by logic outside of this package and revalidated. Reasons: 1) we have to read files to verify the metadata, which is handled differently in different runtimes (Browser, QuickJS, Node.js); 2) we want to keep this package dependency free.
501
+ value: src,
502
+ fixes: ["image:replace-metadata"]
503
+ }]);
504
+ }
505
+ return _defineProperty({}, path, [{
506
+ message: "Could not validate Image metadata.",
507
+ value: src,
508
+ fixes: ["image:add-metadata"]
509
+ }]);
510
+ }
511
+ }, {
512
+ key: "assert",
513
+ value: function assert(src) {
514
+ if (this.opt && (src === null || src === undefined)) {
515
+ return true;
516
+ }
517
+ return (src === null || src === void 0 ? void 0 : src[FILE_REF_PROP]) === "image" && (src === null || src === void 0 ? void 0 : src[VAL_EXTENSION]) === "file";
518
+ }
519
+ }, {
520
+ key: "optional",
521
+ value: function optional() {
522
+ return new ImageSchema(this.options, true);
523
+ }
524
+ }, {
525
+ key: "serialize",
526
+ value: function serialize() {
527
+ return {
528
+ type: "image",
529
+ options: this.options,
530
+ opt: this.opt
531
+ };
532
+ }
533
+ }]);
534
+ return ImageSchema;
535
+ }(Schema);
536
+ var image = function image(options) {
537
+ return new ImageSchema(options);
538
+ };
539
+ var convertImageSource = function convertImageSource(src) {
540
+ var _src$metadata;
541
+ // TODO: /public should be configurable
542
+ return {
543
+ url: src[FILE_REF_PROP].slice("/public".length) + "?sha256=".concat((_src$metadata = src.metadata) === null || _src$metadata === void 0 ? void 0 : _src$metadata.sha256),
544
+ metadata: src.metadata
545
+ };
546
+ };
547
+
548
+ function isSerializedVal(val) {
549
+ return _typeof(val) === "object" && val !== null && val !== undefined && ("val" in val || "valPath" in val);
550
+ }
551
+ function isVal(val) {
552
+ return _typeof(val) === "object" && val !== null && val !== undefined && Path in val && "val" in val;
553
+ }
554
+
555
+ /**
556
+ * The path of the source value.
557
+ *
558
+ * @example
559
+ * '/app/blogs.0.text' // the text property of the first element of the /app/blogs module
560
+ */
561
+
562
+ /**
563
+ * The path inside the module.
564
+ *
565
+ * @example
566
+ * '0."text"' // the text property of the first element of the module
567
+ */
568
+
569
+ /**
570
+ * The id of the module.
571
+ *
572
+ * @example
573
+ * '/app/blogs' // the /app/blogs module
574
+ */
575
+
576
+ function getValPath(valOrSelector) {
577
+ return valOrSelector[Path];
578
+ }
579
+
580
+ function hasOwn(obj, prop) {
581
+ return Object.prototype.hasOwnProperty.call(obj, prop);
582
+ }
583
+ function _andThen(f, source, path) {
584
+ if (source) {
585
+ return newSelectorProxy(f(newSelectorProxy(source, path)));
586
+ }
587
+ return newSelectorProxy(source, path);
588
+ }
589
+ function isSelector(source) {
590
+ return _typeof(source) === "object" && source !== null && (SourceOrExpr in source || Path in source);
591
+ }
592
+ function newSelectorProxy(source, path, moduleSchema) {
593
+ var _ref;
594
+ if (_typeof(source) === "object") {
595
+ if (isSelector(source)) {
596
+ return source;
597
+ } else if (isSerializedVal(source)) {
598
+ return newSelectorProxy(source.val, source.valPath);
599
+ }
600
+ }
601
+ if (source && source[FILE_REF_PROP] && source[VAL_EXTENSION] === "file") {
602
+ var fileRef = source[FILE_REF_PROP];
603
+ if (typeof fileRef !== "string") {
604
+ throw Error("Invalid file ref: " + fileRef);
605
+ }
606
+ return newSelectorProxy(convertImageSource(source), path, moduleSchema);
607
+ }
608
+ switch (_typeof(source)) {
609
+ case "function":
610
+ case "symbol":
611
+ throw Error("Invalid selector type: ".concat(_typeof(source), ": ").concat(source));
612
+ case "object":
613
+ // Handles both objects and arrays!
614
+ if (source !== null) {
615
+ return new Proxy(source, {
616
+ // TODO: see proxy docs if we want more traps
617
+ has: function has(target, prop) {
618
+ if (prop === SourceOrExpr) {
619
+ return true;
620
+ }
621
+ if (prop === Path) {
622
+ return true;
623
+ }
624
+ if (prop === "andThen") {
625
+ return true;
626
+ }
627
+ if (prop === GetSchema) {
628
+ return true;
629
+ }
630
+ return prop in target;
631
+ },
632
+ get: function get(target, prop) {
633
+ if (prop === SourceOrExpr) {
634
+ return source;
635
+ }
636
+ if (prop === Path) {
637
+ return path;
638
+ }
639
+ if (prop === GetSchema) {
640
+ return moduleSchema;
641
+ }
642
+ if (prop === "andThen") {
643
+ return function (f) {
644
+ return _andThen(f, source, path);
645
+ };
646
+ }
647
+ if (Array.isArray(target)) {
648
+ if (prop === "filter") {
649
+ return function (f) {
650
+ var filtered = target.map(function (a, i) {
651
+ return newSelectorProxy(a, createValPathOfItem(path, i), moduleSchema === null || moduleSchema === void 0 ? void 0 : moduleSchema.item);
652
+ }).filter(function (a) {
653
+ if (f && f instanceof Schema) {
654
+ return f.assert(unValify(a));
655
+ } else {
656
+ return unValify(f(a));
657
+ }
658
+ });
659
+ return newSelectorProxy(filtered, path, moduleSchema);
660
+ };
661
+ } else if (prop === "map") {
662
+ return function (f) {
663
+ var filtered = target.map(function (a, i) {
664
+ var valueOrSelector = f(newSelectorProxy(a, createValPathOfItem(path, i), moduleSchema === null || moduleSchema === void 0 ? void 0 : moduleSchema.item), newSelectorProxy(i));
665
+ if (isSelector(valueOrSelector)) {
666
+ return valueOrSelector;
667
+ }
668
+ return newSelectorProxy(valueOrSelector);
669
+ });
670
+ return newSelectorProxy(filtered, path, moduleSchema);
671
+ };
672
+ }
673
+ }
674
+ if (Array.isArray(target) && prop === "length") {
675
+ return newSelectorProxy(target.length);
676
+ }
677
+ var reflectedValue = Reflect.get(target, prop);
678
+ if (hasOwn(source, prop)) {
679
+ if (!Number.isNaN(Number(prop))) {
680
+ return newSelectorProxy(reflectedValue, createValPathOfItem(path, Number(prop)), moduleSchema === null || moduleSchema === void 0 ? void 0 : moduleSchema.item);
681
+ }
682
+ return newSelectorProxy(reflectedValue, createValPathOfItem(path, prop), moduleSchema === null || moduleSchema === void 0 ? void 0 : moduleSchema.items[prop]);
683
+ }
684
+ return reflectedValue;
685
+ }
686
+ });
687
+ }
688
+ // intentional fallthrough
689
+ // eslint-disable-next-line no-fallthrough
690
+ default:
691
+ return _ref = {
692
+ eq: function eq(other) {
693
+ var otherValue = other;
694
+ if (isSelector(other)) {
695
+ otherValue = other[SourceOrExpr];
696
+ if (otherValue instanceof Expr) {
697
+ throw Error("TODO: Cannot evaluate equality with an Expr");
698
+ }
699
+ }
700
+ return newSelectorProxy(source === otherValue, undefined);
701
+ },
702
+ andThen: function andThen(f) {
703
+ return _andThen(f, source === undefined ? null : source, path);
704
+ }
705
+ }, _defineProperty(_ref, SourceOrExpr, source === undefined ? null : source), _defineProperty(_ref, Path, path), _defineProperty(_ref, GetSchema, moduleSchema), _ref;
706
+ }
707
+ }
708
+ function createValPathOfItem(arrayPath, prop) {
709
+ if (_typeof(prop) === "symbol") {
710
+ throw Error("Cannot create val path of array item with symbol prop: ".concat(prop.toString()));
711
+ }
712
+ return arrayPath && "".concat(arrayPath, ".").concat(JSON.stringify(prop));
713
+ }
714
+
715
+ // TODO: could we do .val on the objects instead?
716
+ function unValify(valueOrSelector) {
717
+ if (_typeof(valueOrSelector) === "object" && (SourceOrExpr in valueOrSelector || Path in valueOrSelector)) {
718
+ var selectorValue = valueOrSelector[SourceOrExpr];
719
+ return selectorValue;
720
+ }
721
+ return valueOrSelector;
722
+ }
723
+
724
+ exports.Call = Call;
725
+ exports.Expr = Expr;
726
+ exports.FILE_REF_PROP = FILE_REF_PROP;
727
+ exports.GenericSelector = GenericSelector;
728
+ exports.ImageSchema = ImageSchema;
729
+ exports.NilSym = NilSym;
730
+ exports.Path = Path;
731
+ exports.Schema = Schema;
732
+ exports.SourceOrExpr = SourceOrExpr;
733
+ exports.StringLiteral = StringLiteral;
734
+ exports.StringTemplate = StringTemplate;
735
+ exports.Sym = Sym;
736
+ exports.VAL_EXTENSION = VAL_EXTENSION;
737
+ exports._classCallCheck = _classCallCheck;
738
+ exports._createClass = _createClass;
739
+ exports._createSuper = _createSuper;
740
+ exports._defineProperty = _defineProperty;
741
+ exports._inherits = _inherits;
742
+ exports._objectSpread2 = _objectSpread2;
743
+ exports._slicedToArray = _slicedToArray;
744
+ exports._toConsumableArray = _toConsumableArray;
745
+ exports._typeof = _typeof;
746
+ exports.convertImageSource = convertImageSource;
747
+ exports.createValPathOfItem = createValPathOfItem;
748
+ exports.file = file;
749
+ exports.getSchema = getSchema;
750
+ exports.getValPath = getValPath;
751
+ exports.image = image;
752
+ exports.isFile = isFile;
753
+ exports.isSelector = isSelector;
754
+ exports.isSerializedVal = isSerializedVal;
755
+ exports.isVal = isVal;
756
+ exports.newSelectorProxy = newSelectorProxy;