@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/index.js CHANGED
@@ -944,20 +944,29 @@ function assertNoSerializedNameCollisions(ir) {
944
944
  }
945
945
 
946
946
  // src/json-schema/ir-generator.ts
947
+ function parseEnumSerialization(value) {
948
+ switch (value) {
949
+ case void 0:
950
+ case "enum":
951
+ return "enum";
952
+ case "oneOf":
953
+ return "oneOf";
954
+ case "smart-size":
955
+ return "smart-size";
956
+ default:
957
+ throw new Error(
958
+ `Invalid enumSerialization "${String(value)}". Expected "enum", "oneOf", or "smart-size".`
959
+ );
960
+ }
961
+ }
947
962
  function makeContext(options) {
948
963
  const vendorPrefix = options?.vendorPrefix ?? "x-formspec";
949
- const rawEnumSerialization = options?.enumSerialization;
964
+ const enumSerialization = parseEnumSerialization(options?.enumSerialization);
950
965
  if (!vendorPrefix.startsWith("x-")) {
951
966
  throw new Error(
952
967
  `Invalid vendorPrefix "${vendorPrefix}". Extension JSON Schema keywords must start with "x-".`
953
968
  );
954
969
  }
955
- if (rawEnumSerialization !== void 0 && rawEnumSerialization !== "enum" && rawEnumSerialization !== "oneOf") {
956
- throw new Error(
957
- `Invalid enumSerialization "${rawEnumSerialization}". Expected "enum" or "oneOf".`
958
- );
959
- }
960
- const enumSerialization = rawEnumSerialization ?? "enum";
961
970
  return {
962
971
  defs: {},
963
972
  typeNameMap: {},
@@ -1165,7 +1174,7 @@ function generatePrimitiveType(type) {
1165
1174
  };
1166
1175
  }
1167
1176
  function generateEnumType(type, ctx) {
1168
- if (ctx.enumSerialization === "oneOf") {
1177
+ if (ctx.enumSerialization === "oneOf" || ctx.enumSerialization === "smart-size" && shouldSerializeEnumAsOneOf(type)) {
1169
1178
  return {
1170
1179
  oneOf: type.members.map((m) => {
1171
1180
  const stringValue = String(m.value);
@@ -1175,12 +1184,21 @@ function generateEnumType(type, ctx) {
1175
1184
  };
1176
1185
  }
1177
1186
  const schema = { enum: type.members.map((m) => m.value) };
1187
+ if (ctx.enumSerialization === "smart-size") {
1188
+ return schema;
1189
+ }
1178
1190
  const displayNames = buildEnumDisplayNameExtension(type);
1179
1191
  if (displayNames !== void 0) {
1180
1192
  schema[`${ctx.vendorPrefix}-display-names`] = displayNames;
1181
1193
  }
1182
1194
  return schema;
1183
1195
  }
1196
+ function shouldSerializeEnumAsOneOf(type) {
1197
+ return type.members.some((member) => {
1198
+ const title = member.displayName ?? String(member.value);
1199
+ return title !== String(member.value);
1200
+ });
1201
+ }
1184
1202
  function buildEnumDisplayNameExtension(type) {
1185
1203
  if (!type.members.some((member) => member.displayName !== void 0)) {
1186
1204
  return void 0;