@sanity/blueprints 0.35.0 → 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 (97) hide show
  1. package/README.md +1 -2
  2. package/dist/definers/agents.js +1 -1
  3. package/dist/definers/agents.js.map +1 -1
  4. package/dist/definers/applications.js +6 -6
  5. package/dist/definers/applications.js.map +1 -1
  6. package/dist/definers/blueprints.js +4 -2
  7. package/dist/definers/blueprints.js.map +1 -1
  8. package/dist/definers/context.js +2 -2
  9. package/dist/definers/context.js.map +1 -1
  10. package/dist/definers/cors.js +1 -1
  11. package/dist/definers/cors.js.map +1 -1
  12. package/dist/definers/datasets.d.ts +18 -1
  13. package/dist/definers/datasets.js +21 -2
  14. package/dist/definers/datasets.js.map +1 -1
  15. package/dist/definers/functions/document.d.ts +1 -2
  16. package/dist/definers/functions/document.js +1 -1
  17. package/dist/definers/functions/document.js.map +1 -1
  18. package/dist/definers/functions/durable.js +1 -1
  19. package/dist/definers/functions/durable.js.map +1 -1
  20. package/dist/definers/functions/index.js +1 -1
  21. package/dist/definers/functions/index.js.map +1 -1
  22. package/dist/definers/functions/media-library.d.ts +1 -2
  23. package/dist/definers/functions/media-library.js +2 -3
  24. package/dist/definers/functions/media-library.js.map +1 -1
  25. package/dist/definers/functions/pubsub.js +1 -1
  26. package/dist/definers/functions/pubsub.js.map +1 -1
  27. package/dist/definers/functions/queue.d.ts +16 -7
  28. package/dist/definers/functions/queue.js +18 -10
  29. package/dist/definers/functions/queue.js.map +1 -1
  30. package/dist/definers/functions/scheduled.js +1 -1
  31. package/dist/definers/functions/scheduled.js.map +1 -1
  32. package/dist/definers/functions/sync-tag-invalidate.js +1 -1
  33. package/dist/definers/functions/sync-tag-invalidate.js.map +1 -1
  34. package/dist/definers/installation-config.js +1 -1
  35. package/dist/definers/installation-config.js.map +1 -1
  36. package/dist/definers/permissions.js +4 -3
  37. package/dist/definers/permissions.js.map +1 -1
  38. package/dist/definers/projects.js +1 -1
  39. package/dist/definers/projects.js.map +1 -1
  40. package/dist/definers/robotTokens.d.ts +1 -2
  41. package/dist/definers/robotTokens.js +38 -4
  42. package/dist/definers/robotTokens.js.map +1 -1
  43. package/dist/definers/roles.d.ts +22 -30
  44. package/dist/definers/roles.js +39 -38
  45. package/dist/definers/roles.js.map +1 -1
  46. package/dist/definers/studios.js +1 -1
  47. package/dist/definers/studios.js.map +1 -1
  48. package/dist/definers/webhooks.js +3 -5
  49. package/dist/definers/webhooks.js.map +1 -1
  50. package/dist/types/applications.d.ts +1 -1
  51. package/dist/types/functions/index.d.ts +36 -11
  52. package/dist/types/installation-config.d.ts +1 -1
  53. package/dist/types/robotTokens.d.ts +14 -1
  54. package/dist/types/roles.d.ts +10 -5
  55. package/dist/types/studios.d.ts +1 -1
  56. package/dist/utils/validation.d.ts +14 -11
  57. package/dist/utils/validation.js +25 -17
  58. package/dist/utils/validation.js.map +1 -1
  59. package/dist/validation/blueprints.js +120 -4
  60. package/dist/validation/blueprints.js.map +1 -1
  61. package/dist/validation/functions.d.ts +9 -0
  62. package/dist/validation/functions.js +65 -32
  63. package/dist/validation/functions.js.map +1 -1
  64. package/dist/validation/resources.js +1 -1
  65. package/dist/validation/resources.js.map +1 -1
  66. package/package.json +1 -1
  67. package/src/definers/agents.ts +1 -1
  68. package/src/definers/applications.ts +6 -6
  69. package/src/definers/blueprints.ts +3 -2
  70. package/src/definers/context.ts +2 -2
  71. package/src/definers/cors.ts +1 -1
  72. package/src/definers/datasets.ts +29 -2
  73. package/src/definers/functions/document.ts +2 -3
  74. package/src/definers/functions/durable.ts +1 -1
  75. package/src/definers/functions/index.ts +1 -1
  76. package/src/definers/functions/media-library.ts +2 -3
  77. package/src/definers/functions/pubsub.ts +1 -1
  78. package/src/definers/functions/queue.ts +18 -10
  79. package/src/definers/functions/scheduled.ts +1 -1
  80. package/src/definers/functions/sync-tag-invalidate.ts +1 -1
  81. package/src/definers/installation-config.ts +1 -1
  82. package/src/definers/permissions.ts +6 -4
  83. package/src/definers/projects.ts +1 -1
  84. package/src/definers/robotTokens.ts +40 -4
  85. package/src/definers/roles.ts +45 -41
  86. package/src/definers/studios.ts +1 -1
  87. package/src/definers/webhooks.ts +3 -6
  88. package/src/types/applications.ts +1 -1
  89. package/src/types/functions/index.ts +37 -11
  90. package/src/types/installation-config.ts +1 -1
  91. package/src/types/robotTokens.ts +15 -1
  92. package/src/types/roles.ts +10 -5
  93. package/src/types/studios.ts +5 -1
  94. package/src/utils/validation.ts +24 -17
  95. package/src/validation/blueprints.ts +132 -5
  96. package/src/validation/functions.ts +68 -33
  97. package/src/validation/resources.ts +1 -1
@@ -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
+ }
@@ -451,16 +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'})
454
+ errors.push(...validateConcurrencyConfig(functionResource.concurrency))
464
455
  }
465
456
 
466
457
  if ('event' in functionResource && typeof functionResource.event !== 'undefined') {
@@ -561,18 +552,6 @@ export function validateDurableFunction(functionResource: unknown): BlueprintErr
561
552
  errors.push({type: 'invalid_value', message: '`type` must be `sanity.function.durable`'})
562
553
  }
563
554
 
564
- if ('concurrency' in functionResource && typeof functionResource.concurrency !== 'number') {
565
- errors.push({type: 'invalid_type', message: '`concurrency` must be a number'})
566
- }
567
-
568
- if ('concurrency' in functionResource && typeof functionResource.concurrency === 'number' && functionResource.concurrency < 1) {
569
- errors.push({type: 'invalid_value', message: '`concurrency` must be at least 1'})
570
- }
571
-
572
- if ('concurrency' in functionResource && typeof functionResource.concurrency === 'number' && functionResource.concurrency > 500) {
573
- errors.push({type: 'invalid_value', message: '`concurrency` must be less than 500'})
574
- }
575
-
576
555
  if ('durableTimeout' in functionResource && typeof functionResource.durableTimeout !== 'number') {
577
556
  errors.push({type: 'invalid_type', message: '`durableTimeout` must be a number'})
578
557
  }
@@ -590,11 +569,67 @@ export function validateDurableFunction(functionResource: unknown): BlueprintErr
590
569
  errors.push(...validateFunctionContentLakeEvent(functionResource.event))
591
570
  }
592
571
 
593
- if ('debounce' in functionResource) {
594
- 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}`'}]
595
591
  }
596
592
 
597
- 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
+ }
598
633
 
599
634
  return errors
600
635
  }
@@ -735,21 +770,21 @@ function validatePositiveInteger(field: string, value: unknown): BlueprintError[
735
770
  return []
736
771
  }
737
772
 
738
- /**
739
- * SQS's hard limit on how many times a message can be received before it is moved to a Dead Letter Queue.
740
- * The functions service may set a lower limit.
741
- */
742
- const MAX_QUEUE_ATTEMPTS = 1000
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
743
778
 
744
779
  /**
745
780
  * Dotted path into the `{event, context}` envelope a queue feature reads its grouping key from, e.g. `event.data.documentId`.
746
- * Each segment after the root is a JavaScript identifier, so non-ASCII property names such as `event.data.vær` are allowed.
781
+ * Matches the functions service: each segment after the root is an ASCII JavaScript identifier.
747
782
  */
748
- const KEY_PATH_PATTERN = /^(context|event)(\.[\p{ID_Start}$_][\p{ID_Continue}$\u200C\u200D]*)+$/u
783
+ const KEY_PATH_PATTERN = /^(context|event)(\.[A-Za-z_$][\w$]*)+$/
749
784
  const MAX_KEY_PATH_LENGTH = 256
750
785
 
751
786
  /**
752
- * Validates the `key` of a debounce or throttle config.
787
+ * Validates the `key` of a concurrency, debounce or throttle config.
753
788
  * @param key The key to validate
754
789
  * @returns Array of validation errors, empty if valid
755
790
  */
@@ -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
  }