@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.
@@ -4439,20 +4439,29 @@ function assertNoSerializedNameCollisions(ir) {
4439
4439
  }
4440
4440
 
4441
4441
  // src/json-schema/ir-generator.ts
4442
+ function parseEnumSerialization(value) {
4443
+ switch (value) {
4444
+ case void 0:
4445
+ case "enum":
4446
+ return "enum";
4447
+ case "oneOf":
4448
+ return "oneOf";
4449
+ case "smart-size":
4450
+ return "smart-size";
4451
+ default:
4452
+ throw new Error(
4453
+ `Invalid enumSerialization "${String(value)}". Expected "enum", "oneOf", or "smart-size".`
4454
+ );
4455
+ }
4456
+ }
4442
4457
  function makeContext(options) {
4443
4458
  const vendorPrefix = options?.vendorPrefix ?? "x-formspec";
4444
- const rawEnumSerialization = options?.enumSerialization;
4459
+ const enumSerialization = parseEnumSerialization(options?.enumSerialization);
4445
4460
  if (!vendorPrefix.startsWith("x-")) {
4446
4461
  throw new Error(
4447
4462
  `Invalid vendorPrefix "${vendorPrefix}". Extension JSON Schema keywords must start with "x-".`
4448
4463
  );
4449
4464
  }
4450
- if (rawEnumSerialization !== void 0 && rawEnumSerialization !== "enum" && rawEnumSerialization !== "oneOf") {
4451
- throw new Error(
4452
- `Invalid enumSerialization "${rawEnumSerialization}". Expected "enum" or "oneOf".`
4453
- );
4454
- }
4455
- const enumSerialization = rawEnumSerialization ?? "enum";
4456
4465
  return {
4457
4466
  defs: {},
4458
4467
  typeNameMap: {},
@@ -4660,7 +4669,7 @@ function generatePrimitiveType(type) {
4660
4669
  };
4661
4670
  }
4662
4671
  function generateEnumType(type, ctx) {
4663
- if (ctx.enumSerialization === "oneOf") {
4672
+ if (ctx.enumSerialization === "oneOf" || ctx.enumSerialization === "smart-size" && shouldSerializeEnumAsOneOf(type)) {
4664
4673
  return {
4665
4674
  oneOf: type.members.map((m) => {
4666
4675
  const stringValue = String(m.value);
@@ -4670,12 +4679,21 @@ function generateEnumType(type, ctx) {
4670
4679
  };
4671
4680
  }
4672
4681
  const schema = { enum: type.members.map((m) => m.value) };
4682
+ if (ctx.enumSerialization === "smart-size") {
4683
+ return schema;
4684
+ }
4673
4685
  const displayNames = buildEnumDisplayNameExtension(type);
4674
4686
  if (displayNames !== void 0) {
4675
4687
  schema[`${ctx.vendorPrefix}-display-names`] = displayNames;
4676
4688
  }
4677
4689
  return schema;
4678
4690
  }
4691
+ function shouldSerializeEnumAsOneOf(type) {
4692
+ return type.members.some((member) => {
4693
+ const title = member.displayName ?? String(member.value);
4694
+ return title !== String(member.value);
4695
+ });
4696
+ }
4679
4697
  function buildEnumDisplayNameExtension(type) {
4680
4698
  if (!type.members.some((member) => member.displayName !== void 0)) {
4681
4699
  return void 0;