@sanity/blueprints 0.7.1 → 0.8.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 (91) hide show
  1. package/dist/definers/blueprints.d.ts +12 -0
  2. package/dist/definers/blueprints.js +12 -0
  3. package/dist/definers/blueprints.js.map +1 -1
  4. package/dist/definers/cors.d.ts +10 -0
  5. package/dist/definers/cors.js +10 -0
  6. package/dist/definers/cors.js.map +1 -1
  7. package/dist/definers/datasets.d.ts +9 -0
  8. package/dist/definers/datasets.js +9 -0
  9. package/dist/definers/datasets.js.map +1 -1
  10. package/dist/definers/functions.d.ts +62 -0
  11. package/dist/definers/functions.js +58 -3
  12. package/dist/definers/functions.js.map +1 -1
  13. package/dist/definers/resources.d.ts +5 -0
  14. package/dist/definers/resources.js +5 -0
  15. package/dist/definers/resources.js.map +1 -1
  16. package/dist/definers/robots.d.ts +18 -0
  17. package/dist/definers/robots.js +30 -0
  18. package/dist/definers/robots.js.map +1 -0
  19. package/dist/definers/roles.d.ts +36 -0
  20. package/dist/definers/roles.js +39 -0
  21. package/dist/definers/roles.js.map +1 -1
  22. package/dist/definers/webhooks.d.ts +13 -0
  23. package/dist/definers/webhooks.js +13 -0
  24. package/dist/definers/webhooks.js.map +1 -1
  25. package/dist/index.d.ts +3 -0
  26. package/dist/index.js +3 -0
  27. package/dist/index.js.map +1 -1
  28. package/dist/types/blueprints.d.ts +5 -5
  29. package/dist/types/cors.d.ts +30 -4
  30. package/dist/types/datasets.d.ts +16 -4
  31. package/dist/types/functions.d.ts +29 -0
  32. package/dist/types/resources.d.ts +47 -1
  33. package/dist/types/robots.d.ts +30 -0
  34. package/dist/types/robots.js +2 -0
  35. package/dist/types/robots.js.map +1 -0
  36. package/dist/types/roles.d.ts +16 -2
  37. package/dist/types/webhooks.d.ts +3 -0
  38. package/dist/utils/validation.d.ts +4 -0
  39. package/dist/utils/validation.js +4 -0
  40. package/dist/utils/validation.js.map +1 -1
  41. package/dist/validation/blueprints.d.ts +5 -0
  42. package/dist/validation/blueprints.js +5 -0
  43. package/dist/validation/blueprints.js.map +1 -1
  44. package/dist/validation/cors.d.ts +6 -1
  45. package/dist/validation/cors.js +7 -1
  46. package/dist/validation/cors.js.map +1 -1
  47. package/dist/validation/datasets.d.ts +6 -1
  48. package/dist/validation/datasets.js +7 -1
  49. package/dist/validation/datasets.js.map +1 -1
  50. package/dist/validation/functions.d.ts +19 -1
  51. package/dist/validation/functions.js +39 -5
  52. package/dist/validation/functions.js.map +1 -1
  53. package/dist/validation/resources.d.ts +9 -1
  54. package/dist/validation/resources.js +55 -1
  55. package/dist/validation/resources.js.map +1 -1
  56. package/dist/validation/robots.d.ts +8 -0
  57. package/dist/validation/robots.js +97 -0
  58. package/dist/validation/robots.js.map +1 -0
  59. package/dist/validation/roles.d.ts +11 -6
  60. package/dist/validation/roles.js +30 -23
  61. package/dist/validation/roles.js.map +1 -1
  62. package/dist/validation/webhooks.d.ts +7 -2
  63. package/dist/validation/webhooks.js +35 -29
  64. package/dist/validation/webhooks.js.map +1 -1
  65. package/package.json +1 -1
  66. package/src/definers/blueprints.ts +12 -0
  67. package/src/definers/cors.ts +10 -0
  68. package/src/definers/datasets.ts +9 -0
  69. package/src/definers/functions.ts +80 -3
  70. package/src/definers/resources.ts +5 -0
  71. package/src/definers/robots.ts +33 -0
  72. package/src/definers/roles.ts +40 -0
  73. package/src/definers/webhooks.ts +13 -0
  74. package/src/index.ts +3 -0
  75. package/src/types/blueprints.ts +5 -5
  76. package/src/types/cors.ts +30 -4
  77. package/src/types/datasets.ts +16 -4
  78. package/src/types/functions.ts +29 -0
  79. package/src/types/resources.ts +54 -1
  80. package/src/types/robots.ts +35 -0
  81. package/src/types/roles.ts +19 -5
  82. package/src/types/webhooks.ts +5 -0
  83. package/src/utils/validation.ts +4 -0
  84. package/src/validation/blueprints.ts +5 -0
  85. package/src/validation/cors.ts +7 -2
  86. package/src/validation/datasets.ts +7 -2
  87. package/src/validation/functions.ts +45 -12
  88. package/src/validation/resources.ts +50 -1
  89. package/src/validation/robots.ts +96 -0
  90. package/src/validation/roles.ts +30 -24
  91. package/src/validation/webhooks.ts +35 -30
@@ -1,5 +1,10 @@
1
1
  import type {BlueprintError} from '../index.js'
2
2
 
3
+ /**
4
+ * Validates that the given input is a valid Blueprint
5
+ * @param blueprintConfig The blueprint configuration to be validated
6
+ * @returns A list of validation errors
7
+ */
3
8
  export function validateBlueprint(blueprintConfig: unknown): BlueprintError[] {
4
9
  if (!blueprintConfig) return [{type: 'invalid_value', message: 'blueprint config must be provided'}]
5
10
  if (typeof blueprintConfig !== 'object') return [{type: 'invalid_type', message: 'blueprint config must be an object'}]
@@ -1,10 +1,15 @@
1
- import type {BlueprintError} from '../index.js'
1
+ import {type BlueprintError, validateResource} from '../index.js'
2
2
 
3
+ /**
4
+ * Validates that the given resource is a valid CORS origin.
5
+ * @param resource The CORS origin resource
6
+ * @returns A list of validation errors
7
+ */
3
8
  export function validateCorsOrigin(resource: unknown): BlueprintError[] {
4
9
  if (!resource) return [{type: 'invalid_value', message: 'CORS Origin config must be provided'}]
5
10
  if (typeof resource !== 'object') return [{type: 'invalid_type', message: 'CORS Origin config must be an object'}]
6
11
 
7
- const errors: BlueprintError[] = []
12
+ const errors: BlueprintError[] = validateResource(resource, {projectContained: true})
8
13
 
9
14
  if (!('name' in resource) || !resource.name) {
10
15
  errors.push({type: 'missing_parameter', message: 'CORS Origin name is required'})
@@ -1,10 +1,15 @@
1
- import type {BlueprintError} from '../index.js'
1
+ import {type BlueprintError, validateResource} from '../index.js'
2
2
 
3
+ /**
4
+ * Validates that the given resource is a valid Dataset.
5
+ * @param resource The Dataset resource
6
+ * @returns A list of validation errors
7
+ */
3
8
  export function validateDataset(resource: unknown): BlueprintError[] {
4
9
  if (!resource) return [{type: 'invalid_value', message: 'Dataset config must be provided'}]
5
10
  if (typeof resource !== 'object') return [{type: 'invalid_type', message: 'Dataset config must be an object'}]
6
11
 
7
- const errors: BlueprintError[] = []
12
+ const errors: BlueprintError[] = validateResource(resource, {projectContained: true})
8
13
 
9
14
  if (!('name' in resource) || !resource.name) {
10
15
  errors.push({type: 'missing_parameter', message: 'Dataset name is required'})
@@ -1,8 +1,9 @@
1
- import type {
2
- BlueprintDocumentFunctionResourceEvent,
3
- BlueprintError,
4
- BlueprintFunctionBaseResourceEvent,
5
- BlueprintMediaLibraryFunctionResourceEvent,
1
+ import {
2
+ type BlueprintDocumentFunctionResourceEvent,
3
+ type BlueprintError,
4
+ type BlueprintFunctionBaseResourceEvent,
5
+ type BlueprintMediaLibraryFunctionResourceEvent,
6
+ validateResource,
6
7
  } from '../index.js'
7
8
 
8
9
  type BaseFunctionEventKey = keyof BlueprintFunctionBaseResourceEvent
@@ -12,11 +13,17 @@ const DOCUMENT_EVENT_KEYS = new Set<DocumentFunctionEventKey>(['includeAllVersio
12
13
  type MediaLibraryFunctionEventKey = keyof BlueprintMediaLibraryFunctionResourceEvent
13
14
  const MEDIA_LIBRARY_EVENT_KEYS = new Set<MediaLibraryFunctionEventKey>(['resource', ...BASE_EVENT_KEYS.values()])
14
15
 
16
+ /**
17
+ * Validates a document function resource configuration.
18
+ * Checks that the function has a valid event configuration, correct type, and all required base properties.
19
+ * @param functionResource The function resource to validate
20
+ * @returns Array of validation errors, empty if valid
21
+ */
15
22
  export function validateDocumentFunction(functionResource: unknown): BlueprintError[] {
16
23
  if (!functionResource) return [{type: 'invalid_value', message: 'Function config must be provided'}]
17
24
  if (typeof functionResource !== 'object') return [{type: 'invalid_type', message: 'Function config must be an object'}]
18
25
 
19
- const errors: BlueprintError[] = []
26
+ const errors: BlueprintError[] = validateFunction(functionResource)
20
27
 
21
28
  // event validation
22
29
  if ('event' in functionResource) {
@@ -38,16 +45,20 @@ export function validateDocumentFunction(functionResource: unknown): BlueprintEr
38
45
  errors.push({type: 'invalid_value', message: '`type` must be `sanity.function.document`'})
39
46
  }
40
47
 
41
- errors.push(...validateFunction(functionResource))
42
-
43
48
  return errors
44
49
  }
45
50
 
51
+ /**
52
+ * Validates a media library asset function resource configuration.
53
+ * Checks that the function has a valid event configuration with required resource, correct type, and all required base properties.
54
+ * @param functionResource The function resource to validate
55
+ * @returns Array of validation errors, empty if valid
56
+ */
46
57
  export function validateMediaLibraryAssetFunction(functionResource: unknown): BlueprintError[] {
47
58
  if (!functionResource) return [{type: 'invalid_value', message: 'Function config must be provided'}]
48
59
  if (typeof functionResource !== 'object') return [{type: 'invalid_type', message: 'Function config must be an object'}]
49
60
 
50
- const errors: BlueprintError[] = []
61
+ const errors: BlueprintError[] = validateFunction(functionResource)
51
62
 
52
63
  if ('event' in functionResource) {
53
64
  errors.push(...validateMediaLibraryFunctionEvent(functionResource.event))
@@ -59,16 +70,20 @@ export function validateMediaLibraryAssetFunction(functionResource: unknown): Bl
59
70
  errors.push({type: 'invalid_value', message: '`type` must be `sanity.function.media-library.asset`'})
60
71
  }
61
72
 
62
- errors.push(...validateFunction(functionResource))
63
-
64
73
  return errors
65
74
  }
66
75
 
76
+ /**
77
+ * Validates base function resource properties.
78
+ * Checks that required fields (name, type) are present and that optional fields have correct types.
79
+ * @param functionResource The function resource to validate
80
+ * @returns Array of validation errors, empty if valid
81
+ */
67
82
  export function validateFunction(functionResource: unknown): BlueprintError[] {
68
83
  if (!functionResource) return [{type: 'invalid_value', message: 'Function config must be provided'}]
69
84
  if (typeof functionResource !== 'object') return [{type: 'invalid_type', message: 'Function config must be an object'}]
70
85
 
71
- const errors: BlueprintError[] = []
86
+ const errors: BlueprintError[] = validateResource(functionResource)
72
87
 
73
88
  if (!('name' in functionResource)) {
74
89
  errors.push({type: 'missing_parameter', message: '`name` is required'})
@@ -94,9 +109,21 @@ export function validateFunction(functionResource: unknown): BlueprintError[] {
94
109
  }
95
110
  }
96
111
 
112
+ if ('robotToken' in functionResource) {
113
+ if (typeof functionResource.robotToken !== 'string' && typeof functionResource.robotToken !== 'undefined') {
114
+ errors.push({type: 'invalid_type', message: '`robotToken` must be a string'})
115
+ }
116
+ }
117
+
97
118
  return errors
98
119
  }
99
120
 
121
+ /**
122
+ * Validates a document function event configuration.
123
+ * Checks event trigger types, optional filter/projection, and optional dataset resource scoping.
124
+ * @param event The event configuration to validate
125
+ * @returns Array of validation errors, empty if valid
126
+ */
100
127
  function validateDocumentFunctionEvent(event: unknown): BlueprintError[] {
101
128
  if (!event) return [{type: 'invalid_value', message: 'Function event must be provided'}]
102
129
  if (typeof event !== 'object') return [{type: 'invalid_type', message: 'Function event must be an object'}]
@@ -120,6 +147,12 @@ function validateDocumentFunctionEvent(event: unknown): BlueprintError[] {
120
147
  return errors
121
148
  }
122
149
 
150
+ /**
151
+ * Validates a media library function event configuration.
152
+ * Checks event trigger types and ensures required media library resource is present.
153
+ * @param event The event configuration to validate
154
+ * @returns Array of validation errors, empty if valid
155
+ */
123
156
  function validateMediaLibraryFunctionEvent(event: unknown): BlueprintError[] {
124
157
  if (!event) return [{type: 'invalid_value', message: 'Function event must be provided'}]
125
158
  if (typeof event !== 'object') return [{type: 'invalid_type', message: 'Function event must be an object'}]
@@ -1,7 +1,13 @@
1
1
  import type {BlueprintError, BlueprintResource} from '../index.js'
2
2
  import {runValidation} from '../utils/validation.js'
3
3
 
4
- export function validateResource(resourceConfig: unknown): BlueprintError[] {
4
+ /**
5
+ * Validates that the given resource is a valid resource.
6
+ * @param resource The resource
7
+ * @param options Validation options
8
+ * @returns A list of validation errors
9
+ */
10
+ export function validateResource(resourceConfig: unknown, options?: {projectContained?: boolean}): BlueprintError[] {
5
11
  if (!resourceConfig) return [{type: 'invalid_value', message: 'Resource config must be provided'}]
6
12
  if (typeof resourceConfig !== 'object') return [{type: 'invalid_type', message: 'Resource config must be an object'}]
7
13
 
@@ -19,6 +25,49 @@ export function validateResource(resourceConfig: unknown): BlueprintError[] {
19
25
  errors.push({type: 'invalid_type', message: '`type` must be a string'})
20
26
  }
21
27
 
28
+ if ('lifecycle' in resourceConfig) {
29
+ if (typeof resourceConfig.lifecycle !== 'object' || resourceConfig.lifecycle === null) {
30
+ errors.push({type: 'invalid_type', message: '`lifecycle` must be an object'})
31
+ } else {
32
+ if ('deletionPolicy' in resourceConfig.lifecycle) {
33
+ if (typeof resourceConfig.lifecycle.deletionPolicy !== 'string') {
34
+ errors.push({type: 'invalid_type', message: '`deletionPolicy` must be a string'})
35
+ } else if (!['allow', 'retain', 'replace', 'protect'].includes(resourceConfig.lifecycle.deletionPolicy)) {
36
+ errors.push({type: 'invalid_value', message: '`deletionPolicy` must be one of allow, retain, replace, protect'})
37
+ }
38
+ }
39
+
40
+ if ('ownershipAction' in resourceConfig.lifecycle) {
41
+ const ownershipAction = resourceConfig.lifecycle.ownershipAction
42
+ if (typeof ownershipAction !== 'object' || ownershipAction === null) {
43
+ errors.push({type: 'invalid_type', message: '`ownershipAction` must be an object'})
44
+ } else if (!('type' in ownershipAction)) {
45
+ errors.push({type: 'missing_parameter', message: '`ownershipAction.type` is required'})
46
+ } else if (typeof ownershipAction.type !== 'string') {
47
+ errors.push({type: 'invalid_type', message: '`ownershipAction.type` must be a string'})
48
+ } else if (!['attach'].includes(ownershipAction.type)) {
49
+ errors.push({type: 'invalid_value', message: '`ownershipAction.type` must be attach'})
50
+ } else {
51
+ if (ownershipAction.type === 'attach') {
52
+ if (!('id' in ownershipAction)) {
53
+ errors.push({type: 'missing_parameter', message: '`ownershipAction.id` is required for attach'})
54
+ } else if (typeof ownershipAction.id !== 'string') {
55
+ errors.push({type: 'invalid_type', message: '`ownershipAction.id` must be a string'})
56
+ }
57
+
58
+ if (options?.projectContained) {
59
+ if (!('projectId' in ownershipAction)) {
60
+ errors.push({type: 'missing_parameter', message: '`ownershipAction.projectId` is required for attach'})
61
+ } else if (typeof ownershipAction.projectId !== 'string') {
62
+ errors.push({type: 'invalid_type', message: '`ownershipAction.projectId` must be a string'})
63
+ }
64
+ }
65
+ }
66
+ }
67
+ }
68
+ }
69
+ }
70
+
22
71
  return errors
23
72
  }
24
73
 
@@ -0,0 +1,96 @@
1
+ import {type BlueprintError, validateResource} from '../index.js'
2
+
3
+ /**
4
+ * Validates that the given resource is a valid Robot.
5
+ * @param resource The Robot resource
6
+ * @returns A list of validation errors
7
+ */
8
+ export function validateRobot(resource: unknown): BlueprintError[] {
9
+ if (!resource) return [{type: 'invalid_value', message: 'Robot config must be provided'}]
10
+ if (typeof resource !== 'object') return [{type: 'invalid_type', message: 'Robot config must be an object'}]
11
+
12
+ const errors: BlueprintError[] = validateResource(resource, {projectContained: true})
13
+
14
+ if (!('name' in resource) || !resource.name) {
15
+ errors.push({type: 'missing_parameter', message: 'Robot name is required'})
16
+ } else if (typeof resource.name !== 'string') {
17
+ errors.push({type: 'invalid_type', message: 'Robot name must be a string'})
18
+ }
19
+
20
+ if (!('type' in resource)) {
21
+ errors.push({type: 'missing_parameter', message: 'Robot type is required'})
22
+ } else if (resource.type !== 'sanity.access.robot') {
23
+ errors.push({type: 'invalid_value', message: 'Robot type must be `sanity.access.robot`'})
24
+ }
25
+
26
+ if (!('label' in resource) || !resource.label) {
27
+ errors.push({type: 'missing_parameter', message: 'Robot label is required'})
28
+ } else if (typeof resource.label !== 'string') {
29
+ errors.push({type: 'invalid_type', message: 'Robot label must be a string'})
30
+ }
31
+
32
+ if (!('memberships' in resource)) {
33
+ errors.push({type: 'missing_parameter', message: 'Robot memberships array is required'})
34
+ } else if (!Array.isArray(resource.memberships)) {
35
+ errors.push({type: 'invalid_type', message: 'Robot memberships must be an array'})
36
+ } else if (resource.memberships.length === 0) {
37
+ errors.push({type: 'invalid_value', message: 'Robot must have at least one membership'})
38
+ } else {
39
+ errors.push(...resource.memberships.flatMap(validateRobotMembership))
40
+ }
41
+
42
+ if ('resourceType' in resource) {
43
+ if (resource.resourceType !== 'organization' && resource.resourceType !== 'project') {
44
+ errors.push({type: 'invalid_value', message: 'Robot resource type must be `organization` or `project`'})
45
+ }
46
+ if (!('resourceId' in resource)) {
47
+ errors.push({type: 'missing_parameter', message: 'Robot resource ID is required when resource type is provided'})
48
+ }
49
+ }
50
+
51
+ if ('resourceId' in resource) {
52
+ if (typeof resource.resourceId !== 'string') {
53
+ errors.push({type: 'invalid_type', message: 'Robot resource ID must be a string'})
54
+ }
55
+ if (!('resourceType' in resource)) {
56
+ errors.push({type: 'missing_parameter', message: 'Robot resource type is required when resource ID is provided'})
57
+ }
58
+ }
59
+
60
+ return errors
61
+ }
62
+
63
+ export function validateRobotMembership(membership: unknown): BlueprintError[] {
64
+ if (!membership) return [{type: 'invalid_value', message: 'Membership config must be provided'}]
65
+ if (typeof membership !== 'object') return [{type: 'invalid_type', message: 'Membership config must be an object'}]
66
+
67
+ const errors: BlueprintError[] = []
68
+
69
+ if (!('resourceType' in membership)) {
70
+ errors.push({type: 'missing_parameter', message: 'Membership resource type is required'})
71
+ } else if (membership.resourceType !== 'organization' && membership.resourceType !== 'project') {
72
+ errors.push({type: 'invalid_value', message: 'Membership resource type must be `organization` or `project`'})
73
+ }
74
+
75
+ if (!('resourceId' in membership) || !membership.resourceId) {
76
+ errors.push({type: 'missing_parameter', message: 'Membership resource ID is required'})
77
+ } else if (typeof membership.resourceId !== 'string') {
78
+ errors.push({type: 'invalid_type', message: 'Membership resource ID must be a string'})
79
+ }
80
+
81
+ if (!('roleNames' in membership)) {
82
+ errors.push({type: 'missing_parameter', message: 'Membership role names array is required'})
83
+ } else if (!Array.isArray(membership.roleNames)) {
84
+ errors.push({type: 'invalid_type', message: 'Membership role names must be an array'})
85
+ } else if (membership.roleNames.length === 0) {
86
+ errors.push({type: 'invalid_value', message: 'Membership must have at least one role name'})
87
+ } else {
88
+ for (const roleName of membership.roleNames) {
89
+ if (!roleName || typeof roleName !== 'string') {
90
+ errors.push({type: 'invalid_value', message: 'Membership role name must have at least one character'})
91
+ }
92
+ }
93
+ }
94
+
95
+ return errors
96
+ }
@@ -1,57 +1,63 @@
1
- import type {BlueprintError} from '../index.js'
1
+ import {type BlueprintError, validateResource} from '../index.js'
2
2
 
3
3
  /**
4
- * Validates a role.
5
- * @param parameters The configuration of the role
6
- * @returns The errors
4
+ * Validates that the given resource is a valid Role.
5
+ * @param resource The Role resource
6
+ * @returns A list of validation errors
7
7
  */
8
- export function validateRole(parameters: unknown): BlueprintError[] {
9
- if (!parameters) return [{type: 'invalid_value', message: 'Role config must be provided'}]
10
- if (typeof parameters !== 'object') return [{type: 'invalid_type', message: 'Role config must be an object'}]
8
+ export function validateRole(resource: unknown): BlueprintError[] {
9
+ if (!resource) return [{type: 'invalid_value', message: 'Role config must be provided'}]
10
+ if (typeof resource !== 'object') return [{type: 'invalid_type', message: 'Role config must be an object'}]
11
11
 
12
- const errors: BlueprintError[] = []
12
+ const errors: BlueprintError[] = validateResource(resource, {projectContained: true})
13
13
 
14
- if (!('name' in parameters)) {
14
+ if (!('name' in resource)) {
15
15
  errors.push({type: 'missing_parameter', message: 'Role name is required'})
16
- } else if (typeof parameters.name !== 'string') {
16
+ } else if (typeof resource.name !== 'string') {
17
17
  errors.push({type: 'invalid_type', message: 'Role name must be a string'})
18
18
  }
19
19
 
20
- if (!('type' in parameters)) {
20
+ if (!('type' in resource)) {
21
21
  errors.push({type: 'missing_parameter', message: 'Role type is required'})
22
- } else if (parameters.type !== 'sanity.access.role') {
22
+ } else if (resource.type !== 'sanity.access.role') {
23
23
  errors.push({type: 'invalid_value', message: 'Role type must be `sanity.access.role`'})
24
24
  }
25
25
 
26
- if (!('title' in parameters)) {
26
+ if (!('title' in resource)) {
27
27
  errors.push({type: 'missing_parameter', message: 'Role title is required'})
28
- } else if (typeof parameters.title !== 'string') {
28
+ } else if (typeof resource.title !== 'string') {
29
29
  errors.push({type: 'invalid_type', message: 'Role title must be a string'})
30
- } else if (parameters.title.length > 100) {
30
+ } else if (resource.title.length > 100) {
31
31
  errors.push({type: 'invalid_value', message: 'Role title must be 100 characters or less'})
32
32
  }
33
33
 
34
- if (!('permissions' in parameters) || !Array.isArray(parameters.permissions) || parameters.permissions.length < 1) {
34
+ if (!('permissions' in resource) || !Array.isArray(resource.permissions) || resource.permissions.length < 1) {
35
35
  errors.push({type: 'invalid_value', message: 'Role must have at least one permission'})
36
36
  }
37
37
 
38
38
  return errors
39
39
  }
40
40
 
41
- export function validateProjectRole(parameters: unknown): BlueprintError[] {
42
- const errors = validateRole(parameters)
41
+ /**
42
+ * Validates that the given resource is a valid Role.
43
+ * @param resource The Role resource
44
+ * @returns A list of validation errors
45
+ */
46
+ export function validateProjectRole(resource: unknown): BlueprintError[] {
47
+ // validate the main part of the role resource
48
+ const errors = validateRole(resource)
43
49
 
44
- // only do validation if parameters is provided, otherwise just return the errors
45
- if (parameters && typeof parameters === 'object') {
46
- if (!('resourceType' in parameters)) {
50
+ // only do validation if resource is provided, otherwise just return the errors
51
+ if (resource && typeof resource === 'object') {
52
+ if (!('resourceType' in resource)) {
47
53
  errors.push({type: 'missing_parameter', message: 'Role resource type must be `project`'})
48
- } else if (parameters.resourceType !== 'project') {
54
+ } else if (resource.resourceType !== 'project') {
49
55
  errors.push({type: 'invalid_value', message: 'Role resource type must be `project`'})
50
56
  }
51
57
 
52
- if (!('resourceId' in parameters)) {
58
+ if (!('resourceId' in resource)) {
53
59
  errors.push({type: 'missing_parameter', message: 'Role resource ID is required'})
54
- } else if (typeof parameters.resourceId !== 'string') {
60
+ } else if (typeof resource.resourceId !== 'string') {
55
61
  errors.push({type: 'invalid_type', message: 'Role resource ID must be a string'})
56
62
  }
57
63
  }
@@ -1,51 +1,56 @@
1
- import type {BlueprintError} from '../index.js'
1
+ import {type BlueprintError, validateResource} from '../index.js'
2
2
 
3
- export function validateDocumentWebhook(parameters: unknown): BlueprintError[] {
4
- if (!parameters) return [{type: 'invalid_value', message: 'Webhook config must be provided'}]
5
- if (typeof parameters !== 'object') return [{type: 'invalid_type', message: 'Webhook config must be an object'}]
3
+ /**
4
+ * Validates that the given resource is a valid Document Webhook.
5
+ * @param resource The Document Webhook resource
6
+ * @returns A list of validation errors
7
+ */
8
+ export function validateDocumentWebhook(resource: unknown): BlueprintError[] {
9
+ if (!resource) return [{type: 'invalid_value', message: 'Webhook config must be provided'}]
10
+ if (typeof resource !== 'object') return [{type: 'invalid_type', message: 'Webhook config must be an object'}]
6
11
 
7
- const errors: BlueprintError[] = []
12
+ const errors: BlueprintError[] = validateResource(resource, {projectContained: true})
8
13
 
9
- if (!('name' in parameters)) {
14
+ if (!('name' in resource)) {
10
15
  errors.push({type: 'missing_parameter', message: 'Webhook name is required'})
11
- } else if (typeof parameters.name !== 'string') {
16
+ } else if (typeof resource.name !== 'string') {
12
17
  errors.push({type: 'invalid_type', message: 'Webhook name must be a string'})
13
18
  }
14
19
 
15
- if (!('type' in parameters)) {
20
+ if (!('type' in resource)) {
16
21
  errors.push({type: 'missing_parameter', message: 'Webhook type is required'})
17
- } else if (parameters.type !== 'sanity.project.webhook') {
22
+ } else if (resource.type !== 'sanity.project.webhook') {
18
23
  errors.push({type: 'invalid_value', message: 'Webhook type must be `sanity.project.webhook`'})
19
24
  }
20
25
 
21
- if ('displayName' in parameters) {
22
- if (typeof parameters.displayName !== 'string') {
26
+ if ('displayName' in resource) {
27
+ if (typeof resource.displayName !== 'string') {
23
28
  errors.push({type: 'invalid_type', message: 'Display name must be a string'})
24
- } else if (parameters.displayName.length > 100) {
29
+ } else if (resource.displayName.length > 100) {
25
30
  errors.push({type: 'invalid_value', message: 'Display name must be 100 characters or less'})
26
31
  }
27
32
  }
28
33
 
29
- if (!('url' in parameters)) {
34
+ if (!('url' in resource)) {
30
35
  errors.push({type: 'missing_parameter', message: 'Webhook URL is required'})
31
- } else if (typeof parameters.url !== 'string') {
36
+ } else if (typeof resource.url !== 'string') {
32
37
  errors.push({type: 'invalid_type', message: 'Webhook URL must be a string'})
33
38
  } else {
34
39
  // Validate URL format
35
40
  try {
36
- new URL(parameters.url)
41
+ new URL(resource.url)
37
42
  } catch {
38
43
  errors.push({type: 'invalid_value', message: 'Webhook URL must be a valid URL'})
39
44
  }
40
45
  }
41
46
 
42
- if (!('on' in parameters) || !Array.isArray(parameters.on) || parameters.on.length === 0) {
47
+ if (!('on' in resource) || !Array.isArray(resource.on) || resource.on.length === 0) {
43
48
  errors.push({type: 'invalid_value', message: 'At least one event type must be specified in the "on" field'})
44
49
  } else {
45
50
  // Validate event types
46
51
  const validEvents = ['create', 'update', 'delete']
47
- if (parameters.on) {
48
- const invalidEvents = parameters.on.filter((event: string) => !validEvents.includes(event))
52
+ if (resource.on) {
53
+ const invalidEvents = resource.on.filter((event: string) => !validEvents.includes(event))
49
54
  if (invalidEvents.length > 0) {
50
55
  errors.push({
51
56
  type: 'invalid_value',
@@ -56,20 +61,20 @@ export function validateDocumentWebhook(parameters: unknown): BlueprintError[] {
56
61
  }
57
62
 
58
63
  // Validate dataset pattern
59
- if (!('dataset' in parameters)) {
64
+ if (!('dataset' in resource)) {
60
65
  errors.push({type: 'missing_parameter', message: 'Webhook dataset is required'})
61
- } else if (typeof parameters.dataset !== 'string' || !/^[a-z0-9-_]+$/.test(parameters.dataset)) {
66
+ } else if (typeof resource.dataset !== 'string' || !/^[a-z0-9-_]+$/.test(resource.dataset)) {
62
67
  errors.push({type: 'invalid_format', message: 'Dataset must match pattern: ^[a-z0-9-_]+$'})
63
68
  }
64
69
 
65
70
  // Validate HTTP method
66
- if ('httpMethod' in parameters) {
71
+ if ('httpMethod' in resource) {
67
72
  const validMethods = ['POST', 'PUT', 'PATCH', 'DELETE', 'GET']
68
- const message = `Invalid HTTP method: ${parameters.httpMethod}. Valid methods are: ${validMethods.join(', ')}`
69
- if (typeof parameters.httpMethod !== 'string') {
73
+ const message = `Invalid HTTP method: ${resource.httpMethod}. Valid methods are: ${validMethods.join(', ')}`
74
+ if (typeof resource.httpMethod !== 'string') {
70
75
  errors.push({type: 'invalid_type', message})
71
76
  } else {
72
- if (!validMethods.includes(parameters.httpMethod)) {
77
+ if (!validMethods.includes(resource.httpMethod)) {
73
78
  errors.push({
74
79
  type: 'invalid_value',
75
80
  message,
@@ -79,22 +84,22 @@ export function validateDocumentWebhook(parameters: unknown): BlueprintError[] {
79
84
  }
80
85
 
81
86
  // Validate status
82
- if ('status' in parameters) {
87
+ if ('status' in resource) {
83
88
  const message = 'Status must be either "enabled" or "disabled"'
84
- if (typeof parameters.status !== 'string') {
89
+ if (typeof resource.status !== 'string') {
85
90
  errors.push({type: 'invalid_type', message})
86
- } else if (!['enabled', 'disabled'].includes(parameters.status)) {
91
+ } else if (!['enabled', 'disabled'].includes(resource.status)) {
87
92
  errors.push({type: 'invalid_value', message})
88
93
  }
89
94
  }
90
95
 
91
96
  // Validate headers pattern
92
- if ('headers' in parameters) {
93
- if (typeof parameters.headers !== 'object' || !parameters.headers || Array.isArray(parameters.headers)) {
97
+ if ('headers' in resource) {
98
+ if (typeof resource.headers !== 'object' || !resource.headers || Array.isArray(resource.headers)) {
94
99
  errors.push({type: 'invalid_type', message: 'Webhook headers must be an object'})
95
100
  } else {
96
101
  const headerNamePattern = /^[a-zA-Z][a-zA-Z0-9-_]*$/
97
- for (const [key, value] of Object.entries(parameters.headers)) {
102
+ for (const [key, value] of Object.entries(resource.headers)) {
98
103
  if (!headerNamePattern.test(key)) {
99
104
  errors.push({type: 'invalid_format', message: `Header key "${key}" must match pattern: ${headerNamePattern.source}`})
100
105
  }