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