@orval/mock 8.36.0 → 8.37.0

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.mjs CHANGED
@@ -520,6 +520,7 @@ function getMockObject({ item, mockOptions, operationId, tags, combine, context,
520
520
  if (!isRequired && !resolvedValue.overrided && !hasDefault) {
521
521
  if (resolvedValue.nullWrapped) return `${keyDefinition}: ${resolvedValue.value}`;
522
522
  const omitValue = mockOptions?.nonNullable || !hasNullable ? "undefined" : "null";
523
+ if (omitValue === "undefined" && mockOptions?.exactOptional) return `...(faker.datatype.boolean() ? {${keyDefinition}: ${resolvedValue.value}} : {})`;
523
524
  return `${keyDefinition}: faker.helpers.arrayElement([${resolvedValue.value}, ${omitValue}])`;
524
525
  }
525
526
  if (Array.isArray(prop.type) && prop.type.includes("null") && !resolvedValue.nullWrapped && !resolvedValue.overrided && !mockOptions?.nonNullable) return `${keyDefinition}: faker.helpers.arrayElement([${resolvedValue.value}, null])`;
@@ -934,7 +935,10 @@ function getMockScalar({ item, imports, mockOptions, operationId, tags, combine,
934
935
  value = getNullable(`faker.number.float(${floatParts.length > 0 ? `{${floatParts.join(", ")}}` : ""})`, isNullable, nonNullableOption);
935
936
  }
936
937
  const numberImports = [];
937
- if (item.enum) value = getEnum(item, numberImports, context, existingReferencedProperties, "number");
938
+ if (item.enum) value = getEnum(item, numberImports, context, existingReferencedProperties, {
939
+ type: "number",
940
+ exactOptional: safeMockOptions.exactOptional
941
+ });
938
942
  else if ("const" in item) value = JSON.stringify(item.const);
939
943
  return {
940
944
  value,
@@ -947,7 +951,10 @@ function getMockScalar({ item, imports, mockOptions, operationId, tags, combine,
947
951
  case "boolean": {
948
952
  let value = "faker.datatype.boolean()";
949
953
  const booleanImports = [];
950
- if (item.enum) value = getEnum(item, booleanImports, context, existingReferencedProperties, "boolean");
954
+ if (item.enum) value = getEnum(item, booleanImports, context, existingReferencedProperties, {
955
+ type: "boolean",
956
+ exactOptional: safeMockOptions.exactOptional
957
+ });
951
958
  else if ("const" in item) value = JSON.stringify(item.const);
952
959
  return {
953
960
  value,
@@ -1076,7 +1083,10 @@ function getMockScalar({ item, imports, mockOptions, operationId, tags, combine,
1076
1083
  if (strMin !== void 0 && strMax !== void 0) strLenParts.push(`min: ${strMin}`, `max: ${strMax}`);
1077
1084
  let value = `faker.string.alpha(${strLenParts.length > 0 ? `{length: {${strLenParts.join(", ")}}}` : ""})`;
1078
1085
  const stringImports = [];
1079
- if (item.enum) value = getEnum(item, stringImports, context, existingReferencedProperties, "string");
1086
+ if (item.enum) value = getEnum(item, stringImports, context, existingReferencedProperties, {
1087
+ type: "string",
1088
+ exactOptional: safeMockOptions.exactOptional
1089
+ });
1080
1090
  else if (item.pattern) value = `faker.helpers.fromRegExp(${JSON.stringify(item.pattern)})`;
1081
1091
  else if ("const" in item) value = JSON.stringify(item.const);
1082
1092
  return {
@@ -1097,7 +1107,7 @@ function getMockScalar({ item, imports, mockOptions, operationId, tags, combine,
1097
1107
  if (item.enum) {
1098
1108
  const enumImports = [];
1099
1109
  return {
1100
- value: getEnum(item, enumImports, context, existingReferencedProperties),
1110
+ value: getEnum(item, enumImports, context, existingReferencedProperties, { exactOptional: safeMockOptions.exactOptional }),
1101
1111
  enums: item.enum,
1102
1112
  imports: enumImports,
1103
1113
  name: item.name
@@ -1180,7 +1190,7 @@ function safeTypeIdentifier(name) {
1180
1190
  const identifier = name.endsWith("[]") ? name.slice(0, -2) : name;
1181
1191
  return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(identifier) ? identifier : void 0;
1182
1192
  }
1183
- function getEnum(item, imports, context, existingReferencedProperties, type) {
1193
+ function getEnum(item, imports, context, existingReferencedProperties, { type, exactOptional } = {}) {
1184
1194
  if (!item.enum) return "";
1185
1195
  let enumValue = `[${item.enum.filter((e) => e !== null).map((e) => formatEnumMember(e)).join(",")}]`;
1186
1196
  if (context.output.override.enumGenerationType === EnumGeneration.ENUM) {
@@ -1196,7 +1206,8 @@ function getEnum(item, imports, context, existingReferencedProperties, type) {
1196
1206
  if (!parentReference) return "";
1197
1207
  const parentIdentifier = safeTypeIdentifier(parentReference);
1198
1208
  if (parentIdentifier) {
1199
- enumValue += ` as ${parentIdentifier}[${getStringLiteralType(item.name)}]`;
1209
+ const indexedType = `${parentIdentifier}[${getStringLiteralType(item.name)}]`;
1210
+ enumValue += exactOptional ? ` as Exclude<${indexedType}, undefined>` : ` as ${indexedType}`;
1200
1211
  if (!item.path?.endsWith("[]")) enumValue += "[]";
1201
1212
  imports.push({ name: parentIdentifier });
1202
1213
  } else enumValue += " as const";
@@ -1638,6 +1649,7 @@ function getMockWithoutFunc(spec, override) {
1638
1649
  numberMax: override?.mock?.numberMax,
1639
1650
  required: override?.mock?.required,
1640
1651
  nonNullable: override?.mock?.nonNullable,
1652
+ exactOptional: override?.mock?.exactOptional,
1641
1653
  fractionDigits: override?.mock?.fractionDigits,
1642
1654
  ...override?.mock?.properties ? { properties: getMockPropertiesWithoutFunc(override.mock.properties, spec) } : {},
1643
1655
  ...override?.mock?.format ? { format: getMockPropertiesWithoutFunc(override.mock.format, spec) } : {},