@sanity/schema 5.0.1-next.40 → 5.0.1-next.7
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/_chunks-es/Rule.js +13 -1
- package/lib/_chunks-es/Rule.js.map +1 -1
- package/lib/_internal.js +34 -47
- package/lib/_internal.js.map +1 -1
- package/package.json +8 -8
package/lib/_internal.js
CHANGED
|
@@ -1,8 +1,17 @@
|
|
|
1
1
|
import { SetBuilder, processSetSynchronization } from "@sanity/descriptors";
|
|
2
|
-
import
|
|
2
|
+
import isEqual from "lodash/isEqual.js";
|
|
3
|
+
import isObject from "lodash/isObject.js";
|
|
3
4
|
import { OWN_PROPS_NAME, Rule, Schema } from "./_chunks-es/Rule.js";
|
|
4
5
|
import { ALL_FIELDS_GROUP_NAME, DEFAULT_MAX_FIELD_DEPTH, resolveSearchConfig, resolveSearchConfigForBaseFieldPaths } from "./_chunks-es/Rule.js";
|
|
6
|
+
import difference from "lodash/difference.js";
|
|
7
|
+
import cloneDeep from "lodash/cloneDeep.js";
|
|
8
|
+
import flatten from "lodash/flatten.js";
|
|
9
|
+
import get from "lodash/get.js";
|
|
10
|
+
import uniq from "lodash/uniq.js";
|
|
5
11
|
import humanizeList from "humanize-list";
|
|
12
|
+
import partition from "lodash/partition.js";
|
|
13
|
+
import isPlainObject from "lodash/isPlainObject.js";
|
|
14
|
+
import omit from "lodash/omit.js";
|
|
6
15
|
import leven from "leven";
|
|
7
16
|
import inspect from "object-inspect";
|
|
8
17
|
import { createReferenceTypeNode } from "groq-js";
|
|
@@ -2305,7 +2314,7 @@ const documentDefaultFields = (typeName) => ({
|
|
|
2305
2314
|
["email", { type: "string" }]
|
|
2306
2315
|
]);
|
|
2307
2316
|
function extractSchema(schemaDef, extractOptions = {}) {
|
|
2308
|
-
const inlineFields = /* @__PURE__ */ new Set(), documentTypes = /* @__PURE__ */ new Map(), schema = [],
|
|
2317
|
+
const inlineFields = /* @__PURE__ */ new Set(), documentTypes = /* @__PURE__ */ new Map(), schema = [], generatedTypes = /* @__PURE__ */ new Map(), { sortedSchemaTypeNames, repeated } = sortByDependencies(schemaDef);
|
|
2309
2318
|
repeated.forEach((key, objectField) => {
|
|
2310
2319
|
const base = convertSchemaType(objectField.type);
|
|
2311
2320
|
if (base !== null) {
|
|
@@ -2319,7 +2328,7 @@ function extractSchema(schemaDef, extractOptions = {}) {
|
|
|
2319
2328
|
}
|
|
2320
2329
|
schema.push({
|
|
2321
2330
|
type: "type",
|
|
2322
|
-
name: key,
|
|
2331
|
+
name: getGeneratedTypeName(key),
|
|
2323
2332
|
value: base
|
|
2324
2333
|
});
|
|
2325
2334
|
}
|
|
@@ -2330,15 +2339,15 @@ function extractSchema(schemaDef, extractOptions = {}) {
|
|
|
2330
2339
|
const base = convertBaseType(schemaType);
|
|
2331
2340
|
base !== null && (base.type === "type" && inlineFields.add(schemaType), base.type === "document" && documentTypes.set(typeName, base), schema.push(base));
|
|
2332
2341
|
});
|
|
2333
|
-
function
|
|
2334
|
-
const name =
|
|
2342
|
+
function getGeneratedTypeName(typeName, suffix = "") {
|
|
2343
|
+
const name = generatedTypes.get(typeName);
|
|
2335
2344
|
if (name) return name;
|
|
2336
2345
|
for (let i = 0; i < 5; i++) {
|
|
2337
|
-
const uniqueName = `${
|
|
2338
|
-
if (!
|
|
2339
|
-
return
|
|
2346
|
+
const uniqueName = `${typeName}${suffix}${i || ""}`;
|
|
2347
|
+
if (!sortedSchemaTypeNames.includes(uniqueName))
|
|
2348
|
+
return generatedTypes.set(typeName, uniqueName), uniqueName;
|
|
2340
2349
|
}
|
|
2341
|
-
|
|
2350
|
+
throw new Error(`Unable to generate unique type name for ${typeName}.`);
|
|
2342
2351
|
}
|
|
2343
2352
|
function convertBaseType(schemaType) {
|
|
2344
2353
|
let typeName;
|
|
@@ -2409,7 +2418,7 @@ function extractSchema(schemaDef, extractOptions = {}) {
|
|
|
2409
2418
|
if (hoisted && !isTopLevelSchemaType)
|
|
2410
2419
|
value = {
|
|
2411
2420
|
type: "inline",
|
|
2412
|
-
name: hoisted
|
|
2421
|
+
name: getGeneratedTypeName(hoisted)
|
|
2413
2422
|
};
|
|
2414
2423
|
else {
|
|
2415
2424
|
if (value = convertSchemaType(field.type), value === null)
|
|
@@ -2461,34 +2470,22 @@ function extractSchema(schemaDef, extractOptions = {}) {
|
|
|
2461
2470
|
}
|
|
2462
2471
|
function createReferenceTypeNodeDefintion(reference2) {
|
|
2463
2472
|
const references = gatherReferenceNames(reference2);
|
|
2464
|
-
for (const name of references)
|
|
2465
|
-
|
|
2466
|
-
|
|
2467
|
-
|
|
2468
|
-
|
|
2469
|
-
|
|
2470
|
-
|
|
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 {
|
|
2473
|
+
for (const name of references)
|
|
2474
|
+
generatedTypes.has(name) || schema.push({
|
|
2475
|
+
type: "type",
|
|
2476
|
+
name: getGeneratedTypeName(name, ".reference"),
|
|
2477
|
+
value: createReferenceTypeNode(name)
|
|
2478
|
+
});
|
|
2479
|
+
return references.length === 1 ? { type: "inline", name: getGeneratedTypeName(references[0], ".reference") } : {
|
|
2480
2480
|
type: "union",
|
|
2481
|
-
of: references.map((name) => {
|
|
2482
|
-
|
|
2483
|
-
|
|
2484
|
-
})
|
|
2481
|
+
of: references.map((name) => ({
|
|
2482
|
+
type: "inline",
|
|
2483
|
+
name: getGeneratedTypeName(name, ".reference")
|
|
2484
|
+
}))
|
|
2485
2485
|
};
|
|
2486
2486
|
}
|
|
2487
2487
|
return schema;
|
|
2488
2488
|
}
|
|
2489
|
-
function getInlineRefName(typeName) {
|
|
2490
|
-
return `${typeName}.reference`;
|
|
2491
|
-
}
|
|
2492
2489
|
function createKeyField() {
|
|
2493
2490
|
return {
|
|
2494
2491
|
type: "objectAttribute",
|
|
@@ -2607,18 +2604,12 @@ function lastType(typeDef) {
|
|
|
2607
2604
|
function sortByDependencies(compiledSchema) {
|
|
2608
2605
|
const seen = /* @__PURE__ */ new Set(), objectMap = /* @__PURE__ */ new Set(), repeated = /* @__PURE__ */ new Map(), repeatedNames = /* @__PURE__ */ new Set();
|
|
2609
2606
|
function pickRepeatedName(path) {
|
|
2610
|
-
for (let idx = path.length - 1; idx >=
|
|
2607
|
+
for (let idx = path.length - 1; idx >= 0; idx--) {
|
|
2611
2608
|
const name = path.slice(idx).join(".");
|
|
2612
|
-
if (!repeatedNames.has(name)
|
|
2609
|
+
if (!repeatedNames.has(name))
|
|
2613
2610
|
return repeatedNames.add(name), name;
|
|
2614
2611
|
}
|
|
2615
|
-
|
|
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;
|
|
2612
|
+
throw new Error(`Unable to pick repeated name: ${path.join(".")}`);
|
|
2622
2613
|
}
|
|
2623
2614
|
function walkDependencies(schemaType, dependencies, path, hoistRepetitions = !0) {
|
|
2624
2615
|
if (!seen.has(schemaType)) {
|
|
@@ -2635,11 +2626,7 @@ function sortByDependencies(compiledSchema) {
|
|
|
2635
2626
|
field.type.to.forEach((ref) => dependencies.add(ref.type));
|
|
2636
2627
|
else if (dependencies.add(field.type), hoistRepetitions && !validSchemaNames.has(field.type.name)) {
|
|
2637
2628
|
const fieldPath = path.concat([field.name]);
|
|
2638
|
-
|
|
2639
|
-
const name = pickRepeatedName(fieldPath);
|
|
2640
|
-
name !== null && repeated.set(field, name);
|
|
2641
|
-
}
|
|
2642
|
-
objectMap.add(field);
|
|
2629
|
+
!repeated.has(field) && objectMap.has(field) && repeated.set(field, pickRepeatedName(fieldPath)), objectMap.add(field);
|
|
2643
2630
|
}
|
|
2644
2631
|
} else field.type && dependencies.add(field.type);
|
|
2645
2632
|
walkDependencies(field.type, dependencies, path.concat([field.name]));
|