@atscript/typescript 0.2.2 → 0.2.4

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/utils.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
- const require_unique_name = require('./unique-name-xWd9uO-k.cjs');
2
+ const require_user_text = require('./user-text-BddKk8EU.cjs');
3
3
 
4
4
  //#region packages/typescript/src/runtime/coerce.ts
5
5
  /**
@@ -207,48 +207,6 @@ function isPlainObject$1(value) {
207
207
  //#endregion
208
208
  //#region packages/typescript/src/runtime/json-schema.ts
209
209
  /**
210
- * Detects a discriminator property across union items.
211
- *
212
- * Scans all items for object-typed members that share a common property
213
- * with distinct const/literal values. If exactly one such property exists,
214
- * it is returned as the discriminator. Returns `null` when no qualifying
215
- * property exists, or when more than one does (ambiguous).
216
- */ function detectDiscriminator(items) {
217
- if (items.length < 2) return null;
218
- for (const item of items) if (item.type.kind !== "object") return null;
219
- const firstObj = items[0].type;
220
- const candidates = [];
221
- for (const [propName, propType] of firstObj.props.entries()) if (propType.type.kind === "" && propType.type.value !== void 0) candidates.push(propName);
222
- let result = null;
223
- for (const candidate of candidates) {
224
- const values = /* @__PURE__ */ new Set();
225
- const indexMapping = {};
226
- let valid = true;
227
- for (let i = 0; i < items.length; i++) {
228
- const prop = items[i].type.props.get(candidate);
229
- if (!prop || prop.type.kind !== "" || prop.type.value === void 0) {
230
- valid = false;
231
- break;
232
- }
233
- const val = prop.type.value;
234
- if (values.has(val)) {
235
- valid = false;
236
- break;
237
- }
238
- values.add(val);
239
- indexMapping[String(val)] = i;
240
- }
241
- if (valid) {
242
- if (result) return null;
243
- result = {
244
- propertyName: candidate,
245
- indexMapping
246
- };
247
- }
248
- }
249
- return result;
250
- }
251
- /**
252
210
  * Builds a JSON Schema from an {@link TAtscriptAnnotatedType}.
253
211
  *
254
212
  * Translates the atscript type structure and validation metadata
@@ -340,7 +298,7 @@ function isPlainObject$1(value) {
340
298
  const properties = {};
341
299
  const required = [];
342
300
  for (const [key, val] of d.type.props.entries()) {
343
- if (require_unique_name.isPhantomType(val)) continue;
301
+ if (require_user_text.isPhantomType(val)) continue;
344
302
  if (val.optional) {
345
303
  const schema = yield build(val);
346
304
  properties[key] = val.metadata.get("meta.required") ? schema : nullable(schema);
@@ -376,7 +334,7 @@ function isPlainObject$1(value) {
376
334
  defs.get(typeDef).schema = yield buildObject(def);
377
335
  building = outer;
378
336
  }
379
- return ref(typeDef);
337
+ return withKeyCounts(ref(typeDef), def.metadata);
380
338
  }
381
339
  if (defs.has(typeDef)) return yield refTo(typeDef, def, shared.get(typeDef)?.first ?? ownOf(def));
382
340
  const repeated = atRoot ? void 0 : shared.get(typeDef);
@@ -424,7 +382,11 @@ function isPlainObject$1(value) {
424
382
  return out;
425
383
  };
426
384
  const kind = def.type.kind;
427
- if (kind === "object") return yield buildObject(def);
385
+ if (kind === "object") {
386
+ const schema = yield buildObject(def);
387
+ applyKeyCounts(schema, meta);
388
+ return schema;
389
+ }
428
390
  if (kind === "array") {
429
391
  const d = def;
430
392
  const schema = {
@@ -445,7 +407,8 @@ function isPlainObject$1(value) {
445
407
  minItems: items.length,
446
408
  maxItems: items.length
447
409
  };
448
- const disc = detectDiscriminator(items);
410
+ const disc = require_user_text.detectDiscriminator(items);
411
+ let schema;
449
412
  if (disc) {
450
413
  const mapping = {};
451
414
  for (const [val, idx] of Object.entries(disc.indexMapping)) {
@@ -457,16 +420,19 @@ function isPlainObject$1(value) {
457
420
  ]);
458
421
  else mapping[val] = `#/oneOf/${idx}`;
459
422
  }
460
- return {
423
+ schema = {
461
424
  oneOf: built,
462
425
  discriminator: {
463
426
  propertyName: disc.propertyName,
464
427
  mapping
465
428
  }
466
429
  };
430
+ } else {
431
+ if (!require_user_text.metaEmpty(meta)) applyUnionConstraints(built, meta, items);
432
+ schema = { anyOf: built };
467
433
  }
468
- if (!require_unique_name.metaEmpty(meta)) applyUnionConstraints(built, meta, items);
469
- return { anyOf: built };
434
+ applyKeyCounts(schema, meta);
435
+ return schema;
470
436
  }
471
437
  return forAnnotatedType(def, {
472
438
  phantom() {
@@ -480,7 +446,7 @@ function isPlainObject$1(value) {
480
446
  const dt = d.type.designType;
481
447
  schema.type = dt === "undefined" ? "null" : dt === "decimal" ? "string" : dt;
482
448
  }
483
- if (d.type.designType === "decimal" && d.type.value === void 0) schema.pattern = require_unique_name.DECIMAL_RE.source;
449
+ if (d.type.designType === "decimal" && d.type.value === void 0) schema.pattern = require_user_text.DECIMAL_RE.source;
484
450
  applyScalarConstraints(schema, meta);
485
451
  return schema;
486
452
  },
@@ -573,7 +539,7 @@ function isPlainObject$1(value) {
573
539
  const taken = /* @__PURE__ */ new Set();
574
540
  const names = /* @__PURE__ */ new Map();
575
541
  for (const [typeDef, { base }] of defs) {
576
- const name = require_unique_name.uniqueName(base, (n) => taken.has(n) || n !== base && asked.has(n), "_");
542
+ const name = require_user_text.uniqueName(base, (n) => taken.has(n) || n !== base && asked.has(n), "_");
577
543
  taken.add(name);
578
544
  names.set(typeDef, name);
579
545
  }
@@ -590,8 +556,8 @@ function isPlainObject$1(value) {
590
556
  for (const seen = /* @__PURE__ */ new Set(); own !== root && own.ref && !seen.has(own);) {
591
557
  seen.add(own);
592
558
  const ref = own.ref.type();
593
- const target = require_unique_name.isAnnotatedType(ref) ? require_unique_name.propAtChain(ref, own.ref.field ? own.ref.field.split(".") : []) : void 0;
594
- if (!require_unique_name.isAnnotatedType(target) || target.type !== def.type) break;
559
+ const target = require_user_text.isAnnotatedType(ref) ? require_user_text.propAtChain(ref, own.ref.field ? own.ref.field.split(".") : []) : void 0;
560
+ if (!require_user_text.isAnnotatedType(target) || target.type !== def.type) break;
595
561
  own = target;
596
562
  }
597
563
  return own === def && !def.ref ? void 0 : own;
@@ -674,7 +640,7 @@ function withoutUniqueItems(meta) {
674
640
  const exclude = declared.length > 0 ? `^(?!(?:${declared.map((n) => n.replaceAll(/[\\^$.*+?()[\]{}|/]/g, "\\$&")).join("|")})$)[\\s\\S]*?` : "";
675
641
  const out = {};
676
642
  for (const [regex, schema] of patterns) {
677
- const pattern = require_unique_name.jsonSchemaPattern(regex.source, regex.flags);
643
+ const pattern = require_user_text.jsonSchemaPattern(regex.source, regex.flags);
678
644
  const key = exclude ? `${exclude}(?:${pattern})` : pattern;
679
645
  const taken = out[key];
680
646
  out[key] = taken ? { anyOf: [...Object.keys(taken).length === 1 && taken.anyOf ? taken.anyOf : [taken], schema] } : schema;
@@ -690,7 +656,7 @@ function withoutUniqueItems(meta) {
690
656
  return Array.isArray(schema.anyOf) && schema.anyOf.some((s) => acceptsNull(s, refs));
691
657
  }
692
658
  /** `a` and `b` carry the same schema constraints. */ function sameConstraints(a, b) {
693
- return a.metadata === b.metadata || require_unique_name.VALIDATOR_KEYS.every((key) => {
659
+ return a.metadata === b.metadata || require_user_text.VALIDATOR_KEYS.every((key) => {
694
660
  const x = a.metadata.get(key);
695
661
  const y = b.metadata.get(key);
696
662
  return x === y || JSON.stringify(x) === JSON.stringify(y);
@@ -701,21 +667,38 @@ function withoutUniqueItems(meta) {
701
667
  * `@expect.array.uniqueItems` → `uniqueItems` unless the items are objects with
702
668
  * `@expect.array.key` props (unique by those props: JSON Schema compares whole items).
703
669
  */ function applyArrayConstraints(schema, meta, of) {
704
- if (meta.get("expect.array.uniqueItems") && require_unique_name.arrayKeyProps(of).length === 0) schema.uniqueItems = true;
670
+ if (meta.get("expect.array.uniqueItems") && require_user_text.arrayKeyProps(of).length === 0) schema.uniqueItems = true;
705
671
  const minLength = meta.get("expect.minLength");
706
- if (minLength !== void 0) schema.minItems = require_unique_name.boundOf(minLength, "length");
672
+ if (minLength !== void 0) schema.minItems = require_user_text.boundOf(minLength, "length");
707
673
  const maxLength = meta.get("expect.maxLength");
708
- if (maxLength !== void 0) schema.maxItems = require_unique_name.boundOf(maxLength, "length");
674
+ if (maxLength !== void 0) schema.maxItems = require_user_text.boundOf(maxLength, "length");
675
+ }
676
+ /**
677
+ * `@expect.minKeys` / `maxKeys` → `minProperties` / `maxProperties` (they constrain objects
678
+ * only, so they also sit on a union's schema as they are).
679
+ */ function applyKeyCounts(schema, meta) {
680
+ const minKeys = meta.get("expect.minKeys");
681
+ if (minKeys !== void 0) schema.minProperties = require_user_text.boundOf(minKeys, "count");
682
+ const maxKeys = meta.get("expect.maxKeys");
683
+ if (maxKeys !== void 0) schema.maxProperties = require_user_text.boundOf(maxKeys, "count");
684
+ }
685
+ /** `schema` (a `$ref`) with the key counts of `meta`, if any (beside it, in an `allOf`). */ function withKeyCounts(schema, meta) {
686
+ const counts = {};
687
+ applyKeyCounts(counts, meta);
688
+ return Object.keys(counts).length > 0 ? {
689
+ allOf: [schema],
690
+ ...counts
691
+ } : schema;
709
692
  }
710
693
  /** String / number constraints on a scalar schema whose `type` is already set. */ function applyScalarConstraints(schema, meta) {
711
694
  if (schema.type === "number" && meta.get("expect.int")) schema.type = "integer";
712
695
  if (schema.type === "string") {
713
696
  if (meta.get("meta.required")) schema.minLength = 1;
714
697
  const minLength = meta.get("expect.minLength");
715
- if (minLength !== void 0) schema.minLength = require_unique_name.boundOf(minLength, "length");
698
+ if (minLength !== void 0) schema.minLength = require_user_text.boundOf(minLength, "length");
716
699
  const maxLength = meta.get("expect.maxLength");
717
- if (maxLength !== void 0) schema.maxLength = require_unique_name.boundOf(maxLength, "length");
718
- const patterns = (meta.get("expect.pattern") ?? []).map((p) => require_unique_name.jsonSchemaPattern(p.pattern, p.flags));
700
+ if (maxLength !== void 0) schema.maxLength = require_user_text.boundOf(maxLength, "length");
701
+ const patterns = (meta.get("expect.pattern") ?? []).map((p) => require_user_text.jsonSchemaPattern(p.pattern, p.flags));
719
702
  if (meta.get("meta.required")) patterns.push("\\S");
720
703
  if (schema.pattern !== void 0) {
721
704
  patterns.unshift(schema.pattern);
@@ -727,13 +710,13 @@ function withoutUniqueItems(meta) {
727
710
  if (schema.type === "boolean" && meta.get("meta.required")) schema.const = true;
728
711
  if (schema.type === "number" || schema.type === "integer") {
729
712
  const min = meta.get("expect.min");
730
- if (min !== void 0) schema.minimum = require_unique_name.boundOf(min, "minValue");
713
+ if (min !== void 0) schema.minimum = require_user_text.boundOf(min, "minValue");
731
714
  const max = meta.get("expect.max");
732
- if (max !== void 0) schema.maximum = require_unique_name.boundOf(max, "maxValue");
715
+ if (max !== void 0) schema.maximum = require_user_text.boundOf(max, "maxValue");
733
716
  const exMin = meta.get("expect.exclusiveMin");
734
- if (exMin !== void 0) schema.exclusiveMinimum = require_unique_name.boundOf(exMin, "exclusiveMinValue");
717
+ if (exMin !== void 0) schema.exclusiveMinimum = require_user_text.boundOf(exMin, "exclusiveMinValue");
735
718
  const exMax = meta.get("expect.exclusiveMax");
736
- if (exMax !== void 0) schema.exclusiveMaximum = require_unique_name.boundOf(exMax, "exclusiveMaxValue");
719
+ if (exMax !== void 0) schema.exclusiveMaximum = require_user_text.boundOf(exMax, "exclusiveMaxValue");
737
720
  }
738
721
  }
739
722
  const LOWER_BOUNDS = new Set([
@@ -803,12 +786,12 @@ const UPPER_BOUNDS = new Set([
803
786
  const defsSource = schema.$defs || schema.definitions || {};
804
787
  const resolved = /* @__PURE__ */ new Map();
805
788
  const convert = (s) => {
806
- if (!s || Object.keys(s).length === 0) return require_unique_name.defineAnnotatedType().designType("any").$type;
789
+ if (!s || Object.keys(s).length === 0) return require_user_text.defineAnnotatedType().designType("any").$type;
807
790
  if (s.$ref) {
808
791
  const refName = s.$ref.replace(/^#\/(\$defs|definitions)\//, "");
809
792
  if (resolved.has(refName)) return resolved.get(refName);
810
793
  if (defsSource[refName]) {
811
- const placeholder = require_unique_name.defineAnnotatedType().designType("any").$type;
794
+ const placeholder = require_user_text.defineAnnotatedType().designType("any").$type;
812
795
  resolved.set(refName, placeholder);
813
796
  const type = convert(defsSource[refName]);
814
797
  resolved.set(refName, type);
@@ -819,33 +802,33 @@ const UPPER_BOUNDS = new Set([
819
802
  if ("const" in s) {
820
803
  const val = s.const;
821
804
  const dt = val === null ? "null" : typeof val;
822
- return require_unique_name.defineAnnotatedType().designType(dt).value(val).$type;
805
+ return require_user_text.defineAnnotatedType().designType(dt).value(val).$type;
823
806
  }
824
807
  if (s.enum) {
825
- const handle = require_unique_name.defineAnnotatedType("union");
808
+ const handle = require_user_text.defineAnnotatedType("union");
826
809
  for (const val of s.enum) {
827
810
  const dt = val === null ? "null" : typeof val;
828
- handle.item(require_unique_name.defineAnnotatedType().designType(dt).value(val).$type);
811
+ handle.item(require_user_text.defineAnnotatedType().designType(dt).value(val).$type);
829
812
  }
830
813
  return handle.$type;
831
814
  }
832
815
  if (s.anyOf) {
833
- const handle = require_unique_name.defineAnnotatedType("union");
816
+ const handle = require_user_text.defineAnnotatedType("union");
834
817
  for (const item of s.anyOf) handle.item(convert(item));
835
818
  return handle.$type;
836
819
  }
837
820
  if (s.oneOf) {
838
- const handle = require_unique_name.defineAnnotatedType("union");
821
+ const handle = require_user_text.defineAnnotatedType("union");
839
822
  for (const item of s.oneOf) handle.item(convert(item));
840
823
  return handle.$type;
841
824
  }
842
825
  if (s.allOf && !s.type) {
843
- const handle = require_unique_name.defineAnnotatedType("intersection");
826
+ const handle = require_user_text.defineAnnotatedType("intersection");
844
827
  for (const item of s.allOf) handle.item(convert(item));
845
828
  return handle.$type;
846
829
  }
847
830
  if (Array.isArray(s.type)) {
848
- const handle = require_unique_name.defineAnnotatedType("union");
831
+ const handle = require_user_text.defineAnnotatedType("union");
849
832
  for (const t of s.type) handle.item(convert({
850
833
  ...s,
851
834
  type: t
@@ -853,7 +836,7 @@ const UPPER_BOUNDS = new Set([
853
836
  return handle.$type;
854
837
  }
855
838
  if (s.type === "object") {
856
- const handle = require_unique_name.defineAnnotatedType("object");
839
+ const handle = require_user_text.defineAnnotatedType("object");
857
840
  const required = new Set(s.required || []);
858
841
  if (s.properties) for (const [key, propSchema] of Object.entries(s.properties)) {
859
842
  const optional = !required.has(key);
@@ -862,22 +845,24 @@ const UPPER_BOUNDS = new Set([
862
845
  handle.prop(key, propType);
863
846
  }
864
847
  for (const [pattern, propSchema] of Object.entries(s.patternProperties ?? {})) handle.propPattern(compilePattern(pattern), convert(propSchema));
848
+ if (typeof s.minProperties === "number") handle.annotate("expect.minKeys", { count: s.minProperties });
849
+ if (typeof s.maxProperties === "number") handle.annotate("expect.maxKeys", { count: s.maxProperties });
865
850
  return handle.$type;
866
851
  }
867
852
  if (s.type === "array") {
868
853
  if (Array.isArray(s.items)) {
869
- const handle = require_unique_name.defineAnnotatedType("tuple");
854
+ const handle = require_user_text.defineAnnotatedType("tuple");
870
855
  for (const item of s.items) handle.item(convert(item));
871
856
  return handle.$type;
872
857
  }
873
- const itemType = s.items ? convert(s.items) : require_unique_name.defineAnnotatedType().designType("any").$type;
874
- const handle = require_unique_name.defineAnnotatedType("array").of(itemType);
858
+ const itemType = s.items ? convert(s.items) : require_user_text.defineAnnotatedType().designType("any").$type;
859
+ const handle = require_user_text.defineAnnotatedType("array").of(itemType);
875
860
  if (typeof s.minItems === "number") handle.annotate("expect.minLength", { length: s.minItems });
876
861
  if (typeof s.maxItems === "number") handle.annotate("expect.maxLength", { length: s.maxItems });
877
862
  return handle.$type;
878
863
  }
879
864
  if (s.type === "string") {
880
- const handle = require_unique_name.defineAnnotatedType().designType("string").tags("string");
865
+ const handle = require_user_text.defineAnnotatedType().designType("string").tags("string");
881
866
  if (typeof s.minLength === "number") handle.annotate("expect.minLength", { length: s.minLength });
882
867
  if (typeof s.maxLength === "number") handle.annotate("expect.maxLength", { length: s.maxLength });
883
868
  if (s.pattern) handle.annotate("expect.pattern", { pattern: s.pattern }, true);
@@ -887,19 +872,19 @@ const UPPER_BOUNDS = new Set([
887
872
  return handle.$type;
888
873
  }
889
874
  if (s.type === "integer") {
890
- const handle = require_unique_name.defineAnnotatedType().designType("number").tags("number");
875
+ const handle = require_user_text.defineAnnotatedType().designType("number").tags("number");
891
876
  handle.annotate("expect.int", true);
892
877
  annotateNumberBounds(handle, s);
893
878
  return handle.$type;
894
879
  }
895
880
  if (s.type === "number") {
896
- const handle = require_unique_name.defineAnnotatedType().designType("number").tags("number");
881
+ const handle = require_user_text.defineAnnotatedType().designType("number").tags("number");
897
882
  annotateNumberBounds(handle, s);
898
883
  return handle.$type;
899
884
  }
900
- if (s.type === "boolean") return require_unique_name.defineAnnotatedType().designType("boolean").tags("boolean").$type;
901
- if (s.type === "null") return require_unique_name.defineAnnotatedType().designType("null").tags("null").$type;
902
- return require_unique_name.defineAnnotatedType().designType("any").$type;
885
+ if (s.type === "boolean") return require_user_text.defineAnnotatedType().designType("boolean").tags("boolean").$type;
886
+ if (s.type === "null") return require_user_text.defineAnnotatedType().designType("null").tags("null").$type;
887
+ return require_user_text.defineAnnotatedType().designType("any").$type;
903
888
  };
904
889
  return convert(schema);
905
890
  }
@@ -961,7 +946,7 @@ const UPPER_BOUNDS = new Set([
961
946
  for (const defName of Object.keys(local)) {
962
947
  const family = defName.replace(/_\d+$/, "");
963
948
  const same = Object.keys(mergedDefs).find((merged) => merged.replace(/_\d+$/, "") === family && sameSchema(local[defName], local, mergedDefs[merged], mergedDefs, /* @__PURE__ */ new Set()));
964
- renamed.set(defName, same ?? (defName in mergedDefs || [...renamed.values()].includes(defName) ? require_unique_name.uniqueName(family, (n) => n in mergedDefs || [...renamed.values()].includes(n), "_") : defName));
949
+ renamed.set(defName, same ?? (defName in mergedDefs || [...renamed.values()].includes(defName) ? require_user_text.uniqueName(family, (n) => n in mergedDefs || [...renamed.values()].includes(n), "_") : defName));
965
950
  }
966
951
  for (const [defName, merged] of renamed) if (!(merged in mergedDefs)) mergedDefs[merged] = renameRefs(local[defName], renamed);
967
952
  schemas[name] = renameRefs(schema, renamed);
@@ -1026,27 +1011,31 @@ const DEF_REF = /^#\/\$defs\/(.+)$/;
1026
1011
  }
1027
1012
  if (mode === "db") {
1028
1013
  const dbValue = prop.metadata.get("db.default");
1029
- if (dbValue !== void 0) {
1030
- const parsed = parseRawValue(dbValue, prop);
1031
- if (parsed !== void 0 && prop.validator({ unknownProps: "ignore" }).validate(parsed, true)) return { value: parsed };
1032
- return;
1033
- }
1014
+ if (dbValue !== void 0) return validRawValue(dbValue, prop);
1034
1015
  if (prop.metadata.has("db.default.increment")) return { value: "increment" };
1035
1016
  if (prop.metadata.has("db.default.uuid")) return { value: "uuid" };
1036
1017
  if (prop.metadata.has("db.default.now")) return { value: "now" };
1037
1018
  return;
1038
1019
  }
1039
1020
  const metaKey = mode === "default" ? "meta.default" : "meta.example";
1040
- const rawStr = prop.metadata.get(metaKey);
1041
- if (rawStr === void 0) return;
1042
- const parsed = parseRawValue(rawStr, prop);
1043
- if (parsed === void 0) return;
1044
- if (prop.validator({ unknownProps: "ignore" }).validate(parsed, true)) return { value: parsed };
1021
+ const metaValue = prop.metadata.get(metaKey);
1022
+ if (metaValue === void 0) return;
1023
+ return validRawValue(metaValue, prop);
1045
1024
  }
1046
1025
  /**
1047
- * Parses a raw annotation string into a JS value.
1048
- * Strings are returned as-is for string types; everything else goes through JSON.parse.
1026
+ * An annotation's raw value as a valid value of `prop` (see {@link parseRawValue}); a string the
1027
+ * JSON reading does not fit is tried as the string itself (a `decimal` field, `decimal | null`).
1028
+ */ function validRawValue(raw, prop) {
1029
+ const validator = prop.validator({ unknownProps: "ignore" });
1030
+ const parsed = parseRawValue(raw, prop);
1031
+ if (parsed !== void 0 && validator.validate(parsed, true)) return { value: parsed };
1032
+ if (typeof raw === "string" && parsed !== raw && validator.validate(raw, true)) return { value: raw };
1033
+ }
1034
+ /**
1035
+ * Parses a raw annotation value into a JS value: a number / boolean literal (a `value`
1036
+ * argument) as it is; a string as-is for string types, else through JSON.parse.
1049
1037
  */ function parseRawValue(raw, prop) {
1038
+ if (typeof raw !== "string") return raw;
1050
1039
  if (prop.type.kind === "" && prop.type.designType === "string") return raw;
1051
1040
  try {
1052
1041
  return JSON.parse(raw);
@@ -1061,7 +1050,8 @@ const DEF_REF = /^#\/\$defs\/(.+)$/;
1061
1050
  case "number": return 0;
1062
1051
  case "boolean": return false;
1063
1052
  case "undefined": return;
1064
- case "null": return null;
1053
+ case "null":
1054
+ case "any": return null;
1065
1055
  default: return;
1066
1056
  }
1067
1057
  }
@@ -1104,7 +1094,7 @@ const DEF_REF = /^#\/\$defs\/(.+)$/;
1104
1094
  */ function* build(def, path, mode, walk) {
1105
1095
  const resolved = resolveValue(def, path, mode);
1106
1096
  if (resolved !== void 0) return resolved.value;
1107
- const below = require_unique_name.enterCut(walk.path, def);
1097
+ const below = require_user_text.enterCut(walk.path, def);
1108
1098
  if (below === false) return STOPPED;
1109
1099
  return yield buildStructure(def, path, mode, {
1110
1100
  ...walk,
@@ -1112,7 +1102,7 @@ const DEF_REF = /^#\/\$defs\/(.+)$/;
1112
1102
  });
1113
1103
  }
1114
1104
  function* buildStructure(def, path, mode, walk) {
1115
- const recursive = (node) => require_unique_name.enterCut(walk.path, node) === false;
1105
+ const recursive = (node) => require_user_text.enterCut(walk.path, node) === false;
1116
1106
  const stops = (value) => value === STOPPED && !walk.lenient;
1117
1107
  const type = def.type;
1118
1108
  switch (type.kind) {
@@ -1120,7 +1110,7 @@ function* buildStructure(def, path, mode, walk) {
1120
1110
  case "object": {
1121
1111
  const data = {};
1122
1112
  for (const [key, prop] of type.props.entries()) {
1123
- if (require_unique_name.isPhantomType(prop)) continue;
1113
+ if (require_user_text.isPhantomType(prop)) continue;
1124
1114
  const childPath = path ? `${path}.${key}` : key;
1125
1115
  if (prop.optional) {
1126
1116
  if (mode === "example") {
@@ -1159,7 +1149,12 @@ function* buildStructure(def, path, mode, walk) {
1159
1149
  const items = [];
1160
1150
  for (const item of type.items) items.push(yield build(item, path, mode, walk));
1161
1151
  if (items.some(stops)) return STOPPED;
1162
- const values = items.filter((item) => item !== STOPPED);
1152
+ const isAny = (i) => {
1153
+ const t = type.items[i].type;
1154
+ return t.kind === "" && t.designType === "any";
1155
+ };
1156
+ const kept = items.filter((item, i) => item !== STOPPED && !isAny(i));
1157
+ const values = kept.length > 0 ? kept : items.filter((item) => item !== STOPPED);
1163
1158
  return values.length > 0 && values.every(isPlainObject) ? Object.assign({}, ...values) : values[0];
1164
1159
  }
1165
1160
  default: throw new Error(`Unknown type kind "${type.kind}"`);
@@ -1193,7 +1188,7 @@ function isPlainObject(value) {
1193
1188
  * (`static toJsonSchema()`).
1194
1189
  */ function lazyJsonSchema(type) {
1195
1190
  const known = lazy.get(type);
1196
- const writes = require_unique_name.metadataWriteCount();
1191
+ const writes = require_user_text.metadataWriteCount();
1197
1192
  if (known?.writes === writes) return known.schema;
1198
1193
  const schema = buildJsonSchema(type);
1199
1194
  lazy.set(type, {
@@ -1209,7 +1204,7 @@ function isPlainObject(value) {
1209
1204
  * (`modules`, the realm's applied block modules), else the runtime's (`buildJsonSchema`). Raw
1210
1205
  * metadata writes are not seen (as in 0.1.110). Called by generated code.
1211
1206
  */ function bundledSchemaOf(type, literal, modules = []) {
1212
- const applied = require_unique_name.appliedBlockModules();
1207
+ const applied = require_user_text.appliedBlockModules();
1213
1208
  const known = bundled.get(type);
1214
1209
  if (known?.size === applied.size) return known.schema;
1215
1210
  const accounted = new Set(modules);
@@ -1253,10 +1248,10 @@ function isPlainObject(value) {
1253
1248
  const union = existing;
1254
1249
  for (const [key, value] of def.metadata) union.metadata.set(key, value);
1255
1250
  union.type.items.push(def);
1256
- require_unique_name.touched(union.type);
1251
+ require_user_text.touched(union.type);
1257
1252
  delete union.optional;
1258
1253
  } else if (existing) {
1259
- const flatUnion = require_unique_name.defineAnnotatedType("union").copyMetadata(existing.metadata).copyMetadata(def.metadata);
1254
+ const flatUnion = require_user_text.defineAnnotatedType("union").copyMetadata(existing.metadata).copyMetadata(def.metadata);
1260
1255
  flatUnion.item(existing).item(def);
1261
1256
  const unionType = flatUnion.$type;
1262
1257
  unionType.__flat_union = true;
@@ -1270,11 +1265,11 @@ function isPlainObject(value) {
1270
1265
  const entries = [...def.type.props];
1271
1266
  for (let i = entries.length - 1; i >= 0; i--) {
1272
1267
  const [key, value] = entries[i];
1273
- if (!(skipPhantom && require_unique_name.isPhantomType(value))) tasks.push(() => flattenType(value, prefix ? `${prefix}.${key}` : key, inComplex, path));
1268
+ if (!(skipPhantom && require_user_text.isPhantomType(value))) tasks.push(() => flattenType(value, prefix ? `${prefix}.${key}` : key, inComplex, path));
1274
1269
  }
1275
1270
  }
1276
1271
  /** The sub-paths of array element / union item `def` at `name` (no entry of its own). */ function flattenArray(def, name, path) {
1277
- const below = require_unique_name.enterCut(path, def);
1272
+ const below = require_user_text.enterCut(path, def);
1278
1273
  if (below === false) return;
1279
1274
  switch (def.type.kind) {
1280
1275
  case "object":
@@ -1299,7 +1294,7 @@ function isPlainObject(value) {
1299
1294
  }
1300
1295
  }
1301
1296
  /** Flattens `def` at `prefix`, below `path`. */ function flattenType(def, prefix, inComplexTypeOrArray, path) {
1302
- const below = require_unique_name.enterCut(path, def);
1297
+ const below = require_user_text.enterCut(path, def);
1303
1298
  if (below === false) {
1304
1299
  addFieldToFlatMap(prefix, def);
1305
1300
  fieldDone(prefix, def);
@@ -1314,13 +1309,13 @@ function isPlainObject(value) {
1314
1309
  case "array": {
1315
1310
  let typeArray = def;
1316
1311
  if (!inComplexTypeOrArray) {
1317
- typeArray = require_unique_name.defineAnnotatedType().type(def).copyMetadata(def.metadata).$type;
1312
+ typeArray = require_user_text.defineAnnotatedType().type(def).copyMetadata(def.metadata).$type;
1318
1313
  if (def.optional) typeArray.optional = def.optional;
1319
1314
  if (options?.topLevelArrayTag) typeArray.metadata.set(options.topLevelArrayTag, true);
1320
1315
  }
1321
1316
  addFieldToFlatMap(prefix, typeArray);
1322
1317
  tasks.push(() => fieldDone(prefix, def));
1323
- if (!require_unique_name.isAnnotatedTypeOfPrimitive(typeArray.type.of)) {
1318
+ if (!require_user_text.isAnnotatedTypeOfPrimitive(typeArray.type.of)) {
1324
1319
  const of = typeArray.type.of;
1325
1320
  tasks.push(() => flattenArray(of, prefix, below));
1326
1321
  }
@@ -1405,7 +1400,7 @@ function _define_property(obj, key, value) {
1405
1400
  */ keyOf(def) {
1406
1401
  let key = this.keys.get(def.type);
1407
1402
  if (key === void 0) {
1408
- key = require_unique_name.uniqueName(def.id ?? ANON_ID, (k) => this.taken.has(k), "@");
1403
+ key = require_user_text.uniqueName(def.id ?? ANON_ID, (k) => this.taken.has(k), "@");
1409
1404
  this.taken.add(key);
1410
1405
  this.keys.set(def.type, key);
1411
1406
  }
@@ -1599,7 +1594,7 @@ function* serializeNode(def, path, options, visited) {
1599
1594
  if (counted.has(type) && !node.id) visited.repeated.add(type);
1600
1595
  counted.add(type);
1601
1596
  if (!first || node.id) return;
1602
- const key = require_unique_name.cutKey(node);
1597
+ const key = require_user_text.cutKey(node);
1603
1598
  const group = groups.get(key);
1604
1599
  if (!group) groups.set(key, [type]);
1605
1600
  else if (!group.includes(type)) group.push(type);
@@ -1670,12 +1665,12 @@ function* serializeTypeDef(def, path, options, visited) {
1670
1665
  * invoked. Classes, `function` expressions, async/generator functions and any other function
1671
1666
  * are never called, so serializing never runs user code.
1672
1667
  */ function resolveAnnotationTarget(value) {
1673
- if (require_unique_name.isAnnotatedType(value)) return value;
1668
+ if (require_user_text.isAnnotatedType(value)) return value;
1674
1669
  if (typeof value === "function" && value.length === 0 && !Object.prototype.hasOwnProperty.call(value, "prototype") && value.constructor === Function) {
1675
1670
  const fn = value;
1676
1671
  try {
1677
1672
  const target = fn();
1678
- return require_unique_name.isAnnotatedType(target) ? target : void 0;
1673
+ return require_user_text.isAnnotatedType(target) ? target : void 0;
1679
1674
  } catch {
1680
1675
  return;
1681
1676
  }
@@ -1781,10 +1776,10 @@ function serializeMetadata(owner, site, options, state) {
1781
1776
  return token;
1782
1777
  }
1783
1778
  function emptyObjectTypeDef() {
1784
- return require_unique_name.createObjectTypeDef();
1779
+ return require_user_text.createObjectTypeDef();
1785
1780
  }
1786
1781
  function toMetadataMap(record) {
1787
- return require_unique_name.createMetadataMap(Object.entries(record));
1782
+ return require_user_text.createMetadataMap(Object.entries(record));
1788
1783
  }
1789
1784
  function* deserializeRef(data, resolved) {
1790
1785
  if (!data.ref) return;
@@ -1796,7 +1791,7 @@ function* deserializeRef(data, resolved) {
1796
1791
  field: data.ref.field
1797
1792
  };
1798
1793
  }
1799
- const sentinel = require_unique_name.createAnnotatedTypeNode(emptyObjectTypeDef(), toMetadataMap(refTargetData.metadata), { id: refTargetData.id || void 0 });
1794
+ const sentinel = require_user_text.createAnnotatedTypeNode(emptyObjectTypeDef(), toMetadataMap(refTargetData.metadata), { id: refTargetData.id || void 0 });
1800
1795
  return {
1801
1796
  type: () => sentinel,
1802
1797
  field: data.ref.field
@@ -1807,8 +1802,8 @@ function* deserializeNode(data, resolved) {
1807
1802
  const { id: refId, own } = data.type;
1808
1803
  const target = resolved.get(refId);
1809
1804
  const id = data.anon ? void 0 : data.origId ?? target?.id ?? refId;
1810
- if (!own) return target || require_unique_name.createAnnotatedTypeNode(emptyObjectTypeDef(), require_unique_name.createMetadataMap(), { id });
1811
- const node = require_unique_name.createAnnotatedTypeNode(target?.type ?? emptyObjectTypeDef(), toMetadataMap(data.metadata), {
1805
+ if (!own) return target || require_user_text.createAnnotatedTypeNode(emptyObjectTypeDef(), require_user_text.createMetadataMap(), { id });
1806
+ const node = require_user_text.createAnnotatedTypeNode(target?.type ?? emptyObjectTypeDef(), toMetadataMap(data.metadata), {
1812
1807
  id,
1813
1808
  optional: data.optional || void 0,
1814
1809
  ref: yield deserializeRef(data, resolved)
@@ -1828,7 +1823,7 @@ function* deserializeNode(data, resolved) {
1828
1823
  if (data.id) {
1829
1824
  const existing = resolved.get(data.id);
1830
1825
  if (existing) return existing;
1831
- result = require_unique_name.createAnnotatedTypeNode(emptyObjectTypeDef(), metadata, {
1826
+ result = require_user_text.createAnnotatedTypeNode(emptyObjectTypeDef(), metadata, {
1832
1827
  optional: data.optional || void 0,
1833
1828
  id: data.anon ? void 0 : data.origId ?? data.id
1834
1829
  });
@@ -1836,13 +1831,13 @@ function* deserializeNode(data, resolved) {
1836
1831
  }
1837
1832
  const type = yield deserializeTypeDef(data.type, resolved);
1838
1833
  const ref = yield deserializeRef(data, resolved);
1839
- if (data.cut !== void 0) require_unique_name.cutAs(type, cutToken(resolved, data.cut));
1834
+ if (data.cut !== void 0) require_user_text.cutAs(type, cutToken(resolved, data.cut));
1840
1835
  if (result) {
1841
1836
  result.type = type;
1842
1837
  if (ref) result.ref = ref;
1843
1838
  return result;
1844
1839
  }
1845
- return require_unique_name.createAnnotatedTypeNode(type, metadata, {
1840
+ return require_user_text.createAnnotatedTypeNode(type, metadata, {
1846
1841
  optional: data.optional || void 0,
1847
1842
  id: data.id || void 0,
1848
1843
  ref
@@ -1861,11 +1856,11 @@ function* deserializeTypeDef(t, resolved) {
1861
1856
  return result;
1862
1857
  }
1863
1858
  case "object": {
1864
- const def = require_unique_name.createObjectTypeDef(tags);
1859
+ const def = require_user_text.createObjectTypeDef(tags);
1865
1860
  for (const [key, val] of Object.entries(t.props)) def.props.set(key, yield deserializeNode(val, resolved));
1866
1861
  const patterns = [];
1867
1862
  for (const pp of t.propsPatterns) patterns.push({
1868
- pattern: require_unique_name.statelessRegExp(new RegExp(pp.pattern.source, pp.pattern.flags)),
1863
+ pattern: require_user_text.statelessRegExp(new RegExp(pp.pattern.source, pp.pattern.flags)),
1869
1864
  def: yield deserializeNode(pp.def, resolved)
1870
1865
  });
1871
1866
  def.propsPatterns = patterns;
@@ -1890,51 +1885,60 @@ function* deserializeTypeDef(t, resolved) {
1890
1885
  case "$ref": {
1891
1886
  const existing = resolved.get(t.id);
1892
1887
  if (existing) return existing.type;
1893
- return require_unique_name.createObjectTypeDef(tags);
1888
+ return require_user_text.createObjectTypeDef(tags);
1894
1889
  }
1895
1890
  default: throw new Error(`Unknown serialized type kind "${t.kind}"`);
1896
1891
  }
1897
1892
  }
1898
1893
 
1899
1894
  //#endregion
1900
- exports.$ak = require_unique_name.$ak;
1901
- exports.$alias = require_unique_name.$alias;
1902
- exports.$blk = require_unique_name.$blk;
1903
- exports.$end = require_unique_name.$end;
1904
- exports.$f = require_unique_name.$f;
1905
- exports.$fc = require_unique_name.$fc;
1906
- exports.$ft = require_unique_name.$ft;
1907
- exports.$nr = require_unique_name.$nr;
1908
- exports.$ps = require_unique_name.$ps;
1909
- exports.$q = require_unique_name.$q;
1910
- exports.$sh = require_unique_name.$sh;
1911
- exports.LayerMap = require_unique_name.LayerMap;
1895
+ exports.$ak = require_user_text.$ak;
1896
+ exports.$alias = require_user_text.$alias;
1897
+ exports.$blk = require_user_text.$blk;
1898
+ exports.$end = require_user_text.$end;
1899
+ exports.$f = require_user_text.$f;
1900
+ exports.$fc = require_user_text.$fc;
1901
+ exports.$ft = require_user_text.$ft;
1902
+ exports.$nr = require_user_text.$nr;
1903
+ exports.$ps = require_user_text.$ps;
1904
+ exports.$q = require_user_text.$q;
1905
+ exports.$sh = require_user_text.$sh;
1906
+ exports.$ut = require_user_text.$ut;
1907
+ exports.LayerMap = require_user_text.LayerMap;
1912
1908
  exports.SERIALIZE_VERSION = SERIALIZE_VERSION;
1913
- exports.Validator = require_unique_name.Validator;
1914
- exports.ValidatorError = require_unique_name.ValidatorError;
1915
- exports.annotate = require_unique_name.annotate;
1916
- exports.appliedBlockModules = require_unique_name.appliedBlockModules;
1909
+ exports.VALIDATOR_ERROR_CODES = require_user_text.VALIDATOR_ERROR_CODES;
1910
+ exports.VALIDATOR_MESSAGES = require_user_text.VALIDATOR_MESSAGES;
1911
+ exports.Validator = require_user_text.Validator;
1912
+ exports.ValidatorError = require_user_text.ValidatorError;
1913
+ exports.annotate = require_user_text.annotate;
1914
+ exports.appliedBlockModules = require_user_text.appliedBlockModules;
1917
1915
  exports.buildJsonSchema = buildJsonSchema;
1918
1916
  exports.bundledSchemaOf = bundledSchemaOf;
1919
1917
  exports.coerceForType = coerceForType;
1920
1918
  exports.coerceScalar = coerceScalar;
1921
- exports.createAnnotatedTypeNode = require_unique_name.createAnnotatedTypeNode;
1919
+ exports.createAnnotatedTypeNode = require_user_text.createAnnotatedTypeNode;
1922
1920
  exports.createDataFromAnnotatedType = createDataFromAnnotatedType;
1923
- exports.defineAnnotatedType = require_unique_name.defineAnnotatedType;
1921
+ exports.defineAnnotatedType = require_user_text.defineAnnotatedType;
1924
1922
  exports.deserializeAnnotatedType = deserializeAnnotatedType;
1925
- exports.detectDiscriminator = detectDiscriminator;
1923
+ exports.detectDiscriminator = require_user_text.detectDiscriminator;
1926
1924
  exports.flattenAnnotatedType = flattenAnnotatedType;
1927
1925
  exports.forAnnotatedType = forAnnotatedType;
1926
+ exports.formatValidatorMessage = require_user_text.formatValidatorMessage;
1928
1927
  exports.fromJsonSchema = fromJsonSchema;
1929
- exports.invalidateCompiledValidators = require_unique_name.invalidateCompiledValidators;
1930
- exports.isAnnotatedType = require_unique_name.isAnnotatedType;
1931
- exports.isAnnotatedTypeOfPrimitive = require_unique_name.isAnnotatedTypeOfPrimitive;
1932
- exports.isPhantomType = require_unique_name.isPhantomType;
1928
+ exports.invalidateCompiledValidators = require_user_text.invalidateCompiledValidators;
1929
+ exports.isAnnotatedType = require_user_text.isAnnotatedType;
1930
+ exports.isAnnotatedTypeOfPrimitive = require_user_text.isAnnotatedTypeOfPrimitive;
1931
+ exports.isPhantomType = require_user_text.isPhantomType;
1933
1932
  exports.lazyJsonSchema = lazyJsonSchema;
1933
+ exports.localizeErrors = require_user_text.localizeErrors;
1934
1934
  exports.mergeJsonSchemas = mergeJsonSchemas;
1935
- exports.metaEmpty = require_unique_name.metaEmpty;
1936
- exports.pendingStatements = require_unique_name.pendingStatements;
1937
- exports.registerCompiledCheck = require_unique_name.registerCompiledCheck;
1935
+ exports.messagesTranslator = require_user_text.messagesTranslator;
1936
+ exports.metaEmpty = require_user_text.metaEmpty;
1937
+ exports.pendingStatements = require_user_text.pendingStatements;
1938
+ exports.registerCompiledCheck = require_user_text.registerCompiledCheck;
1939
+ exports.registerUserText = require_user_text.registerUserText;
1938
1940
  exports.serializeAnnotatedType = serializeAnnotatedType;
1939
1941
  exports.throwFeatureDisabled = throwFeatureDisabled;
1940
- exports.watchLayer = require_unique_name.watchLayer;
1942
+ exports.userTextKeys = require_user_text.userTextKeys;
1943
+ exports.userTextOf = require_user_text.userTextOf;
1944
+ exports.watchLayer = require_user_text.watchLayer;