@valbuild/core 0.16.0 → 0.18.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 (104) hide show
  1. package/ROADMAP.md +106 -0
  2. package/dist/declarations/src/ValApi.d.ts +26 -0
  3. package/dist/declarations/src/expr/eval.d.ts +1 -1
  4. package/dist/declarations/src/future/fetchVal.d.ts +5 -0
  5. package/dist/declarations/src/index.d.ts +36 -9
  6. package/dist/declarations/src/initSchema.d.ts +8 -10
  7. package/dist/declarations/src/initVal.d.ts +3 -21
  8. package/dist/declarations/src/module.d.ts +2 -14
  9. package/dist/declarations/src/schema/image.d.ts +1 -1
  10. package/dist/declarations/src/schema/index.d.ts +3 -5
  11. package/dist/declarations/src/schema/keyOf.d.ts +23 -0
  12. package/dist/declarations/src/schema/record.d.ts +19 -0
  13. package/dist/declarations/src/schema/union.d.ts +1 -0
  14. package/dist/declarations/src/selector/SelectorProxy.d.ts +7 -0
  15. package/dist/declarations/src/selector/array.d.ts +2 -13
  16. package/dist/declarations/src/selector/file.d.ts +1 -4
  17. package/dist/declarations/src/selector/future/array.d.ts +17 -0
  18. package/dist/declarations/src/selector/future/boolean.d.ts +2 -0
  19. package/dist/declarations/src/selector/future/file.d.ts +9 -0
  20. package/dist/declarations/src/selector/{i18n.d.ts → future/i18n.d.ts} +2 -2
  21. package/dist/declarations/src/selector/future/index.d.ts +81 -0
  22. package/dist/declarations/src/selector/future/number.d.ts +2 -0
  23. package/dist/declarations/src/selector/future/object.d.ts +10 -0
  24. package/dist/declarations/src/selector/future/primitive.d.ts +9 -0
  25. package/dist/declarations/src/selector/{remote.d.ts → future/remote.d.ts} +1 -1
  26. package/dist/declarations/src/selector/future/string.d.ts +2 -0
  27. package/dist/declarations/src/selector/index.d.ts +5 -32
  28. package/dist/declarations/src/selector/object.d.ts +2 -9
  29. package/dist/declarations/src/selector/primitive.d.ts +3 -9
  30. package/dist/declarations/src/source/{i18n.d.ts → future/i18n.d.ts} +2 -2
  31. package/dist/declarations/src/source/{remote.d.ts → future/remote.d.ts} +3 -3
  32. package/dist/declarations/src/source/index.d.ts +4 -3
  33. package/dist/declarations/src/val/index.d.ts +2 -2
  34. package/dist/{index-2fff5ca8.cjs.dev.js → index-486c7fbf.cjs.dev.js} +242 -54
  35. package/dist/{index-af761363.esm.js → index-4abf3a1f.esm.js} +189 -1
  36. package/dist/{index-cac9ecbd.cjs.prod.js → index-601a7d73.cjs.prod.js} +242 -54
  37. package/dist/{SelectorProxy-2af1b2b8.cjs.prod.js → index-8706c87e.cjs.prod.js} +25 -199
  38. package/dist/{SelectorProxy-873782a5.cjs.dev.js → index-a6e642dd.cjs.dev.js} +25 -199
  39. package/dist/{SelectorProxy-63c2d0e2.esm.js → index-a9235737.esm.js} +23 -195
  40. package/dist/{ops-1b6e0e35.cjs.prod.js → ops-0d09f8ee.cjs.prod.js} +258 -126
  41. package/dist/{ops-74661336.esm.js → ops-a2a295f8.esm.js} +195 -65
  42. package/dist/{ops-ea4827fc.cjs.dev.js → ops-f3015423.cjs.dev.js} +258 -126
  43. package/dist/valbuild-core.cjs.dev.js +784 -197
  44. package/dist/valbuild-core.cjs.prod.js +784 -197
  45. package/dist/valbuild-core.esm.js +719 -133
  46. package/expr/dist/valbuild-core-expr.cjs.dev.js +8 -8
  47. package/expr/dist/valbuild-core-expr.cjs.prod.js +8 -8
  48. package/expr/dist/valbuild-core-expr.esm.js +2 -2
  49. package/package.json +2 -3
  50. package/patch/dist/valbuild-core-patch.cjs.dev.js +24 -24
  51. package/patch/dist/valbuild-core-patch.cjs.prod.js +24 -24
  52. package/patch/dist/valbuild-core-patch.esm.js +3 -3
  53. package/src/ValApi.ts +85 -0
  54. package/src/expr/eval.test.ts +2 -2
  55. package/src/expr/eval.ts +2 -2
  56. package/src/{fetchVal.test.ts → future/fetchVal.test.ts} +57 -57
  57. package/src/{fetchVal.ts → future/fetchVal.ts} +17 -22
  58. package/src/index.ts +46 -14
  59. package/src/initSchema.ts +17 -12
  60. package/src/initVal.ts +42 -52
  61. package/src/module.test.ts +40 -40
  62. package/src/module.ts +53 -43
  63. package/src/patch/deref.test.ts +1 -1
  64. package/src/patch/deref.ts +1 -1
  65. package/src/patch/json.test.ts +0 -1
  66. package/src/schema/{i18n.ts → future/i18n.ts} +12 -10
  67. package/src/schema/{oneOf.ts → future/oneOf.ts} +19 -17
  68. package/src/schema/image.ts +1 -1
  69. package/src/schema/index.ts +16 -13
  70. package/src/schema/keyOf.ts +167 -0
  71. package/src/schema/record.ts +103 -0
  72. package/src/schema/union.ts +1 -1
  73. package/src/schema/validation.test.ts +195 -130
  74. package/src/selector/SelectorProxy.ts +15 -15
  75. package/src/selector/array.ts +2 -26
  76. package/src/selector/file.ts +1 -9
  77. package/src/selector/{ExprProxy.test.ts → future/ExprProxy.test.ts} +2 -2
  78. package/src/selector/{ExprProxy.ts → future/ExprProxy.ts} +9 -2
  79. package/src/selector/{SelectorProxy.test.ts → future/SelectorProxy.test.ts} +4 -4
  80. package/src/selector/future/SelectorProxy.ts +238 -0
  81. package/src/selector/future/array.ts +37 -0
  82. package/src/selector/future/boolean.ts +4 -0
  83. package/src/selector/future/file.ts +14 -0
  84. package/src/selector/{i18n.ts → future/i18n.ts} +2 -2
  85. package/src/selector/future/index.ts +165 -0
  86. package/src/selector/future/number.ts +4 -0
  87. package/src/selector/future/object.ts +22 -0
  88. package/src/selector/future/primitive.ts +17 -0
  89. package/src/selector/{remote.ts → future/remote.ts} +1 -1
  90. package/src/selector/{selector.test.ts → future/selector.test.ts} +8 -28
  91. package/src/selector/{selectorOf.ts → future/selectorOf.ts} +1 -1
  92. package/src/selector/future/string.ts +4 -0
  93. package/src/selector/index.ts +4 -46
  94. package/src/selector/object.ts +2 -19
  95. package/src/selector/primitive.ts +3 -16
  96. package/src/source/{i18n.ts → future/i18n.ts} +2 -2
  97. package/src/source/{remote.ts → future/remote.ts} +3 -3
  98. package/src/source/index.ts +3 -2
  99. package/src/val/array.ts +1 -1
  100. package/src/val/index.ts +2 -2
  101. package/src/val/object.ts +1 -1
  102. package/dist/declarations/src/fetchVal.d.ts +0 -5
  103. package/dist/declarations/src/schema/i18n.d.ts +0 -22
  104. package/dist/declarations/src/schema/oneOf.d.ts +0 -23
@@ -1,6 +1,150 @@
1
- import { _ as _inherits, a as _createSuper, b as _classCallCheck, c as _createClass, f as _typeof, e as _defineProperty, j as _slicedToArray, l as createValPathOfItem, d as _objectSpread2, S as Schema, V as VAL_EXTENSION, n as newSelectorProxy, m as SourceOrExpr, E as Expr, w as _toConsumableArray, I as ImageSchema } from './SelectorProxy-63c2d0e2.esm.js';
1
+ import { e as _typeof, m as isSerializedVal, F as FILE_REF_PROP, V as VAL_EXTENSION, n as convertFileSource, d as _defineProperty, l as GetSource, P as Path, G as GetSchema, S as Schema, E as Expr, _ as _inherits, a as _createSuper, b as _classCallCheck, c as _createClass, f as _slicedToArray, h as _objectSpread2, u as _toConsumableArray, I as ImageSchema } from './index-a9235737.esm.js';
2
2
  import { _ as _createForOfIteratorHelper } from './result-b96df128.esm.js';
3
3
 
4
+ function hasOwn(obj, prop) {
5
+ return Object.prototype.hasOwnProperty.call(obj, prop);
6
+ }
7
+ function _andThen(f, source, path) {
8
+ if (source) {
9
+ return newSelectorProxy(f(newSelectorProxy(source, path)));
10
+ }
11
+ return newSelectorProxy(source, path);
12
+ }
13
+ function isSelector(source) {
14
+ return _typeof(source) === "object" && source !== null && (GetSource in source || Path in source);
15
+ }
16
+ function newSelectorProxy(source, path, moduleSchema) {
17
+ var _ref;
18
+ if (_typeof(source) === "object") {
19
+ if (isSelector(source)) {
20
+ return source;
21
+ } else if (isSerializedVal(source)) {
22
+ return newSelectorProxy(source.val, source.valPath);
23
+ }
24
+ }
25
+ if (source && source[FILE_REF_PROP] && source[VAL_EXTENSION] === "file") {
26
+ var fileRef = source[FILE_REF_PROP];
27
+ if (typeof fileRef !== "string") {
28
+ throw Error("Invalid file ref: " + fileRef);
29
+ }
30
+ return newSelectorProxy(convertFileSource(source), path, moduleSchema);
31
+ }
32
+ switch (_typeof(source)) {
33
+ case "function":
34
+ case "symbol":
35
+ throw Error("Invalid selector type: ".concat(_typeof(source), ": ").concat(source));
36
+ case "object":
37
+ // Handles both objects and arrays!
38
+ if (source !== null) {
39
+ return new Proxy(source, {
40
+ // TODO: see proxy docs if we want more traps
41
+ has: function has(target, prop) {
42
+ if (prop === GetSource) {
43
+ return true;
44
+ }
45
+ if (prop === Path) {
46
+ return true;
47
+ }
48
+ if (prop === "andThen") {
49
+ return true;
50
+ }
51
+ if (prop === GetSchema) {
52
+ return true;
53
+ }
54
+ return prop in target;
55
+ },
56
+ get: function get(target, prop) {
57
+ if (prop === GetSource) {
58
+ return source;
59
+ }
60
+ if (prop === Path) {
61
+ return path;
62
+ }
63
+ if (prop === GetSchema) {
64
+ return moduleSchema;
65
+ }
66
+ if (prop === "andThen") {
67
+ return function (f) {
68
+ return _andThen(f, source, path);
69
+ };
70
+ }
71
+ if (Array.isArray(target)) {
72
+ if (prop === "filter") {
73
+ return function (f) {
74
+ var filtered = target.map(function (a, i) {
75
+ return newSelectorProxy(a, createValPathOfItem(path, i), moduleSchema === null || moduleSchema === void 0 ? void 0 : moduleSchema.item);
76
+ }).filter(function (a) {
77
+ if (f && f instanceof Schema) {
78
+ return f.assert(unValify(a));
79
+ } else {
80
+ return unValify(f(a));
81
+ }
82
+ });
83
+ return newSelectorProxy(filtered, path, moduleSchema);
84
+ };
85
+ } else if (prop === "map") {
86
+ return function (f) {
87
+ var filtered = target.map(function (a, i) {
88
+ var valueOrSelector = f(newSelectorProxy(a, createValPathOfItem(path, i), moduleSchema === null || moduleSchema === void 0 ? void 0 : moduleSchema.item), newSelectorProxy(i));
89
+ if (isSelector(valueOrSelector)) {
90
+ return valueOrSelector;
91
+ }
92
+ return newSelectorProxy(valueOrSelector);
93
+ });
94
+ return newSelectorProxy(filtered, path, moduleSchema);
95
+ };
96
+ }
97
+ }
98
+ if (Array.isArray(target) && prop === "length") {
99
+ return newSelectorProxy(target.length);
100
+ }
101
+ var reflectedValue = Reflect.get(target, prop);
102
+ if (hasOwn(source, prop)) {
103
+ if (!Number.isNaN(Number(prop))) {
104
+ return newSelectorProxy(reflectedValue, createValPathOfItem(path, Number(prop)), moduleSchema === null || moduleSchema === void 0 ? void 0 : moduleSchema.item);
105
+ }
106
+ return newSelectorProxy(reflectedValue, createValPathOfItem(path, prop), moduleSchema === null || moduleSchema === void 0 ? void 0 : moduleSchema.items[prop]);
107
+ }
108
+ return reflectedValue;
109
+ }
110
+ });
111
+ }
112
+ // intentional fallthrough
113
+ // eslint-disable-next-line no-fallthrough
114
+ default:
115
+ return _ref = {
116
+ eq: function eq(other) {
117
+ var otherValue = other;
118
+ if (isSelector(other)) {
119
+ otherValue = other[GetSource];
120
+ if (otherValue instanceof Expr) {
121
+ throw Error("TODO: Cannot evaluate equality with an Expr");
122
+ }
123
+ }
124
+ return newSelectorProxy(source === otherValue, undefined);
125
+ },
126
+ andThen: function andThen(f) {
127
+ return _andThen(f, source === undefined ? null : source, path);
128
+ }
129
+ }, _defineProperty(_ref, GetSource, source === undefined ? null : source), _defineProperty(_ref, Path, path), _defineProperty(_ref, GetSchema, moduleSchema), _ref;
130
+ }
131
+ }
132
+ function createValPathOfItem(arrayPath, prop) {
133
+ if (_typeof(prop) === "symbol") {
134
+ throw Error("Cannot create val path of array item with symbol prop: ".concat(prop.toString()));
135
+ }
136
+ return arrayPath && "".concat(arrayPath, ".").concat(JSON.stringify(prop));
137
+ }
138
+
139
+ // TODO: could we do .val on the objects instead?
140
+ function unValify(valueOrSelector) {
141
+ if (_typeof(valueOrSelector) === "object" && (GetSource in valueOrSelector || Path in valueOrSelector)) {
142
+ var selectorValue = valueOrSelector[GetSource];
143
+ return selectorValue;
144
+ }
145
+ return valueOrSelector;
146
+ }
147
+
4
148
  var ObjectSchema = /*#__PURE__*/function (_Schema) {
5
149
  _inherits(ObjectSchema, _Schema);
6
150
  var _super = _createSuper(ObjectSchema);
@@ -183,53 +327,6 @@ var array = function array(schema) {
183
327
  return new ArraySchema(schema);
184
328
  };
185
329
 
186
- var I18nSchema = /*#__PURE__*/function (_Schema) {
187
- _inherits(I18nSchema, _Schema);
188
- var _super = _createSuper(I18nSchema);
189
- function I18nSchema(locales, item) {
190
- var _this;
191
- var opt = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
192
- _classCallCheck(this, I18nSchema);
193
- _this = _super.call(this);
194
- _this.locales = locales;
195
- _this.item = item;
196
- _this.opt = opt;
197
- return _this;
198
- }
199
- _createClass(I18nSchema, [{
200
- key: "validate",
201
- value: function validate(path, src) {
202
- throw new Error("Method not implemented.");
203
- }
204
- }, {
205
- key: "assert",
206
- value: function assert(src) {
207
- throw new Error("Method not implemented.");
208
- }
209
- }, {
210
- key: "optional",
211
- value: function optional() {
212
- return new I18nSchema(this.locales, this.item, true);
213
- }
214
- }, {
215
- key: "serialize",
216
- value: function serialize() {
217
- return {
218
- type: "i18n",
219
- item: this.item.serialize(),
220
- locales: this.locales,
221
- opt: this.opt
222
- };
223
- }
224
- }]);
225
- return I18nSchema;
226
- }(Schema);
227
- var i18n = function i18n(locales) {
228
- return function (schema) {
229
- return new I18nSchema(locales, schema);
230
- };
231
- };
232
-
233
330
  var UnionSchema = /*#__PURE__*/function (_Schema) {
234
331
  _inherits(UnionSchema, _Schema);
235
332
  var _super = _createSuper(UnionSchema);
@@ -361,10 +458,11 @@ id,
361
458
  schema,
362
459
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
363
460
  source) {
364
- return newSelectorProxy(source, id, schema);
461
+ var _ref;
462
+ return _ref = {}, _defineProperty(_ref, GetSource, source), _defineProperty(_ref, GetSchema, schema), _defineProperty(_ref, Path, id), _ref;
365
463
  }
366
- function getRawSource(valModule) {
367
- var sourceOrExpr = valModule[SourceOrExpr];
464
+ function getSource(valModule) {
465
+ var sourceOrExpr = valModule[GetSource];
368
466
  if (sourceOrExpr instanceof Expr) {
369
467
  throw Error("Cannot get raw source of an Expr");
370
468
  }
@@ -380,9 +478,16 @@ function isObjectSchema(schema) {
380
478
  function isArraySchema(schema) {
381
479
  return schema instanceof ArraySchema || _typeof(schema) === "object" && "type" in schema && schema.type === "array";
382
480
  }
383
- function isI18nSchema(schema) {
384
- return schema instanceof I18nSchema || _typeof(schema) === "object" && "type" in schema && schema.type === "i18n";
385
- }
481
+
482
+ // function isI18nSchema(
483
+ // schema: Schema<SelectorSource> | SerializedSchema
484
+ // ): schema is I18nSchema<readonly string[]> | SerializedI18nSchema {
485
+ // return (
486
+ // schema instanceof I18nSchema ||
487
+ // (typeof schema === "object" && "type" in schema && schema.type === "i18n")
488
+ // );
489
+ // }
490
+
386
491
  function isUnionSchema(schema) {
387
492
  return schema instanceof UnionSchema || _typeof(schema) === "object" && "type" in schema && schema.type === "union";
388
493
  }
@@ -394,6 +499,16 @@ function isRichTextSchema(schema) {
394
499
  function isImageSchema(schema) {
395
500
  return schema instanceof ImageSchema || _typeof(schema) === "object" && "type" in schema && schema.type === "image";
396
501
  }
502
+
503
+ // function isOneOfSchema(
504
+ // schema: Schema<SelectorSource> | SerializedSchema
505
+ // ): schema is OneOfSchema<GenericSelector<SourceArray>> | SerializedOneOfSchema {
506
+ // return (
507
+ // schema instanceof OneOfSchema ||
508
+ // (typeof schema === "object" && "type" in schema && schema.type === "oneOf")
509
+ // );
510
+ // }
511
+
397
512
  function resolvePath(path, valModule, schema) {
398
513
  var parts = parsePath(path);
399
514
  var origParts = _toConsumableArray(parts);
@@ -427,15 +542,23 @@ function resolvePath(path, valModule, schema) {
427
542
  }
428
543
  resolvedSource = resolvedSource[part];
429
544
  resolvedSchema = resolvedSchema.items[part];
430
- } else if (isI18nSchema(resolvedSchema)) {
431
- if (!resolvedSchema.locales.includes(part)) {
432
- throw Error("Invalid path: i18n schema ".concat(resolvedSchema, " supports locales ").concat(resolvedSchema.locales.join(", "), ", but found: ").concat(part));
433
- }
434
- if (!Object.keys(resolvedSource).includes(part)) {
435
- throw Error("Schema type error: expected source to be type of i18n with locale ".concat(part, ", but got ").concat(JSON.stringify(Object.keys(resolvedSource))));
436
- }
437
- resolvedSource = resolvedSource[part];
438
- resolvedSchema = resolvedSchema.item;
545
+ // } else if (isI18nSchema(resolvedSchema)) {
546
+ // if (!resolvedSchema.locales.includes(part)) {
547
+ // throw Error(
548
+ // `Invalid path: i18n schema ${resolvedSchema} supports locales ${resolvedSchema.locales.join(
549
+ // ", "
550
+ // )}, but found: ${part}`
551
+ // );
552
+ // }
553
+ // if (!Object.keys(resolvedSource).includes(part)) {
554
+ // throw Error(
555
+ // `Schema type error: expected source to be type of i18n with locale ${part}, but got ${JSON.stringify(
556
+ // Object.keys(resolvedSource)
557
+ // )}`
558
+ // );
559
+ // }
560
+ // resolvedSource = resolvedSource[part];
561
+ // resolvedSchema = resolvedSchema.item;
439
562
  } else if (isImageSchema(resolvedSchema)) {
440
563
  return {
441
564
  v: {
@@ -487,7 +610,14 @@ function resolvePath(path, valModule, schema) {
487
610
  throw Error("Invalid path: ".concat(parts.join("."), " is not a valid path"));
488
611
  }
489
612
  return {
490
- path: path,
613
+ path: origParts.map(function (p) {
614
+ if (!Number.isNaN(Number(p))) {
615
+ return p;
616
+ } else {
617
+ return JSON.stringify(p);
618
+ }
619
+ }).join("."),
620
+ // TODO: create a function generate path from parts (not sure if this always works)
491
621
  schema: resolvedSchema,
492
622
  source: resolvedSource
493
623
  };
@@ -538,4 +668,4 @@ var PatchError = /*#__PURE__*/_createClass(function PatchError(message) {
538
668
  * NOTE: MAY mutate the input document.
539
669
  */
540
670
 
541
- export { PatchError as P, array as a, resolvePath as b, content as c, getRawSource as g, i18n as i, object as o, richtext as r, splitModuleIdAndModulePath as s, union as u };
671
+ export { PatchError as P, array as a, content as b, createValPathOfItem as c, resolvePath as d, getSource as g, isSelector as i, newSelectorProxy as n, object as o, richtext as r, splitModuleIdAndModulePath as s, union as u };