@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.js CHANGED
@@ -832,20 +832,29 @@ function assertNoSerializedNameCollisions(ir) {
832
832
  }
833
833
 
834
834
  // src/json-schema/ir-generator.ts
835
+ function parseEnumSerialization(value) {
836
+ switch (value) {
837
+ case void 0:
838
+ case "enum":
839
+ return "enum";
840
+ case "oneOf":
841
+ return "oneOf";
842
+ case "smart-size":
843
+ return "smart-size";
844
+ default:
845
+ throw new Error(
846
+ `Invalid enumSerialization "${String(value)}". Expected "enum", "oneOf", or "smart-size".`
847
+ );
848
+ }
849
+ }
835
850
  function makeContext(options) {
836
851
  const vendorPrefix = options?.vendorPrefix ?? "x-formspec";
837
- const rawEnumSerialization = options?.enumSerialization;
852
+ const enumSerialization = parseEnumSerialization(options?.enumSerialization);
838
853
  if (!vendorPrefix.startsWith("x-")) {
839
854
  throw new Error(
840
855
  `Invalid vendorPrefix "${vendorPrefix}". Extension JSON Schema keywords must start with "x-".`
841
856
  );
842
857
  }
843
- if (rawEnumSerialization !== void 0 && rawEnumSerialization !== "enum" && rawEnumSerialization !== "oneOf") {
844
- throw new Error(
845
- `Invalid enumSerialization "${rawEnumSerialization}". Expected "enum" or "oneOf".`
846
- );
847
- }
848
- const enumSerialization = rawEnumSerialization ?? "enum";
849
858
  return {
850
859
  defs: {},
851
860
  typeNameMap: {},
@@ -1053,7 +1062,7 @@ function generatePrimitiveType(type) {
1053
1062
  };
1054
1063
  }
1055
1064
  function generateEnumType(type, ctx) {
1056
- if (ctx.enumSerialization === "oneOf") {
1065
+ if (ctx.enumSerialization === "oneOf" || ctx.enumSerialization === "smart-size" && shouldSerializeEnumAsOneOf(type)) {
1057
1066
  return {
1058
1067
  oneOf: type.members.map((m) => {
1059
1068
  const stringValue = String(m.value);
@@ -1063,12 +1072,21 @@ function generateEnumType(type, ctx) {
1063
1072
  };
1064
1073
  }
1065
1074
  const schema = { enum: type.members.map((m) => m.value) };
1075
+ if (ctx.enumSerialization === "smart-size") {
1076
+ return schema;
1077
+ }
1066
1078
  const displayNames = buildEnumDisplayNameExtension(type);
1067
1079
  if (displayNames !== void 0) {
1068
1080
  schema[`${ctx.vendorPrefix}-display-names`] = displayNames;
1069
1081
  }
1070
1082
  return schema;
1071
1083
  }
1084
+ function shouldSerializeEnumAsOneOf(type) {
1085
+ return type.members.some((member) => {
1086
+ const title = member.displayName ?? String(member.value);
1087
+ return title !== String(member.value);
1088
+ });
1089
+ }
1072
1090
  function buildEnumDisplayNameExtension(type) {
1073
1091
  if (!type.members.some((member) => member.displayName !== void 0)) {
1074
1092
  return void 0;