@sanity/blueprints 0.30.0 → 0.32.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.
@@ -0,0 +1,138 @@
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
+ * @category Validation
43
+ * @returns A list of validation errors
44
+ */
45
+ export function validateContextKnowledgeBase(resource: unknown): BlueprintError[] {
46
+ if (!resource) return [{type: 'invalid_value', message: 'Knowledge base config must be provided'}]
47
+ if (typeof resource !== 'object') return [{type: 'invalid_type', message: 'Knowledge base config must be an object'}]
48
+
49
+ const errors: BlueprintError[] = validateResource(resource)
50
+
51
+ if ('type' in resource && resource.type !== 'sanity.context.knowledge-base') {
52
+ errors.push({type: 'invalid_value', message: 'Knowledge base type must be `sanity.context.knowledge-base`'})
53
+ }
54
+
55
+ errors.push(...validateText(resource, 'title', 'Knowledge base title', {required: true, max: 100}))
56
+ errors.push(...validateText(resource, 'description', 'Knowledge base description', {required: true, max: 2000}))
57
+
58
+ return errors
59
+ }
60
+
61
+ /**
62
+ * Validates that the given resource is a valid Context MCP endpoint.
63
+ * @param resource The Context MCP endpoint resource
64
+ * @category Validation
65
+ * @returns A list of validation errors
66
+ */
67
+ export function validateContextMcpEndpoint(resource: unknown): BlueprintError[] {
68
+ if (!resource) return [{type: 'invalid_value', message: 'MCP endpoint config must be provided'}]
69
+ if (typeof resource !== 'object') return [{type: 'invalid_type', message: 'MCP endpoint config must be an object'}]
70
+
71
+ const errors: BlueprintError[] = validateResource(resource)
72
+
73
+ if ('type' in resource && resource.type !== 'sanity.context.mcp') {
74
+ errors.push({type: 'invalid_value', message: 'MCP endpoint type must be `sanity.context.mcp`'})
75
+ }
76
+
77
+ if ('name' in resource && typeof resource.name === 'string') {
78
+ if (resource.name.length > 64 || !MCP_ENDPOINT_NAME_PATTERN.test(resource.name)) {
79
+ errors.push({
80
+ type: 'invalid_format',
81
+ message: 'MCP endpoint name must be lowercase letters, digits and single hyphens, up to 64 characters',
82
+ })
83
+ } else if (resource.name === 'by-name' || MCP_ENDPOINT_ID_PATTERN.test(resource.name)) {
84
+ errors.push({type: 'invalid_value', message: `MCP endpoint name "${resource.name}" is reserved`})
85
+ }
86
+ }
87
+
88
+ errors.push(...validateText(resource, 'title', 'MCP endpoint title', {required: true, max: 100}))
89
+ errors.push(...validateText(resource, 'instructions', 'MCP endpoint instructions', {required: false, max: 10_000}))
90
+ errors.push(...validateText(resource, 'groqFilter', 'MCP endpoint GROQ filter', {required: false, max: 10_000}))
91
+
92
+ if (!('sources' in resource)) {
93
+ errors.push({type: 'missing_parameter', message: 'MCP endpoint sources are required'})
94
+ return errors
95
+ }
96
+ if (!Array.isArray(resource.sources) || resource.sources.length === 0) {
97
+ errors.push({type: 'invalid_value', message: 'MCP endpoint must have at least one source'})
98
+ return errors
99
+ }
100
+ if (resource.sources.length > 100) {
101
+ errors.push({type: 'invalid_value', message: 'MCP endpoint can have at most 100 sources'})
102
+ }
103
+
104
+ let datasets = 0
105
+ for (const source of resource.sources) {
106
+ if (!source || typeof source !== 'object' || !('type' in source) || !('id' in source)) {
107
+ errors.push({type: 'invalid_value', message: 'Each MCP endpoint source needs a `type` and an `id`'})
108
+ continue
109
+ }
110
+ if (typeof source.id !== 'string' || source.id.length === 0) {
111
+ errors.push({type: 'invalid_type', message: 'MCP endpoint source `id` must be a non-empty string'})
112
+ continue
113
+ }
114
+ if (source.type === 'dataset') {
115
+ datasets++
116
+ // The provider checks the format before references resolve, and no resource exposes `<projectId>.<datasetName>`
117
+ if (source.id.startsWith('$.resources.')) {
118
+ errors.push({
119
+ type: 'invalid_value',
120
+ message: `Dataset source "${source.id}" must be "<projectId>.<datasetName>", not a resource reference`,
121
+ })
122
+ } else if (!isReference(source.id) && !DATASET_SOURCE_PATTERN.test(source.id)) {
123
+ errors.push({type: 'invalid_format', message: `Dataset source "${source.id}" must be "<projectId>.<datasetName>"`})
124
+ }
125
+ errors.push(...validateDatasetSettings(source))
126
+ } else if (source.type !== 'knowledge-base') {
127
+ errors.push({type: 'invalid_value', message: 'MCP endpoint source `type` must be `knowledge-base` or `dataset`'})
128
+ }
129
+ }
130
+ if (datasets > 1) {
131
+ errors.push({type: 'invalid_value', message: 'MCP endpoint can have at most one dataset source'})
132
+ }
133
+ if (datasets === 0 && 'groqFilter' in resource) {
134
+ errors.push({type: 'invalid_value', message: 'MCP endpoint GROQ filter requires a dataset source'})
135
+ }
136
+
137
+ return errors
138
+ }
@@ -1,6 +1,6 @@
1
1
  import type {BlueprintError} from '../types/errors.js'
2
2
  import {APPLICATION_VISIBILITIES} from '../types/studios.js'
3
- import {isReference} from '../utils/validation.js'
3
+ import {isReference, validateStringField} from '../utils/validation.js'
4
4
  import {validateResource} from './resources.js'
5
5
 
6
6
  /** hostname validity pattern */
@@ -105,5 +105,81 @@ export function validateStudio(resource: unknown): BlueprintError[] {
105
105
  }
106
106
  }
107
107
 
108
+ if ('mfManifest' in resource && typeof resource.mfManifest !== 'undefined') {
109
+ if (!resource.mfManifest || typeof resource.mfManifest !== 'object' || Array.isArray(resource.mfManifest)) {
110
+ errors.push({type: 'invalid_type', message: 'Studio mfManifest must be an object'})
111
+ }
112
+ }
113
+
114
+ if ('workspaces' in resource && typeof resource.workspaces !== 'undefined') {
115
+ if (!Array.isArray(resource.workspaces)) {
116
+ errors.push({type: 'invalid_type', message: 'Studio workspaces must be an array'})
117
+ } else {
118
+ resource.workspaces.forEach((workspace, index) => {
119
+ errors.push(...validateStudioWorkspace(workspace, `Studio workspaces[${index}]`))
120
+ })
121
+ }
122
+ }
123
+
124
+ if ('schemaInputs' in resource && typeof resource.schemaInputs !== 'undefined') {
125
+ if (!Array.isArray(resource.schemaInputs)) {
126
+ errors.push({type: 'invalid_type', message: 'Studio schemaInputs must be an array'})
127
+ } else {
128
+ resource.schemaInputs.forEach((schemaInput, index) => {
129
+ errors.push(...validateStudioSchemaInput(schemaInput, `Studio schemaInputs[${index}]`))
130
+ })
131
+ }
132
+ }
133
+
134
+ return errors
135
+ }
136
+
137
+ /**
138
+ * Validates a single workspace extracted from the studio configuration.
139
+ * @param workspace The workspace to validate
140
+ * @param label The label used as the prefix of error messages
141
+ * @returns A list of validation errors
142
+ */
143
+ function validateStudioWorkspace(workspace: unknown, label: string): BlueprintError[] {
144
+ if (!workspace) return [{type: 'invalid_value', message: `${label} must be provided`}]
145
+ if (typeof workspace !== 'object' || Array.isArray(workspace)) return [{type: 'invalid_type', message: `${label} must be an object`}]
146
+
147
+ const errors: BlueprintError[] = []
148
+
149
+ for (const field of ['projectId', 'dataset', 'schemaDescriptorId', 'name']) {
150
+ errors.push(...validateStringField(workspace, field, label, {required: true}))
151
+ }
152
+
153
+ for (const field of ['title', 'subtitle', 'basePath', 'icon']) {
154
+ errors.push(...validateStringField(workspace, field, label, {required: false}))
155
+ }
156
+
157
+ return errors
158
+ }
159
+
160
+ /**
161
+ * Validates a single schema input extracted from the studio's schema definition.
162
+ * @param schemaInput The schema input to validate
163
+ * @param label The label used as the prefix of error messages
164
+ * @returns A list of validation errors
165
+ */
166
+ function validateStudioSchemaInput(schemaInput: unknown, label: string): BlueprintError[] {
167
+ if (!schemaInput) return [{type: 'invalid_value', message: `${label} must be provided`}]
168
+ if (typeof schemaInput !== 'object' || Array.isArray(schemaInput)) return [{type: 'invalid_type', message: `${label} must be an object`}]
169
+
170
+ const errors: BlueprintError[] = []
171
+
172
+ for (const field of ['projectId', 'dataset', 'name']) {
173
+ errors.push(...validateStringField(schemaInput, field, label, {required: true}))
174
+ }
175
+
176
+ errors.push(...validateStringField(schemaInput, 'title', label, {required: false}))
177
+
178
+ if (!('manifestSchema' in schemaInput) || typeof schemaInput.manifestSchema === 'undefined') {
179
+ errors.push({type: 'missing_parameter', message: `${label}.manifestSchema is required`})
180
+ } else if (!Array.isArray(schemaInput.manifestSchema)) {
181
+ errors.push({type: 'invalid_type', message: `${label}.manifestSchema must be an array`})
182
+ }
183
+
108
184
  return errors
109
185
  }