@valbuild/core 0.43.1 → 0.44.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.
@@ -3,6 +3,7 @@ import { ValidationFix } from "./ValidationFix.js";
3
3
  export type ValidationError = {
4
4
  message: string;
5
5
  value?: unknown;
6
+ fatal?: boolean;
6
7
  fixes?: ValidationFix[];
7
8
  };
8
9
  /**
@@ -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
- // TODO:
348
- return false;
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",
@@ -776,6 +974,7 @@ exports.content = content;
776
974
  exports.createValPathOfItem = createValPathOfItem;
777
975
  exports.getSource = getSource;
778
976
  exports.isSelector = isSelector;
977
+ exports.literal = literal;
779
978
  exports.newSelectorProxy = newSelectorProxy;
780
979
  exports.object = object;
781
980
  exports.record = record;
@@ -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
- // TODO:
348
- return false;
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",
@@ -776,6 +974,7 @@ exports.content = content;
776
974
  exports.createValPathOfItem = createValPathOfItem;
777
975
  exports.getSource = getSource;
778
976
  exports.isSelector = isSelector;
977
+ exports.literal = literal;
779
978
  exports.newSelectorProxy = newSelectorProxy;
780
979
  exports.object = object;
781
980
  exports.record = record;
@@ -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
- // TODO:
346
- return false;
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",
@@ -768,4 +966,4 @@ var PatchError = /*#__PURE__*/_createClass(function PatchError(message) {
768
966
  * NOTE: MAY mutate the input document.
769
967
  */
770
968
 
771
- export { PatchError as P, array as a, record as b, content as c, createValPathOfItem as d, resolvePath as e, getSource as g, isSelector as i, newSelectorProxy as n, object as o, richtext as r, splitModuleIdAndModulePath as s, union as u };
969
+ export { PatchError as P, array as a, record as b, content as c, createValPathOfItem as d, resolvePath as e, getSource as g, isSelector as i, literal as l, newSelectorProxy as n, object as o, richtext as r, splitModuleIdAndModulePath as s, union as u };
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var ops = require('./ops-e02ab215.cjs.dev.js');
5
+ var ops = require('./ops-422169e5.cjs.dev.js');
6
6
  var index = require('./index-a5295001.cjs.dev.js');
7
7
  var expr_dist_valbuildCoreExpr = require('./index-6deb169d.cjs.dev.js');
8
8
  var result = require('./result-48320acd.cjs.dev.js');
@@ -195,67 +195,6 @@ var _boolean = function _boolean() {
195
195
  return new BooleanSchema();
196
196
  };
197
197
 
198
- var LiteralSchema = /*#__PURE__*/function (_Schema) {
199
- index._inherits(LiteralSchema, _Schema);
200
- var _super = index._createSuper(LiteralSchema);
201
- function LiteralSchema(value) {
202
- var _this;
203
- var opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
204
- index._classCallCheck(this, LiteralSchema);
205
- _this = _super.call(this);
206
- _this.value = value;
207
- _this.opt = opt;
208
- return _this;
209
- }
210
- index._createClass(LiteralSchema, [{
211
- key: "validate",
212
- value: function validate(path, src) {
213
- if (this.opt && (src === null || src === undefined)) {
214
- return false;
215
- }
216
- if (typeof src !== "string") {
217
- return index._defineProperty({}, path, [{
218
- message: "Expected 'string', got '".concat(index._typeof(src), "'"),
219
- value: src
220
- }]);
221
- }
222
- if (src !== this.value) {
223
- return index._defineProperty({}, path, [{
224
- message: "Expected literal '".concat(this.value, "', got '").concat(src, "'"),
225
- value: src
226
- }]);
227
- }
228
- return false;
229
- }
230
- }, {
231
- key: "assert",
232
- value: function assert(src) {
233
- if (this.opt && (src === null || src === undefined)) {
234
- return true;
235
- }
236
- return typeof src === "string";
237
- }
238
- }, {
239
- key: "optional",
240
- value: function optional() {
241
- return new LiteralSchema(this.value, true);
242
- }
243
- }, {
244
- key: "serialize",
245
- value: function serialize() {
246
- return {
247
- type: "literal",
248
- value: this.value,
249
- opt: this.opt
250
- };
251
- }
252
- }]);
253
- return LiteralSchema;
254
- }(index.Schema);
255
- var literal = function literal(value) {
256
- return new LiteralSchema(value);
257
- };
258
-
259
198
  var KeyOfSchema = /*#__PURE__*/function (_Schema) {
260
199
  index._inherits(KeyOfSchema, _Schema);
261
200
  var _super = index._createSuper(KeyOfSchema);
@@ -403,7 +342,7 @@ function initSchema() {
403
342
  // oneOf,
404
343
  richtext: ops.richtext,
405
344
  image: index.image,
406
- literal: literal,
345
+ literal: ops.literal,
407
346
  keyOf: keyOf,
408
347
  record: ops.record
409
348
  // i18n: i18n(locales),
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var ops = require('./ops-d88e99cd.cjs.prod.js');
5
+ var ops = require('./ops-5bd1bd7a.cjs.prod.js');
6
6
  var index = require('./index-5bdaa229.cjs.prod.js');
7
7
  var expr_dist_valbuildCoreExpr = require('./index-216627d7.cjs.prod.js');
8
8
  var result = require('./result-26f67b40.cjs.prod.js');
@@ -195,67 +195,6 @@ var _boolean = function _boolean() {
195
195
  return new BooleanSchema();
196
196
  };
197
197
 
198
- var LiteralSchema = /*#__PURE__*/function (_Schema) {
199
- index._inherits(LiteralSchema, _Schema);
200
- var _super = index._createSuper(LiteralSchema);
201
- function LiteralSchema(value) {
202
- var _this;
203
- var opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
204
- index._classCallCheck(this, LiteralSchema);
205
- _this = _super.call(this);
206
- _this.value = value;
207
- _this.opt = opt;
208
- return _this;
209
- }
210
- index._createClass(LiteralSchema, [{
211
- key: "validate",
212
- value: function validate(path, src) {
213
- if (this.opt && (src === null || src === undefined)) {
214
- return false;
215
- }
216
- if (typeof src !== "string") {
217
- return index._defineProperty({}, path, [{
218
- message: "Expected 'string', got '".concat(index._typeof(src), "'"),
219
- value: src
220
- }]);
221
- }
222
- if (src !== this.value) {
223
- return index._defineProperty({}, path, [{
224
- message: "Expected literal '".concat(this.value, "', got '").concat(src, "'"),
225
- value: src
226
- }]);
227
- }
228
- return false;
229
- }
230
- }, {
231
- key: "assert",
232
- value: function assert(src) {
233
- if (this.opt && (src === null || src === undefined)) {
234
- return true;
235
- }
236
- return typeof src === "string";
237
- }
238
- }, {
239
- key: "optional",
240
- value: function optional() {
241
- return new LiteralSchema(this.value, true);
242
- }
243
- }, {
244
- key: "serialize",
245
- value: function serialize() {
246
- return {
247
- type: "literal",
248
- value: this.value,
249
- opt: this.opt
250
- };
251
- }
252
- }]);
253
- return LiteralSchema;
254
- }(index.Schema);
255
- var literal = function literal(value) {
256
- return new LiteralSchema(value);
257
- };
258
-
259
198
  var KeyOfSchema = /*#__PURE__*/function (_Schema) {
260
199
  index._inherits(KeyOfSchema, _Schema);
261
200
  var _super = index._createSuper(KeyOfSchema);
@@ -403,7 +342,7 @@ function initSchema() {
403
342
  // oneOf,
404
343
  richtext: ops.richtext,
405
344
  image: index.image,
406
- literal: literal,
345
+ literal: ops.literal,
407
346
  keyOf: keyOf,
408
347
  record: ops.record
409
348
  // i18n: i18n(locales),
@@ -1,4 +1,4 @@
1
- import { a as array, o as object, u as union, r as richtext$1, b as record, c as content, P as PatchError, n as newSelectorProxy, d as createValPathOfItem, i as isSelector, g as getSource, e as resolvePath, s as splitModuleIdAndModulePath } from './ops-266faf09.esm.js';
1
+ import { a as array, o as object, u as union, r as richtext$1, l as literal, b as record, c as content, P as PatchError, n as newSelectorProxy, d as createValPathOfItem, i as isSelector, g as getSource, e as resolvePath, s as splitModuleIdAndModulePath } from './ops-899d8bef.esm.js';
2
2
  import { _ as _inherits, a as _createSuper, b as _classCallCheck, c as _createClass, d as _defineProperty, e as _typeof, S as Schema, G as GetSchema, g as getValPath, i as image, V as VAL_EXTENSION, f as file, h as _slicedToArray, j as isFile, F as FILE_REF_PROP, P as Path, k as GetSource, l as isSerializedVal, m as convertFileSource, n as getSchema, o as isVal } from './index-c83918b8.esm.js';
3
3
  export { F as FILE_REF_PROP, p as GenericSelector, S as Schema, V as VAL_EXTENSION } from './index-c83918b8.esm.js';
4
4
  export { i as expr } from './index-4f821892.esm.js';
@@ -192,67 +192,6 @@ var _boolean = function _boolean() {
192
192
  return new BooleanSchema();
193
193
  };
194
194
 
195
- var LiteralSchema = /*#__PURE__*/function (_Schema) {
196
- _inherits(LiteralSchema, _Schema);
197
- var _super = _createSuper(LiteralSchema);
198
- function LiteralSchema(value) {
199
- var _this;
200
- var opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
201
- _classCallCheck(this, LiteralSchema);
202
- _this = _super.call(this);
203
- _this.value = value;
204
- _this.opt = opt;
205
- return _this;
206
- }
207
- _createClass(LiteralSchema, [{
208
- key: "validate",
209
- value: function validate(path, src) {
210
- if (this.opt && (src === null || src === undefined)) {
211
- return false;
212
- }
213
- if (typeof src !== "string") {
214
- return _defineProperty({}, path, [{
215
- message: "Expected 'string', got '".concat(_typeof(src), "'"),
216
- value: src
217
- }]);
218
- }
219
- if (src !== this.value) {
220
- return _defineProperty({}, path, [{
221
- message: "Expected literal '".concat(this.value, "', got '").concat(src, "'"),
222
- value: src
223
- }]);
224
- }
225
- return false;
226
- }
227
- }, {
228
- key: "assert",
229
- value: function assert(src) {
230
- if (this.opt && (src === null || src === undefined)) {
231
- return true;
232
- }
233
- return typeof src === "string";
234
- }
235
- }, {
236
- key: "optional",
237
- value: function optional() {
238
- return new LiteralSchema(this.value, true);
239
- }
240
- }, {
241
- key: "serialize",
242
- value: function serialize() {
243
- return {
244
- type: "literal",
245
- value: this.value,
246
- opt: this.opt
247
- };
248
- }
249
- }]);
250
- return LiteralSchema;
251
- }(Schema);
252
- var literal = function literal(value) {
253
- return new LiteralSchema(value);
254
- };
255
-
256
195
  var KeyOfSchema = /*#__PURE__*/function (_Schema) {
257
196
  _inherits(KeyOfSchema, _Schema);
258
197
  var _super = _createSuper(KeyOfSchema);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@valbuild/core",
3
- "version": "0.43.1",
3
+ "version": "0.44.0",
4
4
  "private": false,
5
5
  "description": "Val - supercharged hard-coded content",
6
6
  "scripts": {
@@ -5,7 +5,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  var index = require('../../dist/index-a5295001.cjs.dev.js');
6
6
  var result = require('../../dist/result-48320acd.cjs.dev.js');
7
7
  var util = require('../../dist/util-b213092b.cjs.dev.js');
8
- var ops = require('../../dist/ops-e02ab215.cjs.dev.js');
8
+ var ops = require('../../dist/ops-422169e5.cjs.dev.js');
9
9
 
10
10
  function isNotRoot(path) {
11
11
  return result.isNonEmpty(path);
@@ -5,7 +5,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  var index = require('../../dist/index-5bdaa229.cjs.prod.js');
6
6
  var result = require('../../dist/result-26f67b40.cjs.prod.js');
7
7
  var util = require('../../dist/util-030d8a1f.cjs.prod.js');
8
- var ops = require('../../dist/ops-d88e99cd.cjs.prod.js');
8
+ var ops = require('../../dist/ops-5bd1bd7a.cjs.prod.js');
9
9
 
10
10
  function isNotRoot(path) {
11
11
  return result.isNonEmpty(path);
@@ -1,8 +1,8 @@
1
1
  import { e as _typeof, h as _slicedToArray, c as _createClass, b as _classCallCheck, u as _toConsumableArray } from '../../dist/index-c83918b8.esm.js';
2
2
  import { f as isNonEmpty, e as err, o as ok, m as map, g as flatMap, i as isErr, h as flatMapReduce, j as filterOrElse, k as mapErr, l as map$1, n as all, p as flatten, q as allT } from '../../dist/result-b96df128.esm.js';
3
3
  import { p as pipe } from '../../dist/util-18613e99.esm.js';
4
- import { P as PatchError, s as splitModuleIdAndModulePath } from '../../dist/ops-266faf09.esm.js';
5
- export { P as PatchError } from '../../dist/ops-266faf09.esm.js';
4
+ import { P as PatchError, s as splitModuleIdAndModulePath } from '../../dist/ops-899d8bef.esm.js';
5
+ export { P as PatchError } from '../../dist/ops-899d8bef.esm.js';
6
6
 
7
7
  function isNotRoot(path) {
8
8
  return isNonEmpty(path);