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