@sanity/blueprints 0.29.0 → 0.31.0

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.
Files changed (38) hide show
  1. package/README.md +11 -9
  2. package/dist/definers/context.d.ts +90 -0
  3. package/dist/definers/context.js +110 -0
  4. package/dist/definers/context.js.map +1 -0
  5. package/dist/definers/functions/durable.d.ts +1 -5
  6. package/dist/definers/functions/durable.js +5 -9
  7. package/dist/definers/functions/durable.js.map +1 -1
  8. package/dist/definers/permissions.d.ts +71 -0
  9. package/dist/definers/permissions.js +95 -0
  10. package/dist/definers/permissions.js.map +1 -0
  11. package/dist/index.d.ts +6 -0
  12. package/dist/index.js +6 -0
  13. package/dist/index.js.map +1 -1
  14. package/dist/types/context.d.ts +102 -0
  15. package/dist/types/context.js +2 -0
  16. package/dist/types/context.js.map +1 -0
  17. package/dist/types/functions/index.d.ts +4 -10
  18. package/dist/types/permissions.d.ts +63 -0
  19. package/dist/types/permissions.js +2 -0
  20. package/dist/types/permissions.js.map +1 -0
  21. package/dist/types/roles.d.ts +1 -1
  22. package/dist/validation/context.d.ts +19 -0
  23. package/dist/validation/context.js +136 -0
  24. package/dist/validation/context.js.map +1 -0
  25. package/dist/validation/permissions.d.ts +15 -0
  26. package/dist/validation/permissions.js +95 -0
  27. package/dist/validation/permissions.js.map +1 -0
  28. package/package.json +1 -1
  29. package/src/definers/context.ts +124 -0
  30. package/src/definers/functions/durable.ts +5 -9
  31. package/src/definers/permissions.ts +107 -0
  32. package/src/index.ts +6 -0
  33. package/src/types/context.ts +109 -0
  34. package/src/types/functions/index.ts +4 -10
  35. package/src/types/permissions.ts +67 -0
  36. package/src/types/roles.ts +1 -1
  37. package/src/validation/context.ts +142 -0
  38. package/src/validation/permissions.ts +100 -0
@@ -1,5 +1,4 @@
1
1
  import {type BlueprintDurableConfig, type BlueprintDurableFunctionResource, validateDurableFunction} from '../../index.js'
2
- import {createDebounceObject} from '../../utils/debounce.js'
3
2
  import {parseDuration} from '../../utils/parse-duration.js'
4
3
  import {runValidation} from '../../utils/validation.js'
5
4
  import {defineFunction} from './index.js'
@@ -13,29 +12,26 @@ import {defineFunction} from './index.js'
13
12
  * defineDurableFunction({
14
13
  * name: 'daily-cleanup',
15
14
  * event: {type: 'document', on: ['create'], filter: "_type == 'post'"},
16
- * concurrency: 5,
17
- * debounce: 10,
18
15
  * })
19
16
  * ```
20
17
  *
21
- *
22
18
  * @param functionConfig The configuration for the function
23
19
  * @category Definers
24
- * @alpha Deploying Durable Functions via Blueprints is experimental. This feature is not available publicly yet.
20
+ * @beta Deploying Durable Functions via Blueprints is experimental.
25
21
  * @public
26
- * @hidden
27
22
  * @expandType BlueprintDurableConfig
28
23
  * @returns The validated durable function resource
29
24
  */
30
25
  export function defineDurableFunction(functionConfig: BlueprintDurableConfig): BlueprintDurableFunctionResource {
31
- const {name, event, concurrency, debounce, src, durableTimeout} = functionConfig
26
+ const {name, event, src, durableTimeout} = functionConfig
32
27
  const functionResource: BlueprintDurableFunctionResource = {
33
28
  ...defineFunction({...functionConfig, src: src ?? `functions/${name}`}, {skipValidation: true}),
34
29
  type: 'sanity.function.durable',
35
30
  ...(event !== undefined && {event}),
36
31
  ...(durableTimeout !== undefined && {durableTimeout: parseDuration(durableTimeout, 's')}),
37
- ...(concurrency !== undefined && {concurrency}),
38
- ...(debounce !== undefined && {debounce: createDebounceObject(debounce)}),
32
+ // TODO: eventually support queue fields once backend supports it
33
+ // ...(concurrency !== undefined && {concurrency}),
34
+ // ...(debounce !== undefined && {debounce: createDebounceObject(debounce)}),
39
35
  }
40
36
 
41
37
  runValidation(() => validateDurableFunction(functionResource))
@@ -0,0 +1,107 @@
1
+ import {
2
+ type BlueprintContentResourceConfig,
3
+ type BlueprintPermissionConfig,
4
+ type BlueprintPermissionResource,
5
+ validateContentResource,
6
+ validatePermission,
7
+ } from '../index.js'
8
+ import {runValidation} from '../utils/validation.js'
9
+
10
+ /**
11
+ * Defines a custom permission that roles can grant, in the shape of the Access API.
12
+ *
13
+ * ```ts
14
+ * definePermission({
15
+ * name: 'articles-only',
16
+ * permissionType: 'sanity.document.filter.mode',
17
+ * config: {filter: '_type == "article"'},
18
+ * })
19
+ * ```
20
+ *
21
+ * @example Grant the permission with a role
22
+ * ```ts
23
+ * definePermission({
24
+ * name: 'articles-only',
25
+ * title: 'Articles only',
26
+ * description: 'Access to article documents',
27
+ * permissionType: 'sanity.document.filter',
28
+ * config: {filter: '_type == "article"'},
29
+ * })
30
+ *
31
+ * defineRole({
32
+ * name: 'article-editor',
33
+ * title: 'Article editor',
34
+ * appliesToUsers: true,
35
+ * appliesToRobots: false,
36
+ * permissions: [
37
+ * {name: 'sanity-project', action: 'read'},
38
+ * {name: '$.resources.articles-only', action: 'read'},
39
+ * {name: '$.resources.articles-only', action: 'update'},
40
+ * ],
41
+ * })
42
+ * ```
43
+ * @param parameters The configuration of the permission
44
+ * @public
45
+ * @beta Deploying Permissions via Blueprints is experimental. This feature is stabilizing but may still be subject to breaking changes.
46
+ * @category Definers
47
+ * @expandType BlueprintPermissionConfig
48
+ * @returns The permission resource
49
+ */
50
+ export function definePermission(parameters: BlueprintPermissionConfig): BlueprintPermissionResource {
51
+ const permissionResource: BlueprintPermissionResource = {
52
+ ...parameters,
53
+ type: 'sanity.access.permission',
54
+ }
55
+
56
+ runValidation(() => validatePermission(permissionResource))
57
+
58
+ return permissionResource
59
+ }
60
+
61
+ /**
62
+ * Defines a content resource as the Manage UI makes it: a custom permission of type `sanity.document.filter.mode`.
63
+ *
64
+ * ```ts
65
+ * defineContentResource({
66
+ * name: 'movies',
67
+ * documentTypes: ['movie'],
68
+ * })
69
+ * // config.filter: '_type == "movie"'
70
+ * ```
71
+ *
72
+ * @example Document types and a filter
73
+ * ```ts
74
+ * defineContentResource({
75
+ * name: 'london-posts',
76
+ * title: 'London posts',
77
+ * documentTypes: ['post', 'event'],
78
+ * filter: 'branch == "london"',
79
+ * })
80
+ * // config.filter: '_type in ["post", "event"] && (branch == "london")'
81
+ * ```
82
+ * @param parameters The configuration of the content resource
83
+ * @public
84
+ * @beta Deploying Permissions via Blueprints is experimental. This feature is stabilizing but may still be subject to breaking changes.
85
+ * @category Definers
86
+ * @expandType BlueprintContentResourceConfig
87
+ * @returns The permission resource
88
+ */
89
+ export function defineContentResource(parameters: BlueprintContentResourceConfig): BlueprintPermissionResource {
90
+ const {documentTypes, filter, ...permission} = parameters
91
+
92
+ runValidation(() => validateContentResource(parameters))
93
+
94
+ return definePermission({
95
+ ...permission,
96
+ permissionType: 'sanity.document.filter.mode',
97
+ config: {filter: contentResourceFilter(documentTypes, filter)},
98
+ })
99
+ }
100
+
101
+ function contentResourceFilter(documentTypes: unknown, filter?: string): string {
102
+ const types = Array.isArray(documentTypes) ? documentTypes.map((type) => JSON.stringify(type)) : []
103
+ const typeClause = types.length > 1 ? `_type in [${types.join(', ')}]` : types.length === 1 ? `_type == ${types[0]}` : ''
104
+
105
+ if (!typeClause) return filter ?? ''
106
+ return filter ? `${typeClause} && (${filter})` : typeClause
107
+ }
package/src/index.ts CHANGED
@@ -9,6 +9,7 @@
9
9
  */
10
10
  export * from './definers/applications.js'
11
11
  export * from './definers/blueprints.js'
12
+ export * from './definers/context.js'
12
13
  export * from './definers/cors.js'
13
14
  export * from './definers/datasets.js'
14
15
  export * from './definers/functions/document.js'
@@ -20,6 +21,7 @@ export * from './definers/functions/queue.js'
20
21
  export * from './definers/functions/scheduled.js'
21
22
  export * from './definers/functions/sync-tag-invalidate.js'
22
23
  export * from './definers/installation-config.js'
24
+ export * from './definers/permissions.js'
23
25
  export * from './definers/projects.js'
24
26
  export * from './definers/resources.js'
25
27
  export * from './definers/robots.js'
@@ -39,11 +41,13 @@ export * from './definers/webhooks.js'
39
41
  */
40
42
  export * from './types/applications.js'
41
43
  export * from './types/blueprints.js'
44
+ export * from './types/context.js'
42
45
  export * from './types/cors.js'
43
46
  export * from './types/datasets.js'
44
47
  export * from './types/errors.js'
45
48
  export * from './types/functions/index.js'
46
49
  export * from './types/installation-config.js'
50
+ export * from './types/permissions.js'
47
51
  export * from './types/projects.js'
48
52
  export * from './types/resources.js'
49
53
  export * from './types/robots.js'
@@ -59,10 +63,12 @@ export * from './types/webhooks.js'
59
63
  */
60
64
  export * from './validation/applications.js'
61
65
  export * from './validation/blueprints.js'
66
+ export * from './validation/context.js'
62
67
  export * from './validation/cors.js'
63
68
  export * from './validation/datasets.js'
64
69
  export * from './validation/functions.js'
65
70
  export * from './validation/installation-config.js'
71
+ export * from './validation/permissions.js'
66
72
  export * from './validation/projects.js'
67
73
  export * from './validation/resources.js'
68
74
  export * from './validation/robots.js'
@@ -0,0 +1,109 @@
1
+ import type {BlueprintResource} from '../index.js'
2
+
3
+ /**
4
+ * A knowledge base that a Context MCP endpoint serves.
5
+ * @alpha Deploying Context via Blueprints is experimental. This feature is not available publicly yet.
6
+ * @hidden
7
+ * @category Resource Types
8
+ */
9
+ export interface ContextKnowledgeBaseSource {
10
+ type: 'knowledge-base'
11
+ /** A knowledge base in the blueprint as `$.resources.<name>`, or the ID (`kb…`) of a knowledge base. */
12
+ id: string
13
+ }
14
+
15
+ /**
16
+ * A Sanity dataset that a Context MCP endpoint serves.
17
+ * @alpha Deploying Context via Blueprints is experimental. This feature is not available publicly yet.
18
+ * @hidden
19
+ * @category Resource Types
20
+ */
21
+ export interface ContextDatasetSource {
22
+ type: 'dataset'
23
+ /** The dataset as `<projectId>.<datasetName>`, for example `abc123.production`. It can't be a `$.resources.<name>` reference. */
24
+ id: string
25
+ /** The studio workspace whose schema agents use, by name. `null` uses the studio's first workspace. */
26
+ workspace?: string | null
27
+ /** The perspective agents query the dataset with. `null` uses `published`. */
28
+ perspective?: 'published' | 'drafts' | 'raw' | null
29
+ /**
30
+ * Whether agents can use semantic search on the dataset. `false` turns it off. `true` or `null` use it while the
31
+ * dataset has embeddings enabled.
32
+ */
33
+ semanticSearch?: boolean | null
34
+ }
35
+
36
+ /**
37
+ * Content that a Context MCP endpoint serves.
38
+ * @alpha Deploying Context via Blueprints is experimental. This feature is not available publicly yet.
39
+ * @hidden
40
+ * @category Resource Types
41
+ */
42
+ export type ContextMcpEndpointSource = ContextKnowledgeBaseSource | ContextDatasetSource
43
+
44
+ /**
45
+ * A Context knowledge base resource.
46
+ * @alpha Deploying Context via Blueprints is experimental. This feature is not available publicly yet.
47
+ * @hidden
48
+ * @category Resource Types
49
+ */
50
+ export interface BlueprintContextKnowledgeBaseResource extends BlueprintResource {
51
+ type: 'sanity.context.knowledge-base'
52
+ /**
53
+ * The resource name in the blueprint. The knowledge base has no name, only a `title`. With the default deletion
54
+ * policy (`retain`), a deploy fails if this name changes.
55
+ */
56
+ name: string
57
+ /** The knowledge base title, up to 100 characters. */
58
+ title: string
59
+ /** What the knowledge base covers, up to 2,000 characters. */
60
+ description: string
61
+ }
62
+
63
+ /**
64
+ * Configuration for a Context knowledge base.
65
+ * @alpha Deploying Context via Blueprints is experimental. This feature is not available publicly yet.
66
+ * @hidden
67
+ * @category Resource Types
68
+ * @interface
69
+ */
70
+ export type BlueprintContextKnowledgeBaseConfig = Omit<BlueprintContextKnowledgeBaseResource, 'type'>
71
+
72
+ /**
73
+ * A Context MCP endpoint resource.
74
+ * @alpha Deploying Context via Blueprints is experimental. This feature is not available publicly yet.
75
+ * @hidden
76
+ * @category Resource Types
77
+ */
78
+ export interface BlueprintContextMcpEndpointResource extends BlueprintResource {
79
+ type: 'sanity.context.mcp'
80
+ /**
81
+ * The endpoint name in the URL that agents connect to: lowercase letters, digits and single hyphens, up to 64
82
+ * characters. The names `by-name` and `mcp` followed by 8 letters or digits are reserved. With the default deletion
83
+ * policy (`allow`), a change deletes the endpoint and creates a new endpoint with a new URL.
84
+ */
85
+ name: string
86
+ /** The endpoint title, up to 100 characters. */
87
+ title: string
88
+ /** What the endpoint serves: any number of knowledge bases and at most one dataset. */
89
+ sources: ContextMcpEndpointSource[]
90
+ /** Instructions for agents that connect to the endpoint, up to 10,000 characters. */
91
+ instructions?: string
92
+ /** A GROQ filter for the documents that the dataset source serves. It requires a dataset source. */
93
+ groqFilter?: string
94
+ }
95
+
96
+ /**
97
+ * Configuration for a Context MCP endpoint.
98
+ * @alpha Deploying Context via Blueprints is experimental. This feature is not available publicly yet.
99
+ * @hidden
100
+ * @category Resource Types
101
+ * @interface
102
+ */
103
+ export type BlueprintContextMcpEndpointConfig = Omit<BlueprintContextMcpEndpointResource, 'type' | 'sources'> & {
104
+ /**
105
+ * What the endpoint serves: any number of knowledge bases and at most one dataset. A knowledge base source can also
106
+ * be the resource that {@link defineContextKnowledgeBase} returns.
107
+ */
108
+ sources: Array<ContextMcpEndpointSource | BlueprintContextKnowledgeBaseResource>
109
+ }
@@ -205,11 +205,10 @@ export interface BlueprintPubSubFunctionResource extends BlueprintBaseFunctionRe
205
205
 
206
206
  /**
207
207
  * A durable, step-based function.
208
- * @alpha Deploying Durable Functions via Blueprints is experimental. This feature is not available publicly yet.
209
- * @hidden
208
+ * @beta Deploying Durable Functions via Blueprints is experimental.
210
209
  * @category Functions Types
211
210
  */
212
- export interface BlueprintDurableFunctionResource extends BlueprintBaseFunctionResource, QueueConfig {
211
+ export interface BlueprintDurableFunctionResource extends BlueprintBaseFunctionResource {
213
212
  type: 'sanity.function.durable'
214
213
  event?: BlueprintFunctionResourceContentLakeEvent
215
214
  /**
@@ -338,12 +337,11 @@ export type BlueprintPubSubFunctionConfig = Omit<BlueprintPubSubFunctionResource
338
337
  /**
339
338
  * Configuration for defining a durable function.
340
339
  * @public
341
- * @alpha Deploying Durable Functions via Blueprints is experimental. This feature is not available publicly yet.
342
- * @hidden
340
+ * @beta Deploying Durable Functions via Blueprints is experimental.
343
341
  * @category Functions Types
344
342
  * @interface
345
343
  */
346
- export type BlueprintDurableConfig = Omit<BlueprintDurableFunctionResource, 'type' | 'src' | 'event' | 'durableTimeout' | 'debounce'> & {
344
+ export type BlueprintDurableConfig = Omit<BlueprintDurableFunctionResource, 'type' | 'src' | 'event' | 'durableTimeout'> & {
347
345
  /**
348
346
  * Path to the function source code
349
347
  * @defaultValue `functions/${name}`
@@ -358,8 +356,4 @@ export type BlueprintDurableConfig = Omit<BlueprintDurableFunctionResource, 'typ
358
356
  * @defaultValue 86_400 (24 hours)
359
357
  */
360
358
  durableTimeout?: string | number
361
- /**
362
- * Optional debounce configuration, as a duration or a full config
363
- */
364
- debounce?: DebounceInput
365
359
  }
@@ -0,0 +1,67 @@
1
+ import type {BlueprintProjectResourceLifecycle, BlueprintResource} from '../index.js'
2
+
3
+ /**
4
+ * The type of a custom permission.
5
+ *
6
+ * - **sanity.document.filter** A role grants document actions, such as `read` and `update`, on the documents that match the filter.
7
+ * - **sanity.document.filter.mode** A role grants the `mode` action, with a `mode` param such as `read` or `publish`, on the documents that match the filter.
8
+ * @beta This feature is subject to breaking changes.
9
+ * @category Resource Types
10
+ */
11
+ export type BlueprintPermissionType = 'sanity.document.filter' | 'sanity.document.filter.mode'
12
+
13
+ /**
14
+ * A custom permission resource that roles can grant.
15
+ * @beta This feature is subject to breaking changes.
16
+ * @category Resource Types
17
+ */
18
+ export interface BlueprintPermissionResource extends BlueprintResource<BlueprintProjectResourceLifecycle> {
19
+ type: 'sanity.access.permission'
20
+ /**
21
+ * The name of the permission.
22
+ *
23
+ * Lowercase letters, numbers, hyphens, and underscores only. Must not start with `sanity` or `res-`.
24
+ */
25
+ name: string
26
+ /** A human-readable title. Defaults to `name`. */
27
+ title?: string
28
+ /** A description of the permission. Defaults to an empty string. */
29
+ description?: string
30
+ /**
31
+ * The type of the permission. It cannot change after the permission is created.
32
+ *
33
+ * Manage creates content resources as `sanity.document.filter.mode`, and lists only custom permissions of that type.
34
+ */
35
+ permissionType: BlueprintPermissionType
36
+ /** The configuration of the permission. */
37
+ config: {
38
+ /** A GROQ filter that selects the documents that the permission applies to, e.g. `_type == "article"`. */
39
+ filter: string
40
+ }
41
+ /**
42
+ * The project ID of the project that contains the permission.
43
+ *
44
+ * The `project` attribute must be defined if your blueprint is scoped to an organization and the stack has no default project.
45
+ */
46
+ project?: string
47
+ }
48
+
49
+ /**
50
+ * Configuration for a custom permission resource.
51
+ * @beta This feature is subject to breaking changes.
52
+ * @category Resource Types
53
+ * @interface
54
+ */
55
+ export type BlueprintPermissionConfig = Omit<BlueprintPermissionResource, 'type'>
56
+
57
+ /**
58
+ * Configuration for a content resource: a custom permission of type `sanity.document.filter.mode`, as the Manage UI makes it.
59
+ * @beta This feature is subject to breaking changes.
60
+ * @category Resource Types
61
+ */
62
+ export interface BlueprintContentResourceConfig extends Omit<BlueprintPermissionConfig, 'permissionType' | 'config'> {
63
+ /** The document types that the content resource applies to. Omit to apply to documents of any type. */
64
+ documentTypes?: string[]
65
+ /** A GROQ filter that selects documents, e.g. `branch == "london"`. It is joined to `documentTypes` with `&&`. */
66
+ filter?: string
67
+ }
@@ -7,7 +7,7 @@ import type {BlueprintProjectResourceLifecycle, BlueprintResource} from '../inde
7
7
  * @expand
8
8
  */
9
9
  export interface RolePermission {
10
- /** Predefined permission name (e.g., 'sanity-all-documents') */
10
+ /** Predefined permission name (e.g., 'sanity-all-documents'), or a reference to a permission in the blueprint (e.g., '$.resources.articles-only') */
11
11
  name: string
12
12
  /** Permission action (e.g., 'read', 'mode') */
13
13
  action: string
@@ -0,0 +1,142 @@
1
+ import {type BlueprintError, validateResource} from '../index.js'
2
+ import {isReference} from '../utils/validation.js'
3
+
4
+ const MCP_ENDPOINT_NAME_PATTERN = /^[a-z0-9]+(?:-[a-z0-9]+)*$/
5
+ const MCP_ENDPOINT_ID_PATTERN = /^mcp[a-z0-9]{8}$/
6
+ const DATASET_SOURCE_PATTERN = /^[a-z0-9]+\.[a-z0-9][a-z0-9_-]*$/
7
+ const DATASET_PERSPECTIVES: unknown[] = ['published', 'drafts', 'raw']
8
+
9
+ function validateText(resource: object, field: string, label: string, options: {required: boolean; max: number}): BlueprintError[] {
10
+ if (!(field in resource)) {
11
+ return options.required ? [{type: 'missing_parameter', message: `${label} is required`}] : []
12
+ }
13
+ const value: unknown = Reflect.get(resource, field)
14
+ if (typeof value !== 'string') return [{type: 'invalid_type', message: `${label} must be a string`}]
15
+ if (value.length === 0) return [{type: 'invalid_value', message: `${label} must not be empty`}]
16
+ if (value.length > options.max) {
17
+ return [{type: 'invalid_value', message: `${label} must be ${options.max} characters or less`}]
18
+ }
19
+ return []
20
+ }
21
+
22
+ function validateDatasetSettings(source: object): BlueprintError[] {
23
+ const errors: BlueprintError[] = []
24
+ const workspace: unknown = Reflect.get(source, 'workspace')
25
+ const perspective: unknown = Reflect.get(source, 'perspective')
26
+ const semanticSearch: unknown = Reflect.get(source, 'semanticSearch')
27
+ if (workspace != null && (typeof workspace !== 'string' || workspace.length === 0)) {
28
+ errors.push({type: 'invalid_type', message: 'Dataset source `workspace` must be a non-empty string or null'})
29
+ }
30
+ if (perspective != null && !DATASET_PERSPECTIVES.includes(perspective)) {
31
+ errors.push({type: 'invalid_value', message: 'Dataset source `perspective` must be `published`, `drafts`, `raw` or null'})
32
+ }
33
+ if (semanticSearch != null && typeof semanticSearch !== 'boolean') {
34
+ errors.push({type: 'invalid_type', message: 'Dataset source `semanticSearch` must be a boolean or null'})
35
+ }
36
+ return errors
37
+ }
38
+
39
+ /**
40
+ * Validates that the given resource is a valid Context knowledge base.
41
+ * @param resource The Context knowledge base resource
42
+ * @alpha
43
+ * @hidden
44
+ * @category Validation
45
+ * @returns A list of validation errors
46
+ */
47
+ export function validateContextKnowledgeBase(resource: unknown): BlueprintError[] {
48
+ if (!resource) return [{type: 'invalid_value', message: 'Knowledge base config must be provided'}]
49
+ if (typeof resource !== 'object') return [{type: 'invalid_type', message: 'Knowledge base config must be an object'}]
50
+
51
+ const errors: BlueprintError[] = validateResource(resource)
52
+
53
+ if ('type' in resource && resource.type !== 'sanity.context.knowledge-base') {
54
+ errors.push({type: 'invalid_value', message: 'Knowledge base type must be `sanity.context.knowledge-base`'})
55
+ }
56
+
57
+ errors.push(...validateText(resource, 'title', 'Knowledge base title', {required: true, max: 100}))
58
+ errors.push(...validateText(resource, 'description', 'Knowledge base description', {required: true, max: 2000}))
59
+
60
+ return errors
61
+ }
62
+
63
+ /**
64
+ * Validates that the given resource is a valid Context MCP endpoint.
65
+ * @param resource The Context MCP endpoint resource
66
+ * @alpha
67
+ * @hidden
68
+ * @category Validation
69
+ * @returns A list of validation errors
70
+ */
71
+ export function validateContextMcpEndpoint(resource: unknown): BlueprintError[] {
72
+ if (!resource) return [{type: 'invalid_value', message: 'MCP endpoint config must be provided'}]
73
+ if (typeof resource !== 'object') return [{type: 'invalid_type', message: 'MCP endpoint config must be an object'}]
74
+
75
+ const errors: BlueprintError[] = validateResource(resource)
76
+
77
+ if ('type' in resource && resource.type !== 'sanity.context.mcp') {
78
+ errors.push({type: 'invalid_value', message: 'MCP endpoint type must be `sanity.context.mcp`'})
79
+ }
80
+
81
+ if ('name' in resource && typeof resource.name === 'string') {
82
+ if (resource.name.length > 64 || !MCP_ENDPOINT_NAME_PATTERN.test(resource.name)) {
83
+ errors.push({
84
+ type: 'invalid_format',
85
+ message: 'MCP endpoint name must be lowercase letters, digits and single hyphens, up to 64 characters',
86
+ })
87
+ } else if (resource.name === 'by-name' || MCP_ENDPOINT_ID_PATTERN.test(resource.name)) {
88
+ errors.push({type: 'invalid_value', message: `MCP endpoint name "${resource.name}" is reserved`})
89
+ }
90
+ }
91
+
92
+ errors.push(...validateText(resource, 'title', 'MCP endpoint title', {required: true, max: 100}))
93
+ errors.push(...validateText(resource, 'instructions', 'MCP endpoint instructions', {required: false, max: 10_000}))
94
+ errors.push(...validateText(resource, 'groqFilter', 'MCP endpoint GROQ filter', {required: false, max: 10_000}))
95
+
96
+ if (!('sources' in resource)) {
97
+ errors.push({type: 'missing_parameter', message: 'MCP endpoint sources are required'})
98
+ return errors
99
+ }
100
+ if (!Array.isArray(resource.sources) || resource.sources.length === 0) {
101
+ errors.push({type: 'invalid_value', message: 'MCP endpoint must have at least one source'})
102
+ return errors
103
+ }
104
+ if (resource.sources.length > 100) {
105
+ errors.push({type: 'invalid_value', message: 'MCP endpoint can have at most 100 sources'})
106
+ }
107
+
108
+ let datasets = 0
109
+ for (const source of resource.sources) {
110
+ if (!source || typeof source !== 'object' || !('type' in source) || !('id' in source)) {
111
+ errors.push({type: 'invalid_value', message: 'Each MCP endpoint source needs a `type` and an `id`'})
112
+ continue
113
+ }
114
+ if (typeof source.id !== 'string' || source.id.length === 0) {
115
+ errors.push({type: 'invalid_type', message: 'MCP endpoint source `id` must be a non-empty string'})
116
+ continue
117
+ }
118
+ if (source.type === 'dataset') {
119
+ datasets++
120
+ // The provider checks the format before references resolve, and no resource exposes `<projectId>.<datasetName>`
121
+ if (source.id.startsWith('$.resources.')) {
122
+ errors.push({
123
+ type: 'invalid_value',
124
+ message: `Dataset source "${source.id}" must be "<projectId>.<datasetName>", not a resource reference`,
125
+ })
126
+ } else if (!isReference(source.id) && !DATASET_SOURCE_PATTERN.test(source.id)) {
127
+ errors.push({type: 'invalid_format', message: `Dataset source "${source.id}" must be "<projectId>.<datasetName>"`})
128
+ }
129
+ errors.push(...validateDatasetSettings(source))
130
+ } else if (source.type !== 'knowledge-base') {
131
+ errors.push({type: 'invalid_value', message: 'MCP endpoint source `type` must be `knowledge-base` or `dataset`'})
132
+ }
133
+ }
134
+ if (datasets > 1) {
135
+ errors.push({type: 'invalid_value', message: 'MCP endpoint can have at most one dataset source'})
136
+ }
137
+ if (datasets === 0 && 'groqFilter' in resource) {
138
+ errors.push({type: 'invalid_value', message: 'MCP endpoint GROQ filter requires a dataset source'})
139
+ }
140
+
141
+ return errors
142
+ }
@@ -0,0 +1,100 @@
1
+ import {type BlueprintError, validateResource} from '../index.js'
2
+
3
+ const PERMISSION_TYPES = ['sanity.document.filter', 'sanity.document.filter.mode']
4
+ const PERMISSION_NAME = /^(?!res-)(?!sanity)[a-z0-9-_]+$/
5
+
6
+ /**
7
+ * Validates that the given resource is a valid Permission.
8
+ * @param resource The Permission resource
9
+ * @category Validation
10
+ * @returns A list of validation errors
11
+ */
12
+ export function validatePermission(resource: unknown): BlueprintError[] {
13
+ if (!resource) return [{type: 'invalid_value', message: 'Permission config must be provided'}]
14
+ if (typeof resource !== 'object') return [{type: 'invalid_type', message: 'Permission config must be an object'}]
15
+
16
+ const errors: BlueprintError[] = validateResource(resource, {projectContained: true})
17
+
18
+ if (!('name' in resource) || !resource.name) {
19
+ errors.push({type: 'missing_parameter', message: 'Permission name is required'})
20
+ } else if (typeof resource.name !== 'string') {
21
+ errors.push({type: 'invalid_type', message: 'Permission name must be a string'})
22
+ } else if (!PERMISSION_NAME.test(resource.name)) {
23
+ errors.push({
24
+ type: 'invalid_format',
25
+ message:
26
+ 'Permission name must only contain lowercase letters, numbers, hyphens, and underscores, and must not start with "sanity" or "res-"',
27
+ })
28
+ }
29
+
30
+ if (!('type' in resource)) {
31
+ errors.push({type: 'missing_parameter', message: 'Permission type is required'})
32
+ } else if (resource.type !== 'sanity.access.permission') {
33
+ errors.push({type: 'invalid_value', message: 'Permission type must be `sanity.access.permission`'})
34
+ }
35
+
36
+ if ('title' in resource && resource.title !== undefined) {
37
+ if (typeof resource.title !== 'string') {
38
+ errors.push({type: 'invalid_type', message: 'Permission title must be a string'})
39
+ } else if (resource.title.length === 0) {
40
+ errors.push({type: 'invalid_value', message: 'Permission title must not be empty'})
41
+ }
42
+ }
43
+
44
+ if ('description' in resource && resource.description !== undefined && typeof resource.description !== 'string') {
45
+ errors.push({type: 'invalid_type', message: 'Permission description must be a string'})
46
+ }
47
+
48
+ if (!('permissionType' in resource) || !resource.permissionType) {
49
+ errors.push({type: 'missing_parameter', message: 'Permission permissionType is required'})
50
+ } else if (typeof resource.permissionType !== 'string' || !PERMISSION_TYPES.includes(resource.permissionType)) {
51
+ errors.push({type: 'invalid_value', message: `Permission permissionType must be one of ${PERMISSION_TYPES.join(', ')}`})
52
+ }
53
+
54
+ if (!('config' in resource) || !resource.config) {
55
+ errors.push({type: 'missing_parameter', message: 'Permission `config` is required'})
56
+ } else if (typeof resource.config !== 'object') {
57
+ errors.push({type: 'invalid_type', message: 'Permission `config` must be an object'})
58
+ } else if (!('filter' in resource.config) || !resource.config.filter) {
59
+ errors.push({type: 'missing_parameter', message: 'Permission filter is required'})
60
+ } else if (typeof resource.config.filter !== 'string') {
61
+ errors.push({type: 'invalid_type', message: 'Permission filter must be a string'})
62
+ }
63
+
64
+ if ('project' in resource && resource.project !== undefined) {
65
+ if (typeof resource.project !== 'string') {
66
+ errors.push({type: 'invalid_type', message: 'Permission project must be a string'})
67
+ } else if (resource.project.length === 0) {
68
+ errors.push({type: 'invalid_value', message: 'Permission project must not be empty'})
69
+ }
70
+ }
71
+
72
+ return errors
73
+ }
74
+
75
+ /**
76
+ * Validates the fields of a content resource that make its filter.
77
+ * @param resource The content resource configuration
78
+ * @category Validation
79
+ * @returns A list of validation errors
80
+ */
81
+ export function validateContentResource(resource: unknown): BlueprintError[] {
82
+ if (!resource) return [{type: 'invalid_value', message: 'Content resource config must be provided'}]
83
+ if (typeof resource !== 'object') return [{type: 'invalid_type', message: 'Content resource config must be an object'}]
84
+
85
+ const errors: BlueprintError[] = []
86
+
87
+ if (
88
+ 'documentTypes' in resource &&
89
+ resource.documentTypes !== undefined &&
90
+ (!Array.isArray(resource.documentTypes) || !resource.documentTypes.every((type) => typeof type === 'string' && type.length > 0))
91
+ ) {
92
+ errors.push({type: 'invalid_type', message: 'Content resource documentTypes must be an array of non-empty strings'})
93
+ }
94
+
95
+ if ('filter' in resource && resource.filter !== undefined && typeof resource.filter !== 'string') {
96
+ errors.push({type: 'invalid_type', message: 'Content resource filter must be a string'})
97
+ }
98
+
99
+ return errors
100
+ }