@sanity/schema 5.21.0-next.9 → 5.21.1-next.3

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.
@@ -1,6 +1,6 @@
1
1
  import { a as SchemaValidationResult, i as ProblemPathTypeSegment, l as Schema$1, n as ProblemPathPropertySegment, o as TypeWithProblems, r as ProblemPathSegment, s as _FIXME_, t as ProblemPath } from "./_chunks-dts/typedefs.js";
2
2
  import { SetSynchronization, SynchronizationRequest, SynchronizationResult } from "@sanity/descriptors";
3
- import * as _sanity_types0 from "@sanity/types";
3
+ import * as _$_sanity_types0 from "@sanity/types";
4
4
  import { SanityDocumentLike, Schema, SchemaType, SchemaTypeDefinition, SchemaValidationProblem, SchemaValidationProblemGroup } from "@sanity/types";
5
5
  import { ComponentType, ReactNode } from "react";
6
6
  import { SchemaType as SchemaType$1 } from "groq-js";
@@ -233,24 +233,24 @@ declare const builtinTypes: ({
233
233
  type: string;
234
234
  title: string;
235
235
  readOnly: boolean;
236
+ fieldset?: undefined;
236
237
  validation?: undefined;
237
238
  hidden?: undefined;
238
- fieldset?: undefined;
239
239
  } | {
240
240
  name: string;
241
241
  type: string;
242
242
  title: string;
243
- validation?: undefined;
244
- hidden?: undefined;
245
243
  readOnly?: undefined;
246
244
  fieldset?: undefined;
245
+ validation?: undefined;
246
+ hidden?: undefined;
247
247
  } | {
248
248
  name: string;
249
249
  type: string;
250
250
  title: string;
251
251
  readOnly: boolean;
252
252
  fieldset: string;
253
- validation: (Rule: _sanity_types0.Rule) => _sanity_types0.Rule;
253
+ validation: (Rule: _$_sanity_types0.Rule) => _$_sanity_types0.Rule;
254
254
  hidden?: undefined;
255
255
  } | {
256
256
  name: string;
@@ -258,8 +258,8 @@ declare const builtinTypes: ({
258
258
  readOnly: boolean;
259
259
  hidden: boolean;
260
260
  fieldset: string;
261
- validation?: undefined;
262
261
  title?: undefined;
262
+ validation?: undefined;
263
263
  } | {
264
264
  name: string;
265
265
  type: string;
@@ -312,41 +312,41 @@ declare const builtinTypes: ({
312
312
  type: string;
313
313
  title: string;
314
314
  readOnly: boolean;
315
+ fieldset?: undefined;
315
316
  validation?: undefined;
316
317
  hidden?: undefined;
317
- fieldset?: undefined;
318
318
  } | {
319
319
  name: string;
320
320
  type: string;
321
321
  title: string;
322
- validation?: undefined;
323
- hidden?: undefined;
324
322
  readOnly?: undefined;
325
323
  fieldset?: undefined;
324
+ validation?: undefined;
325
+ hidden?: undefined;
326
326
  } | {
327
327
  name: string;
328
328
  type: string;
329
329
  title: string;
330
330
  readOnly: boolean;
331
331
  fieldset: string;
332
- validation: (Rule: _sanity_types0.Rule) => _sanity_types0.Rule;
332
+ validation: (Rule: _$_sanity_types0.Rule) => _$_sanity_types0.Rule;
333
333
  hidden?: undefined;
334
334
  } | {
335
- validation?: undefined;
336
335
  name: string;
337
336
  type: string;
338
337
  readOnly: boolean;
339
338
  hidden: boolean;
340
339
  fieldset: string;
341
340
  title?: undefined;
342
- } | {
343
341
  validation?: undefined;
344
- hidden?: undefined;
342
+ } | {
345
343
  name: string;
346
344
  type: string;
347
345
  title: string;
348
346
  readOnly: boolean;
349
347
  fieldset: string;
348
+ validation?: undefined;
349
+ hidden?: undefined;
350
350
  })[];
351
351
  preview: {
352
352
  select: {
@@ -356,7 +356,7 @@ declare const builtinTypes: ({
356
356
  size: string;
357
357
  media: string;
358
358
  };
359
- prepare(doc: Partial<_sanity_types0.SanityDocument>): {
359
+ prepare(doc: Partial<_$_sanity_types0.SanityDocument>): {
360
360
  title: {};
361
361
  media: {
362
362
  asset: {
@@ -382,7 +382,7 @@ declare const builtinTypes: ({
382
382
  fields: {
383
383
  name: string;
384
384
  type: string;
385
- validation: (Rule: _sanity_types0.Rule) => _sanity_types0.Rule;
385
+ validation: (Rule: _$_sanity_types0.Rule) => _$_sanity_types0.Rule;
386
386
  }[];
387
387
  } | {
388
388
  name: string;
package/lib/_internal.js CHANGED
@@ -107,8 +107,11 @@ class DescriptorConverter {
107
107
  }), schema.parent && builder.addSet(await this.get(schema.parent, { scheduler })), value = builder.build("sanity.schema.registry"), this.cache.set(schema, value), idleScheduler && idleScheduler.end(), value;
108
108
  }
109
109
  }
110
+ function getOwnProps(schemaType) {
111
+ return OWN_PROPS_NAME in schemaType ? schemaType[OWN_PROPS_NAME] : schemaType;
112
+ }
110
113
  function convertCommonTypeDef(schemaType, path, opts) {
111
- const ownProps = OWN_PROPS_NAME in schemaType ? schemaType[OWN_PROPS_NAME] : schemaType;
114
+ const ownProps = getOwnProps(schemaType);
112
115
  let fields;
113
116
  Array.isArray(ownProps.fields) && (fields = ownProps.fields.map((field) => {
114
117
  const fieldPath = `${path}.${field.name}`, value = opts.fields.get(field);
@@ -152,7 +155,7 @@ function convertCommonTypeDef(schemaType, path, opts) {
152
155
  })
153
156
  )
154
157
  ));
155
- const reason = ownProps.deprecated?.reason;
158
+ const reason = isObject(ownProps.deprecated) && "reason" in ownProps.deprecated ? ownProps.deprecated.reason : void 0;
156
159
  let orderings;
157
160
  return Array.isArray(ownProps.orderings) && (orderings = ownProps.orderings.map(maybeOrdering).filter((o) => o !== void 0)), {
158
161
  title: maybeString(ownProps.title),
@@ -200,15 +203,18 @@ function convertTypeDef(schemaType, path, opts) {
200
203
  };
201
204
  case "reference":
202
205
  case "globalDocumentReference":
203
- case "crossDatasetReference":
206
+ case "crossDatasetReference": {
207
+ const ownProps = getOwnProps(schemaType);
204
208
  return {
205
209
  extends: schemaType.type.name,
206
210
  to: filterStringKey(
207
211
  "name",
208
212
  schemaType.to.map((toType) => ({ name: toType.name || toType.type?.name || toType.type }))
209
213
  ),
214
+ weak: maybeTrue(ownProps.weak),
210
215
  ...common2
211
216
  };
217
+ }
212
218
  default:
213
219
  return { extends: schemaType.type.name, ...common2 };
214
220
  }
@@ -283,7 +289,7 @@ function maybeValidations(obj) {
283
289
  case "url":
284
290
  impliedRules.push({
285
291
  type: "uri",
286
- allowRelative: !1
292
+ scheme: ["http", "https"]
287
293
  });
288
294
  break;
289
295
  case "slug":
@@ -314,7 +320,9 @@ function maybeValidations(obj) {
314
320
  const validation = maybeValidation(rule);
315
321
  if (validation === void 0)
316
322
  continue;
317
- const rulesToAdd = impliedRules.filter((ir) => !validation.rules.some((r) => isEqual(r, ir)));
323
+ const rulesToAdd = impliedRules.filter(
324
+ (ir) => !validation.rules.some((r) => r.type === ir.type)
325
+ );
318
326
  rulesToAdd.length > 0 && validation.rules.unshift(...rulesToAdd), !validations.some((v) => isEqual(v, validation)) && validations.push(validation);
319
327
  }
320
328
  return validations.length > 0 ? validations : void 0;
@@ -427,10 +435,13 @@ function convertRuleSpec(spec, optional) {
427
435
  }
428
436
  return;
429
437
  case "uri": {
430
- const allowRelative = isObject(constraint) && "options" in constraint && isObject(constraint.options) && "allowRelative" in constraint.options ? maybeBoolean(constraint.options.allowRelative) : void 0;
438
+ const options = isObject(constraint) && "options" in constraint && isObject(constraint.options) ? constraint.options : void 0, scheme = options && "scheme" in options && Array.isArray(options.scheme) && options.scheme.length > 0 ? options.scheme.map((s) => convertSchemeValue(s)).filter((s) => s !== void 0) : void 0, allowRelative = options && "allowRelative" in options ? maybeBoolean(options.allowRelative) : void 0, relativeOnly = options && "relativeOnly" in options ? maybeBoolean(options.relativeOnly) : void 0, allowCredentials = options && "allowCredentials" in options ? maybeBoolean(options.allowCredentials) : void 0;
431
439
  return {
432
440
  type: "uri",
433
- ...allowRelative !== void 0 && { allowRelative }
441
+ ...scheme !== void 0 && scheme.length > 0 && { scheme },
442
+ ...allowRelative && { allowRelative },
443
+ ...relativeOnly && { relativeOnly },
444
+ ...allowCredentials && { allowCredentials }
434
445
  };
435
446
  }
436
447
  case "custom":
@@ -451,6 +462,18 @@ function convertConstraintValue(constraint) {
451
462
  path: Array.isArray(constraint.path) ? constraint.path : [constraint.path]
452
463
  } : String(constraint);
453
464
  }
465
+ const REGEX_META = /[.*+?^${}()|[\]\\]/;
466
+ function convertSchemeValue(val) {
467
+ if (val instanceof RegExp) {
468
+ const { source, flags } = val;
469
+ if (flags)
470
+ return { type: "regex", pattern: `(?${flags})${source}` };
471
+ const match = source.match(/^\^(.*)\$$/);
472
+ return match && !REGEX_META.test(match[1]) ? match[1] : { type: "regex", pattern: source };
473
+ }
474
+ if (typeof val == "string")
475
+ return val;
476
+ }
454
477
  function maybeBoolean(val) {
455
478
  if (typeof val == "boolean")
456
479
  return val;
@@ -2334,8 +2357,10 @@ function applyRuleSpec(rule, ruleSpec) {
2334
2357
  }
2335
2358
  break;
2336
2359
  case "uri":
2337
- if (isObject(constraint) && "options" in constraint)
2338
- return rule.uri(constraint.options);
2360
+ if (isObject(constraint) && "options" in constraint && isObject(constraint.options)) {
2361
+ const uriOptions = { ...constraint.options };
2362
+ return "scheme" in uriOptions && Array.isArray(uriOptions.scheme) && (uriOptions.scheme = uriOptions.scheme.map((s) => typeof s == "string" ? /^\/(.*)\/([gimuy]*)$/.test(s) ? stringToRegExp(s) : s : isRegexPattern(s) ? regexPatternToRegExp(s) : s)), rule.uri(uriOptions);
2363
+ }
2339
2364
  break;
2340
2365
  case "assetRequired":
2341
2366
  return rule.assetRequired();
@@ -2359,6 +2384,13 @@ function stringToRegExp(str) {
2359
2384
  const match = str.match(/^\/(.*)\/([gimuy]*)$/);
2360
2385
  return match ? new RegExp(match[1], match[2]) : new RegExp(str);
2361
2386
  }
2387
+ function isRegexPattern(val) {
2388
+ return typeof val == "object" && val !== null && "type" in val && val.type === "regex" && "pattern" in val && typeof val.pattern == "string";
2389
+ }
2390
+ function regexPatternToRegExp(val) {
2391
+ const match = val.pattern.match(/^\(\?([a-z]+)\)(.*)$/);
2392
+ return match ? new RegExp(match[2], match[1]) : new RegExp(val.pattern);
2393
+ }
2362
2394
  const DEFAULT_IMAGE_FIELDS = ["asset", "hotspot", "crop", "media"], DEFAULT_FILE_FIELDS = ["asset", "media"], DEFAULT_GEOPOINT_FIELDS = ["lat", "lng", "alt"], DEFAULT_SLUG_FIELDS = ["current", "source"];
2363
2395
  function getCustomFields(type) {
2364
2396
  const fields = type.fieldsets ? type.fieldsets.flatMap((fs) => fs.single ? fs.field : fs.fields.map((field) => ({
@@ -2827,7 +2859,7 @@ function extractSchema(schemaDef, extractOptions = {}) {
2827
2859
  let typeName;
2828
2860
  if (schemaType.type ? typeName = schemaType.type.name : "jsonType" in schemaType && (typeName = schemaType.jsonType), typeName === "document" && isObjectType(schemaType)) {
2829
2861
  const defaultAttributes = documentDefaultFields(schemaType.name), object2 = createObject(schemaType);
2830
- return object2.type === "unknown" ? null : {
2862
+ return {
2831
2863
  name: schemaType.name,
2832
2864
  type: "document",
2833
2865
  attributes: {
@@ -2906,7 +2938,7 @@ function extractSchema(schemaDef, extractOptions = {}) {
2906
2938
  optional
2907
2939
  };
2908
2940
  }
2909
- return extractOptions.enforceRequiredFields && hasAssetRequired(schemaType.validation) && attributes.asset && (attributes.asset.optional = !1), Object.keys(attributes).length === 0 ? { type: "unknown" } : (schemaType.type?.name !== "document" && schemaType.name !== "object" && (attributes._type = {
2941
+ return extractOptions.enforceRequiredFields && hasAssetRequired(schemaType.validation) && attributes.asset && (attributes.asset.optional = !1), schemaType.type?.name !== "document" && schemaType.name !== "object" && (attributes._type = {
2910
2942
  type: "objectAttribute",
2911
2943
  value: {
2912
2944
  type: "string",
@@ -2915,7 +2947,7 @@ function extractSchema(schemaDef, extractOptions = {}) {
2915
2947
  }), {
2916
2948
  type: "object",
2917
2949
  attributes
2918
- });
2950
+ };
2919
2951
  }
2920
2952
  function createArray(arraySchemaType) {
2921
2953
  const of = [];