@sanity/schema 5.0.0-next-major.20251216101132 → 5.0.0-next-major.20251216102111

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.
@@ -233,19 +233,6 @@ export declare class DescriptorConverter {
233
233
  ): Promise<SetSynchronization<RegistryType>>
234
234
  }
235
235
 
236
- /**
237
- * Extracts a GROQ-compatible schema from a Sanity schema definition. The extraction happens in three passes:
238
- *
239
- * 1. **Dependency analysis & hoisting detection** (`sortByDependencies`): Walks the entire schema to sort
240
- * types topologically and identifies inline object fields that are used multiple times (candidates
241
- * for "hoisting").
242
- *
243
- * 2. **Hoisted type creation**: For any repeated inline fields, we create top-level named type definitions
244
- * first, so they exist before being referenced.
245
- *
246
- * 3. **Main type conversion**: Processes each schema type in dependency order. When a field was marked for
247
- * hoisting, we emit an `inline` reference to the hoisted type instead of duplicating the structure.
248
- */
249
236
  export declare function extractSchema(
250
237
  schemaDef: Schema,
251
238
  extractOptions?: ExtractSchemaOptions,
package/lib/_internal.js CHANGED
@@ -2314,32 +2314,15 @@ const documentDefaultFields = (typeName) => ({
2314
2314
  ["email", { type: "string" }]
2315
2315
  ]);
2316
2316
  function extractSchema(schemaDef, extractOptions = {}) {
2317
- const inlineFields = /* @__PURE__ */ new Set(), documentTypes = /* @__PURE__ */ new Map(), schema = [], generatedTypes = /* @__PURE__ */ new Map(), { sortedSchemaTypeNames, repeated } = sortByDependencies(schemaDef);
2318
- repeated.forEach((key, objectField) => {
2319
- const base = convertSchemaType(objectField.type);
2320
- if (base !== null) {
2321
- if (base.type === "inline") {
2322
- repeated.delete(objectField);
2323
- return;
2324
- }
2325
- if (base.type === "unknown") {
2326
- repeated.delete(objectField);
2327
- return;
2328
- }
2329
- schema.push({
2330
- type: "type",
2331
- name: getGeneratedTypeName(key),
2332
- value: base
2333
- });
2334
- }
2335
- }), sortedSchemaTypeNames.forEach((typeName) => {
2317
+ const inlineFields = /* @__PURE__ */ new Set(), documentTypes = /* @__PURE__ */ new Map(), schema = [], generatedTypes = /* @__PURE__ */ new Map(), sortedSchemaTypeNames = sortByDependencies(schemaDef);
2318
+ sortedSchemaTypeNames.forEach((typeName) => {
2336
2319
  const schemaType = schemaDef.get(typeName);
2337
2320
  if (schemaType === void 0)
2338
2321
  return;
2339
2322
  const base = convertBaseType(schemaType);
2340
2323
  base !== null && (base.type === "type" && inlineFields.add(schemaType), base.type === "document" && documentTypes.set(typeName, base), schema.push(base));
2341
2324
  });
2342
- function getGeneratedTypeName(typeName, suffix = "") {
2325
+ function getGeneratedTypeName(typeName, suffix) {
2343
2326
  const name = generatedTypes.get(typeName);
2344
2327
  if (name) return name;
2345
2328
  for (let i = 0; i < 5; i++) {
@@ -2412,19 +2395,10 @@ function extractSchema(schemaDef, extractOptions = {}) {
2412
2395
  function createObject(schemaType) {
2413
2396
  const attributes = {}, fields = gatherFields(schemaType);
2414
2397
  for (const field of fields) {
2415
- const fieldIsRequired = isFieldRequired(field?.type?.validation);
2416
- let value;
2417
- const hoisted = repeated.get(field), isTopLevelSchemaType = sortedSchemaTypeNames.includes(field.type.name);
2418
- if (hoisted && !isTopLevelSchemaType)
2419
- value = {
2420
- type: "inline",
2421
- name: getGeneratedTypeName(hoisted)
2422
- };
2423
- else {
2424
- if (value = convertSchemaType(field.type), value === null)
2425
- continue;
2426
- hasAssetRequired(field?.type?.validation) && value.type === "object" && (value.attributes.asset.optional = !1);
2427
- }
2398
+ const fieldIsRequired = isFieldRequired(field?.type?.validation), value = convertSchemaType(field.type);
2399
+ if (value === null)
2400
+ continue;
2401
+ hasAssetRequired(field?.type?.validation) && value.type === "object" && (value.attributes.asset.optional = !1);
2428
2402
  const optional = extractOptions.enforceRequiredFields ? fieldIsRequired === !1 : !0;
2429
2403
  attributes[field.name] = {
2430
2404
  type: "objectAttribute",
@@ -2602,16 +2576,8 @@ function lastType(typeDef) {
2602
2576
  }
2603
2577
  }
2604
2578
  function sortByDependencies(compiledSchema) {
2605
- const seen = /* @__PURE__ */ new Set(), objectMap = /* @__PURE__ */ new Set(), repeated = /* @__PURE__ */ new Map(), repeatedNames = /* @__PURE__ */ new Set();
2606
- function pickRepeatedName(path) {
2607
- for (let idx = path.length - 1; idx >= 0; idx--) {
2608
- const name = path.slice(idx).join(".");
2609
- if (!repeatedNames.has(name))
2610
- return repeatedNames.add(name), name;
2611
- }
2612
- throw new Error(`Unable to pick repeated name: ${path.join(".")}`);
2613
- }
2614
- function walkDependencies(schemaType, dependencies, path, hoistRepetitions = !0) {
2579
+ const seen = /* @__PURE__ */ new Set();
2580
+ function walkDependencies(schemaType, dependencies) {
2615
2581
  if (!seen.has(schemaType)) {
2616
2582
  if (seen.add(schemaType), "fields" in schemaType)
2617
2583
  for (const field of gatherFields(schemaType)) {
@@ -2621,19 +2587,11 @@ function sortByDependencies(compiledSchema) {
2621
2587
  continue;
2622
2588
  }
2623
2589
  let schemaTypeName;
2624
- if (schemaType.type.type ? schemaTypeName = field.type.type.name : "jsonType" in schemaType.type && (schemaTypeName = field.type.jsonType), schemaTypeName === "object" || schemaTypeName === "block") {
2625
- if (isReferenceType(field.type))
2626
- field.type.to.forEach((ref) => dependencies.add(ref.type));
2627
- else if (dependencies.add(field.type), hoistRepetitions && !validSchemaNames.has(field.type.name)) {
2628
- const fieldPath = path.concat([field.name]);
2629
- !repeated.has(field) && objectMap.has(field) && repeated.set(field, pickRepeatedName(fieldPath)), objectMap.add(field);
2630
- }
2631
- } else field.type && dependencies.add(field.type);
2632
- walkDependencies(field.type, dependencies, path.concat([field.name]));
2590
+ schemaType.type.type ? schemaTypeName = field.type.type.name : "jsonType" in schemaType.type && (schemaTypeName = field.type.jsonType), schemaTypeName === "object" || schemaTypeName === "block" ? isReferenceType(field.type) ? field.type.to.forEach((ref) => dependencies.add(ref.type)) : dependencies.add(field.type) : field.type && dependencies.add(field.type), walkDependencies(field.type, dependencies);
2633
2591
  }
2634
2592
  else if ("of" in schemaType)
2635
2593
  for (const item of schemaType.of)
2636
- walkDependencies(item, dependencies, path.concat(item.name), !isReferenceType(schemaType));
2594
+ walkDependencies(item, dependencies);
2637
2595
  }
2638
2596
  }
2639
2597
  const dependencyMap = /* @__PURE__ */ new Map(), schemaTypeNames = compiledSchema.getTypeNames(), validSchemaNames = /* @__PURE__ */ new Set();
@@ -2643,7 +2601,7 @@ function sortByDependencies(compiledSchema) {
2643
2601
  return;
2644
2602
  validSchemaNames.add(typeName);
2645
2603
  const dependencies = /* @__PURE__ */ new Set();
2646
- walkDependencies(schemaType, dependencies, [typeName]), dependencyMap.set(schemaType, dependencies), seen.clear();
2604
+ walkDependencies(schemaType, dependencies), dependencyMap.set(schemaType, dependencies), seen.clear();
2647
2605
  });
2648
2606
  const typeNames = [], currentlyVisiting = /* @__PURE__ */ new Set(), visited = /* @__PURE__ */ new Set();
2649
2607
  function visit(type) {
@@ -2655,10 +2613,7 @@ function sortByDependencies(compiledSchema) {
2655
2613
  }
2656
2614
  for (const [type] of dependencyMap)
2657
2615
  visit(type);
2658
- return {
2659
- sortedSchemaTypeNames: typeNames.filter((typeName) => validSchemaNames.has(typeName)),
2660
- repeated
2661
- };
2616
+ return typeNames.filter((typeName) => validSchemaNames.has(typeName));
2662
2617
  }
2663
2618
  function validateNoCallbacks(typeDef) {
2664
2619
  const problems = [];