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