@kubb/plugin-faker 5.0.10 → 5.0.11

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
@@ -528,15 +528,23 @@ function getScalarType(node, typeName) {
528
528
  }
529
529
  }
530
530
  /**
531
+ * A nullable scalar inlines its primitive instead of naming the generated type, so the
532
+ * nullability the type plugin emitted has to be reapplied here. Non-scalars keep `typeName`,
533
+ * which already carries it.
534
+ */
535
+ function withNullability(type, node) {
536
+ return node.nullable ? `${type} | null` : type;
537
+ }
538
+ /**
531
539
  * Resolves faker type usage information for a schema.
532
540
  * Determines the data type, return type, and whether it uses the type name.
533
541
  */
534
542
  function resolveFakerTypeUsage(node, typeName, canOverride) {
535
543
  const isScalar = SCALAR_TYPES$1.has(node.type);
536
544
  let dataType = `Partial<${typeName}>`;
537
- if (isScalar) dataType = getScalarType(node, typeName);
545
+ if (isScalar) dataType = withNullability(getScalarType(node, typeName), node);
538
546
  let returnType = canOverride ? typeName : null;
539
- if (isScalar) returnType = getScalarType(node, typeName);
547
+ if (isScalar) returnType = withNullability(getScalarType(node, typeName), node);
540
548
  return {
541
549
  dataType,
542
550
  returnType,
@@ -573,7 +581,7 @@ function Faker({ node, description, name, typeName, printer, canOverride }) {
573
581
  const fakerTextWithOverride = (() => {
574
582
  if (canOverride && node.type === "tuple") return `data && data.length === ${node.items?.length ?? 0} && !data.includes(undefined) ? data : ${fakerText}`;
575
583
  if (canOverride && isArray) return `[\n ...${fakerText},\n ...(data || []).filter((item) => item !== undefined),\n]`;
576
- if (canOverride && (isScalar || isUnion)) return `data ?? ${fakerText}`;
584
+ if (canOverride && (isScalar || isUnion)) return node.nullable ? `data !== undefined ? data : ${fakerText}` : `data ?? ${fakerText}`;
577
585
  return fakerText;
578
586
  })();
579
587
  const { dataType, returnType: resolvedReturnType } = resolveFakerTypeUsage(node, typeName, canOverride);