@valbuild/core 0.43.1 → 0.45.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/declarations/src/index.d.ts +3 -4
- package/dist/declarations/src/initSchema.d.ts +1 -1
- package/dist/declarations/src/module.d.ts +3 -1
- package/dist/declarations/src/schema/file.d.ts +10 -0
- package/dist/declarations/src/schema/image.d.ts +11 -7
- package/dist/declarations/src/schema/validation/ValidationError.d.ts +1 -0
- package/dist/declarations/src/selector/file.d.ts +6 -2
- package/dist/declarations/src/selector/image.d.ts +3 -0
- package/dist/declarations/src/selector/index.d.ts +3 -1
- package/dist/declarations/src/source/file.d.ts +8 -9
- package/dist/declarations/src/source/image.d.ts +7 -0
- package/dist/declarations/src/source/richtext.d.ts +4 -4
- package/dist/{index-6deb169d.cjs.dev.js → index-00276955.cjs.dev.js} +1 -1
- package/dist/{index-c83918b8.esm.js → index-5750c299.esm.js} +3 -81
- package/dist/{index-4f821892.esm.js → index-799bcf49.esm.js} +1 -1
- package/dist/{index-5bdaa229.cjs.prod.js → index-7b467ee6.cjs.prod.js} +2 -82
- package/dist/{index-a5295001.cjs.dev.js → index-d23e237a.cjs.dev.js} +2 -82
- package/dist/{index-216627d7.cjs.prod.js → index-db60fc1d.cjs.prod.js} +1 -1
- package/dist/{ops-266faf09.esm.js → ops-117cb796.esm.js} +281 -4
- package/dist/{ops-d88e99cd.cjs.prod.js → ops-18aded72.cjs.prod.js} +283 -4
- package/dist/{ops-e02ab215.cjs.dev.js → ops-453cddfc.cjs.dev.js} +283 -4
- package/dist/valbuild-core.cjs.dev.js +5 -66
- package/dist/valbuild-core.cjs.prod.js +5 -66
- package/dist/valbuild-core.esm.js +4 -65
- package/expr/dist/valbuild-core-expr.cjs.dev.js +2 -2
- package/expr/dist/valbuild-core-expr.cjs.prod.js +2 -2
- package/expr/dist/valbuild-core-expr.esm.js +2 -2
- package/package.json +1 -1
- package/patch/dist/valbuild-core-patch.cjs.dev.js +2 -2
- package/patch/dist/valbuild-core-patch.cjs.prod.js +2 -2
- package/patch/dist/valbuild-core-patch.esm.js +3 -3
@@ -1,4 +1,4 @@
|
|
1
|
-
import { e as _typeof,
|
1
|
+
import { e as _typeof, k as isSerializedVal, F as FILE_REF_PROP, V as VAL_EXTENSION, l as convertFileSource, d as _defineProperty, G as GetSchema, j as GetSource, E as Expr, P as Path, S as Schema, _ as _inherits, a as _createSuper, b as _classCallCheck, c as _createClass, h as _slicedToArray, p as _objectSpread2, t as _toConsumableArray } from './index-5750c299.esm.js';
|
2
2
|
import { _ as _createForOfIteratorHelper } from './result-b96df128.esm.js';
|
3
3
|
|
4
4
|
function hasOwn(obj, prop) {
|
@@ -161,6 +161,8 @@ var ObjectSchema = /*#__PURE__*/function (_Schema) {
|
|
161
161
|
value: function validate(path, src) {
|
162
162
|
var _this2 = this;
|
163
163
|
var error = false;
|
164
|
+
|
165
|
+
// TODO: src should never be undefined
|
164
166
|
if (this.opt && (src === null || src === undefined)) {
|
165
167
|
return false;
|
166
168
|
}
|
@@ -326,6 +328,67 @@ var array = function array(schema) {
|
|
326
328
|
return new ArraySchema(schema);
|
327
329
|
};
|
328
330
|
|
331
|
+
var LiteralSchema = /*#__PURE__*/function (_Schema) {
|
332
|
+
_inherits(LiteralSchema, _Schema);
|
333
|
+
var _super = _createSuper(LiteralSchema);
|
334
|
+
function LiteralSchema(value) {
|
335
|
+
var _this;
|
336
|
+
var opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
337
|
+
_classCallCheck(this, LiteralSchema);
|
338
|
+
_this = _super.call(this);
|
339
|
+
_this.value = value;
|
340
|
+
_this.opt = opt;
|
341
|
+
return _this;
|
342
|
+
}
|
343
|
+
_createClass(LiteralSchema, [{
|
344
|
+
key: "validate",
|
345
|
+
value: function validate(path, src) {
|
346
|
+
if (this.opt && (src === null || src === undefined)) {
|
347
|
+
return false;
|
348
|
+
}
|
349
|
+
if (typeof src !== "string") {
|
350
|
+
return _defineProperty({}, path, [{
|
351
|
+
message: "Expected 'string', got '".concat(_typeof(src), "'"),
|
352
|
+
value: src
|
353
|
+
}]);
|
354
|
+
}
|
355
|
+
if (src !== this.value) {
|
356
|
+
return _defineProperty({}, path, [{
|
357
|
+
message: "Expected literal '".concat(this.value, "', got '").concat(src, "'"),
|
358
|
+
value: src
|
359
|
+
}]);
|
360
|
+
}
|
361
|
+
return false;
|
362
|
+
}
|
363
|
+
}, {
|
364
|
+
key: "assert",
|
365
|
+
value: function assert(src) {
|
366
|
+
if (this.opt && (src === null || src === undefined)) {
|
367
|
+
return true;
|
368
|
+
}
|
369
|
+
return typeof src === "string";
|
370
|
+
}
|
371
|
+
}, {
|
372
|
+
key: "optional",
|
373
|
+
value: function optional() {
|
374
|
+
return new LiteralSchema(this.value, true);
|
375
|
+
}
|
376
|
+
}, {
|
377
|
+
key: "serialize",
|
378
|
+
value: function serialize() {
|
379
|
+
return {
|
380
|
+
type: "literal",
|
381
|
+
value: this.value,
|
382
|
+
opt: this.opt
|
383
|
+
};
|
384
|
+
}
|
385
|
+
}]);
|
386
|
+
return LiteralSchema;
|
387
|
+
}(Schema);
|
388
|
+
var literal = function literal(value) {
|
389
|
+
return new LiteralSchema(value);
|
390
|
+
};
|
391
|
+
|
329
392
|
var UnionSchema = /*#__PURE__*/function (_Schema) {
|
330
393
|
_inherits(UnionSchema, _Schema);
|
331
394
|
var _super = _createSuper(UnionSchema);
|
@@ -342,8 +405,143 @@ var UnionSchema = /*#__PURE__*/function (_Schema) {
|
|
342
405
|
_createClass(UnionSchema, [{
|
343
406
|
key: "validate",
|
344
407
|
value: function validate(path, src) {
|
345
|
-
|
346
|
-
|
408
|
+
var unknownSrc = src;
|
409
|
+
var errors = false;
|
410
|
+
if (this.opt && (unknownSrc === null || unknownSrc === undefined)) {
|
411
|
+
// TODO: src should never be undefined
|
412
|
+
return false;
|
413
|
+
}
|
414
|
+
if (!this.key) {
|
415
|
+
return _defineProperty({}, path, [{
|
416
|
+
message: "Missing required first argument in union"
|
417
|
+
}]);
|
418
|
+
}
|
419
|
+
var key = this.key;
|
420
|
+
if (!Array.isArray(this.items)) {
|
421
|
+
return _defineProperty({}, path, [{
|
422
|
+
message: "A union schema must take more than 1 schema arguments",
|
423
|
+
fatal: true
|
424
|
+
}]);
|
425
|
+
}
|
426
|
+
if (typeof key === "string") {
|
427
|
+
// tagged union
|
428
|
+
if (this.items.some(function (item) {
|
429
|
+
return !(item instanceof ObjectSchema);
|
430
|
+
})) {
|
431
|
+
return _defineProperty({}, path, [{
|
432
|
+
message: "Key is a string, so all schema items must be objects",
|
433
|
+
fatal: true
|
434
|
+
}]);
|
435
|
+
}
|
436
|
+
var objectSchemas = this.items;
|
437
|
+
var serializedSchemas = objectSchemas.map(function (schema) {
|
438
|
+
return schema.serialize();
|
439
|
+
});
|
440
|
+
var illegalSchemas = serializedSchemas.filter(function (schema) {
|
441
|
+
return !(schema.type === "object") || !(schema.items[key].type === "literal");
|
442
|
+
});
|
443
|
+
if (illegalSchemas.length > 0) {
|
444
|
+
return _defineProperty({}, path, [{
|
445
|
+
message: "All schema items must be objects with a key: ".concat(key, " that is a literal schema. Found: ").concat(JSON.stringify(illegalSchemas, null, 2)),
|
446
|
+
fatal: true
|
447
|
+
}]);
|
448
|
+
}
|
449
|
+
var serializedObjectSchemas = serializedSchemas;
|
450
|
+
var optionalLiterals = serializedObjectSchemas.filter(function (schema) {
|
451
|
+
return schema.items[key].opt;
|
452
|
+
});
|
453
|
+
if (optionalLiterals.length > 1) {
|
454
|
+
return _defineProperty({}, path, [{
|
455
|
+
message: "Schema cannot have an optional keys: ".concat(key),
|
456
|
+
fatal: true
|
457
|
+
}]);
|
458
|
+
}
|
459
|
+
if (_typeof(unknownSrc) !== "object") {
|
460
|
+
return _defineProperty({}, path, [{
|
461
|
+
message: "Expected an object"
|
462
|
+
}]);
|
463
|
+
}
|
464
|
+
var objectSrc = unknownSrc;
|
465
|
+
if (objectSrc[key] === undefined) {
|
466
|
+
return _defineProperty({}, path, [{
|
467
|
+
message: "Missing required key: ".concat(key)
|
468
|
+
}]);
|
469
|
+
}
|
470
|
+
var foundSchemaLiterals = [];
|
471
|
+
var _iterator = _createForOfIteratorHelper(serializedObjectSchemas),
|
472
|
+
_step;
|
473
|
+
try {
|
474
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
475
|
+
var schema = _step.value;
|
476
|
+
var schemaKey = schema.items[key];
|
477
|
+
if (schemaKey.type === "literal") {
|
478
|
+
if (!foundSchemaLiterals.includes(schemaKey.value)) {
|
479
|
+
foundSchemaLiterals.push(schemaKey.value);
|
480
|
+
} else {
|
481
|
+
return _defineProperty({}, path, [{
|
482
|
+
message: "Found duplicate key in schema: ".concat(schemaKey.value),
|
483
|
+
fatal: true
|
484
|
+
}]);
|
485
|
+
}
|
486
|
+
}
|
487
|
+
}
|
488
|
+
} catch (err) {
|
489
|
+
_iterator.e(err);
|
490
|
+
} finally {
|
491
|
+
_iterator.f();
|
492
|
+
}
|
493
|
+
var objectSchemaAtKey = objectSchemas.find(function (schema) {
|
494
|
+
return !schema.items[key].validate(path, objectSrc[key]);
|
495
|
+
});
|
496
|
+
if (!objectSchemaAtKey) {
|
497
|
+
var keyPath = createValPathOfItem(path, key);
|
498
|
+
if (!keyPath) {
|
499
|
+
throw new Error("Internal error: could not create path at ".concat(!path && typeof path === "string" ? "<empty string>" : path, " at key ").concat(key));
|
500
|
+
}
|
501
|
+
return _defineProperty({}, keyPath, [{
|
502
|
+
message: "Invalid key: \"".concat(key, "\". Value was: \"").concat(objectSrc[key], "\". Valid values: ").concat(serializedObjectSchemas.map(function (schema) {
|
503
|
+
var keySchema = schema.items[key];
|
504
|
+
if (keySchema.type === "literal" && keySchema.value) {
|
505
|
+
return "\"".concat(keySchema.value, "\"");
|
506
|
+
} else {
|
507
|
+
// should not happen here, we already checked this
|
508
|
+
throw new Error("Expected literal schema, got ".concat(JSON.stringify(keySchema, null, 2)));
|
509
|
+
}
|
510
|
+
}).join(", "))
|
511
|
+
}]);
|
512
|
+
}
|
513
|
+
var error = objectSchemaAtKey.validate(path, objectSrc);
|
514
|
+
if (error) {
|
515
|
+
return error;
|
516
|
+
}
|
517
|
+
} else if (key instanceof LiteralSchema) {
|
518
|
+
if (this.items.some(function (item) {
|
519
|
+
return !(item instanceof LiteralSchema);
|
520
|
+
})) {
|
521
|
+
return _defineProperty({}, path, [{
|
522
|
+
message: "Key is a literal schema, so all schema items must be literals",
|
523
|
+
fatal: true
|
524
|
+
}]);
|
525
|
+
}
|
526
|
+
var literalItems = [key].concat(_toConsumableArray(this.items));
|
527
|
+
if (typeof unknownSrc === "string") {
|
528
|
+
var isMatch = literalItems.some(function (item) {
|
529
|
+
return !item.validate(path, unknownSrc);
|
530
|
+
});
|
531
|
+
if (!isMatch) {
|
532
|
+
return _defineProperty({}, path, [{
|
533
|
+
message: "Union must match one of the following: ".concat(literalItems.map(function (item) {
|
534
|
+
return "\"".concat(item.value, "\"");
|
535
|
+
}).join(", "))
|
536
|
+
}]);
|
537
|
+
}
|
538
|
+
}
|
539
|
+
} else {
|
540
|
+
return _defineProperty({}, path, [{
|
541
|
+
message: "Expected a string or literal"
|
542
|
+
}]);
|
543
|
+
}
|
544
|
+
return errors;
|
347
545
|
}
|
348
546
|
}, {
|
349
547
|
key: "assert",
|
@@ -428,6 +626,85 @@ var richtext = function richtext(options) {
|
|
428
626
|
return new RichTextSchema(options !== null && options !== void 0 ? options : {});
|
429
627
|
};
|
430
628
|
|
629
|
+
var ImageSchema = /*#__PURE__*/function (_Schema) {
|
630
|
+
_inherits(ImageSchema, _Schema);
|
631
|
+
var _super = _createSuper(ImageSchema);
|
632
|
+
function ImageSchema(options) {
|
633
|
+
var _this;
|
634
|
+
var opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
635
|
+
_classCallCheck(this, ImageSchema);
|
636
|
+
_this = _super.call(this);
|
637
|
+
_this.options = options;
|
638
|
+
_this.opt = opt;
|
639
|
+
return _this;
|
640
|
+
}
|
641
|
+
_createClass(ImageSchema, [{
|
642
|
+
key: "validate",
|
643
|
+
value: function validate(path, src) {
|
644
|
+
if (this.opt && (src === null || src === undefined)) {
|
645
|
+
return false;
|
646
|
+
}
|
647
|
+
if (src === null || src === undefined) {
|
648
|
+
return _defineProperty({}, path, [{
|
649
|
+
message: "Non-optional image was null or undefined.",
|
650
|
+
value: src
|
651
|
+
}]);
|
652
|
+
}
|
653
|
+
if (typeof src[FILE_REF_PROP] !== "string") {
|
654
|
+
return _defineProperty({}, path, [{
|
655
|
+
message: "Image did not have a file reference string. Got: ".concat(_typeof(src[FILE_REF_PROP])),
|
656
|
+
value: src
|
657
|
+
}]);
|
658
|
+
}
|
659
|
+
if (src[VAL_EXTENSION] !== "file") {
|
660
|
+
return _defineProperty({}, path, [{
|
661
|
+
message: "Image did not have the valid file extension type. Got: ".concat(src[VAL_EXTENSION]),
|
662
|
+
value: src
|
663
|
+
}]);
|
664
|
+
}
|
665
|
+
if (src.metadata) {
|
666
|
+
return _defineProperty({}, path, [{
|
667
|
+
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).",
|
668
|
+
// 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.
|
669
|
+
value: src,
|
670
|
+
fixes: ["image:replace-metadata"]
|
671
|
+
}]);
|
672
|
+
}
|
673
|
+
return _defineProperty({}, path, [{
|
674
|
+
message: "Could not validate Image metadata.",
|
675
|
+
value: src,
|
676
|
+
fixes: ["image:add-metadata"]
|
677
|
+
}]);
|
678
|
+
}
|
679
|
+
}, {
|
680
|
+
key: "assert",
|
681
|
+
value: function assert(src) {
|
682
|
+
if (this.opt && (src === null || src === undefined)) {
|
683
|
+
return true;
|
684
|
+
}
|
685
|
+
return (src === null || src === void 0 ? void 0 : src[FILE_REF_PROP]) === "image" && (src === null || src === void 0 ? void 0 : src[VAL_EXTENSION]) === "file";
|
686
|
+
}
|
687
|
+
}, {
|
688
|
+
key: "optional",
|
689
|
+
value: function optional() {
|
690
|
+
return new ImageSchema(this.options, true);
|
691
|
+
}
|
692
|
+
}, {
|
693
|
+
key: "serialize",
|
694
|
+
value: function serialize() {
|
695
|
+
return {
|
696
|
+
type: "image",
|
697
|
+
options: this.options,
|
698
|
+
opt: this.opt
|
699
|
+
};
|
700
|
+
}
|
701
|
+
}]);
|
702
|
+
return ImageSchema;
|
703
|
+
}(Schema);
|
704
|
+
var image = function image(options) {
|
705
|
+
return new ImageSchema(options);
|
706
|
+
};
|
707
|
+
|
431
708
|
var RecordSchema = /*#__PURE__*/function (_Schema) {
|
432
709
|
_inherits(RecordSchema, _Schema);
|
433
710
|
var _super = _createSuper(RecordSchema);
|
@@ -768,4 +1045,4 @@ var PatchError = /*#__PURE__*/_createClass(function PatchError(message) {
|
|
768
1045
|
* NOTE: MAY mutate the input document.
|
769
1046
|
*/
|
770
1047
|
|
771
|
-
export { PatchError as P, array as a, record as b, content as c, createValPathOfItem as d,
|
1048
|
+
export { PatchError as P, array as a, record as b, content as c, createValPathOfItem as d, isSelector as e, resolvePath as f, getSource as g, image as i, literal as l, newSelectorProxy as n, object as o, richtext as r, splitModuleIdAndModulePath as s, union as u };
|
@@ -1,6 +1,6 @@
|
|
1
1
|
'use strict';
|
2
2
|
|
3
|
-
var index = require('./index-
|
3
|
+
var index = require('./index-7b467ee6.cjs.prod.js');
|
4
4
|
var result = require('./result-26f67b40.cjs.prod.js');
|
5
5
|
|
6
6
|
function hasOwn(obj, prop) {
|
@@ -163,6 +163,8 @@ var ObjectSchema = /*#__PURE__*/function (_Schema) {
|
|
163
163
|
value: function validate(path, src) {
|
164
164
|
var _this2 = this;
|
165
165
|
var error = false;
|
166
|
+
|
167
|
+
// TODO: src should never be undefined
|
166
168
|
if (this.opt && (src === null || src === undefined)) {
|
167
169
|
return false;
|
168
170
|
}
|
@@ -328,6 +330,67 @@ var array = function array(schema) {
|
|
328
330
|
return new ArraySchema(schema);
|
329
331
|
};
|
330
332
|
|
333
|
+
var LiteralSchema = /*#__PURE__*/function (_Schema) {
|
334
|
+
index._inherits(LiteralSchema, _Schema);
|
335
|
+
var _super = index._createSuper(LiteralSchema);
|
336
|
+
function LiteralSchema(value) {
|
337
|
+
var _this;
|
338
|
+
var opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
339
|
+
index._classCallCheck(this, LiteralSchema);
|
340
|
+
_this = _super.call(this);
|
341
|
+
_this.value = value;
|
342
|
+
_this.opt = opt;
|
343
|
+
return _this;
|
344
|
+
}
|
345
|
+
index._createClass(LiteralSchema, [{
|
346
|
+
key: "validate",
|
347
|
+
value: function validate(path, src) {
|
348
|
+
if (this.opt && (src === null || src === undefined)) {
|
349
|
+
return false;
|
350
|
+
}
|
351
|
+
if (typeof src !== "string") {
|
352
|
+
return index._defineProperty({}, path, [{
|
353
|
+
message: "Expected 'string', got '".concat(index._typeof(src), "'"),
|
354
|
+
value: src
|
355
|
+
}]);
|
356
|
+
}
|
357
|
+
if (src !== this.value) {
|
358
|
+
return index._defineProperty({}, path, [{
|
359
|
+
message: "Expected literal '".concat(this.value, "', got '").concat(src, "'"),
|
360
|
+
value: src
|
361
|
+
}]);
|
362
|
+
}
|
363
|
+
return false;
|
364
|
+
}
|
365
|
+
}, {
|
366
|
+
key: "assert",
|
367
|
+
value: function assert(src) {
|
368
|
+
if (this.opt && (src === null || src === undefined)) {
|
369
|
+
return true;
|
370
|
+
}
|
371
|
+
return typeof src === "string";
|
372
|
+
}
|
373
|
+
}, {
|
374
|
+
key: "optional",
|
375
|
+
value: function optional() {
|
376
|
+
return new LiteralSchema(this.value, true);
|
377
|
+
}
|
378
|
+
}, {
|
379
|
+
key: "serialize",
|
380
|
+
value: function serialize() {
|
381
|
+
return {
|
382
|
+
type: "literal",
|
383
|
+
value: this.value,
|
384
|
+
opt: this.opt
|
385
|
+
};
|
386
|
+
}
|
387
|
+
}]);
|
388
|
+
return LiteralSchema;
|
389
|
+
}(index.Schema);
|
390
|
+
var literal = function literal(value) {
|
391
|
+
return new LiteralSchema(value);
|
392
|
+
};
|
393
|
+
|
331
394
|
var UnionSchema = /*#__PURE__*/function (_Schema) {
|
332
395
|
index._inherits(UnionSchema, _Schema);
|
333
396
|
var _super = index._createSuper(UnionSchema);
|
@@ -344,8 +407,143 @@ var UnionSchema = /*#__PURE__*/function (_Schema) {
|
|
344
407
|
index._createClass(UnionSchema, [{
|
345
408
|
key: "validate",
|
346
409
|
value: function validate(path, src) {
|
347
|
-
|
348
|
-
|
410
|
+
var unknownSrc = src;
|
411
|
+
var errors = false;
|
412
|
+
if (this.opt && (unknownSrc === null || unknownSrc === undefined)) {
|
413
|
+
// TODO: src should never be undefined
|
414
|
+
return false;
|
415
|
+
}
|
416
|
+
if (!this.key) {
|
417
|
+
return index._defineProperty({}, path, [{
|
418
|
+
message: "Missing required first argument in union"
|
419
|
+
}]);
|
420
|
+
}
|
421
|
+
var key = this.key;
|
422
|
+
if (!Array.isArray(this.items)) {
|
423
|
+
return index._defineProperty({}, path, [{
|
424
|
+
message: "A union schema must take more than 1 schema arguments",
|
425
|
+
fatal: true
|
426
|
+
}]);
|
427
|
+
}
|
428
|
+
if (typeof key === "string") {
|
429
|
+
// tagged union
|
430
|
+
if (this.items.some(function (item) {
|
431
|
+
return !(item instanceof ObjectSchema);
|
432
|
+
})) {
|
433
|
+
return index._defineProperty({}, path, [{
|
434
|
+
message: "Key is a string, so all schema items must be objects",
|
435
|
+
fatal: true
|
436
|
+
}]);
|
437
|
+
}
|
438
|
+
var objectSchemas = this.items;
|
439
|
+
var serializedSchemas = objectSchemas.map(function (schema) {
|
440
|
+
return schema.serialize();
|
441
|
+
});
|
442
|
+
var illegalSchemas = serializedSchemas.filter(function (schema) {
|
443
|
+
return !(schema.type === "object") || !(schema.items[key].type === "literal");
|
444
|
+
});
|
445
|
+
if (illegalSchemas.length > 0) {
|
446
|
+
return index._defineProperty({}, path, [{
|
447
|
+
message: "All schema items must be objects with a key: ".concat(key, " that is a literal schema. Found: ").concat(JSON.stringify(illegalSchemas, null, 2)),
|
448
|
+
fatal: true
|
449
|
+
}]);
|
450
|
+
}
|
451
|
+
var serializedObjectSchemas = serializedSchemas;
|
452
|
+
var optionalLiterals = serializedObjectSchemas.filter(function (schema) {
|
453
|
+
return schema.items[key].opt;
|
454
|
+
});
|
455
|
+
if (optionalLiterals.length > 1) {
|
456
|
+
return index._defineProperty({}, path, [{
|
457
|
+
message: "Schema cannot have an optional keys: ".concat(key),
|
458
|
+
fatal: true
|
459
|
+
}]);
|
460
|
+
}
|
461
|
+
if (index._typeof(unknownSrc) !== "object") {
|
462
|
+
return index._defineProperty({}, path, [{
|
463
|
+
message: "Expected an object"
|
464
|
+
}]);
|
465
|
+
}
|
466
|
+
var objectSrc = unknownSrc;
|
467
|
+
if (objectSrc[key] === undefined) {
|
468
|
+
return index._defineProperty({}, path, [{
|
469
|
+
message: "Missing required key: ".concat(key)
|
470
|
+
}]);
|
471
|
+
}
|
472
|
+
var foundSchemaLiterals = [];
|
473
|
+
var _iterator = result._createForOfIteratorHelper(serializedObjectSchemas),
|
474
|
+
_step;
|
475
|
+
try {
|
476
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
477
|
+
var schema = _step.value;
|
478
|
+
var schemaKey = schema.items[key];
|
479
|
+
if (schemaKey.type === "literal") {
|
480
|
+
if (!foundSchemaLiterals.includes(schemaKey.value)) {
|
481
|
+
foundSchemaLiterals.push(schemaKey.value);
|
482
|
+
} else {
|
483
|
+
return index._defineProperty({}, path, [{
|
484
|
+
message: "Found duplicate key in schema: ".concat(schemaKey.value),
|
485
|
+
fatal: true
|
486
|
+
}]);
|
487
|
+
}
|
488
|
+
}
|
489
|
+
}
|
490
|
+
} catch (err) {
|
491
|
+
_iterator.e(err);
|
492
|
+
} finally {
|
493
|
+
_iterator.f();
|
494
|
+
}
|
495
|
+
var objectSchemaAtKey = objectSchemas.find(function (schema) {
|
496
|
+
return !schema.items[key].validate(path, objectSrc[key]);
|
497
|
+
});
|
498
|
+
if (!objectSchemaAtKey) {
|
499
|
+
var keyPath = createValPathOfItem(path, key);
|
500
|
+
if (!keyPath) {
|
501
|
+
throw new Error("Internal error: could not create path at ".concat(!path && typeof path === "string" ? "<empty string>" : path, " at key ").concat(key));
|
502
|
+
}
|
503
|
+
return index._defineProperty({}, keyPath, [{
|
504
|
+
message: "Invalid key: \"".concat(key, "\". Value was: \"").concat(objectSrc[key], "\". Valid values: ").concat(serializedObjectSchemas.map(function (schema) {
|
505
|
+
var keySchema = schema.items[key];
|
506
|
+
if (keySchema.type === "literal" && keySchema.value) {
|
507
|
+
return "\"".concat(keySchema.value, "\"");
|
508
|
+
} else {
|
509
|
+
// should not happen here, we already checked this
|
510
|
+
throw new Error("Expected literal schema, got ".concat(JSON.stringify(keySchema, null, 2)));
|
511
|
+
}
|
512
|
+
}).join(", "))
|
513
|
+
}]);
|
514
|
+
}
|
515
|
+
var error = objectSchemaAtKey.validate(path, objectSrc);
|
516
|
+
if (error) {
|
517
|
+
return error;
|
518
|
+
}
|
519
|
+
} else if (key instanceof LiteralSchema) {
|
520
|
+
if (this.items.some(function (item) {
|
521
|
+
return !(item instanceof LiteralSchema);
|
522
|
+
})) {
|
523
|
+
return index._defineProperty({}, path, [{
|
524
|
+
message: "Key is a literal schema, so all schema items must be literals",
|
525
|
+
fatal: true
|
526
|
+
}]);
|
527
|
+
}
|
528
|
+
var literalItems = [key].concat(index._toConsumableArray(this.items));
|
529
|
+
if (typeof unknownSrc === "string") {
|
530
|
+
var isMatch = literalItems.some(function (item) {
|
531
|
+
return !item.validate(path, unknownSrc);
|
532
|
+
});
|
533
|
+
if (!isMatch) {
|
534
|
+
return index._defineProperty({}, path, [{
|
535
|
+
message: "Union must match one of the following: ".concat(literalItems.map(function (item) {
|
536
|
+
return "\"".concat(item.value, "\"");
|
537
|
+
}).join(", "))
|
538
|
+
}]);
|
539
|
+
}
|
540
|
+
}
|
541
|
+
} else {
|
542
|
+
return index._defineProperty({}, path, [{
|
543
|
+
message: "Expected a string or literal"
|
544
|
+
}]);
|
545
|
+
}
|
546
|
+
return errors;
|
349
547
|
}
|
350
548
|
}, {
|
351
549
|
key: "assert",
|
@@ -430,6 +628,85 @@ var richtext = function richtext(options) {
|
|
430
628
|
return new RichTextSchema(options !== null && options !== void 0 ? options : {});
|
431
629
|
};
|
432
630
|
|
631
|
+
var ImageSchema = /*#__PURE__*/function (_Schema) {
|
632
|
+
index._inherits(ImageSchema, _Schema);
|
633
|
+
var _super = index._createSuper(ImageSchema);
|
634
|
+
function ImageSchema(options) {
|
635
|
+
var _this;
|
636
|
+
var opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
637
|
+
index._classCallCheck(this, ImageSchema);
|
638
|
+
_this = _super.call(this);
|
639
|
+
_this.options = options;
|
640
|
+
_this.opt = opt;
|
641
|
+
return _this;
|
642
|
+
}
|
643
|
+
index._createClass(ImageSchema, [{
|
644
|
+
key: "validate",
|
645
|
+
value: function validate(path, src) {
|
646
|
+
if (this.opt && (src === null || src === undefined)) {
|
647
|
+
return false;
|
648
|
+
}
|
649
|
+
if (src === null || src === undefined) {
|
650
|
+
return index._defineProperty({}, path, [{
|
651
|
+
message: "Non-optional image was null or undefined.",
|
652
|
+
value: src
|
653
|
+
}]);
|
654
|
+
}
|
655
|
+
if (typeof src[index.FILE_REF_PROP] !== "string") {
|
656
|
+
return index._defineProperty({}, path, [{
|
657
|
+
message: "Image did not have a file reference string. Got: ".concat(index._typeof(src[index.FILE_REF_PROP])),
|
658
|
+
value: src
|
659
|
+
}]);
|
660
|
+
}
|
661
|
+
if (src[index.VAL_EXTENSION] !== "file") {
|
662
|
+
return index._defineProperty({}, path, [{
|
663
|
+
message: "Image did not have the valid file extension type. Got: ".concat(src[index.VAL_EXTENSION]),
|
664
|
+
value: src
|
665
|
+
}]);
|
666
|
+
}
|
667
|
+
if (src.metadata) {
|
668
|
+
return index._defineProperty({}, path, [{
|
669
|
+
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).",
|
670
|
+
// 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.
|
671
|
+
value: src,
|
672
|
+
fixes: ["image:replace-metadata"]
|
673
|
+
}]);
|
674
|
+
}
|
675
|
+
return index._defineProperty({}, path, [{
|
676
|
+
message: "Could not validate Image metadata.",
|
677
|
+
value: src,
|
678
|
+
fixes: ["image:add-metadata"]
|
679
|
+
}]);
|
680
|
+
}
|
681
|
+
}, {
|
682
|
+
key: "assert",
|
683
|
+
value: function assert(src) {
|
684
|
+
if (this.opt && (src === null || src === undefined)) {
|
685
|
+
return true;
|
686
|
+
}
|
687
|
+
return (src === null || src === void 0 ? void 0 : src[index.FILE_REF_PROP]) === "image" && (src === null || src === void 0 ? void 0 : src[index.VAL_EXTENSION]) === "file";
|
688
|
+
}
|
689
|
+
}, {
|
690
|
+
key: "optional",
|
691
|
+
value: function optional() {
|
692
|
+
return new ImageSchema(this.options, true);
|
693
|
+
}
|
694
|
+
}, {
|
695
|
+
key: "serialize",
|
696
|
+
value: function serialize() {
|
697
|
+
return {
|
698
|
+
type: "image",
|
699
|
+
options: this.options,
|
700
|
+
opt: this.opt
|
701
|
+
};
|
702
|
+
}
|
703
|
+
}]);
|
704
|
+
return ImageSchema;
|
705
|
+
}(index.Schema);
|
706
|
+
var image = function image(options) {
|
707
|
+
return new ImageSchema(options);
|
708
|
+
};
|
709
|
+
|
433
710
|
var RecordSchema = /*#__PURE__*/function (_Schema) {
|
434
711
|
index._inherits(RecordSchema, _Schema);
|
435
712
|
var _super = index._createSuper(RecordSchema);
|
@@ -570,7 +847,7 @@ function isRichTextSchema(schema) {
|
|
570
847
|
return schema instanceof RichTextSchema || index._typeof(schema) === "object" && "type" in schema && schema.type === "richtext";
|
571
848
|
}
|
572
849
|
function isImageSchema(schema) {
|
573
|
-
return schema instanceof
|
850
|
+
return schema instanceof ImageSchema || index._typeof(schema) === "object" && "type" in schema && schema.type === "image";
|
574
851
|
}
|
575
852
|
|
576
853
|
// function isOneOfSchema(
|
@@ -775,7 +1052,9 @@ exports.array = array;
|
|
775
1052
|
exports.content = content;
|
776
1053
|
exports.createValPathOfItem = createValPathOfItem;
|
777
1054
|
exports.getSource = getSource;
|
1055
|
+
exports.image = image;
|
778
1056
|
exports.isSelector = isSelector;
|
1057
|
+
exports.literal = literal;
|
779
1058
|
exports.newSelectorProxy = newSelectorProxy;
|
780
1059
|
exports.object = object;
|
781
1060
|
exports.record = record;
|