@sanity/schema 6.16.0 → 6.17.0-next.102
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 +34 -5
- package/lib/_internal.js.map +1 -1
- package/package.json +5 -7
package/lib/_internal.js
CHANGED
|
@@ -17,7 +17,6 @@ import uniq from "lodash-es/uniq.js";
|
|
|
17
17
|
import humanizeList from "humanize-list";
|
|
18
18
|
import partition from "lodash-es/partition.js";
|
|
19
19
|
import leven from "leven";
|
|
20
|
-
import inspect from "object-inspect";
|
|
21
20
|
import { createReferenceTypeNode } from "groq-js";
|
|
22
21
|
const DEFAULT_IMAGE_FIELDS = [
|
|
23
22
|
"asset",
|
|
@@ -1360,6 +1359,7 @@ const ACTIONS_FLAG = "__experimental_actions", DEFAULT_ACTIONS = [
|
|
|
1360
1359
|
OBJECT_FIELD_NAME_INVALID: "schema-object-fields-invalid",
|
|
1361
1360
|
OBJECT_FIELD_DEFINITION_INVALID_TYPE: "schema-object-fields-invalid",
|
|
1362
1361
|
ARRAY_PREDEFINED_CHOICES_INVALID: "schema-predefined-choices-invalid",
|
|
1362
|
+
ARRAY_COLLAPSE_ITEMS_AFTER_INVALID: "schema-array-collapse-items-after-invalid",
|
|
1363
1363
|
ARRAY_OF_ARRAY: "schema-array-of-array",
|
|
1364
1364
|
ARRAY_OF_INVALID: "schema-array-of-invalid",
|
|
1365
1365
|
ARRAY_OF_NOT_UNIQUE: "schema-array-of-invalid",
|
|
@@ -1675,6 +1675,8 @@ var array_default = (typeDef, visitorContext) => {
|
|
|
1675
1675
|
problems.push(warning(`Array cannot contain multiple members with JSON type "${jsonType}" (${humanizeList(typeNames.map(quote$2))}) as there is no way to distinguish between them`, HELP_IDS.ARRAY_OF_DUPLICATE_PRIMITIVE_JSON_TYPE));
|
|
1676
1676
|
}
|
|
1677
1677
|
}
|
|
1678
|
+
let collapseItemsAfter = typeDef?.options?.collapseItemsAfter;
|
|
1679
|
+
collapseItemsAfter !== void 0 && collapseItemsAfter !== !1 && (typeof collapseItemsAfter == "number" && Number.isInteger(collapseItemsAfter) && collapseItemsAfter > 0 || problems.push(error(`The array type's "options.collapseItemsAfter" must be a positive integer, or false to always render every item. Found ${format(collapseItemsAfter)}`, HELP_IDS.ARRAY_COLLAPSE_ITEMS_AFTER_INVALID)));
|
|
1678
1680
|
let list = typeDef?.options?.list;
|
|
1679
1681
|
return !isMixedArray && Array.isArray(list) && (primitiveTypes.length > 0 ? list.forEach((option) => {
|
|
1680
1682
|
let value = option?.value ?? option;
|
|
@@ -1884,7 +1886,34 @@ function getOptionErrors$2(typeDef) {
|
|
|
1884
1886
|
let { options } = typeDef, problems = [];
|
|
1885
1887
|
return problems.push(...["filter", "filterParams"].filter((key) => key in typeDef).map((key) => error(`\`${key}\` is not allowed on a reference type definition - did you mean \`options.${key}\`?`, HELP_IDS.REFERENCE_INVALID_OPTIONS_LOCATION))), options ? isPlainObject(options) ? typeof options.filter == "function" && options.filterParams !== void 0 ? problems.concat(error("`filterParams` cannot be used if `filter` is a function. Either statically define `filter` as a string, or return `params` from the `filter`-function.", HELP_IDS.REFERENCE_INVALID_FILTER_PARAMS_COMBINATION)) : typeof options.filter == "function" || !options.filter && !options.filterParams ? problems : typeof options.filter == "string" ? options.filterParams !== void 0 && !isPlainObject(options.filterParams) ? problems.concat(error("If set, `filterParams` must be an object.")) : options.filterParams ? problems.concat(Object.keys(options.filterParams).filter((key) => key.startsWith("__") || key.startsWith("$")).map((key) => error(`Filter parameter cannot be prefixed with "$" or "__". Got ${key}".`))) : problems : problems.concat(error(`If set, \`filter\` must be a string. Got ${typeof options.filter}`)) : problems.concat(error("The reference type expects `options` to be an object", HELP_IDS.REFERENCE_INVALID_OPTIONS)) : problems;
|
|
1886
1888
|
}
|
|
1887
|
-
|
|
1889
|
+
/** Formats invalid schema values for diagnostics without invoking their serialization hooks. */
|
|
1890
|
+
function inspect(value) {
|
|
1891
|
+
return formatValue(value, []);
|
|
1892
|
+
}
|
|
1893
|
+
function formatValue(value, ancestors) {
|
|
1894
|
+
if (typeof value == "string") return JSON.stringify(value.length > 200 ? `${value.slice(0, 200)}...` : value);
|
|
1895
|
+
if (typeof value == "function") return value.name ? `[Function: ${value.name}]` : "[Function]";
|
|
1896
|
+
if (typeof value == "bigint") return `${value}n`;
|
|
1897
|
+
if (Object.is(value, -0)) return "-0";
|
|
1898
|
+
if (typeof value != "object" || !value) return String(value);
|
|
1899
|
+
if (value instanceof Date) return Date.prototype.toString.call(value);
|
|
1900
|
+
if (value instanceof RegExp) return RegExp.prototype.toString.call(value);
|
|
1901
|
+
if (value instanceof Map) return "[Map]";
|
|
1902
|
+
if (value instanceof Set) return "[Set]";
|
|
1903
|
+
if (ancestors.includes(value)) return "[Circular]";
|
|
1904
|
+
if (ancestors.length >= 3) return Array.isArray(value) ? "[Array]" : "[Object]";
|
|
1905
|
+
let parents = [...ancestors, value];
|
|
1906
|
+
if (Array.isArray(value)) {
|
|
1907
|
+
let entries = Array.from({ length: Math.min(value.length, 10) }, (_, index) => formatProperty(value, String(index), parents));
|
|
1908
|
+
return value.length > 10 && entries.push("..."), entries.length ? `[ ${entries.join(", ")} ]` : "[]";
|
|
1909
|
+
}
|
|
1910
|
+
let keys = Object.keys(value), entries = keys.slice(0, 10).map((key) => `${key.length <= 200 && /^[A-Za-z_$][\w$]*$/.test(key) ? key : formatValue(key, parents)}: ${formatProperty(value, key, parents)}`);
|
|
1911
|
+
return keys.length > 10 && entries.push("..."), entries.length ? `{ ${entries.join(", ")} }` : "{}";
|
|
1912
|
+
}
|
|
1913
|
+
function formatProperty(value, key, ancestors) {
|
|
1914
|
+
let descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
1915
|
+
return descriptor ? "value" in descriptor ? formatValue(descriptor.value, ancestors) : descriptor.get ? "[Getter]" : "[Setter]" : "undefined";
|
|
1916
|
+
}
|
|
1888
1917
|
const REACT_SYM_RE = /^Symbol\(react\..+\)$/;
|
|
1889
1918
|
function isComponentLike(value) {
|
|
1890
1919
|
return typeof value == "function" || typeof value?.$$typeof == "symbol" && REACT_SYM_RE.test(String(value?.$$typeof));
|
|
@@ -1893,14 +1922,14 @@ function validateComponent(typeDef) {
|
|
|
1893
1922
|
let components = "components" in typeDef && typeDef.components;
|
|
1894
1923
|
if (!components) return [];
|
|
1895
1924
|
let warnings = [];
|
|
1896
|
-
return components.input && !isComponentLike(components.input) && warnings.push(warning(`The \`components.input\` property is set but does not appear to be a valid React component (expected a function, but saw ${
|
|
1925
|
+
return components.input && !isComponentLike(components.input) && warnings.push(warning(`The \`components.input\` property is set but does not appear to be a valid React component (expected a function, but saw ${inspect(components.input)}). If you have imported a custom input component, please verify that you have imported the correct named/default export.`)), components.field && !isComponentLike(components.field) && warnings.push(warning(`The \`components.field\` property is set but does not appear to be a valid React component (expected a function, but saw ${inspect(components.field)}). If you have imported a custom field component, please verify that you have imported the correct named/default export.`)), components.item && !isComponentLike(components.item) && warnings.push(warning(`The \`components.item\` property is set but does not appear to be a valid React component (expected a function, but saw ${inspect(components.item)}). If you have imported a custom item component, please verify that you have imported the correct named/default export.`)), components.preview && !isComponentLike(components.preview) && warnings.push(warning(`The \`components.preview\` property is set but does not appear to be a valid React component (expected a function, but saw ${inspect(components.preview)}). If you have imported a custom preview component, please verify that you have imported the correct named/default export.`)), warnings;
|
|
1897
1926
|
}
|
|
1898
1927
|
const VALID_FIELD_RE = /^[A-Za-z]+[0-9A-Za-z_]*$/, CONVENTIONAL_FIELD_RE = /^[A-Za-z_]+[0-9A-Za-z_]*$/;
|
|
1899
1928
|
function validateFieldName(name) {
|
|
1900
|
-
return typeof name == "string" ? name.startsWith("_") ? [error(`Invalid field name "${name}". Field names cannot start with underscores "_" as it's reserved for system fields.`, HELP_IDS.OBJECT_FIELD_NAME_INVALID)] : VALID_FIELD_RE.test(name) ? CONVENTIONAL_FIELD_RE.test(name) ? [] : [warning("Thats an interesting field name for sure! But it is... how to put it... a bit... unconventional? It may be wise to keep special characters out of field names for easier access later on."), HELP_IDS.OBJECT_FIELD_NAME_INVALID] : [error(`Invalid field name: "${name}". Fields can only contain characters from A-Z, numbers and underscores and should not start with a number (must pass the regular expression ${String(VALID_FIELD_RE)}).`, HELP_IDS.OBJECT_FIELD_NAME_INVALID)] : [error(`Field names must be strings. Saw "${
|
|
1929
|
+
return typeof name == "string" ? name.startsWith("_") ? [error(`Invalid field name "${name}". Field names cannot start with underscores "_" as it's reserved for system fields.`, HELP_IDS.OBJECT_FIELD_NAME_INVALID)] : VALID_FIELD_RE.test(name) ? CONVENTIONAL_FIELD_RE.test(name) ? [] : [warning("Thats an interesting field name for sure! But it is... how to put it... a bit... unconventional? It may be wise to keep special characters out of field names for easier access later on."), HELP_IDS.OBJECT_FIELD_NAME_INVALID] : [error(`Invalid field name: "${name}". Fields can only contain characters from A-Z, numbers and underscores and should not start with a number (must pass the regular expression ${String(VALID_FIELD_RE)}).`, HELP_IDS.OBJECT_FIELD_NAME_INVALID)] : [error(`Field names must be strings. Saw "${inspect(name)}"`, HELP_IDS.OBJECT_FIELD_NAME_INVALID)];
|
|
1901
1930
|
}
|
|
1902
1931
|
function validateField(field, visitorContext) {
|
|
1903
|
-
if (!isPlainObject(field)) return [error(`Incorrect type for field definition - should be an object, saw ${
|
|
1932
|
+
if (!isPlainObject(field)) return [error(`Incorrect type for field definition - should be an object, saw ${inspect(field)}`, HELP_IDS.OBJECT_FIELD_DEFINITION_INVALID_TYPE)];
|
|
1904
1933
|
let problems = [];
|
|
1905
1934
|
return problems.push(..."name" in field ? validateFieldName(field.name) : [error("Missing field name", HELP_IDS.OBJECT_FIELD_NAME_INVALID)]), problems.push(...validateComponent(field)), field.type && typeof field.type == "string" && visitorContext.getType(field.type)?.type === "document" && problems.push(warning(`The type "${field.type}" is a document type and should not be used as a field type directly. Use a "reference" if you want to create a link to the document, or use "object" if you want to embed fields inline.`, HELP_IDS.FIELD_TYPE_IS_DOCUMENT)), field.title && typeof field.title != "string" && problems.push(warning(`Field "${field.name}" has a non-string title. This is known to cause problems and will not be supported in future versions. Please use a string instead.`)), field.description && typeof field.description != "string" && problems.push(warning(`Field "${field.name}" has a non-string description. This is known to cause problems and will not be supported in future versions. Please use a string instead.`)), problems;
|
|
1906
1935
|
}
|