@formspec/build 0.1.0-alpha.55 → 0.1.0-alpha.57

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/browser.cjs CHANGED
@@ -880,20 +880,29 @@ function assertNoSerializedNameCollisions(ir) {
880
880
  }
881
881
 
882
882
  // src/json-schema/ir-generator.ts
883
+ function parseEnumSerialization(value) {
884
+ switch (value) {
885
+ case void 0:
886
+ case "enum":
887
+ return "enum";
888
+ case "oneOf":
889
+ return "oneOf";
890
+ case "smart-size":
891
+ return "smart-size";
892
+ default:
893
+ throw new Error(
894
+ `Invalid enumSerialization "${String(value)}". Expected "enum", "oneOf", or "smart-size".`
895
+ );
896
+ }
897
+ }
883
898
  function makeContext(options) {
884
899
  const vendorPrefix = options?.vendorPrefix ?? "x-formspec";
885
- const rawEnumSerialization = options?.enumSerialization;
900
+ const enumSerialization = parseEnumSerialization(options?.enumSerialization);
886
901
  if (!vendorPrefix.startsWith("x-")) {
887
902
  throw new Error(
888
903
  `Invalid vendorPrefix "${vendorPrefix}". Extension JSON Schema keywords must start with "x-".`
889
904
  );
890
905
  }
891
- if (rawEnumSerialization !== void 0 && rawEnumSerialization !== "enum" && rawEnumSerialization !== "oneOf") {
892
- throw new Error(
893
- `Invalid enumSerialization "${rawEnumSerialization}". Expected "enum" or "oneOf".`
894
- );
895
- }
896
- const enumSerialization = rawEnumSerialization ?? "enum";
897
906
  return {
898
907
  defs: {},
899
908
  typeNameMap: {},
@@ -1101,7 +1110,7 @@ function generatePrimitiveType(type) {
1101
1110
  };
1102
1111
  }
1103
1112
  function generateEnumType(type, ctx) {
1104
- if (ctx.enumSerialization === "oneOf") {
1113
+ if (ctx.enumSerialization === "oneOf" || ctx.enumSerialization === "smart-size" && shouldSerializeEnumAsOneOf(type)) {
1105
1114
  return {
1106
1115
  oneOf: type.members.map((m) => {
1107
1116
  const stringValue = String(m.value);
@@ -1111,12 +1120,21 @@ function generateEnumType(type, ctx) {
1111
1120
  };
1112
1121
  }
1113
1122
  const schema = { enum: type.members.map((m) => m.value) };
1123
+ if (ctx.enumSerialization === "smart-size") {
1124
+ return schema;
1125
+ }
1114
1126
  const displayNames = buildEnumDisplayNameExtension(type);
1115
1127
  if (displayNames !== void 0) {
1116
1128
  schema[`${ctx.vendorPrefix}-display-names`] = displayNames;
1117
1129
  }
1118
1130
  return schema;
1119
1131
  }
1132
+ function shouldSerializeEnumAsOneOf(type) {
1133
+ return type.members.some((member) => {
1134
+ const title = member.displayName ?? String(member.value);
1135
+ return title !== String(member.value);
1136
+ });
1137
+ }
1120
1138
  function buildEnumDisplayNameExtension(type) {
1121
1139
  if (!type.members.some((member) => member.displayName !== void 0)) {
1122
1140
  return void 0;