@valbuild/core 0.61.0 → 0.62.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.
@@ -1,1044 +0,0 @@
1
- import { e as _typeof, n as isSerializedVal, F as FILE_REF_PROP, V as VAL_EXTENSION, p as convertFileSource, d as _defineProperty, G as GetSchema, m as GetSource, E as Expr, P as Path, S as Schema, _ as _inherits, a as _classCallCheck, b as _callSuper, c as _createClass, k as _slicedToArray, f as _objectSpread2, w as _toConsumableArray } from './index-051c34f3.esm.js';
2
- import { _ as _createForOfIteratorHelper } from './result-b96df128.esm.js';
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
- if (_typeof(source) === "object") {
18
- if (isSelector(source)) {
19
- return source;
20
- } else if (isSerializedVal(source)) {
21
- return newSelectorProxy(source.val, source.valPath);
22
- }
23
- }
24
- if (source && source[FILE_REF_PROP] && source[VAL_EXTENSION] === "file") {
25
- var fileRef = source[FILE_REF_PROP];
26
- if (typeof fileRef !== "string") {
27
- throw Error("Invalid file ref: " + fileRef);
28
- }
29
- return newSelectorProxy(convertFileSource(source), path, moduleSchema);
30
- }
31
- switch (_typeof(source)) {
32
- case "function":
33
- case "symbol":
34
- throw Error("Invalid selector type: ".concat(_typeof(source), ": ").concat(source));
35
- case "object":
36
- // Handles both objects and arrays!
37
- if (source !== null) {
38
- return new Proxy(source, {
39
- // TODO: see proxy docs if we want more traps
40
- has: function has(target, prop) {
41
- if (prop === GetSource) {
42
- return true;
43
- }
44
- if (prop === Path) {
45
- return true;
46
- }
47
- if (prop === "andThen") {
48
- return true;
49
- }
50
- if (prop === GetSchema) {
51
- return true;
52
- }
53
- return prop in target;
54
- },
55
- get: function get(target, prop) {
56
- if (prop === GetSource) {
57
- return source;
58
- }
59
- if (prop === Path) {
60
- return path;
61
- }
62
- if (prop === GetSchema) {
63
- return moduleSchema;
64
- }
65
- if (prop === "andThen") {
66
- return function (f) {
67
- return _andThen(f, source, path);
68
- };
69
- }
70
- if (Array.isArray(target)) {
71
- if (prop === "filter") {
72
- return function (f) {
73
- var filtered = target.map(function (a, i) {
74
- return newSelectorProxy(a, createValPathOfItem(path, i), moduleSchema === null || moduleSchema === void 0 ? void 0 : moduleSchema.item);
75
- }).filter(function (a) {
76
- if (f && f instanceof Schema) {
77
- return f.assert(unValify(a));
78
- } else {
79
- return unValify(f(a));
80
- }
81
- });
82
- return newSelectorProxy(filtered, path, moduleSchema);
83
- };
84
- } else if (prop === "map") {
85
- return function (f) {
86
- var filtered = target.map(function (a, i) {
87
- var valueOrSelector = f(newSelectorProxy(a, createValPathOfItem(path, i), moduleSchema === null || moduleSchema === void 0 ? void 0 : moduleSchema.item), newSelectorProxy(i));
88
- if (isSelector(valueOrSelector)) {
89
- return valueOrSelector;
90
- }
91
- return newSelectorProxy(valueOrSelector);
92
- });
93
- return newSelectorProxy(filtered, path, moduleSchema);
94
- };
95
- }
96
- }
97
- if (Array.isArray(target) && prop === "length") {
98
- return newSelectorProxy(target.length);
99
- }
100
- var reflectedValue = Reflect.get(target, prop);
101
- if (hasOwn(source, prop)) {
102
- if (!Number.isNaN(Number(prop))) {
103
- return newSelectorProxy(reflectedValue, createValPathOfItem(path, Number(prop)), moduleSchema === null || moduleSchema === void 0 ? void 0 : moduleSchema.item);
104
- }
105
- return newSelectorProxy(reflectedValue, createValPathOfItem(path, prop), moduleSchema === null || moduleSchema === void 0 ? void 0 : moduleSchema.items[prop]);
106
- }
107
- return reflectedValue;
108
- }
109
- });
110
- }
111
- // intentional fallthrough
112
- // eslint-disable-next-line no-fallthrough
113
- default:
114
- return _defineProperty(_defineProperty(_defineProperty({
115
- eq: function eq(other) {
116
- var otherValue = other;
117
- if (isSelector(other)) {
118
- otherValue = other[GetSource];
119
- if (otherValue instanceof Expr) {
120
- throw Error("TODO: Cannot evaluate equality with an Expr");
121
- }
122
- }
123
- return newSelectorProxy(source === otherValue, undefined);
124
- },
125
- andThen: function andThen(f) {
126
- return _andThen(f, source === undefined ? null : source, path);
127
- }
128
- }, GetSource, source === undefined ? null : source), Path, path), GetSchema, moduleSchema);
129
- }
130
- }
131
- function createValPathOfItem(arrayPath, prop) {
132
- if (_typeof(prop) === "symbol") {
133
- throw Error("Cannot create val path of array item with symbol prop: ".concat(prop.toString()));
134
- }
135
- return arrayPath !== undefined ? "".concat(arrayPath, ".").concat(JSON.stringify(prop)) : undefined;
136
- }
137
-
138
- // TODO: could we do .val on the objects instead?
139
- function unValify(valueOrSelector) {
140
- if (_typeof(valueOrSelector) === "object" && (GetSource in valueOrSelector || Path in valueOrSelector)) {
141
- var selectorValue = valueOrSelector[GetSource];
142
- return selectorValue;
143
- }
144
- return valueOrSelector;
145
- }
146
-
147
- var ObjectSchema = /*#__PURE__*/function (_Schema) {
148
- _inherits(ObjectSchema, _Schema);
149
- function ObjectSchema(items) {
150
- var _this;
151
- var opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
152
- _classCallCheck(this, ObjectSchema);
153
- _this = _callSuper(this, ObjectSchema);
154
- _this.items = items;
155
- _this.opt = opt;
156
- return _this;
157
- }
158
- _createClass(ObjectSchema, [{
159
- key: "validate",
160
- value: function validate(path, src) {
161
- var _this2 = this;
162
- var error = false;
163
-
164
- // TODO: src should never be undefined
165
- if (this.opt && (src === null || src === undefined)) {
166
- return false;
167
- }
168
- if (_typeof(src) !== "object") {
169
- return _defineProperty({}, path, [{
170
- message: "Expected 'object', got '".concat(_typeof(src), "'")
171
- }]);
172
- } else if (Array.isArray(src)) {
173
- return _defineProperty({}, path, [{
174
- message: "Expected 'object', got 'array'"
175
- }]);
176
- }
177
- Object.entries(this.items).forEach(function (_ref3) {
178
- var _ref4 = _slicedToArray(_ref3, 2),
179
- key = _ref4[0],
180
- schema = _ref4[1];
181
- var subPath = createValPathOfItem(path, key);
182
- if (!subPath) {
183
- error = _this2.appendValidationError(error, path, "Internal error: could not create path at ".concat(!path && typeof path === "string" ? "<empty string>" : path, " at key ").concat(key),
184
- // Should! never happen
185
- src);
186
- } else {
187
- var subError = schema.validate(subPath, src[key]);
188
- if (subError && error) {
189
- error = _objectSpread2(_objectSpread2({}, subError), error);
190
- } else if (subError) {
191
- error = subError;
192
- }
193
- }
194
- });
195
- return error;
196
- }
197
- }, {
198
- key: "assert",
199
- value: function assert(src) {
200
- if (this.opt && (src === null || src === undefined)) {
201
- return true;
202
- }
203
- if (!src) {
204
- return false;
205
- }
206
- for (var _i = 0, _Object$entries = Object.entries(this.items); _i < _Object$entries.length; _i++) {
207
- var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
208
- _key = _Object$entries$_i[0],
209
- schema = _Object$entries$_i[1];
210
- if (!schema.assert(src[_key])) {
211
- return false;
212
- }
213
- }
214
- return _typeof(src) === "object" && !Array.isArray(src);
215
- }
216
- }, {
217
- key: "nullable",
218
- value: function nullable() {
219
- return new ObjectSchema(this.items, true);
220
- }
221
- }, {
222
- key: "serialize",
223
- value: function serialize() {
224
- return {
225
- type: "object",
226
- items: Object.fromEntries(Object.entries(this.items).map(function (_ref5) {
227
- var _ref6 = _slicedToArray(_ref5, 2),
228
- key = _ref6[0],
229
- schema = _ref6[1];
230
- return [key, schema.serialize()];
231
- })),
232
- opt: this.opt
233
- };
234
- }
235
- }]);
236
- return ObjectSchema;
237
- }(Schema);
238
- var object = function object(schema) {
239
- return new ObjectSchema(schema);
240
- };
241
-
242
- var ArraySchema = /*#__PURE__*/function (_Schema) {
243
- _inherits(ArraySchema, _Schema);
244
- function ArraySchema(item) {
245
- var _this;
246
- var opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
247
- _classCallCheck(this, ArraySchema);
248
- _this = _callSuper(this, ArraySchema);
249
- _this.item = item;
250
- _this.opt = opt;
251
- return _this;
252
- }
253
- _createClass(ArraySchema, [{
254
- key: "validate",
255
- value: function validate(path, src) {
256
- var _this2 = this;
257
- var error = false;
258
- if (this.opt && (src === null || src === undefined)) {
259
- return false;
260
- }
261
- if (_typeof(src) !== "object" || !Array.isArray(src)) {
262
- return _defineProperty({}, path, [{
263
- message: "Expected 'array', got '".concat(_typeof(src), "'")
264
- }]);
265
- }
266
- src.forEach(function (i, idx) {
267
- var subPath = createValPathOfItem(path, idx);
268
- if (!subPath) {
269
- error = _this2.appendValidationError(error, path, "Internal error: could not create path at ".concat(!path && typeof path === "string" ? "<empty string>" : path, " at index ").concat(idx),
270
- // Should! never happen
271
- src);
272
- } else {
273
- var subError = _this2.item.validate(subPath, i);
274
- if (subError && error) {
275
- error = _objectSpread2(_objectSpread2({}, subError), error);
276
- } else if (subError) {
277
- error = subError;
278
- }
279
- }
280
- });
281
- return error;
282
- }
283
- }, {
284
- key: "assert",
285
- value: function assert(src) {
286
- if (this.opt && (src === null || src === undefined)) {
287
- return true;
288
- }
289
- if (!src) {
290
- return false;
291
- }
292
- var _iterator = _createForOfIteratorHelper(src),
293
- _step;
294
- try {
295
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
296
- var _item = _step.value;
297
- if (!this.item.assert(_item)) {
298
- return false;
299
- }
300
- }
301
- } catch (err) {
302
- _iterator.e(err);
303
- } finally {
304
- _iterator.f();
305
- }
306
- return _typeof(src) === "object" && Array.isArray(src);
307
- }
308
- }, {
309
- key: "nullable",
310
- value: function nullable() {
311
- return new ArraySchema(this.item, true);
312
- }
313
- }, {
314
- key: "serialize",
315
- value: function serialize() {
316
- return {
317
- type: "array",
318
- item: this.item.serialize(),
319
- opt: this.opt
320
- };
321
- }
322
- }]);
323
- return ArraySchema;
324
- }(Schema);
325
- var array = function array(schema) {
326
- return new ArraySchema(schema);
327
- };
328
-
329
- var LiteralSchema = /*#__PURE__*/function (_Schema) {
330
- _inherits(LiteralSchema, _Schema);
331
- function LiteralSchema(value) {
332
- var _this;
333
- var opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
334
- _classCallCheck(this, LiteralSchema);
335
- _this = _callSuper(this, LiteralSchema);
336
- _this.value = value;
337
- _this.opt = opt;
338
- return _this;
339
- }
340
- _createClass(LiteralSchema, [{
341
- key: "validate",
342
- value: function validate(path, src) {
343
- if (this.opt && (src === null || src === undefined)) {
344
- return false;
345
- }
346
- if (typeof src !== "string") {
347
- return _defineProperty({}, path, [{
348
- message: "Expected 'string', got '".concat(_typeof(src), "'"),
349
- value: src
350
- }]);
351
- }
352
- if (src !== this.value) {
353
- return _defineProperty({}, path, [{
354
- message: "Expected literal '".concat(this.value, "', got '").concat(src, "'"),
355
- value: src
356
- }]);
357
- }
358
- return false;
359
- }
360
- }, {
361
- key: "assert",
362
- value: function assert(src) {
363
- if (this.opt && (src === null || src === undefined)) {
364
- return true;
365
- }
366
- return typeof src === "string";
367
- }
368
- }, {
369
- key: "nullable",
370
- value: function nullable() {
371
- return new LiteralSchema(this.value, true);
372
- }
373
- }, {
374
- key: "serialize",
375
- value: function serialize() {
376
- return {
377
- type: "literal",
378
- value: this.value,
379
- opt: this.opt
380
- };
381
- }
382
- }]);
383
- return LiteralSchema;
384
- }(Schema);
385
- var literal = function literal(value) {
386
- return new LiteralSchema(value);
387
- };
388
-
389
- var UnionSchema = /*#__PURE__*/function (_Schema) {
390
- _inherits(UnionSchema, _Schema);
391
- function UnionSchema(key, items) {
392
- var _this;
393
- var opt = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
394
- _classCallCheck(this, UnionSchema);
395
- _this = _callSuper(this, UnionSchema);
396
- _this.key = key;
397
- _this.items = items;
398
- _this.opt = opt;
399
- return _this;
400
- }
401
- _createClass(UnionSchema, [{
402
- key: "validate",
403
- value: function validate(path, src) {
404
- var unknownSrc = src;
405
- var errors = false;
406
- if (this.opt && (unknownSrc === null || unknownSrc === undefined)) {
407
- // TODO: src should never be undefined
408
- return false;
409
- }
410
- if (!this.key) {
411
- return _defineProperty({}, path, [{
412
- message: "Missing required first argument in union"
413
- }]);
414
- }
415
- var key = this.key;
416
- if (!Array.isArray(this.items)) {
417
- return _defineProperty({}, path, [{
418
- message: "A union schema must take more than 1 schema arguments",
419
- fatal: true
420
- }]);
421
- }
422
- if (typeof key === "string") {
423
- // tagged union
424
- if (this.items.some(function (item) {
425
- return !(item instanceof ObjectSchema);
426
- })) {
427
- return _defineProperty({}, path, [{
428
- message: "Key is a string, so all schema items must be objects",
429
- fatal: true
430
- }]);
431
- }
432
- var objectSchemas = this.items;
433
- var serializedSchemas = objectSchemas.map(function (schema) {
434
- return schema.serialize();
435
- });
436
- var illegalSchemas = serializedSchemas.filter(function (schema) {
437
- return !(schema.type === "object") || !(schema.items[key].type === "literal");
438
- });
439
- if (illegalSchemas.length > 0) {
440
- return _defineProperty({}, path, [{
441
- message: "All schema items must be objects with a key: ".concat(key, " that is a literal schema. Found: ").concat(JSON.stringify(illegalSchemas, null, 2)),
442
- fatal: true
443
- }]);
444
- }
445
- var serializedObjectSchemas = serializedSchemas;
446
- var optionalLiterals = serializedObjectSchemas.filter(function (schema) {
447
- return schema.items[key].opt;
448
- });
449
- if (optionalLiterals.length > 1) {
450
- return _defineProperty({}, path, [{
451
- message: "Schema cannot have an optional keys: ".concat(key),
452
- fatal: true
453
- }]);
454
- }
455
- if (_typeof(unknownSrc) !== "object") {
456
- return _defineProperty({}, path, [{
457
- message: "Expected an object"
458
- }]);
459
- }
460
- var objectSrc = unknownSrc;
461
- if (objectSrc[key] === undefined) {
462
- return _defineProperty({}, path, [{
463
- message: "Missing required key: ".concat(key)
464
- }]);
465
- }
466
- var foundSchemaLiterals = [];
467
- var _iterator = _createForOfIteratorHelper(serializedObjectSchemas),
468
- _step;
469
- try {
470
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
471
- var schema = _step.value;
472
- var schemaKey = schema.items[key];
473
- if (schemaKey.type === "literal") {
474
- if (!foundSchemaLiterals.includes(schemaKey.value)) {
475
- foundSchemaLiterals.push(schemaKey.value);
476
- } else {
477
- return _defineProperty({}, path, [{
478
- message: "Found duplicate key in schema: ".concat(schemaKey.value),
479
- fatal: true
480
- }]);
481
- }
482
- }
483
- }
484
- } catch (err) {
485
- _iterator.e(err);
486
- } finally {
487
- _iterator.f();
488
- }
489
- var objectSchemaAtKey = objectSchemas.find(function (schema) {
490
- return !schema.items[key].validate(path, objectSrc[key]);
491
- });
492
- if (!objectSchemaAtKey) {
493
- var keyPath = createValPathOfItem(path, key);
494
- if (!keyPath) {
495
- throw new Error("Internal error: could not create path at ".concat(!path && typeof path === "string" ? "<empty string>" : path, " at key ").concat(key));
496
- }
497
- return _defineProperty({}, keyPath, [{
498
- message: "Invalid key: \"".concat(key, "\". Value was: \"").concat(objectSrc[key], "\". Valid values: ").concat(serializedObjectSchemas.map(function (schema) {
499
- var keySchema = schema.items[key];
500
- if (keySchema.type === "literal" && keySchema.value) {
501
- return "\"".concat(keySchema.value, "\"");
502
- } else {
503
- // should not happen here, we already checked this
504
- throw new Error("Expected literal schema, got ".concat(JSON.stringify(keySchema, null, 2)));
505
- }
506
- }).join(", "))
507
- }]);
508
- }
509
- var error = objectSchemaAtKey.validate(path, objectSrc);
510
- if (error) {
511
- return error;
512
- }
513
- } else if (key instanceof LiteralSchema) {
514
- if (this.items.some(function (item) {
515
- return !(item instanceof LiteralSchema);
516
- })) {
517
- return _defineProperty({}, path, [{
518
- message: "Key is a literal schema, so all schema items must be literals",
519
- fatal: true
520
- }]);
521
- }
522
- var literalItems = [key].concat(_toConsumableArray(this.items));
523
- if (typeof unknownSrc === "string") {
524
- var isMatch = literalItems.some(function (item) {
525
- return !item.validate(path, unknownSrc);
526
- });
527
- if (!isMatch) {
528
- return _defineProperty({}, path, [{
529
- message: "Union must match one of the following: ".concat(literalItems.map(function (item) {
530
- return "\"".concat(item.value, "\"");
531
- }).join(", "))
532
- }]);
533
- }
534
- }
535
- } else {
536
- return _defineProperty({}, path, [{
537
- message: "Expected a string or literal"
538
- }]);
539
- }
540
- return errors;
541
- }
542
- }, {
543
- key: "assert",
544
- value: function assert(src) {
545
- return true;
546
- }
547
- }, {
548
- key: "nullable",
549
- value: function nullable() {
550
- return new UnionSchema(this.key, this.items, true);
551
- }
552
- }, {
553
- key: "serialize",
554
- value: function serialize() {
555
- if (typeof this.key === "string") {
556
- return {
557
- type: "union",
558
- key: this.key,
559
- items: this.items.map(function (o) {
560
- return o.serialize();
561
- }),
562
- opt: this.opt
563
- };
564
- }
565
- return {
566
- type: "union",
567
- key: this.key.serialize(),
568
- items: this.items.map(function (o) {
569
- return o.serialize();
570
- }),
571
- opt: this.opt
572
- };
573
- }
574
- }]);
575
- return UnionSchema;
576
- }(Schema);
577
- var union = function union(key) {
578
- for (var _len = arguments.length, objects = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
579
- objects[_key - 1] = arguments[_key];
580
- }
581
- return new UnionSchema(key, objects);
582
- };
583
-
584
- var RichTextSchema = /*#__PURE__*/function (_Schema) {
585
- _inherits(RichTextSchema, _Schema);
586
- function RichTextSchema(options) {
587
- var _this;
588
- var opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
589
- _classCallCheck(this, RichTextSchema);
590
- _this = _callSuper(this, RichTextSchema);
591
- _this.options = options;
592
- _this.opt = opt;
593
- return _this;
594
- }
595
- _createClass(RichTextSchema, [{
596
- key: "validate",
597
- value: function validate(path, src) {
598
- return false; //TODO
599
- }
600
- }, {
601
- key: "assert",
602
- value: function assert(src) {
603
- return true; // TODO
604
- }
605
- }, {
606
- key: "nullable",
607
- value: function nullable() {
608
- return new RichTextSchema(this.options, true);
609
- }
610
- }, {
611
- key: "serialize",
612
- value: function serialize() {
613
- return {
614
- type: "richtext",
615
- opt: this.opt,
616
- options: this.options
617
- };
618
- }
619
- }]);
620
- return RichTextSchema;
621
- }(Schema);
622
- var richtext = function richtext(options) {
623
- return new RichTextSchema(options !== null && options !== void 0 ? options : {});
624
- };
625
-
626
- var ImageSchema = /*#__PURE__*/function (_Schema) {
627
- _inherits(ImageSchema, _Schema);
628
- function ImageSchema(options) {
629
- var _this;
630
- var opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
631
- _classCallCheck(this, ImageSchema);
632
- _this = _callSuper(this, ImageSchema);
633
- _this.options = options;
634
- _this.opt = opt;
635
- return _this;
636
- }
637
- _createClass(ImageSchema, [{
638
- key: "validate",
639
- value: function validate(path, src) {
640
- if (this.opt && (src === null || src === undefined)) {
641
- return false;
642
- }
643
- if (src === null || src === undefined) {
644
- return _defineProperty({}, path, [{
645
- message: "Non-optional image was null or undefined.",
646
- value: src
647
- }]);
648
- }
649
- if (typeof src[FILE_REF_PROP] !== "string") {
650
- return _defineProperty({}, path, [{
651
- message: "Image did not have a file reference string. Got: ".concat(_typeof(src[FILE_REF_PROP])),
652
- value: src
653
- }]);
654
- }
655
- if (src[VAL_EXTENSION] !== "file") {
656
- return _defineProperty({}, path, [{
657
- message: "Image did not have the valid file extension type. Got: ".concat(src[VAL_EXTENSION]),
658
- value: src
659
- }]);
660
- }
661
- if (src.metadata) {
662
- return _defineProperty({}, path, [{
663
- 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).",
664
- // 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.
665
- value: src,
666
- fixes: ["image:replace-metadata"]
667
- }]);
668
- }
669
- return _defineProperty({}, path, [{
670
- message: "Could not validate Image metadata.",
671
- value: src,
672
- fixes: ["image:add-metadata"]
673
- }]);
674
- }
675
- }, {
676
- key: "assert",
677
- value: function assert(src) {
678
- if (this.opt && (src === null || src === undefined)) {
679
- return true;
680
- }
681
- return (src === null || src === void 0 ? void 0 : src[FILE_REF_PROP]) === "image" && (src === null || src === void 0 ? void 0 : src[VAL_EXTENSION]) === "file";
682
- }
683
- }, {
684
- key: "nullable",
685
- value: function nullable() {
686
- return new ImageSchema(this.options, true);
687
- }
688
- }, {
689
- key: "serialize",
690
- value: function serialize() {
691
- return {
692
- type: "image",
693
- options: this.options,
694
- opt: this.opt
695
- };
696
- }
697
- }]);
698
- return ImageSchema;
699
- }(Schema);
700
- var image = function image(options) {
701
- return new ImageSchema(options);
702
- };
703
-
704
- var RecordSchema = /*#__PURE__*/function (_Schema) {
705
- _inherits(RecordSchema, _Schema);
706
- function RecordSchema(item) {
707
- var _this;
708
- var opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
709
- _classCallCheck(this, RecordSchema);
710
- _this = _callSuper(this, RecordSchema);
711
- _this.item = item;
712
- _this.opt = opt;
713
- return _this;
714
- }
715
- _createClass(RecordSchema, [{
716
- key: "validate",
717
- value: function validate(path, src) {
718
- var _this2 = this;
719
- var error = false;
720
- if (this.opt && (src === null || src === undefined)) {
721
- return false;
722
- }
723
- if (_typeof(src) !== "object") {
724
- return _defineProperty({}, path, [{
725
- message: "Expected 'object', got '".concat(_typeof(src), "'")
726
- }]);
727
- }
728
- if (Array.isArray(src)) {
729
- return _defineProperty({}, path, [{
730
- message: "Expected 'object', got 'array'"
731
- }]);
732
- }
733
- Object.entries(src).forEach(function (_ref3) {
734
- var _ref4 = _slicedToArray(_ref3, 2),
735
- key = _ref4[0],
736
- elem = _ref4[1];
737
- var subPath = createValPathOfItem(path, key);
738
- if (!subPath) {
739
- error = _this2.appendValidationError(error, path, "Internal error: could not create path at ".concat(!path && typeof path === "string" ? "<empty string>" : path, " at key ").concat(elem),
740
- // Should! never happen
741
- src);
742
- } else {
743
- var subError = _this2.item.validate(subPath, elem);
744
- if (subError && error) {
745
- error = _objectSpread2(_objectSpread2({}, subError), error);
746
- } else if (subError) {
747
- error = subError;
748
- }
749
- }
750
- });
751
- return error;
752
- }
753
- }, {
754
- key: "assert",
755
- value: function assert(src) {
756
- if (this.opt && (src === null || src === undefined)) {
757
- return true;
758
- }
759
- if (!src) {
760
- return false;
761
- }
762
- for (var _i = 0, _Object$entries = Object.entries(src); _i < _Object$entries.length; _i++) {
763
- var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
764
- _item = _Object$entries$_i[1];
765
- if (!this.item.assert(_item)) {
766
- return false;
767
- }
768
- }
769
- return _typeof(src) === "object" && !Array.isArray(src);
770
- }
771
- }, {
772
- key: "nullable",
773
- value: function nullable() {
774
- return new RecordSchema(this.item, true);
775
- }
776
- }, {
777
- key: "serialize",
778
- value: function serialize() {
779
- return {
780
- type: "record",
781
- item: this.item.serialize(),
782
- opt: this.opt
783
- };
784
- }
785
- }]);
786
- return RecordSchema;
787
- }(Schema);
788
- var record = function record(schema) {
789
- return new RecordSchema(schema);
790
- };
791
-
792
- function define(
793
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
794
- id,
795
- // TODO: `/${string}`
796
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
797
- schema,
798
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
799
- source) {
800
- return _defineProperty(_defineProperty(_defineProperty({}, GetSource, source), GetSchema, schema), Path, id);
801
- }
802
- function getSource(valModule) {
803
- var sourceOrExpr = valModule[GetSource];
804
- if (sourceOrExpr instanceof Expr) {
805
- throw Error("Cannot get raw source of an Expr");
806
- }
807
- var source = sourceOrExpr;
808
- return source;
809
- }
810
- function splitModuleIdAndModulePath(path) {
811
- if (path.indexOf(".") === -1) {
812
- return [path, ""];
813
- }
814
- return [path.slice(0, path.indexOf(".")), path.slice(path.indexOf(".") + 1)];
815
- }
816
- function isObjectSchema(schema) {
817
- return schema instanceof ObjectSchema || _typeof(schema) === "object" && "type" in schema && schema.type === "object";
818
- }
819
- function isRecordSchema(schema) {
820
- return schema instanceof RecordSchema || _typeof(schema) === "object" && "type" in schema && schema.type === "record";
821
- }
822
- function isArraySchema(schema) {
823
- return schema instanceof ArraySchema || _typeof(schema) === "object" && "type" in schema && schema.type === "array";
824
- }
825
-
826
- // function isI18nSchema(
827
- // schema: Schema<SelectorSource> | SerializedSchema
828
- // ): schema is I18nSchema<readonly string[]> | SerializedI18nSchema {
829
- // return (
830
- // schema instanceof I18nSchema ||
831
- // (typeof schema === "object" && "type" in schema && schema.type === "i18n")
832
- // );
833
- // }
834
-
835
- function isUnionSchema(schema) {
836
- return schema instanceof UnionSchema || _typeof(schema) === "object" && "type" in schema && schema.type === "union";
837
- }
838
-
839
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
840
- function isRichTextSchema(schema) {
841
- return schema instanceof RichTextSchema || _typeof(schema) === "object" && "type" in schema && schema.type === "richtext";
842
- }
843
- function isImageSchema(schema) {
844
- return schema instanceof ImageSchema || _typeof(schema) === "object" && "type" in schema && schema.type === "image";
845
- }
846
-
847
- // function isOneOfSchema(
848
- // schema: Schema<SelectorSource> | SerializedSchema
849
- // ): schema is OneOfSchema<GenericSelector<SourceArray>> | SerializedOneOfSchema {
850
- // return (
851
- // schema instanceof OneOfSchema ||
852
- // (typeof schema === "object" && "type" in schema && schema.type === "oneOf")
853
- // );
854
- // }
855
-
856
- function resolvePath(path, valModule, schema) {
857
- var parts = parsePath(path);
858
- var origParts = _toConsumableArray(parts);
859
- var resolvedSchema = schema;
860
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
861
- var resolvedSource = valModule;
862
- var _loop = function _loop() {
863
- var part = parts.shift();
864
- if (part === undefined) {
865
- throw Error("Unexpected undefined part");
866
- }
867
- if (isArraySchema(resolvedSchema)) {
868
- if (Number.isNaN(Number(part))) {
869
- throw Error("Invalid path: array schema ".concat(JSON.stringify(resolvedSchema), " must have a number as path, but got ").concat(part, ". Path: ").concat(path));
870
- }
871
- if (_typeof(resolvedSource) !== "object" && !Array.isArray(resolvedSource)) {
872
- throw Error("Schema type error: expected source to be type of array, but got ".concat(_typeof(resolvedSource)));
873
- }
874
- if (resolvedSource[part] === undefined) {
875
- var _resolvedSource;
876
- throw Error("Invalid path: array source (length: ".concat((_resolvedSource = resolvedSource) === null || _resolvedSource === void 0 ? void 0 : _resolvedSource.length, ") did not have index ").concat(part, " from path: ").concat(path));
877
- }
878
- resolvedSource = resolvedSource[part];
879
- resolvedSchema = resolvedSchema.item;
880
- } else if (isRecordSchema(resolvedSchema)) {
881
- if (typeof part !== "string") {
882
- throw Error("Invalid path: record schema ".concat(resolvedSchema, " must have path: ").concat(part, " as string"));
883
- }
884
- if (_typeof(resolvedSource) !== "object" && !Array.isArray(resolvedSource)) {
885
- throw Error("Schema type error: expected source to be type of record, but got ".concat(_typeof(resolvedSource)));
886
- }
887
- if (!resolvedSource[part]) {
888
- throw Error("Invalid path: record source did not have key ".concat(part, " from path: ").concat(path));
889
- }
890
- resolvedSource = resolvedSource[part];
891
- resolvedSchema = resolvedSchema.item;
892
- } else if (isObjectSchema(resolvedSchema)) {
893
- if (_typeof(resolvedSource) !== "object") {
894
- throw Error("Schema type error: expected source to be type of object, but got ".concat(_typeof(resolvedSource)));
895
- }
896
- if (!resolvedSource[part]) {
897
- throw Error("Invalid path: object source did not have key ".concat(part, " from path: ").concat(path));
898
- }
899
- resolvedSource = resolvedSource[part];
900
- resolvedSchema = resolvedSchema.items[part];
901
- // } else if (isI18nSchema(resolvedSchema)) {
902
- // if (!resolvedSchema.locales.includes(part)) {
903
- // throw Error(
904
- // `Invalid path: i18n schema ${resolvedSchema} supports locales ${resolvedSchema.locales.join(
905
- // ", "
906
- // )}, but found: ${part}`
907
- // );
908
- // }
909
- // if (!Object.keys(resolvedSource).includes(part)) {
910
- // throw Error(
911
- // `Schema type error: expected source to be type of i18n with locale ${part}, but got ${JSON.stringify(
912
- // Object.keys(resolvedSource)
913
- // )}`
914
- // );
915
- // }
916
- // resolvedSource = resolvedSource[part];
917
- // resolvedSchema = resolvedSchema.item;
918
- } else if (isImageSchema(resolvedSchema)) {
919
- return {
920
- v: {
921
- path: origParts.slice(0, origParts.length - parts.length - 1).map(function (p) {
922
- return JSON.stringify(p);
923
- }).join("."),
924
- // TODO: create a function generate path from parts (not sure if this always works)
925
- schema: resolvedSchema,
926
- source: resolvedSource
927
- }
928
- };
929
- } else if (isUnionSchema(resolvedSchema)) {
930
- var _key = resolvedSchema.key;
931
- if (typeof _key !== "string") {
932
- return {
933
- v: {
934
- path: origParts.map(function (p) {
935
- if (!Number.isNaN(Number(p))) {
936
- return p;
937
- } else {
938
- return JSON.stringify(p);
939
- }
940
- }).join("."),
941
- // TODO: create a function generate path from parts (not sure if this always works)
942
- schema: resolvedSchema,
943
- source: resolvedSource
944
- }
945
- };
946
- }
947
- var keyValue = resolvedSource[_key];
948
- if (!keyValue) {
949
- throw Error("Invalid path: union source ".concat(resolvedSchema, " did not have required key ").concat(_key, " in path: ").concat(path));
950
- }
951
- var schemaOfUnionKey = resolvedSchema.items.find(
952
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
953
- function (child) {
954
- var _child$items;
955
- return (child === null || child === void 0 || (_child$items = child.items) === null || _child$items === void 0 || (_child$items = _child$items[_key]) === null || _child$items === void 0 ? void 0 : _child$items.value) === keyValue;
956
- });
957
- if (!schemaOfUnionKey) {
958
- throw Error("Invalid path: union schema ".concat(resolvedSchema, " did not have a child object with ").concat(_key, " of value ").concat(keyValue, " in path: ").concat(path));
959
- }
960
- resolvedSchema = schemaOfUnionKey.items[part];
961
- resolvedSource = resolvedSource[part];
962
- } else if (isRichTextSchema(resolvedSchema)) {
963
- return {
964
- v: {
965
- path: origParts.slice(0, origParts.length - parts.length - 1).map(function (p) {
966
- return JSON.stringify(p);
967
- }).join("."),
968
- // TODO: create a function generate path from parts (not sure if this always works)
969
- schema: resolvedSchema,
970
- source: resolvedSource
971
- }
972
- };
973
- } else {
974
- throw Error("Invalid path: ".concat(part, " resolved to an unexpected schema ").concat(JSON.stringify(resolvedSchema)));
975
- }
976
- },
977
- _ret;
978
- while (parts.length > 0) {
979
- _ret = _loop();
980
- if (_ret) return _ret.v;
981
- }
982
- if (parts.length > 0) {
983
- throw Error("Invalid path: ".concat(parts.join("."), " is not a valid path"));
984
- }
985
- return {
986
- path: origParts.map(function (p) {
987
- if (!Number.isNaN(Number(p))) {
988
- return p;
989
- } else {
990
- return JSON.stringify(p);
991
- }
992
- }).join("."),
993
- // TODO: create a function generate path from parts (not sure if this always works)
994
- schema: resolvedSchema,
995
- source: resolvedSource
996
- };
997
- }
998
- function parsePath(input) {
999
- var result = [];
1000
- var i = 0;
1001
- while (i < input.length) {
1002
- var part = "";
1003
- if (input[i] === '"') {
1004
- // Parse a quoted string
1005
- i++;
1006
- while (i < input.length && input[i] !== '"') {
1007
- if (input[i] === "\\" && input[i + 1] === '"') {
1008
- // Handle escaped double quotes
1009
- part += '"';
1010
- i++;
1011
- } else {
1012
- part += input[i];
1013
- }
1014
- i++;
1015
- }
1016
- if (input[i] !== '"') {
1017
- var _input$i;
1018
- throw new Error("Invalid input (".concat(JSON.stringify(input), "): Missing closing double quote: ").concat((_input$i = input[i]) !== null && _input$i !== void 0 ? _input$i : "at end of string", " (char: ").concat(i, "; length: ").concat(input.length, ")"));
1019
- }
1020
- } else {
1021
- // Parse a regular string
1022
- while (i < input.length && input[i] !== ".") {
1023
- part += input[i];
1024
- i++;
1025
- }
1026
- }
1027
- if (part !== "") {
1028
- result.push(part);
1029
- }
1030
- i++;
1031
- }
1032
- return result;
1033
- }
1034
-
1035
- var PatchError = /*#__PURE__*/_createClass(function PatchError(message) {
1036
- _classCallCheck(this, PatchError);
1037
- this.message = message;
1038
- });
1039
-
1040
- /**
1041
- * NOTE: MAY mutate the input document.
1042
- */
1043
-
1044
- export { ArraySchema as A, ImageSchema as I, LiteralSchema as L, ObjectSchema as O, PatchError as P, RecordSchema as R, UnionSchema as U, array as a, record as b, createValPathOfItem as c, define as d, isSelector as e, RichTextSchema as f, getSource as g, resolvePath as h, image as i, literal as l, newSelectorProxy as n, object as o, parsePath as p, richtext as r, splitModuleIdAndModulePath as s, union as u };