@sanity/blueprints 0.34.1 → 0.36.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 (164) hide show
  1. package/README.md +1 -2
  2. package/dist/definers/agents.d.ts +3 -1
  3. package/dist/definers/agents.js +4 -2
  4. package/dist/definers/agents.js.map +1 -1
  5. package/dist/definers/applications.d.ts +30 -18
  6. package/dist/definers/applications.js +36 -24
  7. package/dist/definers/applications.js.map +1 -1
  8. package/dist/definers/blueprints.d.ts +1 -0
  9. package/dist/definers/blueprints.js +5 -27
  10. package/dist/definers/blueprints.js.map +1 -1
  11. package/dist/definers/context.d.ts +6 -2
  12. package/dist/definers/context.js +8 -4
  13. package/dist/definers/context.js.map +1 -1
  14. package/dist/definers/cors.d.ts +11 -1
  15. package/dist/definers/cors.js +11 -13
  16. package/dist/definers/cors.js.map +1 -1
  17. package/dist/definers/datasets.d.ts +30 -3
  18. package/dist/definers/datasets.js +32 -16
  19. package/dist/definers/datasets.js.map +1 -1
  20. package/dist/definers/functions/document.d.ts +29 -0
  21. package/dist/definers/functions/document.js +1 -1
  22. package/dist/definers/functions/document.js.map +1 -1
  23. package/dist/definers/functions/durable.d.ts +4 -3
  24. package/dist/definers/functions/durable.js +5 -4
  25. package/dist/definers/functions/durable.js.map +1 -1
  26. package/dist/definers/functions/index.js +1 -1
  27. package/dist/definers/functions/index.js.map +1 -1
  28. package/dist/definers/functions/media-library.d.ts +30 -0
  29. package/dist/definers/functions/media-library.js +19 -21
  30. package/dist/definers/functions/media-library.js.map +1 -1
  31. package/dist/definers/functions/pubsub.d.ts +2 -3
  32. package/dist/definers/functions/pubsub.js +3 -4
  33. package/dist/definers/functions/pubsub.js.map +1 -1
  34. package/dist/definers/functions/queue.d.ts +26 -6
  35. package/dist/definers/functions/queue.js +31 -10
  36. package/dist/definers/functions/queue.js.map +1 -1
  37. package/dist/definers/functions/scheduled.d.ts +7 -6
  38. package/dist/definers/functions/scheduled.js +8 -7
  39. package/dist/definers/functions/scheduled.js.map +1 -1
  40. package/dist/definers/functions/sync-tag-invalidate.d.ts +5 -2
  41. package/dist/definers/functions/sync-tag-invalidate.js +6 -3
  42. package/dist/definers/functions/sync-tag-invalidate.js.map +1 -1
  43. package/dist/definers/installation-config.d.ts +4 -2
  44. package/dist/definers/installation-config.js +5 -3
  45. package/dist/definers/installation-config.js.map +1 -1
  46. package/dist/definers/permissions.d.ts +8 -2
  47. package/dist/definers/permissions.js +12 -5
  48. package/dist/definers/permissions.js.map +1 -1
  49. package/dist/definers/projects.d.ts +3 -4
  50. package/dist/definers/projects.js +4 -5
  51. package/dist/definers/projects.js.map +1 -1
  52. package/dist/definers/resources.d.ts +1 -0
  53. package/dist/definers/resources.js +1 -11
  54. package/dist/definers/resources.js.map +1 -1
  55. package/dist/definers/robotTokens.d.ts +22 -4
  56. package/dist/definers/robotTokens.js +53 -20
  57. package/dist/definers/robotTokens.js.map +1 -1
  58. package/dist/definers/roles.d.ts +49 -9
  59. package/dist/definers/roles.js +54 -55
  60. package/dist/definers/roles.js.map +1 -1
  61. package/dist/definers/studios.d.ts +5 -3
  62. package/dist/definers/studios.js +6 -4
  63. package/dist/definers/studios.js.map +1 -1
  64. package/dist/definers/webhooks.d.ts +19 -1
  65. package/dist/definers/webhooks.js +18 -22
  66. package/dist/definers/webhooks.js.map +1 -1
  67. package/dist/index.d.ts +18 -18
  68. package/dist/index.js +19 -20
  69. package/dist/index.js.map +1 -1
  70. package/dist/resolve/index.d.ts +2 -3
  71. package/dist/resolve/index.js +2 -3
  72. package/dist/resolve/index.js.map +1 -1
  73. package/dist/types/agents.d.ts +7 -3
  74. package/dist/types/applications.d.ts +68 -50
  75. package/dist/types/applications.js +9 -6
  76. package/dist/types/applications.js.map +1 -1
  77. package/dist/types/context.d.ts +17 -7
  78. package/dist/types/cors.d.ts +0 -2
  79. package/dist/types/datasets.d.ts +0 -6
  80. package/dist/types/functions/index.d.ts +96 -21
  81. package/dist/types/functions/index.js +2 -1
  82. package/dist/types/functions/index.js.map +1 -1
  83. package/dist/types/installation-config.d.ts +14 -11
  84. package/dist/types/permissions.d.ts +8 -4
  85. package/dist/types/projects.d.ts +5 -4
  86. package/dist/types/robotTokens.d.ts +14 -4
  87. package/dist/types/roles.d.ts +10 -9
  88. package/dist/types/studios.d.ts +29 -14
  89. package/dist/types/studios.js +3 -2
  90. package/dist/types/studios.js.map +1 -1
  91. package/dist/types/webhooks.d.ts +0 -3
  92. package/dist/utils/debounce.d.ts +0 -11
  93. package/dist/utils/debounce.js +0 -22
  94. package/dist/utils/debounce.js.map +1 -1
  95. package/dist/utils/parse-duration.d.ts +13 -0
  96. package/dist/utils/parse-duration.js +24 -0
  97. package/dist/utils/parse-duration.js.map +1 -1
  98. package/dist/utils/throttle.d.ts +9 -0
  99. package/dist/utils/throttle.js +22 -0
  100. package/dist/utils/throttle.js.map +1 -0
  101. package/dist/utils/validation.d.ts +14 -11
  102. package/dist/utils/validation.js +25 -17
  103. package/dist/utils/validation.js.map +1 -1
  104. package/dist/validation/applications.js +7 -2
  105. package/dist/validation/applications.js.map +1 -1
  106. package/dist/validation/blueprints.js +120 -4
  107. package/dist/validation/blueprints.js.map +1 -1
  108. package/dist/validation/functions.d.ts +19 -0
  109. package/dist/validation/functions.js +169 -36
  110. package/dist/validation/functions.js.map +1 -1
  111. package/dist/validation/projects.js +3 -0
  112. package/dist/validation/projects.js.map +1 -1
  113. package/dist/validation/resources.js +1 -1
  114. package/dist/validation/resources.js.map +1 -1
  115. package/dist/validation/studios.js +4 -0
  116. package/dist/validation/studios.js.map +1 -1
  117. package/package.json +1 -1
  118. package/src/definers/agents.ts +4 -2
  119. package/src/definers/applications.ts +36 -24
  120. package/src/definers/blueprints.ts +4 -27
  121. package/src/definers/context.ts +8 -4
  122. package/src/definers/cors.ts +11 -13
  123. package/src/definers/datasets.ts +40 -16
  124. package/src/definers/functions/document.ts +17 -25
  125. package/src/definers/functions/durable.ts +5 -4
  126. package/src/definers/functions/index.ts +1 -1
  127. package/src/definers/functions/media-library.ts +19 -21
  128. package/src/definers/functions/pubsub.ts +3 -4
  129. package/src/definers/functions/queue.ts +31 -10
  130. package/src/definers/functions/scheduled.ts +8 -7
  131. package/src/definers/functions/sync-tag-invalidate.ts +6 -3
  132. package/src/definers/installation-config.ts +5 -3
  133. package/src/definers/permissions.ts +14 -6
  134. package/src/definers/projects.ts +4 -5
  135. package/src/definers/resources.ts +1 -11
  136. package/src/definers/robotTokens.ts +55 -20
  137. package/src/definers/roles.ts +60 -58
  138. package/src/definers/studios.ts +6 -4
  139. package/src/definers/webhooks.ts +18 -23
  140. package/src/index.ts +20 -21
  141. package/src/resolve/index.ts +2 -4
  142. package/src/types/agents.ts +7 -3
  143. package/src/types/applications.ts +68 -50
  144. package/src/types/context.ts +17 -7
  145. package/src/types/cors.ts +0 -2
  146. package/src/types/datasets.ts +0 -6
  147. package/src/types/functions/index.ts +100 -21
  148. package/src/types/installation-config.ts +14 -11
  149. package/src/types/permissions.ts +8 -4
  150. package/src/types/projects.ts +6 -4
  151. package/src/types/robotTokens.ts +15 -4
  152. package/src/types/roles.ts +10 -9
  153. package/src/types/studios.ts +33 -14
  154. package/src/types/webhooks.ts +0 -3
  155. package/src/utils/debounce.ts +0 -25
  156. package/src/utils/parse-duration.ts +27 -0
  157. package/src/utils/throttle.ts +25 -0
  158. package/src/utils/validation.ts +24 -17
  159. package/src/validation/applications.ts +6 -2
  160. package/src/validation/blueprints.ts +132 -5
  161. package/src/validation/functions.ts +183 -37
  162. package/src/validation/projects.ts +4 -0
  163. package/src/validation/resources.ts +1 -1
  164. package/src/validation/studios.ts +3 -0
@@ -1,37 +1,44 @@
1
1
  import type {BlueprintError} from '../types/errors.js'
2
2
 
3
- const collectedErrors: BlueprintError[] = []
3
+ const errorsByTarget = new WeakMap<object, BlueprintError[]>()
4
4
 
5
5
  /**
6
- * Executes the given validator function and throws a formatted error if any are returned and throwError is enabled.
7
- * Otherwise collects errors for use later during blueprint validation.
8
- * @param validator A function that returns a list of validation errors.
6
+ * Runs a validator and records its errors against `target`, which is the object a definer returns.
7
+ * `defineBlueprint` reports the errors of the resources in its `resources` list, so errors for a resource that never makes
8
+ * it into a blueprint are not reported, and errors don't carry over from one blueprint to the next.
9
+ * @param target The object the errors belong to, usually the resource a definer returns
10
+ * @param validator A function that returns a list of validation errors
9
11
  * @internal
10
12
  */
11
- export function runValidation(validator: () => BlueprintError[], options?: {throwError?: boolean}) {
13
+ export function runValidation(target: unknown, validator: () => BlueprintError[]): void {
12
14
  const errors = validator()
13
- if (options?.throwError && errors.length > 0) {
14
- const message = errors.map((err) => err.message).join('\n')
15
- throw new Error(message)
16
- }
17
- collectedErrors.push(...errors)
15
+ if (errors.length === 0) return
16
+
17
+ // there is nothing to attach the errors to, so report them right away
18
+ if (typeof target !== 'object' || target === null) throw new Error(formatErrors(errors))
19
+
20
+ errorsByTarget.set(target, [...(errorsByTarget.get(target) ?? []), ...errors])
18
21
  }
19
22
 
20
23
  /**
21
- * Gets the current set of errors that were returned during validation when resource definers were invoked.
22
- * @returns The errors collected during the validation of resources
24
+ * Gets the validation errors that were recorded against an object when a definer created it.
25
+ * @param target The object the errors belong to, usually a resource
26
+ * @returns The recorded errors, or an empty list
23
27
  * @internal
24
28
  */
25
- export function getCollectedErrors(): BlueprintError[] {
26
- return collectedErrors
29
+ export function getValidationErrors(target: unknown): BlueprintError[] {
30
+ if (typeof target !== 'object' || target === null) return []
31
+ return errorsByTarget.get(target) ?? []
27
32
  }
28
33
 
29
34
  /**
30
- * Resets the list of collected errors back to an empty list.
35
+ * Formats a list of validation errors as an error message, one error per line.
36
+ * @param errors The errors to format
37
+ * @returns The error message
31
38
  * @internal
32
39
  */
33
- export function resetCollectedErrors() {
34
- collectedErrors.splice(0, collectedErrors.length)
40
+ export function formatErrors(errors: BlueprintError[]): string {
41
+ return errors.map((err) => err.message).join('\n')
35
42
  }
36
43
 
37
44
  const REFERENCE_PREFIXES = ['$.resources.', '$.values.', '$.parameters.', '$.params.']
@@ -284,8 +284,12 @@ export function validateApplication(resource: unknown): BlueprintError[] {
284
284
  errors.push({type: 'invalid_value', message: 'Application root must be a relative path within the blueprint directory'})
285
285
  }
286
286
 
287
- if ('icon' in resource && typeof resource.icon !== 'string') {
288
- errors.push({type: 'invalid_type', message: 'Application icon must be a string'})
287
+ if ('icon' in resource) {
288
+ if (typeof resource.icon !== 'string') {
289
+ errors.push({type: 'invalid_type', message: 'Application icon must be a string'})
290
+ } else if (!isReference(resource.icon) && !isContainedRelativePath(resource.icon)) {
291
+ errors.push({type: 'invalid_value', message: 'Application icon must be a relative path within the application root'})
292
+ }
289
293
  }
290
294
 
291
295
  if (
@@ -1,5 +1,5 @@
1
1
  import type {BlueprintError} from '../index.js'
2
- import {getCollectedErrors, resetCollectedErrors} from '../utils/validation.js'
2
+ import {getValidationErrors} from '../utils/validation.js'
3
3
 
4
4
  /**
5
5
  * Validates that the given input is a valid Blueprint
@@ -17,11 +17,138 @@ export function validateBlueprint(blueprintConfig: unknown): BlueprintError[] {
17
17
  const {resources} = blueprintConfig
18
18
 
19
19
  if (resources && !Array.isArray(resources)) errors.push({type: 'invalid_format', message: '`resources` must be an array'})
20
+ else if (Array.isArray(resources)) {
21
+ errors.push(...validateResourceList(resources), ...validateResourceNames(resources), ...validateResourceReferences(resources))
22
+ }
20
23
  }
21
24
 
22
- // add any errors that were collected when definers were called
23
- errors.push(...getCollectedErrors())
24
- resetCollectedErrors()
25
-
26
25
  return errors
27
26
  }
27
+
28
+ /**
29
+ * Collects the errors that the definers recorded for each resource, prefixed with the name of the resource they belong to.
30
+ * @param resources The resources of the blueprint
31
+ * @returns A list of validation errors
32
+ */
33
+ function validateResourceList(resources: unknown[]): BlueprintError[] {
34
+ return resources.flatMap((resource, index) => {
35
+ const label = resourceLabel(resource, index)
36
+ return getValidationErrors(resource).map((error) => ({...error, message: `${label}: ${error.message}`}))
37
+ })
38
+ }
39
+
40
+ function resourceLabel(resource: unknown, index: number): string {
41
+ const name = resourceName(resource)
42
+ return name === undefined ? `\`resources[${index}]\`` : `\`${name}\``
43
+ }
44
+
45
+ /**
46
+ * Names identify resources in a blueprint, and `$.resources.<name>` references resolve by name.
47
+ * @param resources The resources of the blueprint
48
+ * @returns An error for each name that more than one resource has
49
+ */
50
+ function validateResourceNames(resources: unknown[]): BlueprintError[] {
51
+ const seen = new Set<string>()
52
+ const duplicates = new Set<string>()
53
+ for (const resource of resources) {
54
+ const name = resourceName(resource)
55
+ if (name === undefined) continue
56
+ if (seen.has(name)) duplicates.add(name)
57
+ else seen.add(name)
58
+ }
59
+
60
+ return Array.from(duplicates, (name) => ({type: 'invalid_value', message: `More than one resource is named \`${name}\``}))
61
+ }
62
+
63
+ /**
64
+ * Checks the `$.resources.<name>` references in each resource, the same way the Blueprints API does when it deploys:
65
+ * every reference has to point at a resource in the blueprint. References in project options, such as `project`, also
66
+ * have to point at a project.
67
+ * @param resources The resources of the blueprint
68
+ * @returns A list of validation errors
69
+ */
70
+ function validateResourceReferences(resources: unknown[]): BlueprintError[] {
71
+ const typesByName = new Map<string, unknown>()
72
+ for (const resource of resources) {
73
+ const name = resourceName(resource)
74
+ if (name !== undefined && !typesByName.has(name)) typesByName.set(name, resourceTypeOf(resource))
75
+ }
76
+
77
+ return resources.flatMap((resource, index) => {
78
+ const label = resourceLabel(resource, index)
79
+ const errors: BlueprintError[] = []
80
+
81
+ for (const reference of findResourceReferences(resource)) {
82
+ const name = referencedName(reference)
83
+ if (!typesByName.has(name)) {
84
+ errors.push({type: 'missing_resource', message: `${label}: \`${reference}\` refers to \`${name}\`, which is not in the blueprint`})
85
+ }
86
+ }
87
+
88
+ for (const reference of findProjectReferences(resource)) {
89
+ const name = referencedName(reference)
90
+ const type = typesByName.get(name)
91
+ if (typesByName.has(name) && type !== 'sanity.project') {
92
+ errors.push({
93
+ type: 'invalid_reference',
94
+ message: `${label}: \`${reference}\` refers to \`${name}\`, which is a \`${type}\`, not a project`,
95
+ })
96
+ }
97
+ }
98
+
99
+ return errors
100
+ })
101
+ }
102
+
103
+ const RESOURCES_PREFIX = '$.resources.'
104
+
105
+ function referencedName(reference: string): string {
106
+ return reference.slice(RESOURCES_PREFIX.length).split('.')[0]
107
+ }
108
+
109
+ /** Every string in the resource that is a `$.resources.` reference */
110
+ function findResourceReferences(value: unknown): string[] {
111
+ if (typeof value === 'string') return value.startsWith(RESOURCES_PREFIX) ? [value] : []
112
+ if (Array.isArray(value)) return value.flatMap(findResourceReferences)
113
+ if (typeof value === 'object' && value !== null) return Object.values(value).flatMap(findResourceReferences)
114
+ return []
115
+ }
116
+
117
+ /**
118
+ * The `$.resources.<name>` references without an attribute in the options that hold a project ID. Without an attribute, a
119
+ * reference resolves to the main identifier of the resource, which is only a project ID for a project.
120
+ */
121
+ function findProjectReferences(resource: unknown): string[] {
122
+ if (typeof resource !== 'object' || resource === null) return []
123
+
124
+ const scopes: unknown[] = [resource]
125
+ if ('memberships' in resource && Array.isArray(resource.memberships)) scopes.push(...resource.memberships)
126
+
127
+ const references: unknown[] = []
128
+ if ('project' in resource) references.push(resource.project)
129
+ for (const scope of scopes) {
130
+ if (
131
+ typeof scope === 'object' &&
132
+ scope !== null &&
133
+ 'resourceType' in scope &&
134
+ scope.resourceType === 'project' &&
135
+ 'resourceId' in scope
136
+ ) {
137
+ references.push(scope.resourceId)
138
+ }
139
+ }
140
+
141
+ return references.filter(
142
+ (reference): reference is string =>
143
+ typeof reference === 'string' && reference.startsWith(RESOURCES_PREFIX) && !reference.slice(RESOURCES_PREFIX.length).includes('.'),
144
+ )
145
+ }
146
+
147
+ function resourceTypeOf(resource: unknown): unknown {
148
+ return typeof resource === 'object' && resource !== null && 'type' in resource ? resource.type : undefined
149
+ }
150
+
151
+ function resourceName(resource: unknown): string | undefined {
152
+ if (typeof resource !== 'object' || resource === null || !('name' in resource)) return undefined
153
+ return typeof resource.name === 'string' && resource.name ? resource.name : undefined
154
+ }
@@ -7,7 +7,7 @@ import {
7
7
  VALID_RUNTIMES,
8
8
  validateResource,
9
9
  } from '../index.js'
10
- import {resolveDebounceDuration} from '../utils/debounce.js'
10
+ import {resolveDuration} from '../utils/parse-duration.js'
11
11
  import {isReference} from '../utils/validation.js'
12
12
 
13
13
  type BaseFunctionEventKey = keyof BlueprintFunctionBaseResourceEvent
@@ -451,19 +451,7 @@ export function validateQueueFunction(functionResource: unknown): BlueprintError
451
451
  }
452
452
 
453
453
  if ('concurrency' in functionResource) {
454
- if (typeof functionResource.concurrency !== 'number') {
455
- errors.push({type: 'invalid_type', message: '`concurrency` must be a number'})
456
- } else if (functionResource.concurrency < 1) {
457
- errors.push({type: 'invalid_type', message: '`concurrency` must be at least 1'})
458
- } else if (functionResource.concurrency > 500) {
459
- errors.push({type: 'invalid_value', message: '`concurrency` must be less than 500'})
460
- }
461
- }
462
- if ('fifo' in functionResource && typeof functionResource.fifo !== 'boolean') {
463
- errors.push({type: 'invalid_type', message: '`fifo` must be a boolean'})
464
- }
465
- if ('dlq' in functionResource && typeof functionResource.dlq !== 'boolean') {
466
- errors.push({type: 'invalid_type', message: '`dlq` must be a boolean'})
454
+ errors.push(...validateConcurrencyConfig(functionResource.concurrency))
467
455
  }
468
456
 
469
457
  if ('event' in functionResource && typeof functionResource.event !== 'undefined') {
@@ -474,6 +462,22 @@ export function validateQueueFunction(functionResource: unknown): BlueprintError
474
462
  errors.push(...validateDebounceConfig(functionResource.debounce))
475
463
  }
476
464
 
465
+ if ('throttle' in functionResource) {
466
+ errors.push(...validateThrottleConfig(functionResource.throttle))
467
+ }
468
+
469
+ if ('maxAttempts' in functionResource) {
470
+ const maxAttemptsErrors = validatePositiveInteger('maxAttempts', functionResource.maxAttempts)
471
+ errors.push(...maxAttemptsErrors)
472
+ if (
473
+ maxAttemptsErrors.length === 0 &&
474
+ typeof functionResource.maxAttempts === 'number' &&
475
+ functionResource.maxAttempts > MAX_QUEUE_ATTEMPTS
476
+ ) {
477
+ errors.push({type: 'invalid_value', message: `\`maxAttempts\` must be at most ${MAX_QUEUE_ATTEMPTS}`})
478
+ }
479
+ }
480
+
477
481
  errors.push(...validateFunction(functionResource))
478
482
 
479
483
  return errors
@@ -548,18 +552,6 @@ export function validateDurableFunction(functionResource: unknown): BlueprintErr
548
552
  errors.push({type: 'invalid_value', message: '`type` must be `sanity.function.durable`'})
549
553
  }
550
554
 
551
- if ('concurrency' in functionResource && typeof functionResource.concurrency !== 'number') {
552
- errors.push({type: 'invalid_type', message: '`concurrency` must be a number'})
553
- }
554
-
555
- if ('concurrency' in functionResource && typeof functionResource.concurrency === 'number' && functionResource.concurrency < 1) {
556
- errors.push({type: 'invalid_value', message: '`concurrency` must be at least 1'})
557
- }
558
-
559
- if ('concurrency' in functionResource && typeof functionResource.concurrency === 'number' && functionResource.concurrency > 500) {
560
- errors.push({type: 'invalid_value', message: '`concurrency` must be less than 500'})
561
- }
562
-
563
555
  if ('durableTimeout' in functionResource && typeof functionResource.durableTimeout !== 'number') {
564
556
  errors.push({type: 'invalid_type', message: '`durableTimeout` must be a number'})
565
557
  }
@@ -577,11 +569,67 @@ export function validateDurableFunction(functionResource: unknown): BlueprintErr
577
569
  errors.push(...validateFunctionContentLakeEvent(functionResource.event))
578
570
  }
579
571
 
580
- if ('debounce' in functionResource) {
581
- errors.push(...validateDebounceConfig(functionResource.debounce))
572
+ errors.push(...validateFunction(functionResource))
573
+
574
+ return errors
575
+ }
576
+
577
+ /**
578
+ * Validates the `concurrency` config of a queue function.
579
+ * @param concurrency The config to validate
580
+ * @alpha
581
+ * @hidden
582
+ * @category Functions Types
583
+ * @returns Array of validation errors, empty if valid
584
+ */
585
+ export function validateConcurrencyConfig(concurrency: unknown): BlueprintError[] {
586
+ if (concurrency === undefined || concurrency === null) {
587
+ return [{type: 'invalid_value', message: '`concurrency` must be provided'}]
588
+ }
589
+ if (typeof concurrency !== 'object' || Array.isArray(concurrency)) {
590
+ return [{type: 'invalid_type', message: '`concurrency` must be an object, e.g. `{limit: 5}`'}]
582
591
  }
583
592
 
584
- errors.push(...validateFunction(functionResource))
593
+ const errors: BlueprintError[] = []
594
+
595
+ let limit: number | undefined
596
+ if (!('limit' in concurrency)) {
597
+ errors.push({type: 'missing_parameter', message: '`concurrency.limit` must be provided'})
598
+ } else {
599
+ const limitErrors = validatePositiveInteger('concurrency.limit', concurrency.limit)
600
+ errors.push(...limitErrors)
601
+ if (limitErrors.length === 0 && typeof concurrency.limit === 'number') {
602
+ if (concurrency.limit > MAX_QUEUE_CONCURRENCY) {
603
+ errors.push({type: 'invalid_value', message: `\`concurrency.limit\` must be at most ${MAX_QUEUE_CONCURRENCY}`})
604
+ } else {
605
+ limit = concurrency.limit
606
+ }
607
+ }
608
+ }
609
+
610
+ let fifo = false
611
+ if ('fifo' in concurrency && concurrency.fifo !== undefined) {
612
+ if (typeof concurrency.fifo === 'boolean') fifo = concurrency.fifo
613
+ else errors.push({type: 'invalid_type', message: '`concurrency.fifo` must be a boolean'})
614
+ }
615
+
616
+ const hasKey = 'key' in concurrency && concurrency.key !== undefined
617
+ if (hasKey) errors.push(...validateKeyPath(concurrency.key))
618
+
619
+ // Same wording as the functions service, so the CLI and the API read the same.
620
+ if (limit === 1 && !fifo) {
621
+ errors.push({
622
+ type: 'invalid_value',
623
+ message:
624
+ 'A `concurrency` limit of 1 needs `fifo: true`. Use `concurrency: {limit: 1, fifo: true}` to process messages one at a time, in order, or a limit of at least 2',
625
+ })
626
+ } else if (limit !== undefined && limit > 1 && fifo && !hasKey) {
627
+ errors.push({
628
+ type: 'invalid_value',
629
+ message:
630
+ '`concurrency.fifo` with a limit above 1 needs a `key`, or messages are still processed one at a time. Add a `key`, or use a limit of 1',
631
+ })
632
+ }
585
633
 
586
634
  return errors
587
635
  }
@@ -601,7 +649,7 @@ export function validateDebounceConfig(debounce: unknown): BlueprintError[] {
601
649
 
602
650
  // A bare duration is shorthand for `{window: <duration>}`
603
651
  if (typeof debounce === 'string' || typeof debounce === 'number') {
604
- const resolved = resolveDebounceDuration('debounce', debounce)
652
+ const resolved = resolveDuration('debounce', debounce)
605
653
  return resolved.seconds === undefined ? resolved.errors : validateDebounceConfig({window: resolved.seconds})
606
654
  }
607
655
 
@@ -618,7 +666,7 @@ export function validateDebounceConfig(debounce: unknown): BlueprintError[] {
618
666
  }
619
667
 
620
668
  if ('window' in debounce) {
621
- const resolved = resolveDebounceDuration('window', debounce.window)
669
+ const resolved = resolveDuration('window', debounce.window)
622
670
  errors.push(...resolved.errors)
623
671
 
624
672
  if (resolved.seconds !== undefined) {
@@ -631,7 +679,7 @@ export function validateDebounceConfig(debounce: unknown): BlueprintError[] {
631
679
  }
632
680
 
633
681
  if ('maxWindow' in debounce) {
634
- const resolved = resolveDebounceDuration('maxWindow', debounce.maxWindow)
682
+ const resolved = resolveDuration('maxWindow', debounce.maxWindow)
635
683
  errors.push(...resolved.errors)
636
684
 
637
685
  if (resolved.seconds !== undefined) {
@@ -643,14 +691,112 @@ export function validateDebounceConfig(debounce: unknown): BlueprintError[] {
643
691
  }
644
692
  }
645
693
 
646
- // `maxWindow` caps how far `window` can be extended from the first event, so it has to leave room to extend
647
- if (window !== undefined && maxWindow !== undefined && maxWindow <= window) {
648
- errors.push({type: 'invalid_value', message: '`maxWindow` must be greater than `window`'})
694
+ // `maxWindow` caps how far `window` can be extended from the first event, so it can't be shorter than `window`
695
+ if (window !== undefined && maxWindow !== undefined && maxWindow < window) {
696
+ errors.push({type: 'invalid_value', message: '`maxWindow` must be greater than or equal to `window`'})
649
697
  }
650
698
 
651
- if ('key' in debounce && typeof debounce.key !== 'string') {
652
- errors.push({type: 'invalid_type', message: '`key` must be a string'})
699
+ if ('key' in debounce) {
700
+ errors.push(...validateKeyPath(debounce.key))
653
701
  }
654
702
 
655
703
  return errors
656
704
  }
705
+
706
+ /**
707
+ * Validates a throttle config.
708
+ * Only checks shape; rate and period ceilings are enforced by the functions service.
709
+ * @param throttle The config to validate
710
+ * @alpha
711
+ * @hidden
712
+ * @category Functions Types
713
+ * @returns Array of validation errors, empty if valid
714
+ */
715
+ export function validateThrottleConfig(throttle: unknown): BlueprintError[] {
716
+ if (throttle === undefined || throttle === null) {
717
+ return [{type: 'invalid_value', message: 'Throttle config must be provided'}]
718
+ }
719
+
720
+ if (typeof throttle !== 'object') return [{type: 'invalid_type', message: 'Throttle config must be an object'}]
721
+
722
+ const errors: BlueprintError[] = []
723
+
724
+ // `limit` is only kept when it is valid, so the `burst` comparison below doesn't pile onto an existing error
725
+ let limit: number | undefined
726
+
727
+ if (!('limit' in throttle)) {
728
+ errors.push({type: 'missing_parameter', message: '`limit` must be provided'})
729
+ } else {
730
+ const limitErrors = validatePositiveInteger('limit', throttle.limit)
731
+ errors.push(...limitErrors)
732
+ if (limitErrors.length === 0 && typeof throttle.limit === 'number') limit = throttle.limit
733
+ }
734
+
735
+ if (!('period' in throttle)) {
736
+ errors.push({type: 'missing_parameter', message: '`period` must be provided'})
737
+ } else {
738
+ const resolved = resolveDuration('period', throttle.period)
739
+ errors.push(...resolved.errors)
740
+
741
+ if (resolved.seconds !== undefined && (!Number.isInteger(resolved.seconds) || resolved.seconds < 1)) {
742
+ errors.push({type: 'invalid_value', message: '`period` must be a whole number of seconds, at least 1'})
743
+ }
744
+ }
745
+
746
+ if ('burst' in throttle) {
747
+ const burstErrors = validatePositiveInteger('burst', throttle.burst)
748
+ errors.push(...burstErrors)
749
+ if (burstErrors.length === 0 && limit !== undefined && typeof throttle.burst === 'number' && throttle.burst > limit) {
750
+ errors.push({type: 'invalid_value', message: '`burst` must be less than or equal to `limit`'})
751
+ }
752
+ }
753
+
754
+ if ('key' in throttle) {
755
+ errors.push(...validateKeyPath(throttle.key))
756
+ }
757
+
758
+ return errors
759
+ }
760
+
761
+ /**
762
+ * Validates a whole number of at least 1, e.g. a count or a number of attempts.
763
+ * @param field The name of the field being validated, used in error messages
764
+ * @param value The value to validate
765
+ * @returns Array of validation errors, empty if valid
766
+ */
767
+ function validatePositiveInteger(field: string, value: unknown): BlueprintError[] {
768
+ if (typeof value !== 'number') return [{type: 'invalid_type', message: `\`${field}\` must be a number`}]
769
+ if (!Number.isInteger(value) || value < 1) return [{type: 'invalid_value', message: `\`${field}\` must be a whole number of at least 1`}]
770
+ return []
771
+ }
772
+
773
+ /** The functions service's limit on how many times a queue message is delivered before it is moved to a Dead Letter Queue. */
774
+ const MAX_QUEUE_ATTEMPTS = 100
775
+
776
+ /** The functions service's limit on `concurrency.limit`, which follows SQS `MaximumConcurrency` (at most 1000). */
777
+ const MAX_QUEUE_CONCURRENCY = 1000
778
+
779
+ /**
780
+ * Dotted path into the `{event, context}` envelope a queue feature reads its grouping key from, e.g. `event.data.documentId`.
781
+ * Matches the functions service: each segment after the root is an ASCII JavaScript identifier.
782
+ */
783
+ const KEY_PATH_PATTERN = /^(context|event)(\.[A-Za-z_$][\w$]*)+$/
784
+ const MAX_KEY_PATH_LENGTH = 256
785
+
786
+ /**
787
+ * Validates the `key` of a concurrency, debounce or throttle config.
788
+ * @param key The key to validate
789
+ * @returns Array of validation errors, empty if valid
790
+ */
791
+ function validateKeyPath(key: unknown): BlueprintError[] {
792
+ if (typeof key !== 'string') return [{type: 'invalid_type', message: '`key` must be a string'}]
793
+ if (key.length > MAX_KEY_PATH_LENGTH) {
794
+ return [{type: 'invalid_value', message: `\`key\` must be at most ${MAX_KEY_PATH_LENGTH} characters`}]
795
+ }
796
+ if (!KEY_PATH_PATTERN.test(key)) {
797
+ return [
798
+ {type: 'invalid_format', message: '`key` must be a dotted path starting with `event.` or `context.`, e.g. `event.data.documentId`'},
799
+ ]
800
+ }
801
+ return []
802
+ }
@@ -17,5 +17,9 @@ export function validateProject(resource: unknown): BlueprintError[] {
17
17
  errors.push({type: 'invalid_value', message: 'Project type must be `sanity.project`'})
18
18
  }
19
19
 
20
+ if ('logo' in resource && typeof resource.logo !== 'string') {
21
+ errors.push({type: 'invalid_type', message: 'Project logo must be a string'})
22
+ }
23
+
20
24
  return errors
21
25
  }
@@ -109,5 +109,5 @@ export function validateResource(resource: unknown, options?: {projectContained?
109
109
  * @returns The resource
110
110
  */
111
111
  export function assertResource(resource: unknown): asserts resource is BlueprintResource {
112
- runValidation(() => validateResource(resource))
112
+ runValidation(resource, () => validateResource(resource))
113
113
  }
@@ -1,6 +1,7 @@
1
1
  import type {BlueprintError} from '../types/errors.js'
2
2
  import {APPLICATION_VISIBILITIES} from '../types/studios.js'
3
3
  import {isReference, validateStringField} from '../utils/validation.js'
4
+ import {isContainedRelativePath} from './applications.js'
4
5
  import {validateResource} from './resources.js'
5
6
 
6
7
  /** hostname validity pattern */
@@ -52,6 +53,8 @@ export function validateStudio(resource: unknown): BlueprintError[] {
52
53
  if ('icon' in resource) {
53
54
  if (typeof resource.icon !== 'string') {
54
55
  errors.push({type: 'invalid_type', message: 'Studio icon must be a string'})
56
+ } else if (!isReference(resource.icon) && !isContainedRelativePath(resource.icon)) {
57
+ errors.push({type: 'invalid_value', message: 'Studio icon must be a relative path within the studio root'})
55
58
  }
56
59
  }
57
60