@valbuild/core 0.17.0 → 0.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,6 +7,8 @@ import { union } from "./schema/union.js";
7
7
  import { richtext } from "./schema/richtext.js";
8
8
  import { image } from "./schema/image.js";
9
9
  import { literal } from "./schema/literal.js";
10
+ import { keyOf } from "./schema/keyOf.js";
11
+ import { record } from "./schema/record.js";
10
12
  export type InitSchema = {
11
13
  readonly string: typeof string;
12
14
  readonly boolean: typeof boolean;
@@ -17,6 +19,8 @@ export type InitSchema = {
17
19
  readonly richtext: typeof richtext;
18
20
  readonly image: typeof image;
19
21
  readonly literal: typeof literal;
22
+ readonly keyOf: typeof keyOf;
23
+ readonly record: typeof record;
20
24
  };
21
25
  export declare function initSchema(): {
22
26
  string: <T extends string>(options?: {
@@ -46,4 +50,6 @@ export declare function initSchema(): {
46
50
  richtext: () => import("./schema/index.js").Schema<import("./index.js").RichTextSource>;
47
51
  image: (options?: import("./schema/image.js").ImageOptions | undefined) => import("./schema/index.js").Schema<import("./index.js").FileSource<import("./schema/image.js").ImageMetadata>>;
48
52
  literal: <T_2 extends string>(value: T_2) => import("./schema/index.js").Schema<T_2>;
53
+ keyOf: <Src extends import("./selector/index.js").GenericSelector<import("./source/index.js").SourceObject | import("./source/index.js").SourceArray, undefined> & import("./module.js").ValModuleBrand>(valModule: Src) => import("./schema/index.js").Schema<Src extends import("./selector/index.js").GenericSelector<infer S_2 extends import("./source/index.js").Source, undefined> ? S_2 extends readonly any[] ? number : S_2 extends import("./source/index.js").SourceObject ? keyof S_2 : S_2 extends Record<string, any> ? string : never : never>;
54
+ record: <S_3 extends import("./schema/index.js").Schema<import("./selector/index.js").SelectorSource>>(schema: S_3) => import("./schema/index.js").Schema<Record<string, import("./schema/index.js").SchemaTypeOf<S_3>>>;
49
55
  };
@@ -1,7 +1,7 @@
1
- import { Schema, SerializedSchema, SourceObject } from "../index.js";
1
+ import { Schema, SerializedSchema } from "./index.js";
2
2
  import { ValModuleBrand } from "../module.js";
3
3
  import { GenericSelector } from "../selector/index.js";
4
- import { SourceArray } from "../source/index.js";
4
+ import { SourceArray, SourceObject } from "../source/index.js";
5
5
  import { SourcePath } from "../val/index.js";
6
6
  import { ValidationErrors } from "./validation/ValidationError.js";
7
7
  export type SerializedKeyOfSchema = {
@@ -1,4 +1,4 @@
1
- import { f as _objectSpread2, j as _slicedToArray, c as _createClass, b as _classCallCheck, q as Sym, C as Call, r as StringLiteral, s as StringTemplate, e as _typeof, l as isSerializedVal, F as FILE_REF_PROP, V as VAL_EXTENSION, m as convertFileSource, d as _defineProperty, S as Schema, E as Expr, t as _toConsumableArray, N as NilSym } from './index-369caccf.esm.js';
1
+ import { h as _objectSpread2, f as _slicedToArray, c as _createClass, b as _classCallCheck, r as Sym, C as Call, s as StringLiteral, t as StringTemplate, e as _typeof, m as isSerializedVal, F as FILE_REF_PROP, V as VAL_EXTENSION, n as convertFileSource, d as _defineProperty, S as Schema, E as Expr, u as _toConsumableArray, N as NilSym } from './index-a9235737.esm.js';
2
2
  import { i as isErr, e as err, o as ok, a as isOk } from './result-b96df128.esm.js';
3
3
 
4
4
  var WHITE_SPACE = ["\n", "\r", "\t", " "];
@@ -547,4 +547,4 @@ function getValPath(valOrSelector) {
547
547
  return valOrSelector[Path];
548
548
  }
549
549
 
550
- export { Call as C, Expr as E, FILE_REF_PROP as F, GetSource as G, ImageSchema as I, NilSym as N, Path as P, Schema as S, VAL_EXTENSION as V, _inherits as _, _createSuper as a, _classCallCheck as b, _createClass as c, _defineProperty as d, _typeof as e, _objectSpread2 as f, getValPath as g, file as h, image as i, _slicedToArray as j, isFile as k, isSerializedVal as l, convertFileSource as m, getSchema as n, isVal as o, GenericSelector as p, Sym as q, StringLiteral as r, StringTemplate as s, _toConsumableArray as t, GetSchema as u };
550
+ export { Call as C, Expr as E, FILE_REF_PROP as F, GetSchema as G, ImageSchema as I, NilSym as N, Path as P, Schema as S, VAL_EXTENSION as V, _inherits as _, _createSuper as a, _classCallCheck as b, _createClass as c, _defineProperty as d, _typeof as e, _slicedToArray as f, getValPath as g, _objectSpread2 as h, image as i, file as j, isFile as k, GetSource as l, isSerializedVal as m, convertFileSource as n, getSchema as o, isVal as p, GenericSelector as q, Sym as r, StringLiteral as s, StringTemplate as t, _toConsumableArray as u };
@@ -1,4 +1,4 @@
1
- import { e as _typeof, l as isSerializedVal, F as FILE_REF_PROP, V as VAL_EXTENSION, m as convertFileSource, d as _defineProperty, G as GetSource, P as Path, u as GetSchema, S as Schema, E as Expr, _ as _inherits, a as _createSuper, b as _classCallCheck, c as _createClass, j as _slicedToArray, f as _objectSpread2, t as _toConsumableArray, I as ImageSchema } from './index-369caccf.esm.js';
1
+ import { e as _typeof, m as isSerializedVal, F as FILE_REF_PROP, V as VAL_EXTENSION, n as convertFileSource, d as _defineProperty, l as GetSource, P as Path, G as GetSchema, S as Schema, E as Expr, _ as _inherits, a as _createSuper, b as _classCallCheck, c as _createClass, f as _slicedToArray, h as _objectSpread2, u as _toConsumableArray, I as ImageSchema } from './index-a9235737.esm.js';
2
2
  import { _ as _createForOfIteratorHelper } from './result-b96df128.esm.js';
3
3
 
4
4
  function hasOwn(obj, prop) {
@@ -668,4 +668,4 @@ var PatchError = /*#__PURE__*/_createClass(function PatchError(message) {
668
668
  * NOTE: MAY mutate the input document.
669
669
  */
670
670
 
671
- export { PatchError as P, array as a, createValPathOfItem as b, content as c, resolvePath as d, getSource as g, isSelector as i, newSelectorProxy as n, object as o, richtext as r, splitModuleIdAndModulePath as s, union as u };
671
+ export { PatchError as P, array as a, content as b, createValPathOfItem as c, resolvePath as d, getSource as g, isSelector as i, newSelectorProxy as n, object as o, richtext as r, splitModuleIdAndModulePath as s, union as u };
@@ -231,6 +231,204 @@ var literal = function literal(value) {
231
231
  return new LiteralSchema(value);
232
232
  };
233
233
 
234
+ var KeyOfSchema = /*#__PURE__*/function (_Schema) {
235
+ index._inherits(KeyOfSchema, _Schema);
236
+ var _super = index._createSuper(KeyOfSchema);
237
+ function KeyOfSchema(selector) {
238
+ var _this;
239
+ var opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
240
+ index._classCallCheck(this, KeyOfSchema);
241
+ _this = _super.call(this);
242
+ _this.selector = selector;
243
+ _this.opt = opt;
244
+ return _this;
245
+ }
246
+ index._createClass(KeyOfSchema, [{
247
+ key: "validate",
248
+ value: function validate(path, src) {
249
+ if (this.opt && (src === null || src === undefined)) {
250
+ return false;
251
+ }
252
+ var schema = this.selector[index.GetSchema];
253
+ if (!schema) {
254
+ return index._defineProperty({}, path, [{
255
+ message: "Schema not found for module. keyOf must be used with a Val Module"
256
+ }]);
257
+ }
258
+ var serializedSchema = schema.serialize();
259
+ if (!(serializedSchema.type === "array" || serializedSchema.type === "object" || serializedSchema.type === "record")) {
260
+ return index._defineProperty({}, path, [{
261
+ message: "Schema in keyOf must be an 'array', 'object' or 'record'. Found '".concat(serializedSchema.type, "'")
262
+ }]);
263
+ }
264
+ if (serializedSchema.opt && (src === null || src === undefined)) {
265
+ return false;
266
+ }
267
+ if (serializedSchema.type === "array" && typeof src !== "number") {
268
+ return index._defineProperty({}, path, [{
269
+ message: "Type of value in keyof (array) must be 'number'"
270
+ }]);
271
+ }
272
+ if (serializedSchema.type === "record" && typeof src !== "string") {
273
+ return index._defineProperty({}, path, [{
274
+ message: "Type of value in keyof (record) must be 'string'"
275
+ }]);
276
+ }
277
+ if (serializedSchema.type === "object") {
278
+ var keys = Object.keys(serializedSchema.items);
279
+ if (!keys.includes(src)) {
280
+ return index._defineProperty({}, path, [{
281
+ message: "Value of keyOf (object) must be: ".concat(keys.join(", "), ". Found: ").concat(src)
282
+ }]);
283
+ }
284
+ }
285
+ return false;
286
+ }
287
+ }, {
288
+ key: "assert",
289
+ value: function assert(src) {
290
+ if (this.opt && (src === null || src === undefined)) {
291
+ return true;
292
+ }
293
+ var schema = this.selector[index.GetSchema];
294
+ if (!schema) {
295
+ return false;
296
+ }
297
+ var serializedSchema = schema.serialize();
298
+ if (!(serializedSchema.type === "array" || serializedSchema.type === "object" || serializedSchema.type === "record")) {
299
+ return false;
300
+ }
301
+ if (serializedSchema.opt && (src === null || src === undefined)) {
302
+ return true;
303
+ }
304
+ if (serializedSchema.type === "array" && typeof src !== "number") {
305
+ return false;
306
+ }
307
+ if (serializedSchema.type === "record" && typeof src !== "string") {
308
+ return false;
309
+ }
310
+ if (serializedSchema.type === "object") {
311
+ var keys = Object.keys(serializedSchema.items);
312
+ if (!keys.includes(src)) {
313
+ return false;
314
+ }
315
+ }
316
+ return true;
317
+ }
318
+ }, {
319
+ key: "optional",
320
+ value: function optional() {
321
+ return new KeyOfSchema(this.selector, true);
322
+ }
323
+ }, {
324
+ key: "serialize",
325
+ value: function serialize() {
326
+ var path = index.getValPath(this.selector);
327
+ if (!path) {
328
+ throw new Error("Cannot serialize oneOf schema with empty selector. keyOf must be used with a Val Module.");
329
+ }
330
+ return {
331
+ type: "keyOf",
332
+ selector: path,
333
+ opt: this.opt
334
+ };
335
+ }
336
+ }]);
337
+ return KeyOfSchema;
338
+ }(index.Schema);
339
+ var keyOf = function keyOf(valModule) {
340
+ return new KeyOfSchema(valModule);
341
+ };
342
+
343
+ var RecordSchema = /*#__PURE__*/function (_Schema) {
344
+ index._inherits(RecordSchema, _Schema);
345
+ var _super = index._createSuper(RecordSchema);
346
+ function RecordSchema(item) {
347
+ var _this;
348
+ var opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
349
+ index._classCallCheck(this, RecordSchema);
350
+ _this = _super.call(this);
351
+ _this.item = item;
352
+ _this.opt = opt;
353
+ return _this;
354
+ }
355
+ index._createClass(RecordSchema, [{
356
+ key: "validate",
357
+ value: function validate(path, src) {
358
+ var _this2 = this;
359
+ var error = false;
360
+ if (this.opt && (src === null || src === undefined)) {
361
+ return false;
362
+ }
363
+ if (index._typeof(src) !== "object") {
364
+ return index._defineProperty({}, path, [{
365
+ message: "Expected 'object', got '".concat(index._typeof(src), "'")
366
+ }]);
367
+ }
368
+ if (Array.isArray(src)) {
369
+ return index._defineProperty({}, path, [{
370
+ message: "Expected 'object', got 'array'"
371
+ }]);
372
+ }
373
+ Object.entries(src).forEach(function (_ref3) {
374
+ var _ref4 = index._slicedToArray(_ref3, 2),
375
+ key = _ref4[0],
376
+ elem = _ref4[1];
377
+ var subPath = ops.createValPathOfItem(path, key);
378
+ if (!subPath) {
379
+ error = _this2.appendValidationError(error, path, "Internal error: could not create path at ".concat(!path && typeof path === "string" ? "<empty string>" : path, " at key ").concat(elem),
380
+ // Should! never happen
381
+ src);
382
+ } else {
383
+ var subError = _this2.item.validate(subPath, elem);
384
+ if (subError && error) {
385
+ error = index._objectSpread2(index._objectSpread2({}, subError), error);
386
+ } else if (subError) {
387
+ error = subError;
388
+ }
389
+ }
390
+ });
391
+ return error;
392
+ }
393
+ }, {
394
+ key: "assert",
395
+ value: function assert(src) {
396
+ if (this.opt && (src === null || src === undefined)) {
397
+ return true;
398
+ }
399
+ if (!src) {
400
+ return false;
401
+ }
402
+ for (var _i = 0, _Object$entries = Object.entries(src); _i < _Object$entries.length; _i++) {
403
+ var _Object$entries$_i = index._slicedToArray(_Object$entries[_i], 2),
404
+ _item = _Object$entries$_i[1];
405
+ if (!this.item.assert(_item)) {
406
+ return false;
407
+ }
408
+ }
409
+ return index._typeof(src) === "object" && !Array.isArray(src);
410
+ }
411
+ }, {
412
+ key: "optional",
413
+ value: function optional() {
414
+ return new RecordSchema(this.item, true);
415
+ }
416
+ }, {
417
+ key: "serialize",
418
+ value: function serialize() {
419
+ return {
420
+ type: "record",
421
+ item: this.item.serialize(),
422
+ opt: this.opt
423
+ };
424
+ }
425
+ }]);
426
+ return RecordSchema;
427
+ }(index.Schema);
428
+ var record = function record(schema) {
429
+ return new RecordSchema(schema);
430
+ };
431
+
234
432
  // import type { F } from "ts-toolbelt";
235
433
  // import { i18n, I18n } from "./schema/future/i18n";
236
434
  // import { oneOf } from "./schema/future/oneOf";
@@ -249,7 +447,9 @@ function initSchema() {
249
447
  // oneOf,
250
448
  richtext: ops.richtext,
251
449
  image: index.image,
252
- literal: literal
450
+ literal: literal,
451
+ keyOf: keyOf,
452
+ record: record
253
453
  // i18n: i18n(locales),
254
454
  };
255
455
  }
@@ -231,6 +231,204 @@ var literal = function literal(value) {
231
231
  return new LiteralSchema(value);
232
232
  };
233
233
 
234
+ var KeyOfSchema = /*#__PURE__*/function (_Schema) {
235
+ index._inherits(KeyOfSchema, _Schema);
236
+ var _super = index._createSuper(KeyOfSchema);
237
+ function KeyOfSchema(selector) {
238
+ var _this;
239
+ var opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
240
+ index._classCallCheck(this, KeyOfSchema);
241
+ _this = _super.call(this);
242
+ _this.selector = selector;
243
+ _this.opt = opt;
244
+ return _this;
245
+ }
246
+ index._createClass(KeyOfSchema, [{
247
+ key: "validate",
248
+ value: function validate(path, src) {
249
+ if (this.opt && (src === null || src === undefined)) {
250
+ return false;
251
+ }
252
+ var schema = this.selector[index.GetSchema];
253
+ if (!schema) {
254
+ return index._defineProperty({}, path, [{
255
+ message: "Schema not found for module. keyOf must be used with a Val Module"
256
+ }]);
257
+ }
258
+ var serializedSchema = schema.serialize();
259
+ if (!(serializedSchema.type === "array" || serializedSchema.type === "object" || serializedSchema.type === "record")) {
260
+ return index._defineProperty({}, path, [{
261
+ message: "Schema in keyOf must be an 'array', 'object' or 'record'. Found '".concat(serializedSchema.type, "'")
262
+ }]);
263
+ }
264
+ if (serializedSchema.opt && (src === null || src === undefined)) {
265
+ return false;
266
+ }
267
+ if (serializedSchema.type === "array" && typeof src !== "number") {
268
+ return index._defineProperty({}, path, [{
269
+ message: "Type of value in keyof (array) must be 'number'"
270
+ }]);
271
+ }
272
+ if (serializedSchema.type === "record" && typeof src !== "string") {
273
+ return index._defineProperty({}, path, [{
274
+ message: "Type of value in keyof (record) must be 'string'"
275
+ }]);
276
+ }
277
+ if (serializedSchema.type === "object") {
278
+ var keys = Object.keys(serializedSchema.items);
279
+ if (!keys.includes(src)) {
280
+ return index._defineProperty({}, path, [{
281
+ message: "Value of keyOf (object) must be: ".concat(keys.join(", "), ". Found: ").concat(src)
282
+ }]);
283
+ }
284
+ }
285
+ return false;
286
+ }
287
+ }, {
288
+ key: "assert",
289
+ value: function assert(src) {
290
+ if (this.opt && (src === null || src === undefined)) {
291
+ return true;
292
+ }
293
+ var schema = this.selector[index.GetSchema];
294
+ if (!schema) {
295
+ return false;
296
+ }
297
+ var serializedSchema = schema.serialize();
298
+ if (!(serializedSchema.type === "array" || serializedSchema.type === "object" || serializedSchema.type === "record")) {
299
+ return false;
300
+ }
301
+ if (serializedSchema.opt && (src === null || src === undefined)) {
302
+ return true;
303
+ }
304
+ if (serializedSchema.type === "array" && typeof src !== "number") {
305
+ return false;
306
+ }
307
+ if (serializedSchema.type === "record" && typeof src !== "string") {
308
+ return false;
309
+ }
310
+ if (serializedSchema.type === "object") {
311
+ var keys = Object.keys(serializedSchema.items);
312
+ if (!keys.includes(src)) {
313
+ return false;
314
+ }
315
+ }
316
+ return true;
317
+ }
318
+ }, {
319
+ key: "optional",
320
+ value: function optional() {
321
+ return new KeyOfSchema(this.selector, true);
322
+ }
323
+ }, {
324
+ key: "serialize",
325
+ value: function serialize() {
326
+ var path = index.getValPath(this.selector);
327
+ if (!path) {
328
+ throw new Error("Cannot serialize oneOf schema with empty selector. keyOf must be used with a Val Module.");
329
+ }
330
+ return {
331
+ type: "keyOf",
332
+ selector: path,
333
+ opt: this.opt
334
+ };
335
+ }
336
+ }]);
337
+ return KeyOfSchema;
338
+ }(index.Schema);
339
+ var keyOf = function keyOf(valModule) {
340
+ return new KeyOfSchema(valModule);
341
+ };
342
+
343
+ var RecordSchema = /*#__PURE__*/function (_Schema) {
344
+ index._inherits(RecordSchema, _Schema);
345
+ var _super = index._createSuper(RecordSchema);
346
+ function RecordSchema(item) {
347
+ var _this;
348
+ var opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
349
+ index._classCallCheck(this, RecordSchema);
350
+ _this = _super.call(this);
351
+ _this.item = item;
352
+ _this.opt = opt;
353
+ return _this;
354
+ }
355
+ index._createClass(RecordSchema, [{
356
+ key: "validate",
357
+ value: function validate(path, src) {
358
+ var _this2 = this;
359
+ var error = false;
360
+ if (this.opt && (src === null || src === undefined)) {
361
+ return false;
362
+ }
363
+ if (index._typeof(src) !== "object") {
364
+ return index._defineProperty({}, path, [{
365
+ message: "Expected 'object', got '".concat(index._typeof(src), "'")
366
+ }]);
367
+ }
368
+ if (Array.isArray(src)) {
369
+ return index._defineProperty({}, path, [{
370
+ message: "Expected 'object', got 'array'"
371
+ }]);
372
+ }
373
+ Object.entries(src).forEach(function (_ref3) {
374
+ var _ref4 = index._slicedToArray(_ref3, 2),
375
+ key = _ref4[0],
376
+ elem = _ref4[1];
377
+ var subPath = ops.createValPathOfItem(path, key);
378
+ if (!subPath) {
379
+ error = _this2.appendValidationError(error, path, "Internal error: could not create path at ".concat(!path && typeof path === "string" ? "<empty string>" : path, " at key ").concat(elem),
380
+ // Should! never happen
381
+ src);
382
+ } else {
383
+ var subError = _this2.item.validate(subPath, elem);
384
+ if (subError && error) {
385
+ error = index._objectSpread2(index._objectSpread2({}, subError), error);
386
+ } else if (subError) {
387
+ error = subError;
388
+ }
389
+ }
390
+ });
391
+ return error;
392
+ }
393
+ }, {
394
+ key: "assert",
395
+ value: function assert(src) {
396
+ if (this.opt && (src === null || src === undefined)) {
397
+ return true;
398
+ }
399
+ if (!src) {
400
+ return false;
401
+ }
402
+ for (var _i = 0, _Object$entries = Object.entries(src); _i < _Object$entries.length; _i++) {
403
+ var _Object$entries$_i = index._slicedToArray(_Object$entries[_i], 2),
404
+ _item = _Object$entries$_i[1];
405
+ if (!this.item.assert(_item)) {
406
+ return false;
407
+ }
408
+ }
409
+ return index._typeof(src) === "object" && !Array.isArray(src);
410
+ }
411
+ }, {
412
+ key: "optional",
413
+ value: function optional() {
414
+ return new RecordSchema(this.item, true);
415
+ }
416
+ }, {
417
+ key: "serialize",
418
+ value: function serialize() {
419
+ return {
420
+ type: "record",
421
+ item: this.item.serialize(),
422
+ opt: this.opt
423
+ };
424
+ }
425
+ }]);
426
+ return RecordSchema;
427
+ }(index.Schema);
428
+ var record = function record(schema) {
429
+ return new RecordSchema(schema);
430
+ };
431
+
234
432
  // import type { F } from "ts-toolbelt";
235
433
  // import { i18n, I18n } from "./schema/future/i18n";
236
434
  // import { oneOf } from "./schema/future/oneOf";
@@ -249,7 +447,9 @@ function initSchema() {
249
447
  // oneOf,
250
448
  richtext: ops.richtext,
251
449
  image: index.image,
252
- literal: literal
450
+ literal: literal,
451
+ keyOf: keyOf,
452
+ record: record
253
453
  // i18n: i18n(locales),
254
454
  };
255
455
  }
@@ -1,7 +1,7 @@
1
- import { _ as _inherits, a as _createSuper, b as _classCallCheck, c as _createClass, d as _defineProperty, e as _typeof, S as Schema, i as image, V as VAL_EXTENSION, f as _objectSpread2, g as getValPath, h as file, j as _slicedToArray, k as isFile, F as FILE_REF_PROP, P as Path, G as GetSource, l as isSerializedVal, m as convertFileSource, n as getSchema, o as isVal } from './index-369caccf.esm.js';
2
- export { F as FILE_REF_PROP, p as GenericSelector, S as Schema, V as VAL_EXTENSION } from './index-369caccf.esm.js';
3
- import { a as array, o as object, u as union, r as richtext$1, c as content, P as PatchError, n as newSelectorProxy, b as createValPathOfItem, i as isSelector, g as getSource, d as resolvePath, s as splitModuleIdAndModulePath } from './ops-23a5abb2.esm.js';
4
- export { i as expr } from './index-3e3e839e.esm.js';
1
+ 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, f as _slicedToArray, h as _objectSpread2, i as image, V as VAL_EXTENSION, j as file, k as isFile, F as FILE_REF_PROP, P as Path, l as GetSource, m as isSerializedVal, n as convertFileSource, o as getSchema, p as isVal } from './index-a9235737.esm.js';
2
+ export { F as FILE_REF_PROP, q as GenericSelector, S as Schema, V as VAL_EXTENSION } from './index-a9235737.esm.js';
3
+ import { c as createValPathOfItem, a as array, o as object, u as union, r as richtext$1, b as content, P as PatchError, n as newSelectorProxy, i as isSelector, g as getSource, d as resolvePath, s as splitModuleIdAndModulePath } from './ops-a2a295f8.esm.js';
4
+ export { i as expr } from './index-4abf3a1f.esm.js';
5
5
  import { _ as _createForOfIteratorHelper, i as isErr, a as isOk, e as err, o as ok, r as result } from './result-b96df128.esm.js';
6
6
 
7
7
  var NumberSchema = /*#__PURE__*/function (_Schema) {
@@ -228,6 +228,204 @@ var literal = function literal(value) {
228
228
  return new LiteralSchema(value);
229
229
  };
230
230
 
231
+ var KeyOfSchema = /*#__PURE__*/function (_Schema) {
232
+ _inherits(KeyOfSchema, _Schema);
233
+ var _super = _createSuper(KeyOfSchema);
234
+ function KeyOfSchema(selector) {
235
+ var _this;
236
+ var opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
237
+ _classCallCheck(this, KeyOfSchema);
238
+ _this = _super.call(this);
239
+ _this.selector = selector;
240
+ _this.opt = opt;
241
+ return _this;
242
+ }
243
+ _createClass(KeyOfSchema, [{
244
+ key: "validate",
245
+ value: function validate(path, src) {
246
+ if (this.opt && (src === null || src === undefined)) {
247
+ return false;
248
+ }
249
+ var schema = this.selector[GetSchema];
250
+ if (!schema) {
251
+ return _defineProperty({}, path, [{
252
+ message: "Schema not found for module. keyOf must be used with a Val Module"
253
+ }]);
254
+ }
255
+ var serializedSchema = schema.serialize();
256
+ if (!(serializedSchema.type === "array" || serializedSchema.type === "object" || serializedSchema.type === "record")) {
257
+ return _defineProperty({}, path, [{
258
+ message: "Schema in keyOf must be an 'array', 'object' or 'record'. Found '".concat(serializedSchema.type, "'")
259
+ }]);
260
+ }
261
+ if (serializedSchema.opt && (src === null || src === undefined)) {
262
+ return false;
263
+ }
264
+ if (serializedSchema.type === "array" && typeof src !== "number") {
265
+ return _defineProperty({}, path, [{
266
+ message: "Type of value in keyof (array) must be 'number'"
267
+ }]);
268
+ }
269
+ if (serializedSchema.type === "record" && typeof src !== "string") {
270
+ return _defineProperty({}, path, [{
271
+ message: "Type of value in keyof (record) must be 'string'"
272
+ }]);
273
+ }
274
+ if (serializedSchema.type === "object") {
275
+ var keys = Object.keys(serializedSchema.items);
276
+ if (!keys.includes(src)) {
277
+ return _defineProperty({}, path, [{
278
+ message: "Value of keyOf (object) must be: ".concat(keys.join(", "), ". Found: ").concat(src)
279
+ }]);
280
+ }
281
+ }
282
+ return false;
283
+ }
284
+ }, {
285
+ key: "assert",
286
+ value: function assert(src) {
287
+ if (this.opt && (src === null || src === undefined)) {
288
+ return true;
289
+ }
290
+ var schema = this.selector[GetSchema];
291
+ if (!schema) {
292
+ return false;
293
+ }
294
+ var serializedSchema = schema.serialize();
295
+ if (!(serializedSchema.type === "array" || serializedSchema.type === "object" || serializedSchema.type === "record")) {
296
+ return false;
297
+ }
298
+ if (serializedSchema.opt && (src === null || src === undefined)) {
299
+ return true;
300
+ }
301
+ if (serializedSchema.type === "array" && typeof src !== "number") {
302
+ return false;
303
+ }
304
+ if (serializedSchema.type === "record" && typeof src !== "string") {
305
+ return false;
306
+ }
307
+ if (serializedSchema.type === "object") {
308
+ var keys = Object.keys(serializedSchema.items);
309
+ if (!keys.includes(src)) {
310
+ return false;
311
+ }
312
+ }
313
+ return true;
314
+ }
315
+ }, {
316
+ key: "optional",
317
+ value: function optional() {
318
+ return new KeyOfSchema(this.selector, true);
319
+ }
320
+ }, {
321
+ key: "serialize",
322
+ value: function serialize() {
323
+ var path = getValPath(this.selector);
324
+ if (!path) {
325
+ throw new Error("Cannot serialize oneOf schema with empty selector. keyOf must be used with a Val Module.");
326
+ }
327
+ return {
328
+ type: "keyOf",
329
+ selector: path,
330
+ opt: this.opt
331
+ };
332
+ }
333
+ }]);
334
+ return KeyOfSchema;
335
+ }(Schema);
336
+ var keyOf = function keyOf(valModule) {
337
+ return new KeyOfSchema(valModule);
338
+ };
339
+
340
+ var RecordSchema = /*#__PURE__*/function (_Schema) {
341
+ _inherits(RecordSchema, _Schema);
342
+ var _super = _createSuper(RecordSchema);
343
+ function RecordSchema(item) {
344
+ var _this;
345
+ var opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
346
+ _classCallCheck(this, RecordSchema);
347
+ _this = _super.call(this);
348
+ _this.item = item;
349
+ _this.opt = opt;
350
+ return _this;
351
+ }
352
+ _createClass(RecordSchema, [{
353
+ key: "validate",
354
+ value: function validate(path, src) {
355
+ var _this2 = this;
356
+ var error = false;
357
+ if (this.opt && (src === null || src === undefined)) {
358
+ return false;
359
+ }
360
+ if (_typeof(src) !== "object") {
361
+ return _defineProperty({}, path, [{
362
+ message: "Expected 'object', got '".concat(_typeof(src), "'")
363
+ }]);
364
+ }
365
+ if (Array.isArray(src)) {
366
+ return _defineProperty({}, path, [{
367
+ message: "Expected 'object', got 'array'"
368
+ }]);
369
+ }
370
+ Object.entries(src).forEach(function (_ref3) {
371
+ var _ref4 = _slicedToArray(_ref3, 2),
372
+ key = _ref4[0],
373
+ elem = _ref4[1];
374
+ var subPath = createValPathOfItem(path, key);
375
+ if (!subPath) {
376
+ error = _this2.appendValidationError(error, path, "Internal error: could not create path at ".concat(!path && typeof path === "string" ? "<empty string>" : path, " at key ").concat(elem),
377
+ // Should! never happen
378
+ src);
379
+ } else {
380
+ var subError = _this2.item.validate(subPath, elem);
381
+ if (subError && error) {
382
+ error = _objectSpread2(_objectSpread2({}, subError), error);
383
+ } else if (subError) {
384
+ error = subError;
385
+ }
386
+ }
387
+ });
388
+ return error;
389
+ }
390
+ }, {
391
+ key: "assert",
392
+ value: function assert(src) {
393
+ if (this.opt && (src === null || src === undefined)) {
394
+ return true;
395
+ }
396
+ if (!src) {
397
+ return false;
398
+ }
399
+ for (var _i = 0, _Object$entries = Object.entries(src); _i < _Object$entries.length; _i++) {
400
+ var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
401
+ _item = _Object$entries$_i[1];
402
+ if (!this.item.assert(_item)) {
403
+ return false;
404
+ }
405
+ }
406
+ return _typeof(src) === "object" && !Array.isArray(src);
407
+ }
408
+ }, {
409
+ key: "optional",
410
+ value: function optional() {
411
+ return new RecordSchema(this.item, true);
412
+ }
413
+ }, {
414
+ key: "serialize",
415
+ value: function serialize() {
416
+ return {
417
+ type: "record",
418
+ item: this.item.serialize(),
419
+ opt: this.opt
420
+ };
421
+ }
422
+ }]);
423
+ return RecordSchema;
424
+ }(Schema);
425
+ var record = function record(schema) {
426
+ return new RecordSchema(schema);
427
+ };
428
+
231
429
  // import type { F } from "ts-toolbelt";
232
430
  // import { i18n, I18n } from "./schema/future/i18n";
233
431
  // import { oneOf } from "./schema/future/oneOf";
@@ -246,7 +444,9 @@ function initSchema() {
246
444
  // oneOf,
247
445
  richtext: richtext$1,
248
446
  image: image,
249
- literal: literal
447
+ literal: literal,
448
+ keyOf: keyOf,
449
+ record: record
250
450
  // i18n: i18n(locales),
251
451
  };
252
452
  }
@@ -1,3 +1,3 @@
1
- export { e as evaluate, p as parse } from '../../dist/index-3e3e839e.esm.js';
2
- export { C as Call, E as Expr, N as NilSym, r as StringLiteral, s as StringTemplate, q as Sym } from '../../dist/index-369caccf.esm.js';
1
+ export { e as evaluate, p as parse } from '../../dist/index-4abf3a1f.esm.js';
2
+ export { C as Call, E as Expr, N as NilSym, s as StringLiteral, t as StringTemplate, r as Sym } from '../../dist/index-a9235737.esm.js';
3
3
  import '../../dist/result-b96df128.esm.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@valbuild/core",
3
- "version": "0.17.0",
3
+ "version": "0.18.0",
4
4
  "private": false,
5
5
  "description": "Val - supercharged hard-coded content",
6
6
  "scripts": {
@@ -1,8 +1,8 @@
1
- import { e as _typeof, j as _slicedToArray, c as _createClass, b as _classCallCheck, t as _toConsumableArray } from '../../dist/index-369caccf.esm.js';
1
+ import { e as _typeof, f as _slicedToArray, c as _createClass, b as _classCallCheck, u as _toConsumableArray } from '../../dist/index-a9235737.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-23a5abb2.esm.js';
5
- export { P as PatchError } from '../../dist/ops-23a5abb2.esm.js';
4
+ import { P as PatchError, s as splitModuleIdAndModulePath } from '../../dist/ops-a2a295f8.esm.js';
5
+ export { P as PatchError } from '../../dist/ops-a2a295f8.esm.js';
6
6
 
7
7
  function isNotRoot(path) {
8
8
  return isNonEmpty(path);
package/src/initSchema.ts CHANGED
@@ -8,6 +8,8 @@ import { union } from "./schema/union";
8
8
  import { richtext } from "./schema/richtext";
9
9
  import { image } from "./schema/image";
10
10
  import { literal } from "./schema/literal";
11
+ import { keyOf } from "./schema/keyOf";
12
+ import { record } from "./schema/record";
11
13
  // import { i18n, I18n } from "./schema/future/i18n";
12
14
  // import { oneOf } from "./schema/future/oneOf";
13
15
 
@@ -22,6 +24,8 @@ export type InitSchema = {
22
24
  readonly richtext: typeof richtext;
23
25
  readonly image: typeof image;
24
26
  readonly literal: typeof literal;
27
+ readonly keyOf: typeof keyOf;
28
+ readonly record: typeof record;
25
29
  };
26
30
  // export type InitSchemaLocalized<Locales extends readonly string[]> = {
27
31
  // readonly i18n: I18n<Locales>;
@@ -39,6 +43,8 @@ export function initSchema() {
39
43
  richtext,
40
44
  image,
41
45
  literal,
46
+ keyOf,
47
+ record,
42
48
  // i18n: i18n(locales),
43
49
  };
44
50
  }
@@ -1,8 +1,8 @@
1
1
  /* eslint-disable @typescript-eslint/no-unused-vars */
2
- import { Schema, SerializedSchema, SourceObject } from "..";
2
+ import { Schema, SerializedSchema } from "../schema";
3
3
  import { ValModuleBrand } from "../module";
4
4
  import { GenericSelector, GetSchema } from "../selector";
5
- import { SourceArray } from "../source";
5
+ import { SourceArray, SourceObject } from "../source";
6
6
  import { SourcePath, getValPath } from "../val";
7
7
  import { ValidationErrors } from "./validation/ValidationError";
8
8