@valbuild/core 0.72.4 → 0.73.1

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 (55) hide show
  1. package/dist/declarations/src/index.d.ts +20 -4
  2. package/dist/declarations/src/initSchema.d.ts +3 -3
  3. package/dist/declarations/src/initVal.d.ts +3 -1
  4. package/dist/declarations/src/patch/index.d.ts +1 -1
  5. package/dist/declarations/src/patch/operation.d.ts +3 -0
  6. package/dist/declarations/src/remote/fileHash.d.ts +5 -0
  7. package/dist/declarations/src/remote/splitRemoteRef.d.ts +13 -0
  8. package/dist/declarations/src/remote/validationBasis.d.ts +13 -0
  9. package/dist/declarations/src/schema/file.d.ts +7 -3
  10. package/dist/declarations/src/schema/image.d.ts +7 -3
  11. package/dist/declarations/src/schema/remote.d.ts +11 -0
  12. package/dist/declarations/src/schema/richtext.d.ts +5 -3
  13. package/dist/declarations/src/schema/validation/ValidationFix.d.ts +1 -1
  14. package/dist/declarations/src/selector/index.d.ts +2 -1
  15. package/dist/declarations/src/source/file.d.ts +3 -3
  16. package/dist/declarations/src/source/image.d.ts +2 -2
  17. package/dist/declarations/src/source/index.d.ts +3 -4
  18. package/dist/declarations/src/source/remote.d.ts +23 -0
  19. package/dist/declarations/src/source/richtext.d.ts +30 -3
  20. package/dist/declarations/src/val/index.d.ts +4 -3
  21. package/dist/{index-25ee166a.cjs.prod.js → index-da9933cf.cjs.prod.js} +529 -1332
  22. package/dist/{index-a49a4c82.cjs.dev.js → index-f6fd3df3.cjs.dev.js} +529 -1332
  23. package/dist/{index-e01fe365.esm.js → index-fee3aa6d.esm.js} +531 -1326
  24. package/dist/{result-168dfc1d.esm.js → result-daff1cae.esm.js} +1 -1
  25. package/dist/valbuild-core.cjs.dev.js +27 -27
  26. package/dist/valbuild-core.cjs.prod.js +27 -27
  27. package/dist/valbuild-core.esm.js +2 -2
  28. package/fp/dist/valbuild-core-fp.esm.js +1 -1
  29. package/package.json +2 -9
  30. package/patch/dist/valbuild-core-patch.cjs.dev.js +38 -37
  31. package/patch/dist/valbuild-core-patch.cjs.prod.js +38 -37
  32. package/patch/dist/valbuild-core-patch.esm.js +5 -4
  33. package/dist/declarations/src/expr/eval.d.ts +0 -20
  34. package/dist/declarations/src/expr/expr.d.ts +0 -32
  35. package/dist/declarations/src/expr/index.d.ts +0 -3
  36. package/dist/declarations/src/expr/parser.d.ts +0 -8
  37. package/dist/declarations/src/future/fetchVal.d.ts +0 -5
  38. package/dist/declarations/src/selector/future/array.d.ts +0 -17
  39. package/dist/declarations/src/selector/future/boolean.d.ts +0 -2
  40. package/dist/declarations/src/selector/future/file.d.ts +0 -9
  41. package/dist/declarations/src/selector/future/i18n.d.ts +0 -11
  42. package/dist/declarations/src/selector/future/index.d.ts +0 -82
  43. package/dist/declarations/src/selector/future/number.d.ts +0 -2
  44. package/dist/declarations/src/selector/future/object.d.ts +0 -10
  45. package/dist/declarations/src/selector/future/primitive.d.ts +0 -9
  46. package/dist/declarations/src/selector/future/remote.d.ts +0 -7
  47. package/dist/declarations/src/selector/future/string.d.ts +0 -2
  48. package/dist/declarations/src/source/future/i18n.d.ts +0 -29
  49. package/dist/declarations/src/source/future/remote.d.ts +0 -29
  50. package/expr/dist/valbuild-core-expr.cjs.d.ts +0 -2
  51. package/expr/dist/valbuild-core-expr.cjs.dev.js +0 -17
  52. package/expr/dist/valbuild-core-expr.cjs.js +0 -7
  53. package/expr/dist/valbuild-core-expr.cjs.prod.js +0 -17
  54. package/expr/dist/valbuild-core-expr.esm.js +0 -2
  55. package/expr/package.json +0 -4
@@ -229,12 +229,12 @@ var Schema = /*#__PURE__*/function () {
229
229
  /**
230
230
  * @internal
231
231
  */
232
- var GetSchema$1 = Symbol("GetSchema");
232
+ var GetSchema = Symbol("GetSchema");
233
233
  /**
234
234
  /**
235
235
  * @internal
236
236
  */
237
- var Path$1 = Symbol("Path");
237
+ var Path = Symbol("Path");
238
238
  /**
239
239
  * @internal
240
240
  */
@@ -245,10 +245,10 @@ var GetSource = Symbol("GetSource");
245
245
  var ValError = Symbol("ValError");
246
246
  var GenericSelector = /*#__PURE__*/_createClass(function GenericSelector(valOrExpr, path, schema, error) {
247
247
  _classCallCheck(this, GenericSelector);
248
- this[Path$1] = path;
248
+ this[Path] = path;
249
249
  this[GetSource] = valOrExpr;
250
250
  this[ValError] = error;
251
- this[GetSchema$1] = schema;
251
+ this[GetSchema] = schema;
252
252
  });
253
253
 
254
254
  /**
@@ -258,99 +258,9 @@ var GenericSelector = /*#__PURE__*/_createClass(function GenericSelector(valOrEx
258
258
  **/
259
259
 
260
260
  function getSchema(selector) {
261
- return selector[GetSchema$1];
261
+ return selector[GetSchema];
262
262
  }
263
263
 
264
- /* eslint-disable @typescript-eslint/no-unused-vars */
265
-
266
- var Expr = /*#__PURE__*/_createClass(function Expr(span) {
267
- _classCallCheck(this, Expr);
268
- this.span = span;
269
- });
270
- var StringLiteral = /*#__PURE__*/function (_Expr) {
271
- function StringLiteral(value, span) {
272
- var _this;
273
- _classCallCheck(this, StringLiteral);
274
- _this = _callSuper(this, StringLiteral, [span]);
275
- _defineProperty(_this, "type", "StringLiteral");
276
- _this.value = value;
277
- return _this;
278
- }
279
- _inherits(StringLiteral, _Expr);
280
- return _createClass(StringLiteral, [{
281
- key: "transpile",
282
- value: function transpile() {
283
- return "'".concat(this.value, "'");
284
- }
285
- }]);
286
- }(Expr);
287
- var Sym = /*#__PURE__*/function (_Expr2) {
288
- function Sym(value, span) {
289
- var _this2;
290
- _classCallCheck(this, Sym);
291
- _this2 = _callSuper(this, Sym, [span]);
292
- _defineProperty(_this2, "type", "Sym");
293
- _this2.value = value;
294
- return _this2;
295
- }
296
- _inherits(Sym, _Expr2);
297
- return _createClass(Sym, [{
298
- key: "transpile",
299
- value: function transpile() {
300
- return this.value;
301
- }
302
- }]);
303
- }(Expr);
304
- var NilSym = new Sym("()");
305
- var StringTemplate = /*#__PURE__*/function (_Expr3) {
306
- function StringTemplate(children, span) {
307
- var _this3;
308
- _classCallCheck(this, StringTemplate);
309
- _this3 = _callSuper(this, StringTemplate, [span]);
310
- _defineProperty(_this3, "type", "StringTemplate");
311
- _this3.children = children;
312
- return _this3;
313
- }
314
- _inherits(StringTemplate, _Expr3);
315
- return _createClass(StringTemplate, [{
316
- key: "transpile",
317
- value: function transpile() {
318
- return "'".concat(this.children.map(function (child) {
319
- if (child instanceof StringLiteral) {
320
- return child.value;
321
- } else {
322
- return "${".concat(child.transpile(), "}");
323
- }
324
- }).join(""), "'");
325
- }
326
- }]);
327
- }(Expr);
328
- var Call = /*#__PURE__*/function (_Expr4) {
329
- function Call(children, isAnon, span) {
330
- var _this4;
331
- _classCallCheck(this, Call);
332
- _this4 = _callSuper(this, Call, [span]);
333
- _defineProperty(_this4, "type", "Call");
334
- _this4.children = children;
335
- _this4.isAnon = isAnon;
336
- return _this4;
337
- }
338
- _inherits(Call, _Expr4);
339
- return _createClass(Call, [{
340
- key: "transpile",
341
- value: function transpile() {
342
- if (this.isAnon) {
343
- return "!(".concat(this.children.map(function (child) {
344
- return child.transpile();
345
- }).join(" "), ")");
346
- }
347
- return "(".concat(this.children.map(function (child) {
348
- return child.transpile();
349
- }).join(" "), ")");
350
- }
351
- }]);
352
- }(Expr);
353
-
354
264
  /* Branded extension types: file, remote, i18n */
355
265
  var VAL_EXTENSION = "_type";
356
266
 
@@ -421,14 +331,21 @@ var FileSchema = /*#__PURE__*/function (_Schema) {
421
331
  function FileSchema(options) {
422
332
  var _this;
423
333
  var opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
334
+ var isRemote = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
424
335
  _classCallCheck(this, FileSchema);
425
336
  _this = _callSuper(this, FileSchema);
426
337
  _this.options = options;
427
338
  _this.opt = opt;
339
+ _this.isRemote = isRemote;
428
340
  return _this;
429
341
  }
430
342
  _inherits(FileSchema, _Schema);
431
343
  return _createClass(FileSchema, [{
344
+ key: "remote",
345
+ value: function remote() {
346
+ return new FileSchema(this.options, this.opt, true);
347
+ }
348
+ }, {
432
349
  key: "validate",
433
350
  value: function validate(path, src) {
434
351
  if (this.opt && (src === null || src === undefined)) {
@@ -446,6 +363,27 @@ var FileSchema = /*#__PURE__*/function (_Schema) {
446
363
  value: src
447
364
  }]);
448
365
  }
366
+ if (this.isRemote && src[VAL_EXTENSION] !== "remote") {
367
+ return _defineProperty({}, path, [{
368
+ message: "Expected a remote file, but got a local file.",
369
+ value: src,
370
+ fixes: ["file:upload-remote"]
371
+ }]);
372
+ }
373
+ if (this.isRemote && src[VAL_EXTENSION] === "remote") {
374
+ return _defineProperty({}, path, [{
375
+ message: "Remote file was not checked.",
376
+ value: src,
377
+ fixes: ["file:check-remote"]
378
+ }]);
379
+ }
380
+ if (!this.isRemote && src[VAL_EXTENSION] === "remote") {
381
+ return _defineProperty({}, path, [{
382
+ message: "Expected locale file, but found remote.",
383
+ value: src,
384
+ fixes: ["file:download-remote"]
385
+ }]);
386
+ }
449
387
  if (src[VAL_EXTENSION] !== "file") {
450
388
  return _defineProperty({}, path, [{
451
389
  message: "File did not have the valid file extension type. Got: ".concat(src[VAL_EXTENSION]),
@@ -453,10 +391,10 @@ var FileSchema = /*#__PURE__*/function (_Schema) {
453
391
  fixes: ["file:change-extension", "file:check-metadata"]
454
392
  }]);
455
393
  }
456
- var _ref4 = this.options || {},
457
- accept = _ref4.accept;
458
- var _ref5 = src.metadata || {},
459
- mimeType = _ref5.mimeType;
394
+ var _ref7 = this.options || {},
395
+ accept = _ref7.accept;
396
+ var _ref8 = src.metadata || {},
397
+ mimeType = _ref8.mimeType;
460
398
  if (accept && mimeType && !mimeType.includes("/")) {
461
399
  return _defineProperty({}, path, [{
462
400
  message: "Invalid mime type format. Got: ".concat(mimeType),
@@ -576,7 +514,8 @@ var FileSchema = /*#__PURE__*/function (_Schema) {
576
514
  return {
577
515
  type: "file",
578
516
  options: this.options,
579
- opt: this.opt
517
+ opt: this.opt,
518
+ remote: this.isRemote
580
519
  };
581
520
  }
582
521
  }]);
@@ -604,11 +543,8 @@ function convertFileSource(src) {
604
543
  };
605
544
  }
606
545
 
607
- function isSerializedVal(val) {
608
- return _typeof(val) === "object" && val !== null && val !== undefined && ("val" in val || "valPath" in val);
609
- }
610
546
  function isVal(val) {
611
- return _typeof(val) === "object" && val !== null && val !== undefined && Path$1 in val && "val" in val;
547
+ return _typeof(val) === "object" && val !== null && val !== undefined && Path in val && "val" in val;
612
548
  }
613
549
 
614
550
  /**
@@ -637,136 +573,9 @@ function isVal(val) {
637
573
  */
638
574
 
639
575
  function getValPath(valOrSelector) {
640
- return valOrSelector[Path$1];
576
+ return valOrSelector[Path];
641
577
  }
642
578
 
643
- function hasOwn$2(obj, prop) {
644
- return Object.prototype.hasOwnProperty.call(obj, prop);
645
- }
646
- function _andThen$1(f, source, path) {
647
- if (source) {
648
- return newSelectorProxy$1(f(newSelectorProxy$1(source, path)));
649
- }
650
- return newSelectorProxy$1(source, path);
651
- }
652
- function isSelector$1(source) {
653
- return _typeof(source) === "object" && source !== null && (GetSource in source || Path$1 in source);
654
- }
655
- function newSelectorProxy$1(source, path, moduleSchema) {
656
- if (_typeof(source) === "object") {
657
- if (isSelector$1(source)) {
658
- return source;
659
- } else if (isSerializedVal(source)) {
660
- return newSelectorProxy$1(source.val, source.valPath);
661
- }
662
- }
663
- if (source && source[FILE_REF_PROP] && source[VAL_EXTENSION] === "file") {
664
- var fileRef = source[FILE_REF_PROP];
665
- if (typeof fileRef !== "string") {
666
- throw Error("Invalid file ref: " + fileRef);
667
- }
668
- return newSelectorProxy$1(convertFileSource(source), path, moduleSchema);
669
- }
670
- switch (_typeof(source)) {
671
- case "function":
672
- case "symbol":
673
- throw Error("Invalid selector type: ".concat(_typeof(source), ": ").concat(source));
674
- case "object":
675
- // Handles both objects and arrays!
676
- if (source !== null) {
677
- return new Proxy(source, {
678
- // TODO: see proxy docs if we want more traps
679
- has: function has(target, prop) {
680
- if (prop === GetSource) {
681
- return true;
682
- }
683
- if (prop === Path$1) {
684
- return true;
685
- }
686
- if (prop === "andThen") {
687
- return true;
688
- }
689
- if (prop === GetSchema$1) {
690
- return true;
691
- }
692
- return prop in target;
693
- },
694
- get: function get(target, prop) {
695
- if (prop === GetSource) {
696
- return source;
697
- }
698
- if (prop === Path$1) {
699
- return path;
700
- }
701
- if (prop === GetSchema$1) {
702
- return moduleSchema;
703
- }
704
- if (prop === "andThen") {
705
- return function (f) {
706
- return _andThen$1(f, source, path);
707
- };
708
- }
709
- if (Array.isArray(target)) {
710
- if (prop === "filter") {
711
- return function (f) {
712
- var filtered = target.map(function (a, i) {
713
- return newSelectorProxy$1(a, createValPathOfItem(path, i), moduleSchema === null || moduleSchema === void 0 ? void 0 : moduleSchema.item);
714
- }).filter(function (a) {
715
- if (f && f instanceof Schema) {
716
- return f.assert(path || "", unValify$1(a)).success;
717
- } else {
718
- return unValify$1(f(a));
719
- }
720
- });
721
- return newSelectorProxy$1(filtered, path, moduleSchema);
722
- };
723
- } else if (prop === "map") {
724
- return function (f) {
725
- var filtered = target.map(function (a, i) {
726
- var valueOrSelector = f(newSelectorProxy$1(a, createValPathOfItem(path, i), moduleSchema === null || moduleSchema === void 0 ? void 0 : moduleSchema.item), newSelectorProxy$1(i));
727
- if (isSelector$1(valueOrSelector)) {
728
- return valueOrSelector;
729
- }
730
- return newSelectorProxy$1(valueOrSelector);
731
- });
732
- return newSelectorProxy$1(filtered, path, moduleSchema);
733
- };
734
- }
735
- }
736
- if (Array.isArray(target) && prop === "length") {
737
- return newSelectorProxy$1(target.length);
738
- }
739
- var reflectedValue = Reflect.get(target, prop);
740
- if (hasOwn$2(source, prop)) {
741
- if (!Number.isNaN(Number(prop))) {
742
- return newSelectorProxy$1(reflectedValue, createValPathOfItem(path, Number(prop)), moduleSchema === null || moduleSchema === void 0 ? void 0 : moduleSchema.item);
743
- }
744
- return newSelectorProxy$1(reflectedValue, createValPathOfItem(path, prop), moduleSchema === null || moduleSchema === void 0 ? void 0 : moduleSchema.items[prop]);
745
- }
746
- return reflectedValue;
747
- }
748
- });
749
- }
750
- // intentional fallthrough
751
- // eslint-disable-next-line no-fallthrough
752
- default:
753
- return _defineProperty(_defineProperty(_defineProperty({
754
- eq: function eq(other) {
755
- var otherValue = other;
756
- if (isSelector$1(other)) {
757
- otherValue = other[GetSource];
758
- if (otherValue instanceof Expr) {
759
- throw Error("TODO: Cannot evaluate equality with an Expr");
760
- }
761
- }
762
- return newSelectorProxy$1(source === otherValue, undefined);
763
- },
764
- andThen: function andThen(f) {
765
- return _andThen$1(f, source === undefined ? null : source, path);
766
- }
767
- }, GetSource, source === undefined ? null : source), Path$1, path), GetSchema$1, moduleSchema);
768
- }
769
- }
770
579
  function createValPathOfItem(arrayPath, prop) {
771
580
  if (_typeof(prop) === "symbol") {
772
581
  throw Error("Cannot create val path of array item with symbol prop: ".concat(prop.toString()));
@@ -794,15 +603,6 @@ function unsafeCreateSourcePath(path, itemKey) {
794
603
  return "".concat(path).concat(Internal.ModuleFilePathSep).concat(JSON.stringify(itemKey));
795
604
  }
796
605
 
797
- // TODO: could we do .val on the objects instead?
798
- function unValify$1(valueOrSelector) {
799
- if (_typeof(valueOrSelector) === "object" && (GetSource in valueOrSelector || Path$1 in valueOrSelector)) {
800
- var selectorValue = valueOrSelector[GetSource];
801
- return selectorValue;
802
- }
803
- return valueOrSelector;
804
- }
805
-
806
606
  var ObjectSchema = /*#__PURE__*/function (_Schema) {
807
607
  function ObjectSchema(items) {
808
608
  var _this;
@@ -946,634 +746,6 @@ var object = function object(schema) {
946
746
  return new ObjectSchema(schema);
947
747
  };
948
748
 
949
- var WHITE_SPACE = ["\n", "\r", "\t", " "];
950
- function tokenize(input) {
951
- var tokens = [];
952
- var cursor = 0;
953
- while (cursor < input.length) {
954
- var _char = input[cursor];
955
- var peek = input[cursor + 1];
956
- // TODO: remove this not used any more
957
- if (_char === "!" && peek === "(") {
958
- tokens.push({
959
- type: "!(",
960
- span: [cursor, cursor + 1]
961
- });
962
- cursor += 2;
963
- } else if (_char === "(") {
964
- tokens.push({
965
- type: "(",
966
- span: [cursor, cursor]
967
- });
968
- cursor++;
969
- } else if (_char === ")") {
970
- tokens.push({
971
- type: ")",
972
- span: [cursor, cursor]
973
- });
974
- cursor++;
975
- } else if (_char === "'" || _char === "}") {
976
- var start = cursor;
977
- var value = "";
978
- var unescapedValue = "";
979
- var escaped = false;
980
- if (_char === "}") {
981
- tokens.push({
982
- type: "}",
983
- span: [cursor, cursor]
984
- });
985
- } else if (_char === "'") {
986
- tokens.push({
987
- type: "'",
988
- span: [cursor, cursor]
989
- });
990
- }
991
- while (cursor < input.length) {
992
- if (_char === "\\") {
993
- escaped = !escaped;
994
- } else {
995
- escaped = false;
996
- }
997
- if (peek === "'" && !escaped) {
998
- cursor += 2;
999
- break;
1000
- } else if (_char === "$" && peek === "{") {
1001
- cursor += 2;
1002
- break;
1003
- }
1004
- cursor++;
1005
- _char = input[cursor];
1006
- peek = input[cursor + 1];
1007
- if (!(_char === "$" && peek === "{") && cursor < input.length) {
1008
- if (!(_char === "\\" && !escaped // counter-intuitive, but escape just became false if this was a backslash we want to escape
1009
- )) {
1010
- value += _char;
1011
- }
1012
- unescapedValue += _char;
1013
- }
1014
- }
1015
- var cursorOffset = peek === "'" && !escaped ? 2 : _char === "$" && peek === "{" ? 3 : 1;
1016
- if (value) {
1017
- tokens.push(_objectSpread2({
1018
- type: "string",
1019
- span: [start + 1, cursor - cursorOffset],
1020
- value: value
1021
- }, unescapedValue !== value && {
1022
- unescapedValue: unescapedValue
1023
- }));
1024
- }
1025
- if (peek === "'" && !escaped) {
1026
- tokens.push({
1027
- type: "'",
1028
- span: [cursor - 1, cursor - 1]
1029
- });
1030
- } else if (_char === "$" && peek === "{") {
1031
- tokens.push({
1032
- type: "${",
1033
- span: [cursor - cursorOffset + 1, cursor - 1]
1034
- });
1035
- }
1036
- } else if (WHITE_SPACE.includes(_char)) {
1037
- var _start = cursor;
1038
- while (WHITE_SPACE.includes(input[cursor]) && cursor < input.length) {
1039
- cursor++;
1040
- }
1041
- tokens.push({
1042
- type: "ws",
1043
- span: [_start, cursor - 1]
1044
- });
1045
- } else {
1046
- var _value = "";
1047
- var _start2 = cursor;
1048
- do {
1049
- _char = input[cursor];
1050
- peek = input[cursor + 1];
1051
- _value += _char;
1052
- cursor++;
1053
- } while (!WHITE_SPACE.includes(peek) && peek !== ")" && peek !== "'" && cursor < input.length);
1054
- tokens.push({
1055
- type: "token",
1056
- span: [_start2, cursor - 1],
1057
- value: _value
1058
- });
1059
- }
1060
- }
1061
- return [tokens, cursor];
1062
- }
1063
-
1064
- var ParserError = /*#__PURE__*/_createClass(function ParserError(message, span) {
1065
- _classCallCheck(this, ParserError);
1066
- this.message = message;
1067
- this.span = span;
1068
- });
1069
- function parseTokens(inputTokens) {
1070
- var tokens = inputTokens.slice();
1071
- function slurpCall(first, isAnon) {
1072
- var _tokens$, _tokens$2, _tokens$3, _tokens$7, _tokens$8, _args$slice$0$span;
1073
- // peek
1074
- if (((_tokens$ = tokens[0]) === null || _tokens$ === void 0 ? void 0 : _tokens$.type) === "ws" && ((_tokens$2 = tokens[1]) === null || _tokens$2 === void 0 ? void 0 : _tokens$2.type) === ")" || ((_tokens$3 = tokens[0]) === null || _tokens$3 === void 0 ? void 0 : _tokens$3.type) === ")") {
1075
- slurpWs();
1076
- tokens.shift();
1077
- return result.ok(new Sym("()", [first.span[0], first.span[1] + 1]));
1078
- }
1079
- var args = [];
1080
- var completed = false;
1081
- while (!completed) {
1082
- var _res = slurp();
1083
- if (result.isOk(_res)) {
1084
- var _tokens$4, _tokens$5, _tokens$6;
1085
- args.push(_res.value);
1086
- completed = ((_tokens$4 = tokens[0]) === null || _tokens$4 === void 0 ? void 0 : _tokens$4.type) !== "ws" || ((_tokens$5 = tokens[0]) === null || _tokens$5 === void 0 ? void 0 : _tokens$5.type) === "ws" && ((_tokens$6 = tokens[1]) === null || _tokens$6 === void 0 ? void 0 : _tokens$6.type) === ")";
1087
- } else {
1088
- return _res;
1089
- }
1090
- }
1091
- if (((_tokens$7 = tokens[0]) === null || _tokens$7 === void 0 ? void 0 : _tokens$7.type) === "ws" && ((_tokens$8 = tokens[1]) === null || _tokens$8 === void 0 ? void 0 : _tokens$8.type) === ")") {
1092
- tokens.shift();
1093
- }
1094
- var last = tokens.shift();
1095
- if ((last === null || last === void 0 ? void 0 : last.type) !== ")") {
1096
- return result.err(new ParserError("unbalanced parens: missing a ')'", [first.span[0], first.span[1] + 1]));
1097
- }
1098
- return result.ok(new Call(args, isAnon, [first.span[0], ((_args$slice$0$span = args.slice(-1)[0].span) === null || _args$slice$0$span === void 0 ? void 0 : _args$slice$0$span[1]) || -1]));
1099
- }
1100
- function slurpWs() {
1101
- while (((_tokens$9 = tokens[0]) === null || _tokens$9 === void 0 ? void 0 : _tokens$9.type) === "ws") {
1102
- var _tokens$9;
1103
- tokens.shift();
1104
- }
1105
- }
1106
- function slurpTemplate(first) {
1107
- var children = [];
1108
- while (tokens.length > 0) {
1109
- var _tokens$10;
1110
- if (((_tokens$10 = tokens[0]) === null || _tokens$10 === void 0 ? void 0 : _tokens$10.type) === "'") {
1111
- break;
1112
- }
1113
- var nextToken = tokens.shift();
1114
- if ((nextToken === null || nextToken === void 0 ? void 0 : nextToken.type) === "${") {
1115
- var _res2 = slurp();
1116
- if (result.isOk(_res2)) {
1117
- children.push(_res2.value);
1118
- var _last = tokens.shift();
1119
- if (!_last) {
1120
- var _children$slice$0$spa;
1121
- return result.err(new ParserError("unbalanced string template: missing a '}'", [first.span[0], (_children$slice$0$spa = children.slice(-1)[0].span) === null || _children$slice$0$spa === void 0 ? void 0 : _children$slice$0$spa[1]]));
1122
- } else if (_last.type !== "}") {
1123
- return result.err(new ParserError("unbalanced string template: expected '}'", _last.span));
1124
- }
1125
- } else {
1126
- return _res2;
1127
- }
1128
- } else if ((nextToken === null || nextToken === void 0 ? void 0 : nextToken.type) === "string") {
1129
- children.push(new StringLiteral(nextToken.unescapedValue || nextToken.value || "", nextToken.span));
1130
- }
1131
- }
1132
- var last = tokens.shift();
1133
- if (!last) {
1134
- var _children$slice$0$spa2;
1135
- return result.err(new ParserError("unbalanced string template: missing a '''", [first.span[0], (_children$slice$0$spa2 = children.slice(-1)[0].span) === null || _children$slice$0$spa2 === void 0 ? void 0 : _children$slice$0$spa2[1]]));
1136
- } else if (last.type !== "'") {
1137
- return result.err(new ParserError("unbalanced string template: expected '''", last.span));
1138
- }
1139
- return result.ok(new StringTemplate(children, [first.span[0], last.span[1]]));
1140
- }
1141
- function slurpString(first) {
1142
- var _tokens$11, _tokens$12, _tokens$13;
1143
- if (((_tokens$11 = tokens[0]) === null || _tokens$11 === void 0 ? void 0 : _tokens$11.type) === "string" && ((_tokens$12 = tokens[1]) === null || _tokens$12 === void 0 ? void 0 : _tokens$12.type) === "'") {
1144
- var stringToken = tokens.shift();
1145
- var last = tokens.shift();
1146
- if (!last || !stringToken) {
1147
- throw Error("Unexpected error: stringToken or last is undefined");
1148
- }
1149
- return result.ok(new StringLiteral(stringToken.unescapedValue || stringToken.value || "", [first.span[0], last.span[1]]));
1150
- } else if (((_tokens$13 = tokens[0]) === null || _tokens$13 === void 0 ? void 0 : _tokens$13.type) === "'") {
1151
- var _last2 = tokens.shift();
1152
- if (!_last2) {
1153
- throw Error("Unexpected error: last is undefined");
1154
- }
1155
- return result.ok(new StringLiteral("", [first.span[0], _last2.span[1]]));
1156
- } else {
1157
- return slurpTemplate(first);
1158
- }
1159
- }
1160
- function slurp() {
1161
- slurpWs();
1162
- var first = tokens.shift();
1163
- if (!first) {
1164
- return result.err(new ParserError("expected '(', '!(', string or literal", [0, 0]));
1165
- }
1166
- if (first.type === "(" || first.type === "!(") {
1167
- return slurpCall(first, first.type === "!(");
1168
- } else if (first.type === "'") {
1169
- return slurpString(first);
1170
- } else if (first.type === "token") {
1171
- var _first$value, _first$value2, _first$value3, _first$value4, _first$value5, _first$value6;
1172
- if ((_first$value = first.value) !== null && _first$value !== void 0 && _first$value.includes("(") || (_first$value2 = first.value) !== null && _first$value2 !== void 0 && _first$value2.includes(")")) {
1173
- return result.err(new ParserError("unexpected token: '(' and ')' are not allowed in tokens", first.span));
1174
- }
1175
- if ((_first$value3 = first.value) !== null && _first$value3 !== void 0 && _first$value3.includes("'")) {
1176
- return result.err(new ParserError('unexpected token: "\'" is not allowed in tokens', first.span));
1177
- }
1178
- if ((_first$value4 = first.value) !== null && _first$value4 !== void 0 && _first$value4.includes(".")) {
1179
- return result.err(new ParserError('unexpected token: "." is not allowed in tokens', first.span));
1180
- }
1181
- if ((_first$value5 = first.value) !== null && _first$value5 !== void 0 && _first$value5.includes("{") || (_first$value6 = first.value) !== null && _first$value6 !== void 0 && _first$value6.includes("}")) {
1182
- return result.err(new ParserError("unexpected token: '{' and '}' are not allowed in tokens", first.span));
1183
- }
1184
- return result.ok(new Sym(first.value || "", first.span));
1185
- } else {
1186
- return result.err(new ParserError("expected '(', '!(' or literal or token".concat(first.value || first.type ? ", got: '".concat(first.value || first.type, "'") : ""), first.span));
1187
- }
1188
- }
1189
- var res = slurp();
1190
- slurpWs();
1191
- if (result.isErr(res)) {
1192
- return res;
1193
- }
1194
- if (tokens.length > 0) {
1195
- return result.err(new ParserError("expected end of input, superfluous tokens", [tokens[0].span[0], tokens.slice(-1)[0].span[1]]));
1196
- }
1197
- return res;
1198
- }
1199
- function parse(input) {
1200
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
1201
- var _tokenize = tokenize(input),
1202
- _tokenize2 = _slicedToArray(_tokenize, 2),
1203
- tokens = _tokenize2[0];
1204
- _tokenize2[1]; // TODO: we can use cursor to improve error messages / spans
1205
- return parseTokens(tokens);
1206
- }
1207
-
1208
- /* eslint-disable @typescript-eslint/no-unused-vars */
1209
-
1210
- /**
1211
- * Selectors can be used to select parts of a Val module.
1212
- * Unlike queries, joins, aggregates etc is and will not be supported.
1213
- *
1214
- * They are designed to be be used as if they were "normal" JSON data,
1215
- * though some concessions had to be made because of TypeScript limitations.
1216
- *
1217
- * Selectors works equally on source content, defined in code, and remote content.
1218
- *
1219
- * @example
1220
- * // Select the title of a document
1221
- * const titles = useVal(docsVal.map((doc) => doc.title));
1222
- *
1223
- * @example
1224
- * // Match on a union type
1225
- * const titles = useVal(docsVal.map((doc) => doc.fold("type")({
1226
- * newsletter: (newsletter) => newsletter.title,
1227
- * email: (email) => email.subject,
1228
- * }));
1229
- *
1230
- */
1231
-
1232
- /**
1233
- * @internal
1234
- */
1235
- var GetSchema = Symbol("GetSchema");
1236
- /**
1237
- /**
1238
- * @internal
1239
- */
1240
- var Path = Symbol("Path");
1241
- /**
1242
- * @internal
1243
- */
1244
- var SourceOrExpr = Symbol("SourceOrExpr");
1245
-
1246
- /**
1247
- * Use this type to convert types that accepts both Source and Selectors
1248
- *
1249
- * An example would be where literals are supported like in most higher order functions (e.g. map in array)
1250
- **/
1251
-
1252
- function hasOwn$1(obj, prop) {
1253
- return Object.prototype.hasOwnProperty.call(obj, prop);
1254
- }
1255
- function _andThen(f, source, path) {
1256
- if (source) {
1257
- return newSelectorProxy(f(newSelectorProxy(source, path)));
1258
- }
1259
- return newSelectorProxy(source, path);
1260
- }
1261
- function isSelector(source) {
1262
- return _typeof(source) === "object" && source !== null && (SourceOrExpr in source || Path in source);
1263
- }
1264
- function newSelectorProxy(source, path, moduleSchema) {
1265
- if (_typeof(source) === "object") {
1266
- if (isSelector(source)) {
1267
- return source;
1268
- } else if (isSerializedVal(source)) {
1269
- return newSelectorProxy(source.val, source.valPath);
1270
- }
1271
- }
1272
- if (source && source[FILE_REF_PROP] && source[VAL_EXTENSION] === "file") {
1273
- var fileRef = source[FILE_REF_PROP];
1274
- if (typeof fileRef !== "string") {
1275
- throw Error("Invalid file ref: " + fileRef);
1276
- }
1277
- return newSelectorProxy(convertFileSource(source), path, moduleSchema);
1278
- }
1279
- switch (_typeof(source)) {
1280
- case "function":
1281
- case "symbol":
1282
- throw Error("Invalid selector type: ".concat(_typeof(source), ": ").concat(source));
1283
- case "object":
1284
- // Handles both objects and arrays!
1285
- if (source !== null) {
1286
- return new Proxy(source, {
1287
- // TODO: see proxy docs if we want more traps
1288
- has: function has(target, prop) {
1289
- if (prop === SourceOrExpr) {
1290
- return true;
1291
- }
1292
- if (prop === Path) {
1293
- return true;
1294
- }
1295
- if (prop === "andThen") {
1296
- return true;
1297
- }
1298
- if (prop === GetSchema) {
1299
- return true;
1300
- }
1301
- return prop in target;
1302
- },
1303
- get: function get(target, prop) {
1304
- if (prop === SourceOrExpr) {
1305
- return source;
1306
- }
1307
- if (prop === Path) {
1308
- return path;
1309
- }
1310
- if (prop === GetSchema) {
1311
- return moduleSchema;
1312
- }
1313
- if (prop === "andThen") {
1314
- return function (f) {
1315
- return _andThen(f, source, path);
1316
- };
1317
- }
1318
- if (Array.isArray(target)) {
1319
- if (prop === "filter") {
1320
- return function (f) {
1321
- var filtered = target.map(function (a, i) {
1322
- return newSelectorProxy(a, createValPathOfItem(path, i), moduleSchema === null || moduleSchema === void 0 ? void 0 : moduleSchema.item);
1323
- }).filter(function (a) {
1324
- if (f && f instanceof Schema) {
1325
- return f.assert(path || "", unValify(a)).success;
1326
- } else {
1327
- return unValify(f(a));
1328
- }
1329
- });
1330
- return newSelectorProxy(filtered, path, moduleSchema);
1331
- };
1332
- } else if (prop === "map") {
1333
- return function (f) {
1334
- var filtered = target.map(function (a, i) {
1335
- var valueOrSelector = f(newSelectorProxy(a, createValPathOfItem(path, i), moduleSchema === null || moduleSchema === void 0 ? void 0 : moduleSchema.item), newSelectorProxy(i));
1336
- if (isSelector(valueOrSelector)) {
1337
- return valueOrSelector;
1338
- }
1339
- return newSelectorProxy(valueOrSelector);
1340
- });
1341
- return newSelectorProxy(filtered, path, moduleSchema);
1342
- };
1343
- }
1344
- }
1345
- if (Array.isArray(target) && prop === "length") {
1346
- return newSelectorProxy(target.length);
1347
- }
1348
- var reflectedValue = Reflect.get(target, prop);
1349
- if (hasOwn$1(source, prop)) {
1350
- if (!Number.isNaN(Number(prop))) {
1351
- return newSelectorProxy(reflectedValue, createValPathOfItem(path, Number(prop)), moduleSchema === null || moduleSchema === void 0 ? void 0 : moduleSchema.item);
1352
- }
1353
- return newSelectorProxy(reflectedValue, createValPathOfItem(path, prop), moduleSchema === null || moduleSchema === void 0 ? void 0 : moduleSchema.items[prop]);
1354
- }
1355
- return reflectedValue;
1356
- }
1357
- });
1358
- }
1359
- // intentional fallthrough
1360
- // eslint-disable-next-line no-fallthrough
1361
- default:
1362
- return _defineProperty(_defineProperty(_defineProperty({
1363
- eq: function eq(other) {
1364
- var otherValue = other;
1365
- if (isSelector(other)) {
1366
- otherValue = other[SourceOrExpr];
1367
- if (otherValue instanceof Expr) {
1368
- throw Error("TODO: Cannot evaluate equality with an Expr");
1369
- }
1370
- }
1371
- return newSelectorProxy(source === otherValue, undefined);
1372
- },
1373
- andThen: function andThen(f) {
1374
- return _andThen(f, source === undefined ? null : source, path);
1375
- }
1376
- }, SourceOrExpr, source === undefined ? null : source), Path, path), GetSchema, moduleSchema);
1377
- }
1378
- }
1379
-
1380
- // TODO: could we do .val on the objects instead?
1381
- function unValify(valueOrSelector) {
1382
- if (_typeof(valueOrSelector) === "object" && (SourceOrExpr in valueOrSelector || Path in valueOrSelector)) {
1383
- var selectorValue = valueOrSelector[SourceOrExpr];
1384
- return selectorValue;
1385
- }
1386
- return valueOrSelector;
1387
- }
1388
-
1389
- var EvalError = /*#__PURE__*/function () {
1390
- function EvalError(message, expr) {
1391
- _classCallCheck(this, EvalError);
1392
- this.message = message;
1393
- this.expr = expr;
1394
- }
1395
- return _createClass(EvalError, [{
1396
- key: "toString",
1397
- value: function toString() {
1398
- return "".concat(this.message, " in: ").concat(this.expr.transpile());
1399
- }
1400
- }]);
1401
- }();
1402
- var MAX_STACK_SIZE = 100; // an arbitrary semi-large number
1403
- function evaluateSync(expr, getSource, stack) {
1404
- // TODO: amount of evaluates should be limited?
1405
- if (stack.length > MAX_STACK_SIZE) {
1406
- throw new EvalError("Stack overflow. Final frames: ".concat(stack.slice(-10).map(function (frame, i) {
1407
- return frame.map(function (s, j) {
1408
- return "@[".concat(i, ",").concat(j, "]: ").concat(JSON.stringify(s));
1409
- }).join(", ");
1410
- }).join(" -> ")), expr);
1411
- }
1412
- if (expr instanceof Call) {
1413
- if (expr.children[0] instanceof Sym) {
1414
- if (expr.children[0].value === "val") {
1415
- if (expr.isAnon) {
1416
- throw new EvalError("cannot call 'val' as anonymous function", expr);
1417
- }
1418
- if (expr.children[1] instanceof StringLiteral) {
1419
- var path = expr.children[1].value;
1420
- return newSelectorProxy(getSource(path), path);
1421
- } else {
1422
- throw new EvalError("argument of 'val' must be a string literal", expr);
1423
- }
1424
- } else if (expr.children[0].value === "json") {
1425
- if (expr.children.length !== 2) {
1426
- throw new EvalError("must call 'json' with exactly one argument", expr);
1427
- }
1428
- var value = evaluateSync(expr.children[1], getSource, stack);
1429
- var valObj = value[SourceOrExpr];
1430
- var valPath = value[Path];
1431
- if (typeof valObj !== "string") {
1432
- throw new EvalError("cannot parse JSON: ".concat(JSON.stringify(valObj), ", expected string"), expr.children[1]);
1433
- }
1434
- try {
1435
- var serialized = JSON.parse(valObj);
1436
- if (isSerializedVal(serialized)) {
1437
- return newSelectorProxy(serialized.val, serialized.valPath);
1438
- }
1439
- var parsedValue = newSelectorProxy(JSON.parse(valObj), valPath);
1440
- return parsedValue;
1441
- } catch (e) {
1442
- if (e instanceof SyntaxError) {
1443
- throw new EvalError("cannot parse JSON: ".concat(valObj, ", ").concat(e.message, " - value: ").concat(JSON.stringify(value)), expr.children[1]);
1444
- }
1445
- throw e;
1446
- }
1447
- } else if (expr.children[0].value === "stringify") {
1448
- // TODO: remove stringify
1449
- if (expr.children.length !== 2) {
1450
- throw new EvalError("must call 'stringify' with exactly one argument", expr);
1451
- }
1452
- var res = evaluateSync(expr.children[1], getSource, stack);
1453
- return newSelectorProxy(JSON.stringify(res[SourceOrExpr]));
1454
- }
1455
- }
1456
- var prop = evaluateSync(expr.children[0], getSource, stack)[SourceOrExpr];
1457
- if (expr.children.length === 1) {
1458
- // TODO: return if literal only?
1459
- return newSelectorProxy(prop);
1460
- }
1461
- var obj = evaluateSync(expr.children[1], getSource, stack);
1462
- if (typeof prop !== "string" && typeof prop !== "number") {
1463
- throw new EvalError("cannot access ".concat(JSON.stringify(obj), " with property ").concat(JSON.stringify(prop), ": is not a string or number"), expr);
1464
- }
1465
- if (prop in obj) {
1466
- if (expr.isAnon) {
1467
- // anon functions:
1468
- var maybeFunction = obj[prop];
1469
- if (typeof maybeFunction !== "function") {
1470
- throw new EvalError("cannot access property ".concat(JSON.stringify(prop), " of ").concat(JSON.stringify(obj), ": required higher ordered function got ").concat(_typeof(obj[prop])), expr);
1471
- }
1472
- if (expr.children[0] instanceof Sym) {
1473
- return maybeFunction(function () {
1474
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
1475
- args[_key] = arguments[_key];
1476
- }
1477
- return evaluateSync(expr.children[2], getSource, stack.concat([args]));
1478
- });
1479
- } else {
1480
- throw new EvalError("cannot call an expression that is not a symbol, got: '".concat(expr.children[0].type, "'"), expr);
1481
- }
1482
- } else {
1483
- // non-anon functions:
1484
- if (expr.children[0] instanceof Sym) {
1485
- if (expr.children[0].value === "val") {
1486
- if (expr.children[1] instanceof StringLiteral) {
1487
- var _path = expr.children[1].value;
1488
- return newSelectorProxy(getSource(_path), _path);
1489
- } else {
1490
- throw new EvalError("argument of 'val' must be a string literal", expr);
1491
- }
1492
- }
1493
- }
1494
- var args = expr.children.slice(2);
1495
- if (args.length > 0) {
1496
- var _maybeFunction = obj[prop];
1497
- if (typeof _maybeFunction !== "function") {
1498
- throw new EvalError("cannot access property ".concat(JSON.stringify(prop), " of ").concat(JSON.stringify(obj), ": required function got ").concat(_typeof(obj[prop])), expr);
1499
- }
1500
- return _maybeFunction.apply(void 0, _toConsumableArray(args.map(function (arg) {
1501
- return evaluateSync(arg, getSource, stack);
1502
- })));
1503
- }
1504
- var maybeValue = obj[prop];
1505
- if (typeof maybeValue === "function") {
1506
- throw new EvalError("cannot access property ".concat(JSON.stringify(prop), " of ").concat(JSON.stringify(obj), ": required value got ").concat(_typeof(obj[prop])), expr);
1507
- }
1508
- return maybeValue;
1509
- }
1510
- }
1511
- } else if (expr instanceof Sym) {
1512
- if (expr.value.startsWith("@")) {
1513
- var _stack$Number;
1514
- var _expr$value$slice$spl = expr.value.slice(2, -1).split(","),
1515
- _expr$value$slice$spl2 = _slicedToArray(_expr$value$slice$spl, 3),
1516
- i = _expr$value$slice$spl2[0],
1517
- j = _expr$value$slice$spl2[1],
1518
- rest = _expr$value$slice$spl2[2];
1519
- if (rest) {
1520
- throw new EvalError("cannot access stack: too many indices", expr);
1521
- }
1522
- var stackValue = (_stack$Number = stack[Number(i)]) === null || _stack$Number === void 0 ? void 0 : _stack$Number[Number(j)];
1523
- if (stackValue === undefined) {
1524
- throw new EvalError("cannot access stack: out of bounds", expr);
1525
- }
1526
- return stackValue;
1527
- } else if (expr.value === "()") {
1528
- return newSelectorProxy(null);
1529
- }
1530
- return newSelectorProxy(expr.value);
1531
- } else if (expr instanceof StringLiteral) {
1532
- return newSelectorProxy(expr.value);
1533
- } else if (expr instanceof StringTemplate) {
1534
- return newSelectorProxy(expr.children.map(function (child) {
1535
- if (child instanceof Sym && child.value === "()") {
1536
- return "null";
1537
- }
1538
- var evalRes = evaluateSync(child, getSource, stack);
1539
- if (child.type === "StringLiteral" || child.type === "StringTemplate") {
1540
- return evalRes[SourceOrExpr];
1541
- }
1542
- if (Path in evalRes) {
1543
- // a selector, so serialize to Val
1544
- return JSON.stringify({
1545
- val: evalRes[SourceOrExpr],
1546
- valPath: evalRes[Path]
1547
- });
1548
- }
1549
- return JSON.stringify(evalRes[SourceOrExpr]);
1550
- }).join(""));
1551
- }
1552
- throw new EvalError("could not evaluate", expr);
1553
- }
1554
- function evaluate(expr, source, stack) {
1555
- try {
1556
- return result.ok(evaluateSync(expr, source, stack));
1557
- } catch (err) {
1558
- if (err instanceof EvalError) {
1559
- return result.err(err);
1560
- }
1561
- throw err;
1562
- }
1563
- }
1564
-
1565
- var index = /*#__PURE__*/Object.freeze({
1566
- __proto__: null,
1567
- parse: parse,
1568
- Call: Call,
1569
- Expr: Expr,
1570
- NilSym: NilSym,
1571
- StringLiteral: StringLiteral,
1572
- StringTemplate: StringTemplate,
1573
- Sym: Sym,
1574
- evaluate: evaluate
1575
- });
1576
-
1577
749
  var ArraySchema = /*#__PURE__*/function (_Schema) {
1578
750
  function ArraySchema(item) {
1579
751
  var _this;
@@ -2089,7 +1261,212 @@ var union = function union(key) {
2089
1261
  return new UnionSchema(key, objects);
2090
1262
  };
2091
1263
 
2092
- var RichTextSchema = /*#__PURE__*/function (_Schema) {
1264
+ var ImageSchema = /*#__PURE__*/function (_Schema) {
1265
+ function ImageSchema(options) {
1266
+ var _this;
1267
+ var opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
1268
+ var isRemote = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
1269
+ _classCallCheck(this, ImageSchema);
1270
+ _this = _callSuper(this, ImageSchema);
1271
+ _this.options = options;
1272
+ _this.opt = opt;
1273
+ _this.isRemote = isRemote;
1274
+ return _this;
1275
+ }
1276
+ _inherits(ImageSchema, _Schema);
1277
+ return _createClass(ImageSchema, [{
1278
+ key: "remote",
1279
+ value: function remote() {
1280
+ return new ImageSchema(this.options, this.opt, true);
1281
+ }
1282
+ }, {
1283
+ key: "validate",
1284
+ value: function validate(path, src) {
1285
+ if (this.opt && (src === null || src === undefined)) {
1286
+ return false;
1287
+ }
1288
+ if (src === null || src === undefined) {
1289
+ return _defineProperty({}, path, [{
1290
+ message: "Non-optional image was null or undefined.",
1291
+ value: src
1292
+ }]);
1293
+ }
1294
+ if (this.isRemote && src[VAL_EXTENSION] !== "remote") {
1295
+ return _defineProperty({}, path, [{
1296
+ message: "Expected a remote image, but got a local image.",
1297
+ value: src,
1298
+ fixes: ["image:upload-remote"]
1299
+ }]);
1300
+ }
1301
+ if (this.isRemote && src[VAL_EXTENSION] === "remote") {
1302
+ return _defineProperty({}, path, [{
1303
+ message: "Remote image was not checked.",
1304
+ value: src,
1305
+ fixes: ["image:check-remote"]
1306
+ }]);
1307
+ }
1308
+ if (!this.isRemote && src[VAL_EXTENSION] === "remote") {
1309
+ return _defineProperty({}, path, [{
1310
+ message: "Expected locale image, but found remote.",
1311
+ value: src,
1312
+ fixes: ["image:download-remote"]
1313
+ }]);
1314
+ }
1315
+ if (typeof src[FILE_REF_PROP] !== "string") {
1316
+ return _defineProperty({}, path, [{
1317
+ message: "Image did not have a file reference string. Got: ".concat(_typeof(src[FILE_REF_PROP])),
1318
+ value: src
1319
+ }]);
1320
+ }
1321
+ if (src[VAL_EXTENSION] !== "file") {
1322
+ return _defineProperty({}, path, [{
1323
+ message: "Image did not have the valid file extension type. Got: ".concat(src[VAL_EXTENSION]),
1324
+ value: src,
1325
+ fixes: ["image:change-extension", "image:check-metadata"]
1326
+ }]);
1327
+ }
1328
+ var _ref7 = this.options || {},
1329
+ accept = _ref7.accept;
1330
+ var _ref8 = src.metadata || {},
1331
+ mimeType = _ref8.mimeType;
1332
+ if (accept && mimeType && !mimeType.includes("/")) {
1333
+ return _defineProperty({}, path, [{
1334
+ message: "Invalid mime type format. Got: '".concat(mimeType, "'"),
1335
+ value: src,
1336
+ fixes: ["image:check-metadata"]
1337
+ }]);
1338
+ }
1339
+ if (accept && mimeType && mimeType.includes("/")) {
1340
+ var acceptedTypes = accept.split(",").map(function (type) {
1341
+ return type.trim();
1342
+ });
1343
+ var isValidMimeType = acceptedTypes.some(function (acceptedType) {
1344
+ if (acceptedType === "*/*") {
1345
+ return true;
1346
+ }
1347
+ if (acceptedType.endsWith("/*")) {
1348
+ var baseType = acceptedType.slice(0, -2);
1349
+ return mimeType.startsWith(baseType);
1350
+ }
1351
+ return acceptedType === mimeType;
1352
+ });
1353
+ if (!isValidMimeType) {
1354
+ return _defineProperty({}, path, [{
1355
+ message: "Mime type mismatch. Found '".concat(mimeType, "' but schema accepts '").concat(accept, "'"),
1356
+ value: src,
1357
+ fixes: ["image:check-metadata"]
1358
+ }]);
1359
+ }
1360
+ }
1361
+ var fileMimeType = Internal.filenameToMimeType(src[FILE_REF_PROP]);
1362
+ if (!fileMimeType) {
1363
+ return _defineProperty({}, path, [{
1364
+ message: "Could not determine mime type from file extension. Got: ".concat(src[FILE_REF_PROP]),
1365
+ value: src,
1366
+ fixes: ["image:check-metadata"]
1367
+ }]);
1368
+ }
1369
+ if (fileMimeType && mimeType && fileMimeType !== mimeType) {
1370
+ return _defineProperty({}, path, [{
1371
+ message: "Mime type and file extension not matching. Mime type is '".concat(mimeType, "' but file extension is '").concat(fileMimeType, "'"),
1372
+ value: src,
1373
+ fixes: ["image:check-metadata"]
1374
+ }]);
1375
+ }
1376
+ if (src.metadata) {
1377
+ if (src.metadata.hotspot) {
1378
+ if (_typeof(src.metadata.hotspot) !== "object" || typeof src.metadata.hotspot.x !== "number" || typeof src.metadata.hotspot.y !== "number") {
1379
+ return _defineProperty({}, path, [{
1380
+ message: "Hotspot must be an object with x and y as numbers.",
1381
+ value: src
1382
+ }]);
1383
+ }
1384
+ }
1385
+ return _defineProperty({}, path, [{
1386
+ 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 the mime type.",
1387
+ // 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.
1388
+ value: src,
1389
+ fixes: ["image:check-metadata"]
1390
+ }]);
1391
+ }
1392
+ return _defineProperty({}, path, [{
1393
+ message: "Could not validate Image metadata.",
1394
+ value: src,
1395
+ fixes: ["image:add-metadata"]
1396
+ }]);
1397
+ }
1398
+ }, {
1399
+ key: "assert",
1400
+ value: function assert(path, src) {
1401
+ if (this.opt && src === null) {
1402
+ return {
1403
+ success: true,
1404
+ data: src
1405
+ };
1406
+ }
1407
+ if (src === null) {
1408
+ return {
1409
+ success: false,
1410
+ errors: _defineProperty({}, path, [{
1411
+ message: "Expected 'object', got 'null'",
1412
+ typeError: true
1413
+ }])
1414
+ };
1415
+ }
1416
+ if (_typeof(src) !== "object") {
1417
+ return {
1418
+ success: false,
1419
+ errors: _defineProperty({}, path, [{
1420
+ message: "Expected 'object', got '".concat(_typeof(src), "'"),
1421
+ typeError: true
1422
+ }])
1423
+ };
1424
+ }
1425
+ if (!(FILE_REF_PROP in src)) {
1426
+ return {
1427
+ success: false,
1428
+ errors: _defineProperty({}, path, [{
1429
+ message: "Value of this schema must use: 'c.image' (error type: missing_ref_prop)",
1430
+ typeError: true
1431
+ }])
1432
+ };
1433
+ }
1434
+ if (!(VAL_EXTENSION in src && src[VAL_EXTENSION] === "file")) {
1435
+ return {
1436
+ success: false,
1437
+ errors: _defineProperty({}, path, [{
1438
+ message: "Value of this schema must use: 'c.image' (error type: missing_file_extension)",
1439
+ typeError: true
1440
+ }])
1441
+ };
1442
+ }
1443
+ return {
1444
+ success: true,
1445
+ data: src
1446
+ };
1447
+ }
1448
+ }, {
1449
+ key: "nullable",
1450
+ value: function nullable() {
1451
+ return new ImageSchema(this.options, true);
1452
+ }
1453
+ }, {
1454
+ key: "serialize",
1455
+ value: function serialize() {
1456
+ return {
1457
+ type: "image",
1458
+ options: this.options,
1459
+ opt: this.opt,
1460
+ remote: this.isRemote
1461
+ };
1462
+ }
1463
+ }]);
1464
+ }(Schema);
1465
+ var image = function image(options) {
1466
+ return new ImageSchema(options);
1467
+ };
1468
+
1469
+ var RichTextSchema = /*#__PURE__*/function (_Schema) {
2093
1470
  function RichTextSchema(options) {
2094
1471
  var _this;
2095
1472
  var opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
@@ -2160,7 +1537,7 @@ var RichTextSchema = /*#__PURE__*/function (_Schema) {
2160
1537
  _step;
2161
1538
  try {
2162
1539
  for (_iterator.s(); !(_step = _iterator.n()).done;) {
2163
- var _this2$options$block, _this2$options$block2, _this2$options$block3, _this2$options$block4, _this2$options$block5, _this2$options$block6, _this2$options$block7, _this2$options$block8, _this2$options$block9, _this2$options$block10, _this2$options$inline, _this2$options$inline2;
1540
+ var _this2$options$block, _this2$options$block2, _this2$options$block3, _this2$options$block4, _this2$options$block5, _this2$options$block6, _this2$options$block7, _this2$options$block8, _this2$options$block9, _this2$options$block10, _this2$options$inline;
2164
1541
  var node = _step.value;
2165
1542
  var path = unsafeCreateSourcePath(rootPath, nodes.indexOf(node));
2166
1543
  if (typeof node === "string") {
@@ -2221,8 +1598,31 @@ var RichTextSchema = /*#__PURE__*/function (_Schema) {
2221
1598
  if (node.tag === "a" && !((_this2$options$inline = _this2.options.inline) !== null && _this2$options$inline !== void 0 && _this2$options$inline.a)) {
2222
1599
  addError(path, "'a' inline is not valid", false);
2223
1600
  }
2224
- if (node.tag === "img" && !((_this2$options$inline2 = _this2.options.inline) !== null && _this2$options$inline2 !== void 0 && _this2$options$inline2.img)) {
2225
- addError(path, "'img' inline is not valid", false);
1601
+ if (node.tag === "img") {
1602
+ var _this2$options$inline2, _this2$options$inline3;
1603
+ if (!((_this2$options$inline2 = _this2.options.inline) !== null && _this2$options$inline2 !== void 0 && _this2$options$inline2.img)) {
1604
+ addError(path, "'img' inline is not valid", false);
1605
+ } else if ((_this2$options$inline3 = _this2.options.inline) !== null && _this2$options$inline3 !== void 0 && _this2$options$inline3.img) {
1606
+ var _this2$options$inline4, _this2$options$inline5;
1607
+ if (!("src" in node)) {
1608
+ return _defineProperty({}, path, [{
1609
+ message: "Expected 'src' in 'img'",
1610
+ typeError: true
1611
+ }]);
1612
+ }
1613
+ var srcPath = unsafeCreateSourcePath(path, "src");
1614
+ var imageValidationErrors = _typeof((_this2$options$inline4 = _this2.options.inline) === null || _this2$options$inline4 === void 0 ? void 0 : _this2$options$inline4.img) === "object" ? (_this2$options$inline5 = _this2.options.inline) === null || _this2$options$inline5 === void 0 ? void 0 : _this2$options$inline5.img.validate(srcPath, node.src) : new ImageSchema({}, false, false).validate(srcPath, node.src);
1615
+ if (imageValidationErrors) {
1616
+ for (var validationErrorPathS in imageValidationErrors) {
1617
+ var _current$validationEr;
1618
+ var validationErrorPath = validationErrorPathS;
1619
+ if (!current[validationErrorPath]) {
1620
+ current[validationErrorPath] = [];
1621
+ }
1622
+ (_current$validationEr = current[validationErrorPath]).push.apply(_current$validationEr, _toConsumableArray(imageValidationErrors[validationErrorPath]));
1623
+ }
1624
+ }
1625
+ }
2226
1626
  }
2227
1627
  if ("styles" in node && node.tag !== "span") {
2228
1628
  return _defineProperty({}, path, [{
@@ -2326,282 +1726,12 @@ var RichTextSchema = /*#__PURE__*/function (_Schema) {
2326
1726
  }
2327
1727
  (_current$rootPath = current[rootPath]).push.apply(_current$rootPath, lengthErrors);
2328
1728
  }
2329
- return current;
2330
- }
2331
- if (lengthErrors.length > 0) {
2332
- return _defineProperty({}, rootPath, lengthErrors);
2333
- }
2334
- return false;
2335
- }
2336
- }, {
2337
- key: "assert",
2338
- value: function assert(path, src) {
2339
- if (this.opt && src === null) {
2340
- return {
2341
- success: true,
2342
- data: src
2343
- };
2344
- }
2345
- if (src === null && !this.opt) {
2346
- return {
2347
- success: false,
2348
- errors: _defineProperty({}, path, [{
2349
- message: "Expected 'array', got 'null'",
2350
- typeError: true
2351
- }])
2352
- };
2353
- }
2354
- if (!Array.isArray(src)) {
2355
- return {
2356
- success: false,
2357
- errors: _defineProperty({}, path, [{
2358
- message: "Expected 'array', got '".concat(_typeof(src), "'"),
2359
- typeError: true
2360
- }])
2361
- };
2362
- }
2363
- var errors = {};
2364
- for (var i = 0; i < src.length; i++) {
2365
- this.recursiveAssert(unsafeCreateSourcePath(path, i), src[i], errors);
2366
- }
2367
- if (Object.keys(errors).length > 0) {
2368
- return {
2369
- success: false,
2370
- errors: errors
2371
- };
2372
- }
2373
- // TODO: validate options
2374
- return {
2375
- success: true,
2376
- data: src
2377
- };
2378
- }
2379
- }, {
2380
- key: "recursiveAssert",
2381
- value: function recursiveAssert(path, node, errors) {
2382
- if (_typeof(node) !== "object") {
2383
- if (!errors[path]) {
2384
- errors[path] = [];
2385
- }
2386
- errors[path].push({
2387
- message: "Expected 'object', got '".concat(_typeof(node), "'"),
2388
- typeError: true
2389
- });
2390
- return;
2391
- }
2392
- if (Array.isArray(node)) {
2393
- if (!errors[path]) {
2394
- errors[path] = [];
2395
- }
2396
- errors[path].push({
2397
- message: "Expected 'object', got 'array'",
2398
- typeError: true
2399
- });
2400
- return;
2401
- }
2402
- if (node === null) {
2403
- if (!errors[path]) {
2404
- errors[path] = [];
2405
- }
2406
- errors[path].push({
2407
- message: "Expected 'object', got 'null'",
2408
- typeError: true
2409
- });
2410
- return;
2411
- }
2412
- if ("tag" in node) {
2413
- if (typeof node.tag !== "string") {
2414
- if (!errors[path]) {
2415
- errors[path] = [];
2416
- }
2417
- errors[path].push({
2418
- message: "Expected 'string', got '".concat(_typeof(node.tag), "'"),
2419
- typeError: true
2420
- });
2421
- return;
2422
- }
2423
- }
2424
- if ("children" in node) {
2425
- if (!Array.isArray(node.children)) {
2426
- if (!errors[path]) {
2427
- errors[path] = [];
2428
- }
2429
- errors[path].push({
2430
- message: "Expected 'array', got '".concat(_typeof(node.children), "'"),
2431
- typeError: true
2432
- });
2433
- return;
2434
- } else {
2435
- for (var i = 0; i < node.children.length; i++) {
2436
- var child = node.children[i];
2437
- var pathAtError = unsafeCreateSourcePath(unsafeCreateSourcePath(path, "children"), i);
2438
- if (_typeof(child) === "object") {
2439
- this.recursiveAssert(pathAtError, child, errors);
2440
- } else if (typeof child === "string") {
2441
- continue;
2442
- } else {
2443
- if (!errors[pathAtError]) {
2444
- errors[pathAtError] = [];
2445
- }
2446
- errors[pathAtError].push({
2447
- message: "Expected 'object' or 'string', got '".concat(_typeof(child), "'"),
2448
- typeError: true
2449
- });
2450
- }
2451
- }
2452
- }
2453
- }
2454
- if ("styles" in node) {
2455
- if (!Array.isArray(node.styles)) {
2456
- if (!errors[path]) {
2457
- errors[path] = [];
2458
- }
2459
- errors[path].push({
2460
- message: "Expected 'array', got '".concat(_typeof(node.styles), "'"),
2461
- typeError: true
2462
- });
2463
- } else {
2464
- for (var _i2 = 0; _i2 < node.styles.length; _i2++) {
2465
- var style = node.styles[_i2];
2466
- if (typeof style !== "string") {
2467
- var _pathAtError = unsafeCreateSourcePath(path, _i2);
2468
- if (!errors[_pathAtError]) {
2469
- errors[_pathAtError] = [];
2470
- }
2471
- errors[_pathAtError].push({
2472
- message: "Expected 'string', got '".concat(_typeof(style), "'"),
2473
- typeError: true
2474
- });
2475
- }
2476
- }
2477
- }
2478
- }
2479
- }
2480
- }, {
2481
- key: "nullable",
2482
- value: function nullable() {
2483
- return new RichTextSchema(this.options, true);
2484
- }
2485
- }, {
2486
- key: "serialize",
2487
- value: function serialize() {
2488
- return {
2489
- type: "richtext",
2490
- opt: this.opt,
2491
- options: this.options
2492
- };
2493
- }
2494
- }]);
2495
- }(Schema);
2496
- var richtext = function richtext(options) {
2497
- return new RichTextSchema(options !== null && options !== void 0 ? options : {});
2498
- };
2499
-
2500
- var ImageSchema = /*#__PURE__*/function (_Schema) {
2501
- function ImageSchema(options) {
2502
- var _this;
2503
- var opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
2504
- _classCallCheck(this, ImageSchema);
2505
- _this = _callSuper(this, ImageSchema);
2506
- _this.options = options;
2507
- _this.opt = opt;
2508
- return _this;
2509
- }
2510
- _inherits(ImageSchema, _Schema);
2511
- return _createClass(ImageSchema, [{
2512
- key: "validate",
2513
- value: function validate(path, src) {
2514
- if (this.opt && (src === null || src === undefined)) {
2515
- return false;
2516
- }
2517
- if (src === null || src === undefined) {
2518
- return _defineProperty({}, path, [{
2519
- message: "Non-optional image was null or undefined.",
2520
- value: src
2521
- }]);
2522
- }
2523
- if (typeof src[FILE_REF_PROP] !== "string") {
2524
- return _defineProperty({}, path, [{
2525
- message: "Image did not have a file reference string. Got: ".concat(_typeof(src[FILE_REF_PROP])),
2526
- value: src
2527
- }]);
2528
- }
2529
- if (src[VAL_EXTENSION] !== "file") {
2530
- return _defineProperty({}, path, [{
2531
- message: "Image did not have the valid file extension type. Got: ".concat(src[VAL_EXTENSION]),
2532
- value: src,
2533
- fixes: ["image:change-extension", "image:check-metadata"]
2534
- }]);
2535
- }
2536
- var _ref4 = this.options || {},
2537
- accept = _ref4.accept;
2538
- var _ref5 = src.metadata || {},
2539
- mimeType = _ref5.mimeType;
2540
- if (accept && mimeType && !mimeType.includes("/")) {
2541
- return _defineProperty({}, path, [{
2542
- message: "Invalid mime type format. Got: '".concat(mimeType, "'"),
2543
- value: src,
2544
- fixes: ["image:check-metadata"]
2545
- }]);
2546
- }
2547
- if (accept && mimeType && mimeType.includes("/")) {
2548
- var acceptedTypes = accept.split(",").map(function (type) {
2549
- return type.trim();
2550
- });
2551
- var isValidMimeType = acceptedTypes.some(function (acceptedType) {
2552
- if (acceptedType === "*/*") {
2553
- return true;
2554
- }
2555
- if (acceptedType.endsWith("/*")) {
2556
- var baseType = acceptedType.slice(0, -2);
2557
- return mimeType.startsWith(baseType);
2558
- }
2559
- return acceptedType === mimeType;
2560
- });
2561
- if (!isValidMimeType) {
2562
- return _defineProperty({}, path, [{
2563
- message: "Mime type mismatch. Found '".concat(mimeType, "' but schema accepts '").concat(accept, "'"),
2564
- value: src,
2565
- fixes: ["image:check-metadata"]
2566
- }]);
2567
- }
2568
- }
2569
- var fileMimeType = Internal.filenameToMimeType(src[FILE_REF_PROP]);
2570
- if (!fileMimeType) {
2571
- return _defineProperty({}, path, [{
2572
- message: "Could not determine mime type from file extension. Got: ".concat(src[FILE_REF_PROP]),
2573
- value: src,
2574
- fixes: ["image:check-metadata"]
2575
- }]);
2576
- }
2577
- if (fileMimeType && mimeType && fileMimeType !== mimeType) {
2578
- return _defineProperty({}, path, [{
2579
- message: "Mime type and file extension not matching. Mime type is '".concat(mimeType, "' but file extension is '").concat(fileMimeType, "'"),
2580
- value: src,
2581
- fixes: ["image:check-metadata"]
2582
- }]);
2583
- }
2584
- if (src.metadata) {
2585
- if (src.metadata.hotspot) {
2586
- if (_typeof(src.metadata.hotspot) !== "object" || typeof src.metadata.hotspot.x !== "number" || typeof src.metadata.hotspot.y !== "number") {
2587
- return _defineProperty({}, path, [{
2588
- message: "Hotspot must be an object with x and y as numbers.",
2589
- value: src
2590
- }]);
2591
- }
2592
- }
2593
- return _defineProperty({}, path, [{
2594
- 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 the mime type.",
2595
- // 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.
2596
- value: src,
2597
- fixes: ["image:check-metadata"]
2598
- }]);
1729
+ return current;
2599
1730
  }
2600
- return _defineProperty({}, path, [{
2601
- message: "Could not validate Image metadata.",
2602
- value: src,
2603
- fixes: ["image:add-metadata"]
2604
- }]);
1731
+ if (lengthErrors.length > 0) {
1732
+ return _defineProperty({}, rootPath, lengthErrors);
1733
+ }
1734
+ return false;
2605
1735
  }
2606
1736
  }, {
2607
1737
  key: "assert",
@@ -2612,40 +1742,32 @@ var ImageSchema = /*#__PURE__*/function (_Schema) {
2612
1742
  data: src
2613
1743
  };
2614
1744
  }
2615
- if (src === null) {
1745
+ if (src === null && !this.opt) {
2616
1746
  return {
2617
1747
  success: false,
2618
1748
  errors: _defineProperty({}, path, [{
2619
- message: "Expected 'object', got 'null'",
1749
+ message: "Expected 'array', got 'null'",
2620
1750
  typeError: true
2621
1751
  }])
2622
1752
  };
2623
1753
  }
2624
- if (_typeof(src) !== "object") {
1754
+ if (!Array.isArray(src)) {
2625
1755
  return {
2626
1756
  success: false,
2627
1757
  errors: _defineProperty({}, path, [{
2628
- message: "Expected 'object', got '".concat(_typeof(src), "'"),
1758
+ message: "Expected 'array', got '".concat(_typeof(src), "'"),
2629
1759
  typeError: true
2630
1760
  }])
2631
1761
  };
2632
1762
  }
2633
- if (!(FILE_REF_PROP in src)) {
2634
- return {
2635
- success: false,
2636
- errors: _defineProperty({}, path, [{
2637
- message: "Value of this schema must use: 'c.image' (error type: missing_ref_prop)",
2638
- typeError: true
2639
- }])
2640
- };
1763
+ var errors = {};
1764
+ for (var i = 0; i < src.length; i++) {
1765
+ this.recursiveAssert(unsafeCreateSourcePath(path, i), src[i], errors);
2641
1766
  }
2642
- if (!(VAL_EXTENSION in src && src[VAL_EXTENSION] === "file")) {
1767
+ if (Object.keys(errors).length > 0) {
2643
1768
  return {
2644
1769
  success: false,
2645
- errors: _defineProperty({}, path, [{
2646
- message: "Value of this schema must use: 'c.image' (error type: missing_file_extension)",
2647
- typeError: true
2648
- }])
1770
+ errors: errors
2649
1771
  };
2650
1772
  }
2651
1773
  return {
@@ -2653,24 +1775,133 @@ var ImageSchema = /*#__PURE__*/function (_Schema) {
2653
1775
  data: src
2654
1776
  };
2655
1777
  }
1778
+ }, {
1779
+ key: "recursiveAssert",
1780
+ value: function recursiveAssert(path, node, errors) {
1781
+ if (_typeof(node) !== "object") {
1782
+ if (!errors[path]) {
1783
+ errors[path] = [];
1784
+ }
1785
+ errors[path].push({
1786
+ message: "Expected 'object', got '".concat(_typeof(node), "'"),
1787
+ typeError: true
1788
+ });
1789
+ return;
1790
+ }
1791
+ if (Array.isArray(node)) {
1792
+ if (!errors[path]) {
1793
+ errors[path] = [];
1794
+ }
1795
+ errors[path].push({
1796
+ message: "Expected 'object', got 'array'",
1797
+ typeError: true
1798
+ });
1799
+ return;
1800
+ }
1801
+ if (node === null) {
1802
+ if (!errors[path]) {
1803
+ errors[path] = [];
1804
+ }
1805
+ errors[path].push({
1806
+ message: "Expected 'object', got 'null'",
1807
+ typeError: true
1808
+ });
1809
+ return;
1810
+ }
1811
+ if ("tag" in node) {
1812
+ if (typeof node.tag !== "string") {
1813
+ if (!errors[path]) {
1814
+ errors[path] = [];
1815
+ }
1816
+ errors[path].push({
1817
+ message: "Expected 'string', got '".concat(_typeof(node.tag), "'"),
1818
+ typeError: true
1819
+ });
1820
+ return;
1821
+ }
1822
+ }
1823
+ if ("children" in node) {
1824
+ if (!Array.isArray(node.children)) {
1825
+ if (!errors[path]) {
1826
+ errors[path] = [];
1827
+ }
1828
+ errors[path].push({
1829
+ message: "Expected 'array', got '".concat(_typeof(node.children), "'"),
1830
+ typeError: true
1831
+ });
1832
+ return;
1833
+ } else {
1834
+ for (var i = 0; i < node.children.length; i++) {
1835
+ var child = node.children[i];
1836
+ var pathAtError = unsafeCreateSourcePath(unsafeCreateSourcePath(path, "children"), i);
1837
+ if (_typeof(child) === "object") {
1838
+ this.recursiveAssert(pathAtError, child, errors);
1839
+ } else if (typeof child === "string") {
1840
+ continue;
1841
+ } else {
1842
+ if (!errors[pathAtError]) {
1843
+ errors[pathAtError] = [];
1844
+ }
1845
+ errors[pathAtError].push({
1846
+ message: "Expected 'object' or 'string', got '".concat(_typeof(child), "'"),
1847
+ typeError: true
1848
+ });
1849
+ }
1850
+ }
1851
+ }
1852
+ }
1853
+ if ("styles" in node) {
1854
+ if (!Array.isArray(node.styles)) {
1855
+ if (!errors[path]) {
1856
+ errors[path] = [];
1857
+ }
1858
+ errors[path].push({
1859
+ message: "Expected 'array', got '".concat(_typeof(node.styles), "'"),
1860
+ typeError: true
1861
+ });
1862
+ } else {
1863
+ for (var _i2 = 0; _i2 < node.styles.length; _i2++) {
1864
+ var style = node.styles[_i2];
1865
+ if (typeof style !== "string") {
1866
+ var _pathAtError = unsafeCreateSourcePath(path, _i2);
1867
+ if (!errors[_pathAtError]) {
1868
+ errors[_pathAtError] = [];
1869
+ }
1870
+ errors[_pathAtError].push({
1871
+ message: "Expected 'string', got '".concat(_typeof(style), "'"),
1872
+ typeError: true
1873
+ });
1874
+ }
1875
+ }
1876
+ }
1877
+ }
1878
+ }
2656
1879
  }, {
2657
1880
  key: "nullable",
2658
1881
  value: function nullable() {
2659
- return new ImageSchema(this.options, true);
1882
+ return new RichTextSchema(this.options, true);
2660
1883
  }
2661
1884
  }, {
2662
1885
  key: "serialize",
2663
1886
  value: function serialize() {
1887
+ var serializedOptions = {
1888
+ style: this.options.style,
1889
+ block: this.options.block,
1890
+ inline: this.options.inline && {
1891
+ a: this.options.inline.a,
1892
+ img: this.options.inline.img && _typeof(this.options.inline.img) === "object" ? this.options.inline.img.serialize() : this.options.inline.img
1893
+ }
1894
+ };
2664
1895
  return {
2665
- type: "image",
2666
- options: this.options,
2667
- opt: this.opt
1896
+ type: "richtext",
1897
+ opt: this.opt,
1898
+ options: serializedOptions
2668
1899
  };
2669
1900
  }
2670
1901
  }]);
2671
1902
  }(Schema);
2672
- var image = function image(options) {
2673
- return new ImageSchema(options);
1903
+ var richtext = function richtext(options) {
1904
+ return new RichTextSchema(options !== null && options !== void 0 ? options : {});
2674
1905
  };
2675
1906
 
2676
1907
  var RecordSchema = /*#__PURE__*/function (_Schema) {
@@ -2787,13 +2018,10 @@ id,
2787
2018
  schema,
2788
2019
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
2789
2020
  source) {
2790
- return _defineProperty(_defineProperty(_defineProperty({}, GetSource, source), GetSchema$1, schema), Path$1, id);
2021
+ return _defineProperty(_defineProperty(_defineProperty({}, GetSource, source), GetSchema, schema), Path, id);
2791
2022
  }
2792
2023
  function getSource(valModule) {
2793
2024
  var sourceOrExpr = valModule[GetSource];
2794
- if (sourceOrExpr instanceof Expr) {
2795
- throw Error("Cannot get raw source of an Expr");
2796
- }
2797
2025
  var source = sourceOrExpr;
2798
2026
  return source;
2799
2027
  }
@@ -2956,16 +2184,11 @@ function resolvePath(path, valModule, schema) {
2956
2184
  resolvedSchema = schemaOfUnionKey.items[part];
2957
2185
  resolvedSource = resolvedSource[part];
2958
2186
  } else if (isRichTextSchema(resolvedSchema)) {
2959
- return {
2960
- v: {
2961
- path: origParts.slice(0, origParts.length - parts.length - 1).map(function (p) {
2962
- return JSON.stringify(p);
2963
- }).join("."),
2964
- // TODO: create a function generate path from parts (not sure if this always works)
2965
- schema: resolvedSchema,
2966
- source: resolvedSource
2967
- }
2968
- };
2187
+ if ("src" in resolvedSource && "tag" in resolvedSource && resolvedSource.tag === "img" && parts.length === 0) {
2188
+ var _resolvedSchema$optio, _resolvedSchema$optio2;
2189
+ resolvedSchema = (_resolvedSchema$optio = resolvedSchema.options) !== null && _resolvedSchema$optio !== void 0 && (_resolvedSchema$optio = _resolvedSchema$optio.inline) !== null && _resolvedSchema$optio !== void 0 && _resolvedSchema$optio.img && typeof ((_resolvedSchema$optio2 = resolvedSchema.options) === null || _resolvedSchema$optio2 === void 0 || (_resolvedSchema$optio2 = _resolvedSchema$optio2.inline) === null || _resolvedSchema$optio2 === void 0 ? void 0 : _resolvedSchema$optio2.img) !== "boolean" ? resolvedSchema.options.inline.img : resolvedSchema;
2190
+ }
2191
+ resolvedSource = resolvedSource[part];
2969
2192
  } else {
2970
2193
  throw Error("Invalid path: ".concat(part, " resolved to an unexpected schema ").concat(JSON.stringify(resolvedSchema)));
2971
2194
  }
@@ -3522,7 +2745,7 @@ var KeyOfSchema = /*#__PURE__*/function (_Schema) {
3522
2745
  }(Schema);
3523
2746
  var keyOf = function keyOf(valModule) {
3524
2747
  var _valModule$GetSchema;
3525
- return new KeyOfSchema(valModule === null || valModule === void 0 || (_valModule$GetSchema = valModule[GetSchema$1]) === null || _valModule$GetSchema === void 0 ? void 0 : _valModule$GetSchema.serialize(), getValPath(valModule));
2748
+ return new KeyOfSchema(valModule === null || valModule === void 0 || (_valModule$GetSchema = valModule[GetSchema]) === null || _valModule$GetSchema === void 0 ? void 0 : _valModule$GetSchema.serialize(), getValPath(valModule));
3526
2749
  };
3527
2750
 
3528
2751
  var DateSchema = /*#__PURE__*/function (_Schema) {
@@ -3737,6 +2960,28 @@ var initImage = function initImage(config) {
3737
2960
  return image;
3738
2961
  };
3739
2962
 
2963
+ /**
2964
+ * A remote source represents data that is not stored locally.
2965
+ */
2966
+
2967
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
2968
+ var initRemote = function initRemote(config) {
2969
+ function remote(ref, metadata) {
2970
+ return _defineProperty(_defineProperty(_defineProperty({}, FILE_REF_PROP, ref), VAL_EXTENSION, "remote"), "metadata", metadata);
2971
+ }
2972
+ return remote;
2973
+ };
2974
+ function createRemoteRef(remoteHost, _ref2) {
2975
+ var publicProjectId = _ref2.publicProjectId,
2976
+ coreVersion = _ref2.coreVersion,
2977
+ validationHash = _ref2.validationHash,
2978
+ fileHash = _ref2.fileHash,
2979
+ filePath = _ref2.filePath,
2980
+ bucket = _ref2.bucket;
2981
+ // NOTE: the core version is part of the validation hash, but it is also in the uri to make it easier to understand which version the remote file was validated against.
2982
+ return "".concat(remoteHost, "/file/p/").concat(publicProjectId, "/b/").concat(bucket, "/v/").concat(coreVersion, "/h/").concat(validationHash, "/f/").concat(fileHash, "/p/").concat(filePath);
2983
+ }
2984
+
3740
2985
  /* eslint-disable @typescript-eslint/ban-types */
3741
2986
  // import { i18n, I18n } from "./source/future/i18n";
3742
2987
  // import { remote } from "./source/future/remote";
@@ -3774,7 +3019,7 @@ var initVal = function initVal(config) {
3774
3019
  },
3775
3020
  c: {
3776
3021
  define: define,
3777
- // remote,
3022
+ remote: initRemote(),
3778
3023
  file: initFile(config),
3779
3024
  image: initImage(config)
3780
3025
  },
@@ -3803,6 +3048,52 @@ function modules(config, modules) {
3803
3048
  };
3804
3049
  }
3805
3050
 
3051
+ var RegEx = /^(https?:\/\/[^/]+)\/file\/p\/([^/]+)\/b\/([^/]+)\/v\/([^/]+)\/h\/([^/]+)\/f\/([^/]+)\/p\/(.+)$/;
3052
+ function splitRemoteRef(ref) {
3053
+ var match = ref.match(RegEx);
3054
+ if (!match) {
3055
+ return {
3056
+ status: "error",
3057
+ error: "Invalid remote ref: " + ref
3058
+ };
3059
+ }
3060
+ if (match[7].indexOf("public/val/") !== 0) {
3061
+ return {
3062
+ status: "error",
3063
+ error: "Invalid remote ref: " + ref
3064
+ };
3065
+ }
3066
+ return {
3067
+ status: "success",
3068
+ remoteHost: match[1],
3069
+ projectId: match[2],
3070
+ bucket: match[3],
3071
+ version: match[4],
3072
+ validationHash: match[5],
3073
+ fileHash: match[6],
3074
+ filePath: match[7]
3075
+ };
3076
+ }
3077
+
3078
+ var DEFAULT_VAL_REMOTE_HOST = "https://remote.val.build";
3079
+ function convertRemoteSource(src) {
3080
+ if (src !== null && src !== void 0 && src.patch_id) {
3081
+ var splitRemoteRefDataRes = splitRemoteRef(src[FILE_REF_PROP]);
3082
+ if (splitRemoteRefDataRes.status === "success") {
3083
+ return {
3084
+ url: "/api/val/files/" + splitRemoteRefDataRes.filePath + "?patch_id=".concat(src["patch_id"], "&remote=true"),
3085
+ metadata: src.metadata
3086
+ };
3087
+ } else {
3088
+ console.warn("Internal Val error: failed to split remote ref: ".concat(src[FILE_REF_PROP], ". The data format is different than what is expected. Check Val versions for mismatches."), splitRemoteRefDataRes.error);
3089
+ }
3090
+ }
3091
+ return {
3092
+ url: src[FILE_REF_PROP],
3093
+ metadata: src.metadata
3094
+ };
3095
+ }
3096
+
3806
3097
  var PatchError = /*#__PURE__*/_createClass(function PatchError(message) {
3807
3098
  _classCallCheck(this, PatchError);
3808
3099
  this.message = message;
@@ -3929,140 +3220,6 @@ function derefPatch(patch, document, ops) {
3929
3220
  });
3930
3221
  }
3931
3222
 
3932
- function getVal(selector) {
3933
- return newValProxy(serializedValOfSelectorSource(selector));
3934
- }
3935
-
3936
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
3937
- function isArrayOrArraySelector(child) {
3938
- if (isSelector$1(child)) {
3939
- return _typeof(child[GetSource]) === "object" && child[GetSource] !== null && Array.isArray(child[GetSource]);
3940
- }
3941
- return Array.isArray(child);
3942
- }
3943
-
3944
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
3945
- function isObjectOrObjectSelector(child) {
3946
- if (isSelector$1(child)) {
3947
- return _typeof(child[GetSource]) === "object" && child[GetSource] !== null && !Array.isArray(child[GetSource]);
3948
- }
3949
- return _typeof(child) === "object";
3950
- }
3951
- function serializedValOfSelectorSource(selector) {
3952
- var wrappedSelector = newSelectorProxy$1(selector); // NOTE: we do this if call-site uses a literal with selectors inside
3953
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
3954
- function rec(child) {
3955
- var isArray = isArrayOrArraySelector(child);
3956
- var isObject = isObjectOrObjectSelector(child);
3957
- if (isArray) {
3958
- var array = GetSource in child ? child[GetSource] : child;
3959
- var valPath = Path$1 in child ? child[Path$1] : undefined;
3960
- return {
3961
- val: array.map(function (item, i) {
3962
- return rec(isSelector$1(item) // NOTE: We do this since selectors currently do not create selectors of items unless specifically required.
3963
- ? item : newSelectorProxy$1(item, createValPathOfItem(valPath, i)));
3964
- }),
3965
- valPath: valPath
3966
- };
3967
- } else if (isObject) {
3968
- var obj = GetSource in child ? child[GetSource] : child;
3969
- var _valPath = Path$1 in child ? child[Path$1] : undefined;
3970
- return {
3971
- val: obj !== null && Object.fromEntries(Object.entries(obj).map(function (_ref) {
3972
- var _ref2 = _slicedToArray(_ref, 2),
3973
- key = _ref2[0],
3974
- value = _ref2[1];
3975
- return [key, rec(isSelector$1(value) // NOTE: We do this since selectors currently do not create selectors of items unless specifically required.
3976
- ? value : newSelectorProxy$1(value, createValPathOfItem(_valPath, key)))];
3977
- })),
3978
- valPath: _valPath
3979
- };
3980
- } else if (isSelector$1(child)) {
3981
- return {
3982
- val: rec(child[GetSource]),
3983
- valPath: child[Path$1]
3984
- };
3985
- } else {
3986
- return child;
3987
- }
3988
- }
3989
- return rec(wrappedSelector);
3990
- }
3991
- function strip(value) {
3992
- var val = isSerializedVal(value) ? value.val : value;
3993
- switch (_typeof(val)) {
3994
- case "function":
3995
- case "symbol":
3996
- throw Error("Invalid val type: ".concat(_typeof(val)));
3997
- case "object":
3998
- if (val === null) {
3999
- return null;
4000
- } else if (Array.isArray(val)) {
4001
- return val.map(strip);
4002
- } else {
4003
- return Object.fromEntries(Object.entries(val).map(function (_ref3) {
4004
- var _ref4 = _slicedToArray(_ref3, 2),
4005
- key = _ref4[0],
4006
- value = _ref4[1];
4007
- return [key, value && strip(value)];
4008
- }));
4009
- }
4010
- // intentional fallthrough
4011
- // eslint-disable-next-line no-fallthrough
4012
- default:
4013
- return val;
4014
- }
4015
- }
4016
- function newValProxy(val) {
4017
- var source = val.val;
4018
- switch (_typeof(source)) {
4019
- case "function":
4020
- case "symbol":
4021
- throw Error("Invalid val type: ".concat(_typeof(source)));
4022
- case "object":
4023
- if (source !== null) {
4024
- // Handles both objects and arrays!
4025
- return new Proxy(source, {
4026
- has: function has(target, prop) {
4027
- if (prop === "val") {
4028
- return true;
4029
- }
4030
- if (prop === Path$1) {
4031
- return true;
4032
- }
4033
- return hasOwn(target, prop);
4034
- },
4035
- get: function get(target, prop) {
4036
- if (prop === Path$1) {
4037
- return val.valPath;
4038
- }
4039
- if (prop === "val") {
4040
- return strip(val);
4041
- }
4042
- if (Array.isArray(target) && prop === "length") {
4043
- return target.length;
4044
- }
4045
- if (hasOwn(source, prop)) {
4046
- var _Reflect$get$valPath, _Reflect$get;
4047
- return newValProxy({
4048
- val: Reflect.get(target, prop).val,
4049
- valPath: (_Reflect$get$valPath = (_Reflect$get = Reflect.get(target, prop)) === null || _Reflect$get === void 0 ? void 0 : _Reflect$get.valPath) !== null && _Reflect$get$valPath !== void 0 ? _Reflect$get$valPath : createValPathOfItem(val.valPath, Array.isArray(target) ? Number(prop) : prop)
4050
- });
4051
- }
4052
- return Reflect.get(target, prop);
4053
- }
4054
- });
4055
- }
4056
- // intentional fallthrough
4057
- // eslint-disable-next-line no-fallthrough
4058
- default:
4059
- return _defineProperty(_defineProperty({}, Path$1, val.valPath), "val", val.val);
4060
- }
4061
- }
4062
- function hasOwn(obj, prop) {
4063
- return Object.prototype.hasOwnProperty.call(obj, prop);
4064
- }
4065
-
4066
3223
  /**
4067
3224
  * From: https://github.com/kawanet/sha256-uint8array/commit/a035f83824c319d01ca1e7559fdcf1632c0cd6c4
4068
3225
  *
@@ -5117,6 +4274,36 @@ function filenameToMimeType(filename) {
5117
4274
  }
5118
4275
  }
5119
4276
 
4277
+ /**
4278
+ * The validation basis is used in remote refs to determine if the remote content needs to be re-validated.
4279
+ *
4280
+ * If the validation basis changes, we need to re-validate the remote content.
4281
+ *
4282
+ * NOTE: We do not care if the file path is different as long as the extension is the same.
4283
+ * This way we can rename a file, without having to re-validate it.
4284
+ * The version is outside of the validation hash, so that it is possible to manually fix the version without having to re-validate.
4285
+ */
4286
+ function getValidationBasis(coreVersion, schema, fileExt, metadata, fileHash) {
4287
+ var metadataValidationBasis = "".concat((metadata === null || metadata === void 0 ? void 0 : metadata.width) || "").concat((metadata === null || metadata === void 0 ? void 0 : metadata.height) || "").concat(metadata === null || metadata === void 0 ? void 0 : metadata.mimeType);
4288
+ var schemaValidationBasis = {
4289
+ type: schema.type,
4290
+ opt: schema.opt,
4291
+ options: _objectSpread2({}, schema.options)
4292
+ };
4293
+ return coreVersion + JSON.stringify(schemaValidationBasis) + fileExt + metadataValidationBasis + fileHash;
4294
+ }
4295
+ function getValidationHash(coreVersion, schema, fileExt, metadata, fileHash, textEncoder) {
4296
+ return getSHA256Hash(textEncoder.encode(getValidationBasis(coreVersion, schema, fileExt, metadata, fileHash))).slice(0, 4); // we do not need a lot of bits for the validation hash, since it is only used to identify the validation basis
4297
+ }
4298
+
4299
+ function hashToRemoteFileHash(hash) {
4300
+ return hash.slice(0, 12 // 12 hex characters = 6 bytes = 48 bits = 2^48 = 281474976710656 possibilities or 1 in 281474976710656 or using birthday problem estimate with 10K files: p = (k, n) => (k*k)/(2x2**n) and p(10_000,12*4) = 1.7763568394002505e-7 chance of collision which should be good enough
4301
+ );
4302
+ }
4303
+ function getFileHash(text) {
4304
+ return hashToRemoteFileHash(getSHA256Hash(new Uint8Array(text)));
4305
+ }
4306
+
5120
4307
  function deserializeSchema(serialized) {
5121
4308
  var _serialized$options;
5122
4309
  switch (serialized.type) {
@@ -5149,7 +4336,16 @@ function deserializeSchema(serialized) {
5149
4336
  // TODO: we do not really need any here - right?
5150
4337
  serialized.opt);
5151
4338
  case "richtext":
5152
- return new RichTextSchema(serialized.options || {}, serialized.opt);
4339
+ {
4340
+ var _serialized$options2, _serialized$options3;
4341
+ var deserializedOptions = _objectSpread2(_objectSpread2({}, serialized.options || {}), {}, {
4342
+ inline: _typeof((_serialized$options2 = serialized.options) === null || _serialized$options2 === void 0 || (_serialized$options2 = _serialized$options2.inline) === null || _serialized$options2 === void 0 ? void 0 : _serialized$options2.img) === "object" ? {
4343
+ a: serialized.options.inline.a,
4344
+ img: deserializeSchema(serialized.options.inline.img)
4345
+ } : (_serialized$options3 = serialized.options) === null || _serialized$options3 === void 0 ? void 0 : _serialized$options3.inline
4346
+ });
4347
+ return new RichTextSchema(deserializedOptions, serialized.opt);
4348
+ }
5153
4349
  case "record":
5154
4350
  return new RecordSchema(deserializeSchema(serialized.item), serialized.opt);
5155
4351
  case "keyOf":
@@ -5187,14 +4383,23 @@ var Internal = {
5187
4383
  }()
5188
4384
  },
5189
4385
  convertFileSource: convertFileSource,
4386
+ convertRemoteSource: convertRemoteSource,
5190
4387
  getSchema: getSchema,
5191
4388
  getValPath: getValPath,
5192
- getVal: getVal,
5193
4389
  getSource: getSource,
5194
4390
  resolvePath: resolvePath,
5195
4391
  splitModuleFilePathAndModulePath: splitModuleFilePathAndModulePath,
5196
4392
  joinModuleFilePathAndModulePath: joinModuleFilePathAndModulePath,
4393
+ remote: {
4394
+ createRemoteRef: createRemoteRef,
4395
+ getValidationBasis: getValidationBasis,
4396
+ getValidationHash: getValidationHash,
4397
+ getFileHash: getFileHash,
4398
+ hashToRemoteFileHash: hashToRemoteFileHash,
4399
+ splitRemoteRef: splitRemoteRef
4400
+ },
5197
4401
  isVal: isVal,
4402
+ isFile: isFile,
5198
4403
  createValPathOfItem: createValPathOfItem,
5199
4404
  getSHA256Hash: getSHA256Hash,
5200
4405
  initSchema: initSchema,
@@ -5260,9 +4465,8 @@ function tryJsonParse(str) {
5260
4465
 
5261
4466
  exports.ArraySchema = ArraySchema;
5262
4467
  exports.BooleanSchema = BooleanSchema;
5263
- exports.Call = Call;
4468
+ exports.DEFAULT_VAL_REMOTE_HOST = DEFAULT_VAL_REMOTE_HOST;
5264
4469
  exports.DateSchema = DateSchema;
5265
- exports.Expr = Expr;
5266
4470
  exports.FATAL_ERROR_TYPES = FATAL_ERROR_TYPES;
5267
4471
  exports.FILE_REF_PROP = FILE_REF_PROP;
5268
4472
  exports.FILE_REF_SUBTYPE_TAG = FILE_REF_SUBTYPE_TAG;
@@ -5273,17 +4477,13 @@ exports.Internal = Internal;
5273
4477
  exports.KeyOfSchema = KeyOfSchema;
5274
4478
  exports.LiteralSchema = LiteralSchema;
5275
4479
  exports.ModuleFilePathSep = ModuleFilePathSep;
5276
- exports.NilSym = NilSym;
5277
4480
  exports.NumberSchema = NumberSchema;
5278
4481
  exports.ObjectSchema = ObjectSchema;
5279
4482
  exports.PatchError = PatchError;
5280
4483
  exports.RecordSchema = RecordSchema;
5281
4484
  exports.RichTextSchema = RichTextSchema;
5282
4485
  exports.Schema = Schema;
5283
- exports.StringLiteral = StringLiteral;
5284
4486
  exports.StringSchema = StringSchema;
5285
- exports.StringTemplate = StringTemplate;
5286
- exports.Sym = Sym;
5287
4487
  exports.UnionSchema = UnionSchema;
5288
4488
  exports.VAL_EXTENSION = VAL_EXTENSION;
5289
4489
  exports._classCallCheck = _classCallCheck;
@@ -5293,9 +4493,6 @@ exports._toConsumableArray = _toConsumableArray;
5293
4493
  exports._typeof = _typeof;
5294
4494
  exports.derefPatch = derefPatch;
5295
4495
  exports.deserializeSchema = deserializeSchema;
5296
- exports.evaluate = evaluate;
5297
- exports.index = index;
5298
4496
  exports.initVal = initVal;
5299
4497
  exports.modules = modules;
5300
- exports.parse = parse;
5301
4498
  exports.splitModuleFilePathAndModulePath = splitModuleFilePathAndModulePath;