@sanity/schema 5.21.0-next.22 → 5.21.0-next.23

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 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) => ({