@sanity/schema 6.21.0-next.1 → 6.21.0-next.106

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 "./typedefs-LNWlFyVi.js";
2
2
  import { SetSynchronization, SynchronizationRequest, SynchronizationResult } from "@sanity/descriptors";
3
- import { SanityDocumentLike, Schema, SchemaType, SchemaTypeDefinition, SchemaValidationProblem, SchemaValidationProblemGroup } from "@sanity/types";
3
+ import { CustomValidator, MediaValidator, SanityDocumentLike, Schema, SchemaType, SchemaTypeDefinition, SchemaValidationProblem, SchemaValidationProblemGroup } from "@sanity/types";
4
4
  import { RequestOptions } from "get-it";
5
5
  import { SchemaType as SchemaType$1 } from "groq-js";
6
6
  import { ComponentType, ReactNode } from "react";
@@ -192,6 +192,11 @@ export declare function prepareSchemaUpload(schema: Schema, options: PrepareSche
192
192
  */
193
193
  export declare function uploadSchema(schema: Schema, options: UploadSchemaOptions): Promise<string>;
194
194
  export declare const isActionEnabled: (schemaType: SchemaType, action: string) => boolean;
195
+ type ValidationCallback = CustomValidator | MediaValidator;
196
+ /** @internal */
197
+ export declare function markInternalValidator<T extends ValidationCallback>(validator: T): T;
198
+ /** @internal */
199
+ export declare function isInternalValidator(validator: unknown): boolean;
195
200
  export declare const DEFAULT_MAX_FIELD_DEPTH = 5;
196
201
  export declare function resolveSearchConfigForBaseFieldPaths(type: any, maxDepth?: number): any;
197
202
  /**
@@ -199,10 +204,26 @@ export declare function resolveSearchConfigForBaseFieldPaths(type: any, maxDepth
199
204
  */
200
205
  export declare function resolveSearchConfig(type: any, maxDepth?: number): any;
201
206
  export declare const ALL_FIELDS_GROUP_NAME = "all-fields";
207
+ interface CreateSchemaFromManifestTypesOptions {
208
+ /**
209
+ * Clone manifest types before converting serialized validation rules.
210
+ *
211
+ * Set this to `false` only when the caller owns the input and accepts that it will be mutated.
212
+ * Defaults to `true`.
213
+ */
214
+ cloneTypes?: boolean;
215
+ /**
216
+ * Validate manifest types before compiling them.
217
+ *
218
+ * Set this to `false` only for manifests that have already passed schema validation.
219
+ * Defaults to `true`.
220
+ */
221
+ validate?: boolean;
222
+ }
202
223
  export declare function createSchemaFromManifestTypes(schemaDef: {
203
224
  name: string;
204
225
  types: unknown[];
205
- }): Schema$1;
226
+ }, options?: CreateSchemaFromManifestTypesOptions): Schema$1;
206
227
  interface MediaLibraryConfig {
207
228
  enabled?: boolean;
208
229
  libraryId?: string;
@@ -387,17 +408,17 @@ export declare const builtinTypes: ({
387
408
  type: string;
388
409
  title: string;
389
410
  readOnly: boolean;
411
+ fieldset?: undefined;
390
412
  validation?: undefined;
391
413
  hidden?: undefined;
392
- fieldset?: undefined;
393
414
  } | {
394
415
  name: string;
395
416
  type: string;
396
417
  title: string;
418
+ fieldset?: undefined;
419
+ readOnly?: undefined;
397
420
  validation?: undefined;
398
421
  hidden?: undefined;
399
- readOnly?: undefined;
400
- fieldset?: undefined;
401
422
  } | {
402
423
  name: string;
403
424
  type: string;
@@ -412,8 +433,8 @@ export declare const builtinTypes: ({
412
433
  readOnly: boolean;
413
434
  hidden: boolean;
414
435
  fieldset: string;
415
- validation?: undefined;
416
436
  title?: undefined;
437
+ validation?: undefined;
417
438
  } | {
418
439
  name: string;
419
440
  type: string;
@@ -466,17 +487,17 @@ export declare const builtinTypes: ({
466
487
  type: string;
467
488
  title: string;
468
489
  readOnly: boolean;
490
+ fieldset?: undefined;
469
491
  validation?: undefined;
470
492
  hidden?: undefined;
471
- fieldset?: undefined;
472
493
  } | {
473
494
  name: string;
474
495
  type: string;
475
496
  title: string;
497
+ fieldset?: undefined;
498
+ readOnly?: undefined;
476
499
  validation?: undefined;
477
500
  hidden?: undefined;
478
- readOnly?: undefined;
479
- fieldset?: undefined;
480
501
  } | {
481
502
  name: string;
482
503
  type: string;
@@ -486,21 +507,21 @@ export declare const builtinTypes: ({
486
507
  validation: (Rule: import("@sanity/types").Rule) => import("@sanity/types").Rule;
487
508
  hidden?: undefined;
488
509
  } | {
489
- validation?: undefined;
490
510
  name: string;
491
511
  type: string;
492
512
  readOnly: boolean;
493
513
  hidden: boolean;
494
514
  fieldset: string;
495
515
  title?: undefined;
496
- } | {
497
516
  validation?: undefined;
498
- hidden?: undefined;
517
+ } | {
499
518
  name: string;
500
519
  type: string;
501
520
  title: string;
502
521
  readOnly: boolean;
503
522
  fieldset: string;
523
+ validation?: undefined;
524
+ hidden?: undefined;
504
525
  })[];
505
526
  preview: {
506
527
  select: {
@@ -550,17 +571,17 @@ export declare const builtinTypes: ({
550
571
  };
551
572
  }[];
552
573
  fields: ({
553
- title?: undefined;
554
- readOnly?: undefined;
555
- fieldset?: undefined;
556
574
  name: string;
557
575
  type: string;
558
- } | {
576
+ fieldset?: undefined;
577
+ title?: undefined;
559
578
  readOnly?: undefined;
579
+ } | {
560
580
  name: string;
561
581
  title: string;
562
582
  type: string;
563
583
  fieldset: string;
584
+ readOnly?: undefined;
564
585
  } | {
565
586
  fieldset?: undefined;
566
587
  name: string;
@@ -634,5 +655,5 @@ export declare class ValidationError extends Error {
634
655
  problems: SchemaValidationProblemGroup[];
635
656
  constructor(problems: SchemaValidationProblemGroup[]);
636
657
  }
637
- export type { CreateManifest, CreateWorkspaceManifest, DefaultWorkspaceSchemaId, DescriptorRequester, _FIXME_ as FIXME, IconResolver, ManifestSchemaType, ManifestWorkspaceFile, ParsedWorkspaceSchemaId, PrepareSchemaUploadOptions, PreparedSchemaUpload, SchemaValidationResult as Problem, SchemaValidationResult as ValidationResult, ProblemPath, ProblemPathPropertySegment, ProblemPathSegment, ProblemPathTypeSegment, SchemaSynchronizationRequest, SchemaSynchronizationResult, StoredWorkspaceSchema, TypeWithProblems, UploadSchemaOptions, UploadSchemaPhase, WorkspaceSchemaId };
658
+ export type { CreateManifest, CreateSchemaFromManifestTypesOptions, CreateWorkspaceManifest, DefaultWorkspaceSchemaId, DescriptorRequester, _FIXME_ as FIXME, IconResolver, ManifestSchemaType, ManifestWorkspaceFile, ParsedWorkspaceSchemaId, PrepareSchemaUploadOptions, PreparedSchemaUpload, SchemaValidationResult as Problem, SchemaValidationResult as ValidationResult, ProblemPath, ProblemPathPropertySegment, ProblemPathSegment, ProblemPathTypeSegment, SchemaSynchronizationRequest, SchemaSynchronizationResult, StoredWorkspaceSchema, TypeWithProblems, UploadSchemaOptions, UploadSchemaPhase, WorkspaceSchemaId };
638
659
  //# sourceMappingURL=_internal.d.ts.map
package/lib/_internal.js CHANGED
@@ -2,7 +2,6 @@ import { c as resolveSearchConfig, d as OWN_PROPS_NAME, f as Rule, l as resolveS
2
2
  import { SetBuilder, processSetSynchronization } from "@sanity/descriptors";
3
3
  import { isSpanSchemaType } from "@sanity/types";
4
4
  import { dequal } from "dequal/lite";
5
- import cloneDeep from "lodash-es/cloneDeep.js";
6
5
  import { HttpError, createRequester } from "get-it";
7
6
  import { retry } from "get-it/middleware";
8
7
  import "@sanity/generate-help-url";
@@ -13,6 +12,15 @@ import get from "lodash-es/get.js";
13
12
  import humanizeList from "humanize-list";
14
13
  import leven from "leven";
15
14
  import { createReferenceTypeNode } from "groq-js";
15
+ const internalValidators = /* @__PURE__ */ new WeakSet();
16
+ /** @internal */
17
+ function markInternalValidator(validator) {
18
+ return internalValidators.add(validator), validator;
19
+ }
20
+ /** @internal */
21
+ function isInternalValidator(validator) {
22
+ return typeof validator == "function" && internalValidators.has(validator);
23
+ }
16
24
  const DEFAULT_IMAGE_FIELDS = [
17
25
  "asset",
18
26
  "hotspot",
@@ -380,9 +388,6 @@ function maybeValidations(obj) {
380
388
  scheme: ["http", "https"]
381
389
  });
382
390
  break;
383
- case "slug":
384
- impliedRules.push({ type: "custom" });
385
- break;
386
391
  case "reference":
387
392
  impliedRules.push({ type: "reference" });
388
393
  break;
@@ -525,7 +530,7 @@ function convertRuleSpec(spec, optional) {
525
530
  ...allowCredentials && { allowCredentials }
526
531
  };
527
532
  }
528
- case "custom": return {
533
+ case "custom": return isInternalValidator(constraint) ? void 0 : {
529
534
  type: "custom",
530
535
  ...optional && { optional }
531
536
  };
@@ -2141,18 +2146,20 @@ const builtinSchema = Schema$1.compile({
2141
2146
  }), unavailableValidator = () => {
2142
2147
  throw Error("This validator is unavailable because it was reconstructed from a serialized schema manifest");
2143
2148
  };
2144
- function createSchemaFromManifestTypes(schemaDef) {
2145
- let problems = groupProblems(validateSchema(schemaDef.types, { parentTypes: builtinTypes }).getTypes()).filter((group) => group.problems.some((problem) => problem.severity === "error"));
2146
- if (problems.length > 0) throw new ValidationError(problems);
2149
+ function createSchemaFromManifestTypes(schemaDef, options = {}) {
2150
+ if (options.validate !== !1) {
2151
+ let problems = groupProblems(validateSchema(schemaDef.types, { parentTypes: builtinTypes }).getTypes()).filter((group) => group.problems.some((problem) => problem.severity === "error"));
2152
+ if (problems.length > 0) throw new ValidationError(problems);
2153
+ }
2147
2154
  return Schema$1.compile({
2148
2155
  name: schemaDef.name,
2149
- types: schemaDef.types.map(coerceType).filter(Boolean),
2156
+ types: schemaDef.types.map((type) => coerceType(type, options.cloneTypes !== !1)).filter(Boolean),
2150
2157
  parent: builtinSchema
2151
2158
  });
2152
2159
  }
2153
- function coerceType(obj) {
2160
+ function coerceType(obj, clone) {
2154
2161
  if (!isObject(obj)) return;
2155
- let typ = cloneDeep(obj);
2162
+ let typ = clone ? structuredClone(obj) : obj;
2156
2163
  return traverse(typ), typ;
2157
2164
  }
2158
2165
  function traverse(obj) {
@@ -2443,7 +2450,7 @@ function transformValidation(validation) {
2443
2450
  rules: _rules.filter((rule) => {
2444
2451
  if (!("constraint" in rule)) return !1;
2445
2452
  let { flag, constraint } = rule;
2446
- return !disallowedFlags.includes(flag) && !(typeof constraint == "object" && "type" in constraint && disallowedConstraintTypes.includes(constraint.type));
2453
+ return flag === "custom" && isInternalValidator(constraint) || disallowedFlags.includes(flag) ? !1 : !(typeof constraint == "object" && "type" in constraint && disallowedConstraintTypes.includes(constraint.type));
2447
2454
  }).reduce((rules, rule) => {
2448
2455
  let transformedRule = (validationRuleTransformers[rule.flag] ?? ((spec) => retainSerializableProps(spec)))(rule);
2449
2456
  return transformedRule ? [...rules, transformedRule] : rules;
@@ -3098,6 +3105,6 @@ function validateMediaLibraryAssetAspect(maybeAspect) {
3098
3105
  }).getTypes()).map((group) => group.problems.filter(({ severity }) => severity === "error")).filter((problems) => problems.length);
3099
3106
  return [errors.length === 0, errors];
3100
3107
  }
3101
- export { ALL_FIELDS_GROUP_NAME, DEFAULT_MAX_FIELD_DEPTH, DescriptorConverter, ValidationError, builtinTypes, createSchemaFromManifestTypes, createStoredWorkspaceSchemaPayload, extractCreateWorkspaceManifest, extractManifestSchemaTypes, extractSchema, getWorkspaceSchemaId, groupProblems, isActionEnabled, parseWorkspaceSchemaId, prepareSchemaUpload, processSchemaSynchronization, resolveSearchConfig, resolveSearchConfigForBaseFieldPaths, uploadSchema, validForNamesChars, validForNamesPattern, validateMediaLibraryAssetAspect, validateSchema };
3108
+ export { ALL_FIELDS_GROUP_NAME, DEFAULT_MAX_FIELD_DEPTH, DescriptorConverter, ValidationError, builtinTypes, createSchemaFromManifestTypes, createStoredWorkspaceSchemaPayload, extractCreateWorkspaceManifest, extractManifestSchemaTypes, extractSchema, getWorkspaceSchemaId, groupProblems, isActionEnabled, isInternalValidator, markInternalValidator, parseWorkspaceSchemaId, prepareSchemaUpload, processSchemaSynchronization, resolveSearchConfig, resolveSearchConfigForBaseFieldPaths, uploadSchema, validForNamesChars, validForNamesPattern, validateMediaLibraryAssetAspect, validateSchema };
3102
3109
 
3103
3110
  //# sourceMappingURL=_internal.js.map