@sanity/schema 6.18.1-next.5 → 6.19.0-next.100
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.d.ts +2 -1
- package/lib/_internal.js +34 -47
- package/lib/_internal.js.map +1 -1
- package/package.json +4 -4
package/lib/_internal.d.ts
CHANGED
|
@@ -616,13 +616,14 @@ declare const typeVisitors: {
|
|
|
616
616
|
globalDocumentReference: (typeDef: any, visitorContext: any) => any;
|
|
617
617
|
};
|
|
618
618
|
interface Options {
|
|
619
|
+
parentTypes?: _FIXME_[];
|
|
619
620
|
transformTypeVisitors?: (visitors: typeof typeVisitors) => Partial<typeof typeVisitors>;
|
|
620
621
|
transformCommonVisitors?: (visitors: any[]) => any[];
|
|
621
622
|
}
|
|
622
623
|
/**
|
|
623
624
|
* @internal
|
|
624
625
|
*/
|
|
625
|
-
export declare function validateSchema(schemaTypes: _FIXME_, { transformTypeVisitors, transformCommonVisitors }?: Options): {
|
|
626
|
+
export declare function validateSchema(schemaTypes: _FIXME_, { parentTypes, transformTypeVisitors, transformCommonVisitors }?: Options): {
|
|
626
627
|
get(typeName: string): any;
|
|
627
628
|
has(typeName: string): boolean;
|
|
628
629
|
getTypeNames(): string[];
|
package/lib/_internal.js
CHANGED
|
@@ -1454,22 +1454,24 @@ const NOOP_VISITOR = (typeDef) => typeDef, TYPE_TYPE = {
|
|
|
1454
1454
|
"time",
|
|
1455
1455
|
"date"
|
|
1456
1456
|
];
|
|
1457
|
-
function traverseSchema(types = [], coreTypes = [], visitor = NOOP_VISITOR) {
|
|
1458
|
-
let coreTypesRegistry = Object.create(null), registry = Object.create(null), coreTypeNames = coreTypes.map((typeDef) => typeDef.name), reservedTypeNames = FUTURE_RESERVED.concat(coreTypeNames), typeNames = types.map((typeDef) => typeDef && typeDef.name).filter(Boolean);
|
|
1457
|
+
function traverseSchema(types = [], coreTypes = [], visitor = NOOP_VISITOR, parentTypes = []) {
|
|
1458
|
+
let coreTypesRegistry = Object.create(null), registry = Object.create(null), parentTypesRegistry = Object.create(null), coreTypeNames = coreTypes.map((typeDef) => typeDef.name), parentTypeNames = parentTypes.map((typeDef) => typeDef.name), reservedTypeNames = FUTURE_RESERVED.concat(coreTypeNames, parentTypeNames), typeNames = types.map((typeDef) => typeDef && typeDef.name).filter(Boolean);
|
|
1459
1459
|
coreTypes.forEach((coreType) => {
|
|
1460
1460
|
coreTypesRegistry[coreType.name] = coreType;
|
|
1461
|
+
}), parentTypes.forEach((type) => {
|
|
1462
|
+
parentTypesRegistry[type.name] = {};
|
|
1461
1463
|
}), types.forEach((type, i) => {
|
|
1462
1464
|
registry[type && type.name || `__unnamed_${i}`] = {};
|
|
1463
1465
|
});
|
|
1464
1466
|
function getType(typeName) {
|
|
1465
|
-
return typeName === "type" ? TYPE_TYPE : coreTypesRegistry[typeName] || registry[typeName] || null;
|
|
1467
|
+
return typeName === "type" ? TYPE_TYPE : coreTypesRegistry[typeName] || registry[typeName] || parentTypesRegistry[typeName] || null;
|
|
1466
1468
|
}
|
|
1467
1469
|
let duplicateNames = uniq(flatten(getDupes(typeNames)));
|
|
1468
1470
|
function isDuplicate(typeName) {
|
|
1469
1471
|
return duplicateNames.includes(typeName);
|
|
1470
1472
|
}
|
|
1471
1473
|
function getTypeNames() {
|
|
1472
|
-
return typeNames.concat(coreTypeNames);
|
|
1474
|
+
return typeNames.concat(parentTypeNames, coreTypeNames);
|
|
1473
1475
|
}
|
|
1474
1476
|
function isReserved(typeName) {
|
|
1475
1477
|
return typeName === "type" || reservedTypeNames.includes(typeName);
|
|
@@ -1485,16 +1487,18 @@ function traverseSchema(types = [], coreTypes = [], visitor = NOOP_VISITOR) {
|
|
|
1485
1487
|
});
|
|
1486
1488
|
return coreTypes.forEach((coreTypeDef) => {
|
|
1487
1489
|
Object.assign(coreTypesRegistry[coreTypeDef.name], visitType(coreTypeDef));
|
|
1490
|
+
}), parentTypes.forEach((typeDef, i) => {
|
|
1491
|
+
Object.assign(parentTypesRegistry[typeDef.name], visitType(!1)(typeDef, i));
|
|
1488
1492
|
}), types.forEach((typeDef, i) => {
|
|
1489
1493
|
Object.assign(registry[typeDef && typeDef.name || `__unnamed_${i}`], visitType(!0)(typeDef, i));
|
|
1490
1494
|
}), {
|
|
1491
1495
|
get(typeName) {
|
|
1492
|
-
let res = registry[typeName] || coreTypesRegistry[typeName];
|
|
1496
|
+
let res = registry[typeName] || parentTypesRegistry[typeName] || coreTypesRegistry[typeName];
|
|
1493
1497
|
if (res) return res;
|
|
1494
1498
|
throw Error(`No such type: ${typeName}`);
|
|
1495
1499
|
},
|
|
1496
1500
|
has(typeName) {
|
|
1497
|
-
return typeName in registry || typeName in coreTypesRegistry;
|
|
1501
|
+
return typeName in registry || typeName in parentTypesRegistry || typeName in coreTypesRegistry;
|
|
1498
1502
|
},
|
|
1499
1503
|
getTypeNames() {
|
|
1500
1504
|
return Object.keys(registry);
|
|
@@ -1614,8 +1618,8 @@ const coreTypes = [
|
|
|
1614
1618
|
type: "type"
|
|
1615
1619
|
}
|
|
1616
1620
|
], coreTypeNames = coreTypes.map((t) => t.name);
|
|
1617
|
-
function traverseSanitySchema(schemaTypes, visitor) {
|
|
1618
|
-
return traverseSchema(schemaTypes, coreTypes, visitor);
|
|
1621
|
+
function traverseSanitySchema(schemaTypes, parentTypes, visitor) {
|
|
1622
|
+
return traverseSchema(schemaTypes, coreTypes, visitor, parentTypes);
|
|
1619
1623
|
}
|
|
1620
1624
|
function isPrimitiveTypeName(typeName) {
|
|
1621
1625
|
return typeName === "string" || typeName === "number" || typeName === "boolean";
|
|
@@ -1811,11 +1815,8 @@ function validateAnnotations(annotations, visitorContext, problems) {
|
|
|
1811
1815
|
});
|
|
1812
1816
|
}
|
|
1813
1817
|
function validateMembers(members, visitorContext, problems) {
|
|
1814
|
-
if (!Array.isArray(members)) {
|
|
1815
|
-
|
|
1816
|
-
return;
|
|
1817
|
-
}
|
|
1818
|
-
return members.map((member) => {
|
|
1818
|
+
if (!Array.isArray(members)) problems.push(error(`"of" declaration should be an array, got ${getTypeOf(members)}`));
|
|
1819
|
+
else return members.map((member) => {
|
|
1819
1820
|
let { _problems } = visitorContext.visit(member, visitorContext);
|
|
1820
1821
|
if (member.type === "object" && member.name && visitorContext.getType(member.name)) return {
|
|
1821
1822
|
...member,
|
|
@@ -2122,8 +2123,8 @@ function combine(...visitors) {
|
|
|
2122
2123
|
/**
|
|
2123
2124
|
* @internal
|
|
2124
2125
|
*/
|
|
2125
|
-
function validateSchema(schemaTypes, { transformTypeVisitors = (visitors) => visitors, transformCommonVisitors = (visitors) => visitors } = {}) {
|
|
2126
|
-
return traverseSanitySchema(schemaTypes, (schemaDef, visitorContext) => {
|
|
2126
|
+
function validateSchema(schemaTypes, { parentTypes = [], transformTypeVisitors = (visitors) => visitors, transformCommonVisitors = (visitors) => visitors } = {}) {
|
|
2127
|
+
return traverseSanitySchema(schemaTypes, parentTypes, (schemaDef, visitorContext) => {
|
|
2127
2128
|
let typeVisitor = schemaDef && schemaDef.type && transformTypeVisitors(typeVisitors)[schemaDef.type] || getNoopVisitor(visitorContext), commonVisitors = transformCommonVisitors([common_default]);
|
|
2128
2129
|
return visitorContext.isRoot ? combine(rootType_default, ...commonVisitors, typeVisitor)(schemaDef, visitorContext) : combine(...commonVisitors, typeVisitor)(schemaDef, visitorContext);
|
|
2129
2130
|
});
|
|
@@ -2141,7 +2142,7 @@ const builtinSchema = Schema$1.compile({
|
|
|
2141
2142
|
throw Error("This validator is unavailable because it was reconstructed from a serialized schema manifest");
|
|
2142
2143
|
};
|
|
2143
2144
|
function createSchemaFromManifestTypes(schemaDef) {
|
|
2144
|
-
let problems = groupProblems(validateSchema(schemaDef.types).getTypes()).filter((group) => group.problems.some((problem) => problem.severity === "error"));
|
|
2145
|
+
let problems = groupProblems(validateSchema(schemaDef.types, { parentTypes: builtinTypes }).getTypes()).filter((group) => group.problems.some((problem) => problem.severity === "error"));
|
|
2145
2146
|
if (problems.length > 0) throw new ValidationError(problems);
|
|
2146
2147
|
return Schema$1.compile({
|
|
2147
2148
|
name: schemaDef.name,
|
|
@@ -2156,12 +2157,11 @@ function coerceType(obj) {
|
|
|
2156
2157
|
}
|
|
2157
2158
|
function traverse(obj) {
|
|
2158
2159
|
if (isObject(obj)) {
|
|
2159
|
-
if (Array.isArray(obj))
|
|
2160
|
-
|
|
2161
|
-
|
|
2160
|
+
if (Array.isArray(obj)) obj.forEach(traverse);
|
|
2161
|
+
else {
|
|
2162
|
+
for (let v of Object.values(obj)) traverse(v);
|
|
2163
|
+
coerceValidation(obj);
|
|
2162
2164
|
}
|
|
2163
|
-
for (let v of Object.values(obj)) traverse(v);
|
|
2164
|
-
coerceValidation(obj);
|
|
2165
2165
|
}
|
|
2166
2166
|
}
|
|
2167
2167
|
function coerceValidation(val) {
|
|
@@ -2539,15 +2539,12 @@ function parseWorkspaceSchemaId(id, errors) {
|
|
|
2539
2539
|
return;
|
|
2540
2540
|
}
|
|
2541
2541
|
let [, workspace] = trimmedId.match(taggedIdPattern) ?? trimmedId.match(baseIdPattern) ?? [];
|
|
2542
|
-
if (!workspace)
|
|
2543
|
-
|
|
2544
|
-
|
|
2545
|
-
|
|
2546
|
-
|
|
2547
|
-
|
|
2548
|
-
return;
|
|
2549
|
-
}
|
|
2550
|
-
return {
|
|
2542
|
+
if (!workspace) errors.push([
|
|
2543
|
+
`id must either match ${SANITY_WORKSPACE_SCHEMA_ID_PREFIX}.<workspaceName> `,
|
|
2544
|
+
`or ${SANITY_WORKSPACE_SCHEMA_ID_PREFIX}.<workspaceName>.tag.<tag> but found: "${trimmedId}". `,
|
|
2545
|
+
`Note that workspace name characters not in [${validForNamesChars}] has to be replaced with _ for schema id.`
|
|
2546
|
+
].join(""));
|
|
2547
|
+
else return {
|
|
2551
2548
|
schemaId: trimmedId,
|
|
2552
2549
|
workspace
|
|
2553
2550
|
};
|
|
@@ -2612,21 +2609,11 @@ function extractSchema(schemaDef, extractOptions = {}) {
|
|
|
2612
2609
|
let inlineFields = /* @__PURE__ */ new Set(), documentTypes = /* @__PURE__ */ new Map(), schema = [], hoistedRefMap = /* @__PURE__ */ new Map(), { sortedSchemaTypeNames, repeated } = sortByDependencies(schemaDef);
|
|
2613
2610
|
repeated.forEach((key, objectField) => {
|
|
2614
2611
|
let base = convertSchemaType(objectField.type);
|
|
2615
|
-
|
|
2616
|
-
|
|
2617
|
-
|
|
2618
|
-
|
|
2619
|
-
|
|
2620
|
-
if (base.type === "unknown") {
|
|
2621
|
-
repeated.delete(objectField);
|
|
2622
|
-
return;
|
|
2623
|
-
}
|
|
2624
|
-
schema.push({
|
|
2625
|
-
type: "type",
|
|
2626
|
-
name: key,
|
|
2627
|
-
value: base
|
|
2628
|
-
});
|
|
2629
|
-
}
|
|
2612
|
+
base !== null && (base.type === "inline" || base.type === "unknown" ? repeated.delete(objectField) : schema.push({
|
|
2613
|
+
type: "type",
|
|
2614
|
+
name: key,
|
|
2615
|
+
value: base
|
|
2616
|
+
}));
|
|
2630
2617
|
}), sortedSchemaTypeNames.forEach((typeName) => {
|
|
2631
2618
|
let schemaType = schemaDef.get(typeName);
|
|
2632
2619
|
if (schemaType === void 0) return;
|
|
@@ -2992,7 +2979,7 @@ function validateComponents(typeDef) {
|
|
|
2992
2979
|
}
|
|
2993
2980
|
function validateOptions(typeDef) {
|
|
2994
2981
|
let problems = [];
|
|
2995
|
-
return !typeDef.options || typeof typeDef.options != "object"
|
|
2982
|
+
return !typeDef.options || typeof typeDef.options != "object" || (problems.push(...validateCommonOptions(typeDef.options)), problems.push(...validateAssetSources(typeDef.options)), problems.push(...validateOtherFunctionOptions(typeDef.options))), problems;
|
|
2996
2983
|
}
|
|
2997
2984
|
function validateCommonOptions(options) {
|
|
2998
2985
|
let problems = [];
|
|
@@ -3041,7 +3028,7 @@ function validateGroups(groups) {
|
|
|
3041
3028
|
}
|
|
3042
3029
|
function validateBlockSpecific(typeDef) {
|
|
3043
3030
|
let problems = [];
|
|
3044
|
-
return typeDef.type === "block"
|
|
3031
|
+
return typeDef.type === "block" && (problems.push(...validateBlockStyles(typeDef.styles)), problems.push(...validateBlockMarks(typeDef.marks))), problems;
|
|
3045
3032
|
}
|
|
3046
3033
|
function validateBlockStyles(styles) {
|
|
3047
3034
|
let problems = [];
|