@sanity/schema 5.0.1-next.37 → 5.0.1-next.39

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/lib/_internal.js CHANGED
@@ -2305,7 +2305,7 @@ const documentDefaultFields = (typeName) => ({
2305
2305
  ["email", { type: "string" }]
2306
2306
  ]);
2307
2307
  function extractSchema(schemaDef, extractOptions = {}) {
2308
- const inlineFields = /* @__PURE__ */ new Set(), documentTypes = /* @__PURE__ */ new Map(), schema = [], generatedTypes = /* @__PURE__ */ new Map(), { sortedSchemaTypeNames, repeated } = sortByDependencies(schemaDef);
2308
+ const inlineFields = /* @__PURE__ */ new Set(), documentTypes = /* @__PURE__ */ new Map(), schema = [], hoistedRefMap = /* @__PURE__ */ new Map(), { sortedSchemaTypeNames, repeated } = sortByDependencies(schemaDef);
2309
2309
  repeated.forEach((key, objectField) => {
2310
2310
  const base = convertSchemaType(objectField.type);
2311
2311
  if (base !== null) {
@@ -2319,7 +2319,7 @@ function extractSchema(schemaDef, extractOptions = {}) {
2319
2319
  }
2320
2320
  schema.push({
2321
2321
  type: "type",
2322
- name: getGeneratedTypeName(key),
2322
+ name: key,
2323
2323
  value: base
2324
2324
  });
2325
2325
  }
@@ -2330,15 +2330,15 @@ function extractSchema(schemaDef, extractOptions = {}) {
2330
2330
  const base = convertBaseType(schemaType);
2331
2331
  base !== null && (base.type === "type" && inlineFields.add(schemaType), base.type === "document" && documentTypes.set(typeName, base), schema.push(base));
2332
2332
  });
2333
- function getGeneratedTypeName(typeName, suffix = "") {
2334
- const name = generatedTypes.get(typeName);
2333
+ function reserveRefName(refName) {
2334
+ const name = hoistedRefMap.get(refName);
2335
2335
  if (name) return name;
2336
2336
  for (let i = 0; i < 5; i++) {
2337
- const uniqueName = `${typeName}${suffix}${i || ""}`;
2338
- if (!sortedSchemaTypeNames.includes(uniqueName))
2339
- return generatedTypes.set(typeName, uniqueName), uniqueName;
2337
+ const uniqueName = `${refName}${i || ""}`;
2338
+ if (!schemaDef.has(uniqueName))
2339
+ return hoistedRefMap.set(refName, uniqueName), uniqueName;
2340
2340
  }
2341
- throw new Error(`Unable to generate unique type name for ${typeName}.`);
2341
+ return null;
2342
2342
  }
2343
2343
  function convertBaseType(schemaType) {
2344
2344
  let typeName;
@@ -2409,7 +2409,7 @@ function extractSchema(schemaDef, extractOptions = {}) {
2409
2409
  if (hoisted && !isTopLevelSchemaType)
2410
2410
  value = {
2411
2411
  type: "inline",
2412
- name: getGeneratedTypeName(hoisted)
2412
+ name: hoisted
2413
2413
  };
2414
2414
  else {
2415
2415
  if (value = convertSchemaType(field.type), value === null)
@@ -2461,22 +2461,34 @@ function extractSchema(schemaDef, extractOptions = {}) {
2461
2461
  }
2462
2462
  function createReferenceTypeNodeDefintion(reference2) {
2463
2463
  const references = gatherReferenceNames(reference2);
2464
- for (const name of references)
2465
- generatedTypes.has(name) || schema.push({
2466
- type: "type",
2467
- name: getGeneratedTypeName(name, ".reference"),
2468
- value: createReferenceTypeNode(name)
2469
- });
2470
- return references.length === 1 ? { type: "inline", name: getGeneratedTypeName(references[0], ".reference") } : {
2464
+ for (const name of references) {
2465
+ const refName = getInlineRefName(name);
2466
+ if (!hoistedRefMap.has(refName)) {
2467
+ const inlined = reserveRefName(refName);
2468
+ inlined && schema.push({
2469
+ type: "type",
2470
+ name: inlined,
2471
+ value: createReferenceTypeNode(name)
2472
+ });
2473
+ }
2474
+ }
2475
+ if (references.length === 1) {
2476
+ const inlined = hoistedRefMap.get(getInlineRefName(references[0]));
2477
+ return inlined ? { type: "inline", name: inlined } : createReferenceTypeNode(references[0]);
2478
+ }
2479
+ return {
2471
2480
  type: "union",
2472
- of: references.map((name) => ({
2473
- type: "inline",
2474
- name: getGeneratedTypeName(name, ".reference")
2475
- }))
2481
+ of: references.map((name) => {
2482
+ const inlined = hoistedRefMap.get(getInlineRefName(name));
2483
+ return inlined ? { type: "inline", name: inlined } : createReferenceTypeNode(name);
2484
+ })
2476
2485
  };
2477
2486
  }
2478
2487
  return schema;
2479
2488
  }
2489
+ function getInlineRefName(typeName) {
2490
+ return `${typeName}.reference`;
2491
+ }
2480
2492
  function createKeyField() {
2481
2493
  return {
2482
2494
  type: "objectAttribute",
@@ -2595,12 +2607,18 @@ function lastType(typeDef) {
2595
2607
  function sortByDependencies(compiledSchema) {
2596
2608
  const seen = /* @__PURE__ */ new Set(), objectMap = /* @__PURE__ */ new Set(), repeated = /* @__PURE__ */ new Map(), repeatedNames = /* @__PURE__ */ new Set();
2597
2609
  function pickRepeatedName(path) {
2598
- for (let idx = path.length - 1; idx >= 0; idx--) {
2610
+ for (let idx = path.length - 1; idx >= 1; idx--) {
2599
2611
  const name = path.slice(idx).join(".");
2600
- if (!repeatedNames.has(name))
2612
+ if (!repeatedNames.has(name) && !compiledSchema.get(name))
2601
2613
  return repeatedNames.add(name), name;
2602
2614
  }
2603
- throw new Error(`Unable to pick repeated name: ${path.join(".")}`);
2615
+ for (let i = 1; i < 10; i++)
2616
+ for (let idx = path.length - 1; idx >= 1; idx--) {
2617
+ const name = `${path.slice(idx).join(".")}${i}`;
2618
+ if (!repeatedNames.has(name) && !compiledSchema.get(name))
2619
+ return repeatedNames.add(name), name;
2620
+ }
2621
+ return null;
2604
2622
  }
2605
2623
  function walkDependencies(schemaType, dependencies, path, hoistRepetitions = !0) {
2606
2624
  if (!seen.has(schemaType)) {
@@ -2617,7 +2635,11 @@ function sortByDependencies(compiledSchema) {
2617
2635
  field.type.to.forEach((ref) => dependencies.add(ref.type));
2618
2636
  else if (dependencies.add(field.type), hoistRepetitions && !validSchemaNames.has(field.type.name)) {
2619
2637
  const fieldPath = path.concat([field.name]);
2620
- !repeated.has(field) && objectMap.has(field) && repeated.set(field, pickRepeatedName(fieldPath)), objectMap.add(field);
2638
+ if (!repeated.has(field) && objectMap.has(field)) {
2639
+ const name = pickRepeatedName(fieldPath);
2640
+ name !== null && repeated.set(field, name);
2641
+ }
2642
+ objectMap.add(field);
2621
2643
  }
2622
2644
  } else field.type && dependencies.add(field.type);
2623
2645
  walkDependencies(field.type, dependencies, path.concat([field.name]));