@sanity/blueprints 0.5.0 → 0.7.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 (99) hide show
  1. package/dist/definers/blueprints.d.ts +1 -22
  2. package/dist/definers/blueprints.js +3 -2
  3. package/dist/definers/blueprints.js.map +1 -1
  4. package/dist/definers/cors.d.ts +1 -15
  5. package/dist/definers/cors.js +5 -12
  6. package/dist/definers/cors.js.map +1 -1
  7. package/dist/definers/datasets.d.ts +1 -15
  8. package/dist/definers/datasets.js +5 -19
  9. package/dist/definers/datasets.js.map +1 -1
  10. package/dist/definers/functions.d.ts +4 -41
  11. package/dist/definers/functions.js +27 -37
  12. package/dist/definers/functions.js.map +1 -1
  13. package/dist/definers/resources.d.ts +1 -1
  14. package/dist/definers/resources.js +4 -8
  15. package/dist/definers/resources.js.map +1 -1
  16. package/dist/definers/roles.d.ts +4 -22
  17. package/dist/definers/roles.js +11 -21
  18. package/dist/definers/roles.js.map +1 -1
  19. package/dist/definers/webhooks.d.ts +1 -21
  20. package/dist/definers/webhooks.js +8 -64
  21. package/dist/definers/webhooks.js.map +1 -1
  22. package/dist/index.d.ts +15 -1
  23. package/dist/index.js +18 -1
  24. package/dist/index.js.map +1 -1
  25. package/dist/types/blueprints.d.ts +22 -0
  26. package/dist/types/blueprints.js +2 -0
  27. package/dist/types/blueprints.js.map +1 -0
  28. package/dist/types/cors.d.ts +15 -0
  29. package/dist/types/cors.js +2 -0
  30. package/dist/types/cors.js.map +1 -0
  31. package/dist/types/datasets.d.ts +15 -0
  32. package/dist/types/datasets.js +2 -0
  33. package/dist/types/datasets.js.map +1 -0
  34. package/dist/types/errors.d.ts +5 -0
  35. package/dist/types/errors.js +2 -0
  36. package/dist/types/errors.js.map +1 -0
  37. package/dist/types/functions.d.ts +41 -0
  38. package/dist/types/functions.js +2 -0
  39. package/dist/types/functions.js.map +1 -0
  40. package/dist/types/resources.js +2 -0
  41. package/dist/types/resources.js.map +1 -0
  42. package/dist/types/roles.d.ts +21 -0
  43. package/dist/types/roles.js +2 -0
  44. package/dist/types/roles.js.map +1 -0
  45. package/dist/types/webhooks.d.ts +21 -0
  46. package/dist/types/webhooks.js +2 -0
  47. package/dist/types/webhooks.js.map +1 -0
  48. package/dist/utils/validation.d.ts +2 -0
  49. package/dist/utils/validation.js +8 -0
  50. package/dist/utils/validation.js.map +1 -0
  51. package/dist/validation/blueprints.d.ts +2 -0
  52. package/dist/validation/blueprints.js +14 -0
  53. package/dist/validation/blueprints.js.map +1 -0
  54. package/dist/validation/cors.d.ts +2 -0
  55. package/dist/validation/cors.js +32 -0
  56. package/dist/validation/cors.js.map +1 -0
  57. package/dist/validation/datasets.d.ts +2 -0
  58. package/dist/validation/datasets.js +39 -0
  59. package/dist/validation/datasets.js.map +1 -0
  60. package/dist/validation/functions.d.ts +4 -0
  61. package/dist/validation/functions.js +125 -0
  62. package/dist/validation/functions.js.map +1 -0
  63. package/dist/validation/resources.d.ts +3 -0
  64. package/dist/validation/resources.js +25 -0
  65. package/dist/validation/resources.js.map +1 -0
  66. package/dist/validation/roles.d.ts +8 -0
  67. package/dist/validation/roles.js +57 -0
  68. package/dist/validation/roles.js.map +1 -0
  69. package/dist/validation/webhooks.d.ts +2 -0
  70. package/dist/validation/webhooks.js +110 -0
  71. package/dist/validation/webhooks.js.map +1 -0
  72. package/package.json +1 -1
  73. package/src/definers/blueprints.ts +3 -27
  74. package/src/definers/cors.ts +7 -34
  75. package/src/definers/datasets.ts +7 -42
  76. package/src/definers/functions.ts +64 -93
  77. package/src/definers/resources.ts +6 -8
  78. package/src/definers/roles.ts +20 -50
  79. package/src/definers/webhooks.ts +10 -95
  80. package/src/index.ts +20 -1
  81. package/src/types/blueprints.ts +26 -0
  82. package/src/types/cors.ts +18 -0
  83. package/src/types/datasets.ts +19 -0
  84. package/src/types/errors.ts +5 -0
  85. package/src/types/functions.ts +48 -0
  86. package/src/types/roles.ts +25 -0
  87. package/src/types/webhooks.ts +22 -0
  88. package/src/utils/validation.ts +9 -0
  89. package/src/validation/blueprints.ts +16 -0
  90. package/src/validation/cors.ts +34 -0
  91. package/src/validation/datasets.ts +41 -0
  92. package/src/validation/functions.ts +144 -0
  93. package/src/validation/resources.ts +27 -0
  94. package/src/validation/roles.ts +60 -0
  95. package/src/validation/webhooks.ts +109 -0
  96. package/dist/types.js +0 -2
  97. package/dist/types.js.map +0 -1
  98. /package/dist/{types.d.ts → types/resources.d.ts} +0 -0
  99. /package/src/{types.ts → types/resources.ts} +0 -0
@@ -1,57 +1,19 @@
1
- import type {BlueprintResource} from '../types'
2
-
3
- export interface RolePermission {
4
- name: string // Required: predefined permission name (e.g., 'sanity-all-documents')
5
- action: string // Required: permission action (e.g., 'read', 'mode')
6
- params?: Record<string, unknown> // Optional: additional parameters for the permission
7
- }
8
-
9
- export interface BlueprintRoleConfig {
10
- name: string
11
- title: string
12
- description?: string
13
- appliesToUsers: boolean
14
- appliesToRobots: boolean
15
- permissions: RolePermission[]
16
- }
17
-
18
- export interface BlueprintRoleResource extends BlueprintRoleConfig, BlueprintResource {
19
- type: 'sanity.access.role'
20
- }
21
-
22
- export interface BlueprintProjectRoleResource extends BlueprintRoleResource {
23
- resourceType: 'project'
24
- resourceId: string
25
- }
1
+ import {
2
+ type BlueprintProjectRoleResource,
3
+ type BlueprintRoleConfig,
4
+ type BlueprintRoleResource,
5
+ validateProjectRole,
6
+ validateRole,
7
+ } from '../index.js'
8
+ import {runValidation} from '../utils/validation.js'
26
9
 
27
10
  /**
28
11
  * Defines a role that is scoped to the same resource as the blueprint.
29
12
  * @param parameters The configuration of the role
30
13
  * @returns The role resource
31
14
  */
32
- export function defineRole(parameters: BlueprintRoleConfig): BlueprintRoleResource {
33
- const errors: string[] = []
34
-
35
- if (!parameters.name || parameters.name.trim() === '') {
36
- errors.push('Role name is required')
37
- }
38
-
39
- if (!parameters.title || parameters.title.trim() === '') {
40
- errors.push('Role title is required')
41
- } else if (parameters.title.length > 100) {
42
- errors.push('Role title must be 100 characters or less')
43
- }
44
-
45
- if (!parameters.permissions || parameters.permissions.length < 1) {
46
- errors.push('Role must have at least one permission')
47
- }
48
-
49
- if (errors.length > 0) {
50
- const message = errors.join('\n')
51
- throw new Error(message)
52
- }
53
-
54
- return {
15
+ export function defineRole(parameters: BlueprintRoleConfig, options?: {skipValidation?: boolean}): BlueprintRoleResource {
16
+ const roleResource: BlueprintRoleResource = {
55
17
  name: parameters.name,
56
18
  type: 'sanity.access.role',
57
19
  title: parameters.title,
@@ -60,6 +22,10 @@ export function defineRole(parameters: BlueprintRoleConfig): BlueprintRoleResour
60
22
  appliesToRobots: parameters.appliesToRobots,
61
23
  permissions: parameters.permissions,
62
24
  }
25
+
26
+ if (options?.skipValidation !== true) runValidation(() => validateRole(roleResource))
27
+
28
+ return roleResource
63
29
  }
64
30
 
65
31
  /**
@@ -69,11 +35,15 @@ export function defineRole(parameters: BlueprintRoleConfig): BlueprintRoleResour
69
35
  * @returns The role resource
70
36
  */
71
37
  export function defineProjectRole(projectId: string, parameters: BlueprintRoleConfig): BlueprintProjectRoleResource {
72
- const roleResource = defineRole(parameters)
38
+ const roleResource = defineRole(parameters, {skipValidation: true})
73
39
 
74
- return {
40
+ const projectRoleResource: BlueprintProjectRoleResource = {
75
41
  ...roleResource,
76
42
  resourceType: 'project',
77
43
  resourceId: projectId,
78
44
  }
45
+
46
+ runValidation(() => validateProjectRole(projectRoleResource))
47
+
48
+ return projectRoleResource
79
49
  }
@@ -1,103 +1,18 @@
1
- import type {BlueprintResource} from '../types'
2
-
3
- export type WebhookTrigger = 'create' | 'update' | 'delete'
4
- export interface BlueprintDocumentWebhookResource extends BlueprintResource {
5
- type: 'sanity.project.webhook'
6
- project?: string
7
- displayName?: string
8
- description?: string | null
9
- url: string
10
- on: WebhookTrigger[]
11
- filter?: string
12
- projection?: string
13
- status?: 'enabled' | 'disabled'
14
- httpMethod?: 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'GET'
15
- headers?: Record<string, string>
16
- includeDrafts?: boolean
17
- includeAllVersions?: boolean
18
- secret?: string
19
- dataset?: string
20
- apiVersion?: string
21
- }
22
- export type BlueprintDocumentWebhookConfig = Omit<BlueprintDocumentWebhookResource, 'type'>
1
+ import {type BlueprintDocumentWebhookConfig, type BlueprintDocumentWebhookResource, validateDocumentWebhook} from '../index.js'
2
+ import {runValidation} from '../utils/validation.js'
23
3
 
24
4
  export function defineDocumentWebhook(parameters: BlueprintDocumentWebhookConfig): BlueprintDocumentWebhookResource {
25
- const errors: string[] = []
26
-
27
- if (!parameters.name || parameters.name.trim() === '') {
28
- errors.push('Webhook name is required')
29
- }
30
-
31
- if (parameters.displayName) {
32
- if (parameters.displayName.length > 100) {
33
- errors.push('Display name must be 100 characters or less')
34
- }
35
- } else {
36
- parameters.displayName = parameters.name?.substring(0, 100)
37
- }
38
-
39
- if (!parameters.url || parameters.url.trim() === '') {
40
- errors.push('Webhook URL is required')
41
- }
42
-
43
- // Validate URL format
44
- try {
45
- new URL(parameters.url)
46
- } catch {
47
- errors.push('Webhook URL must be a valid URL')
48
- }
49
-
50
- if (!parameters.on || parameters.on.length === 0) {
51
- errors.push('At least one event type must be specified in the "on" field')
52
- }
53
-
54
- // Validate event types
55
- const validEvents = ['create', 'update', 'delete']
56
- if (parameters.on) {
57
- const invalidEvents = parameters.on.filter((event: string) => !validEvents.includes(event))
58
- if (invalidEvents.length > 0) {
59
- errors.push(`Invalid event types: ${invalidEvents.join(', ')}. Valid events are: ${validEvents.join(', ')}`)
60
- }
5
+ // default the display name
6
+ if (!parameters.displayName) {
7
+ parameters.displayName = parameters.name.substring(0, 100)
61
8
  }
62
9
 
63
- // Validate dataset pattern
64
- if (!parameters.dataset || !/^[a-z0-9-_]+$/.test(parameters.dataset)) {
65
- errors.push('Dataset must match pattern: ^[a-z0-9-_]+$')
66
- }
67
-
68
- // Validate HTTP method
69
- if (parameters.httpMethod) {
70
- const validMethods = ['POST', 'PUT', 'PATCH', 'DELETE', 'GET']
71
- if (!validMethods.includes(parameters.httpMethod)) {
72
- errors.push(`Invalid HTTP method: ${parameters.httpMethod}. Valid methods are: ${validMethods.join(', ')}`)
73
- }
74
- }
75
-
76
- // Validate status
77
- if (parameters.status && !['enabled', 'disabled'].includes(parameters.status)) {
78
- errors.push('Status must be either "enabled" or "disabled"')
79
- }
80
-
81
- // Validate headers pattern
82
- if (parameters.headers) {
83
- const headerNamePattern = /^[a-zA-Z][a-zA-Z0-9-_]*$/
84
- for (const [key, value] of Object.entries(parameters.headers)) {
85
- if (!headerNamePattern.test(key)) {
86
- errors.push(`Header key "${key}" must match pattern: ${headerNamePattern.source}`)
87
- }
88
- if (typeof value !== 'string') {
89
- errors.push(`Header value for "${key}" must be a string`)
90
- }
91
- }
92
- }
93
-
94
- if (errors.length > 0) {
95
- const message = errors.join('\n')
96
- throw new Error(message)
97
- }
98
-
99
- return {
10
+ const webhookResource: BlueprintDocumentWebhookResource = {
100
11
  ...parameters,
101
12
  type: 'sanity.project.webhook',
102
13
  }
14
+
15
+ runValidation(() => validateDocumentWebhook(webhookResource))
16
+
17
+ return webhookResource
103
18
  }
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- export * from './types.js'
1
+ // DEFINERS - helper functions to create resources
2
2
  export * from './definers/blueprints.js'
3
3
  export * from './definers/cors.js'
4
4
  export * from './definers/datasets.js'
@@ -6,3 +6,22 @@ export * from './definers/functions.js'
6
6
  export * from './definers/resources.js'
7
7
  export * from './definers/roles.js'
8
8
  export * from './definers/webhooks.js'
9
+
10
+ // TYPES - base types for all resources
11
+ export * from './types/blueprints.js'
12
+ export * from './types/cors.js'
13
+ export * from './types/datasets.js'
14
+ export * from './types/errors.js'
15
+ export * from './types/functions.js'
16
+ export * from './types/resources.js'
17
+ export * from './types/roles.js'
18
+ export * from './types/webhooks.js'
19
+
20
+ // VALIDATION - validation for all resources
21
+ export * from './validation/blueprints.js'
22
+ export * from './validation/cors.js'
23
+ export * from './validation/datasets.js'
24
+ export * from './validation/functions.js'
25
+ export * from './validation/resources.js'
26
+ export * from './validation/roles.js'
27
+ export * from './validation/webhooks.js'
@@ -0,0 +1,26 @@
1
+ import type {BlueprintResource} from '../index.js'
2
+
3
+ export interface BlueprintsApiConfig {
4
+ organizationId: string
5
+ projectId: string
6
+ stackId: string
7
+ }
8
+
9
+ export interface BlueprintOutput {
10
+ name: string
11
+ value: string
12
+ }
13
+
14
+ export interface Blueprint {
15
+ $schema: string
16
+ blueprintVersion: string
17
+ resources?: BlueprintResource[]
18
+ values?: Record<string, unknown>
19
+ outputs?: BlueprintOutput[]
20
+ }
21
+
22
+ export type BlueprintModule = ((args?: unknown) => Blueprint) & {
23
+ organizationId?: string
24
+ projectId?: string
25
+ stackId?: string
26
+ }
@@ -0,0 +1,18 @@
1
+ import type {BlueprintResource} from '../index.js'
2
+
3
+ /**
4
+ * Represents a CORS Origin resource.
5
+ */
6
+ export interface BlueprintCorsOriginResource extends BlueprintResource {
7
+ type: 'sanity.project.cors'
8
+ origin: string
9
+ allowCredentials?: boolean
10
+
11
+ /** The `project` attribute must be defined if your blueprint is scoped to an organization. */
12
+ project?: string
13
+ }
14
+
15
+ /** Configuration for a CORS Origin resource. */
16
+ export type BlueprintCorsOriginConfig = Omit<BlueprintCorsOriginResource, 'type' | 'allowCredentials'> & {
17
+ allowCredentials?: boolean
18
+ }
@@ -0,0 +1,19 @@
1
+ import type {BlueprintResource} from '../index.js'
2
+
3
+ export type AclMode = 'public' | 'private' | 'custom'
4
+
5
+ /** Represents a Dataset resource. */
6
+ export interface BlueprintDatasetResource extends BlueprintResource {
7
+ type: 'sanity.project.dataset'
8
+ datasetName: string
9
+ aclMode?: AclMode
10
+
11
+ /** The `project` attribute must be defined if your blueprint is scoped to an organization. */
12
+ project?: string
13
+ }
14
+
15
+ /** Configuration for a Dataset resource. */
16
+ export type BlueprintDatasetConfig = Omit<BlueprintDatasetResource, 'type' | 'datasetName'> & {
17
+ /** The name of the dataset, defaults to the name of the resource. */
18
+ datasetName?: string
19
+ }
@@ -0,0 +1,5 @@
1
+ /** Standard error shape returned by validation functions */
2
+ export interface BlueprintError {
3
+ message: string
4
+ type: string
5
+ }
@@ -0,0 +1,48 @@
1
+ import type {BlueprintResource} from '../index.js'
2
+
3
+ // --- Function Event Types ---
4
+
5
+ export interface BlueprintFunctionBaseResourceEvent {
6
+ on?: [BlueprintFunctionResourceEventName, ...BlueprintFunctionResourceEventName[]]
7
+ filter?: string
8
+ projection?: `{${string}}`
9
+ includeDrafts?: boolean
10
+ }
11
+ export interface BlueprintDocumentFunctionResourceEvent extends BlueprintFunctionBaseResourceEvent {
12
+ includeAllVersions?: boolean
13
+ resource?: BlueprintFunctionResourceEventResourceDataset
14
+ }
15
+ export interface BlueprintMediaLibraryFunctionResourceEvent extends BlueprintFunctionBaseResourceEvent {
16
+ resource: BlueprintFunctionResourceEventResourceMediaLibrary
17
+ }
18
+ export type BlueprintFunctionResourceEvent = BlueprintDocumentFunctionResourceEvent | BlueprintMediaLibraryFunctionResourceEvent
19
+
20
+ interface BlueprintFunctionResourceEventResourceDataset {
21
+ type: 'dataset'
22
+ /** @description A dataset ID in the format <projectId>.<datasetName>. <datasetName> can be `*` to signify "all datasets in project with ID <projectId>." */
23
+ id: string
24
+ }
25
+ interface BlueprintFunctionResourceEventResourceMediaLibrary {
26
+ type: 'media-library'
27
+ id: string
28
+ }
29
+
30
+ type BlueprintFunctionResourceEventName = 'publish' | 'create' | 'delete' | 'update'
31
+
32
+ // --- Main Function Types ---
33
+
34
+ export interface BlueprintBaseFunctionResource extends BlueprintResource {
35
+ displayName?: string
36
+ src: string
37
+ timeout?: number
38
+ memory?: number
39
+ env?: Record<string, string>
40
+ }
41
+ export interface BlueprintDocumentFunctionResource extends BlueprintBaseFunctionResource {
42
+ type: 'sanity.function.document'
43
+ event: BlueprintDocumentFunctionResourceEvent
44
+ }
45
+ export interface BlueprintMediaLibraryAssetFunctionResource extends BlueprintBaseFunctionResource {
46
+ type: 'sanity.function.media-library.asset'
47
+ event: BlueprintMediaLibraryFunctionResourceEvent
48
+ }
@@ -0,0 +1,25 @@
1
+ import type {BlueprintResource} from '../index.js'
2
+
3
+ export interface RolePermission {
4
+ name: string // Required: predefined permission name (e.g., 'sanity-all-documents')
5
+ action: string // Required: permission action (e.g., 'read', 'mode')
6
+ params?: Record<string, unknown> // Optional: additional parameters for the permission
7
+ }
8
+
9
+ export interface BlueprintRoleConfig {
10
+ name: string
11
+ title: string
12
+ description?: string
13
+ appliesToUsers: boolean
14
+ appliesToRobots: boolean
15
+ permissions: RolePermission[]
16
+ }
17
+
18
+ export interface BlueprintRoleResource extends BlueprintRoleConfig, BlueprintResource {
19
+ type: 'sanity.access.role'
20
+ }
21
+
22
+ export interface BlueprintProjectRoleResource extends BlueprintRoleResource {
23
+ resourceType: 'project'
24
+ resourceId: string
25
+ }
@@ -0,0 +1,22 @@
1
+ import type {BlueprintResource} from '../index.js'
2
+
3
+ export type WebhookTrigger = 'create' | 'update' | 'delete'
4
+ export interface BlueprintDocumentWebhookResource extends BlueprintResource {
5
+ type: 'sanity.project.webhook'
6
+ project?: string
7
+ displayName?: string
8
+ description?: string | null
9
+ url: string
10
+ on: WebhookTrigger[]
11
+ filter?: string
12
+ projection?: string
13
+ status?: 'enabled' | 'disabled'
14
+ httpMethod?: 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'GET'
15
+ headers?: Record<string, string>
16
+ includeDrafts?: boolean
17
+ includeAllVersions?: boolean
18
+ secret?: string
19
+ dataset?: string
20
+ apiVersion?: string
21
+ }
22
+ export type BlueprintDocumentWebhookConfig = Omit<BlueprintDocumentWebhookResource, 'type'>
@@ -0,0 +1,9 @@
1
+ import type {BlueprintError} from '../types/errors'
2
+
3
+ export function runValidation(validator: () => BlueprintError[]) {
4
+ const errors = validator()
5
+ if (errors.length > 0) {
6
+ const message = errors.map((err) => err.message).join('\n')
7
+ throw new Error(message)
8
+ }
9
+ }
@@ -0,0 +1,16 @@
1
+ import type {BlueprintError} from '../index.js'
2
+
3
+ export function validateBlueprint(blueprintConfig: unknown): BlueprintError[] {
4
+ if (!blueprintConfig) return [{type: 'invalid_value', message: 'blueprint config must be provided'}]
5
+ if (typeof blueprintConfig !== 'object') return [{type: 'invalid_type', message: 'blueprint config must be an object'}]
6
+
7
+ const errors: BlueprintError[] = []
8
+
9
+ if ('resources' in blueprintConfig) {
10
+ const {resources} = blueprintConfig
11
+
12
+ if (resources && !Array.isArray(resources)) errors.push({type: 'invalid_format', message: '`resources` must be an array'})
13
+ }
14
+
15
+ return errors
16
+ }
@@ -0,0 +1,34 @@
1
+ import type {BlueprintError} from '../index.js'
2
+
3
+ export function validateCorsOrigin(resource: unknown): BlueprintError[] {
4
+ if (!resource) return [{type: 'invalid_value', message: 'CORS Origin config must be provided'}]
5
+ if (typeof resource !== 'object') return [{type: 'invalid_type', message: 'CORS Origin config must be an object'}]
6
+
7
+ const errors: BlueprintError[] = []
8
+
9
+ if (!('name' in resource) || !resource.name) {
10
+ errors.push({type: 'missing_parameter', message: 'CORS Origin name is required'})
11
+ } else if (typeof resource.name !== 'string') {
12
+ errors.push({type: 'invalid_type', message: 'CORS Origin name must be a string'})
13
+ }
14
+
15
+ if (!('type' in resource)) {
16
+ errors.push({type: 'missing_parameter', message: 'CORS Origin type is required'})
17
+ } else if (resource.type !== 'sanity.project.cors') {
18
+ errors.push({type: 'invalid_value', message: 'CORS Origin type must be `sanity.project.cors`'})
19
+ }
20
+
21
+ if (!('origin' in resource) || !resource.origin) {
22
+ errors.push({type: 'missing_parameter', message: 'CORS Origin URL is required'})
23
+ } else if (typeof resource.origin !== 'string') {
24
+ errors.push({type: 'invalid_type', message: 'CORS Origin URL must be a string'})
25
+ }
26
+
27
+ if ('project' in resource) {
28
+ if (typeof resource.project !== 'string') {
29
+ errors.push({type: 'invalid_type', message: 'CORS Origin project must be a string'})
30
+ }
31
+ }
32
+
33
+ return errors
34
+ }
@@ -0,0 +1,41 @@
1
+ import type {BlueprintError} from '../index.js'
2
+
3
+ export function validateDataset(resource: unknown): BlueprintError[] {
4
+ if (!resource) return [{type: 'invalid_value', message: 'Dataset config must be provided'}]
5
+ if (typeof resource !== 'object') return [{type: 'invalid_type', message: 'Dataset config must be an object'}]
6
+
7
+ const errors: BlueprintError[] = []
8
+
9
+ if (!('name' in resource) || !resource.name) {
10
+ errors.push({type: 'missing_parameter', message: 'Dataset name is required'})
11
+ } else if (typeof resource.name !== 'string') {
12
+ errors.push({type: 'invalid_type', message: 'Dataset name must be a string'})
13
+ }
14
+
15
+ if (!('type' in resource)) {
16
+ errors.push({type: 'missing_parameter', message: 'Dataset type is required'})
17
+ } else if (resource.type !== 'sanity.project.dataset') {
18
+ errors.push({type: 'invalid_value', message: 'Dataset type must be `sanity.project.dataset`'})
19
+ }
20
+
21
+ // validate ACL mode if provided
22
+ if ('aclMode' in resource) {
23
+ if (typeof resource.aclMode !== 'string') {
24
+ errors.push({type: 'invalid_type', message: 'Dataset aclMode must be one of `custom`, `public`, or `private`'})
25
+ } else {
26
+ const aclMode: string = resource.aclMode
27
+ if (aclMode !== 'custom' && aclMode !== 'public' && aclMode !== 'private') {
28
+ errors.push({type: 'invalid_value', message: 'Dataset aclMode must be one of `custom`, `public`, or `private`'})
29
+ }
30
+ }
31
+ }
32
+
33
+ // validate project if provided
34
+ if ('project' in resource) {
35
+ if (typeof resource.project !== 'string') {
36
+ errors.push({type: 'invalid_type', message: 'Dataset project must be a string'})
37
+ }
38
+ }
39
+
40
+ return errors
41
+ }
@@ -0,0 +1,144 @@
1
+ import type {
2
+ BlueprintDocumentFunctionResourceEvent,
3
+ BlueprintError,
4
+ BlueprintFunctionBaseResourceEvent,
5
+ BlueprintMediaLibraryFunctionResourceEvent,
6
+ } from '../index.js'
7
+
8
+ type BaseFunctionEventKey = keyof BlueprintFunctionBaseResourceEvent
9
+ const BASE_EVENT_KEYS = new Set<BaseFunctionEventKey>(['on', 'filter', 'projection', 'includeDrafts'])
10
+ type DocumentFunctionEventKey = keyof BlueprintDocumentFunctionResourceEvent
11
+ const DOCUMENT_EVENT_KEYS = new Set<DocumentFunctionEventKey>(['includeAllVersions', 'resource', ...BASE_EVENT_KEYS.values()])
12
+ type MediaLibraryFunctionEventKey = keyof BlueprintMediaLibraryFunctionResourceEvent
13
+ const MEDIA_LIBRARY_EVENT_KEYS = new Set<MediaLibraryFunctionEventKey>(['resource', ...BASE_EVENT_KEYS.values()])
14
+
15
+ export function validateDocumentFunction(functionResource: unknown): BlueprintError[] {
16
+ if (!functionResource) return [{type: 'invalid_value', message: 'Function config must be provided'}]
17
+ if (typeof functionResource !== 'object') return [{type: 'invalid_type', message: 'Function config must be an object'}]
18
+
19
+ const errors: BlueprintError[] = []
20
+
21
+ // event validation
22
+ if ('event' in functionResource) {
23
+ // `event` was specified, but event keys (aggregated in `maybeEvent`) were also specified at the top level. ambiguous and deprecated usage.
24
+ const duplicateKeys = Array.from(DOCUMENT_EVENT_KEYS).filter((key) => key in functionResource)
25
+ if (duplicateKeys.length > 0) {
26
+ errors.push({
27
+ type: 'invalid_property',
28
+ message: `\`event\` properties should be specified under the \`event\` key - specifying them at the top level is deprecated. The following keys were specified at the top level: ${duplicateKeys.map((k) => `\`${k}\``).join(', ')}`,
29
+ })
30
+ } else {
31
+ errors.push(...validateDocumentFunctionEvent(functionResource.event))
32
+ }
33
+ } else {
34
+ errors.push(...validateDocumentFunctionEvent(functionResource))
35
+ }
36
+
37
+ if ('type' in functionResource && functionResource.type !== 'sanity.function.document') {
38
+ errors.push({type: 'invalid_value', message: '`type` must be `sanity.function.document`'})
39
+ }
40
+
41
+ errors.push(...validateFunction(functionResource))
42
+
43
+ return errors
44
+ }
45
+
46
+ export function validateMediaLibraryAssetFunction(functionResource: unknown): BlueprintError[] {
47
+ if (!functionResource) return [{type: 'invalid_value', message: 'Function config must be provided'}]
48
+ if (typeof functionResource !== 'object') return [{type: 'invalid_type', message: 'Function config must be an object'}]
49
+
50
+ const errors: BlueprintError[] = []
51
+
52
+ if ('event' in functionResource) {
53
+ errors.push(...validateMediaLibraryFunctionEvent(functionResource.event))
54
+ } else {
55
+ errors.push({type: 'missing_parameter', message: '`event` is required for a media library function'})
56
+ }
57
+
58
+ if ('type' in functionResource && functionResource.type !== 'sanity.function.media-library.asset') {
59
+ errors.push({type: 'invalid_value', message: '`type` must be `sanity.function.media-library.asset`'})
60
+ }
61
+
62
+ errors.push(...validateFunction(functionResource))
63
+
64
+ return errors
65
+ }
66
+
67
+ export function validateFunction(functionResource: unknown): BlueprintError[] {
68
+ if (!functionResource) return [{type: 'invalid_value', message: 'Function config must be provided'}]
69
+ if (typeof functionResource !== 'object') return [{type: 'invalid_type', message: 'Function config must be an object'}]
70
+
71
+ const errors: BlueprintError[] = []
72
+
73
+ if (!('name' in functionResource)) {
74
+ errors.push({type: 'missing_parameter', message: '`name` is required'})
75
+ } else if (typeof functionResource.name !== 'string') {
76
+ errors.push({type: 'invalid_type', message: '`name` must be a string'})
77
+ }
78
+
79
+ if (!('type' in functionResource)) {
80
+ errors.push({type: 'missing_parameter', message: '`type` is required'})
81
+ } else if (typeof functionResource.type !== 'string') {
82
+ errors.push({type: 'invalid_type', message: '`type` must be a string'})
83
+ }
84
+
85
+ // type validation
86
+ if ('memory' in functionResource) {
87
+ if (typeof functionResource.memory !== 'number' && typeof functionResource.memory !== 'undefined') {
88
+ errors.push({type: 'invalid_type', message: '`memory` must be a number'})
89
+ }
90
+ }
91
+ if ('timeout' in functionResource) {
92
+ if (typeof functionResource.timeout !== 'number' && typeof functionResource.timeout !== 'undefined') {
93
+ errors.push({type: 'invalid_type', message: '`timeout` must be a number'})
94
+ }
95
+ }
96
+
97
+ return errors
98
+ }
99
+
100
+ function validateDocumentFunctionEvent(event: unknown): BlueprintError[] {
101
+ if (!event) return [{type: 'invalid_value', message: 'Function event must be provided'}]
102
+ if (typeof event !== 'object') return [{type: 'invalid_type', message: 'Function event must be an object'}]
103
+
104
+ const cleanEvent = Object.fromEntries(
105
+ Object.entries(event).filter(([key]) => DOCUMENT_EVENT_KEYS.has(key as DocumentFunctionEventKey)),
106
+ ) as Partial<BlueprintDocumentFunctionResourceEvent>
107
+ const errors: BlueprintError[] = []
108
+
109
+ const fullEvent = {
110
+ on: cleanEvent.on || ['publish'],
111
+ ...cleanEvent,
112
+ }
113
+ if (!Array.isArray(fullEvent.on)) errors.push({type: 'invalid_type', message: '`event.on` must be an array'})
114
+ if (fullEvent.resource) {
115
+ if (!fullEvent.resource.type || fullEvent.resource.type !== 'dataset')
116
+ errors.push({type: 'invalid_value', message: '`event.resource.type` must be "dataset"'})
117
+ if (!fullEvent.resource.id || fullEvent.resource.id.split('.').length !== 2)
118
+ errors.push({type: 'invalid_format', message: '`event.resource.id` must be in the format <projectId>.<datasetName>'})
119
+ }
120
+ return errors
121
+ }
122
+
123
+ function validateMediaLibraryFunctionEvent(event: unknown): BlueprintError[] {
124
+ if (!event) return [{type: 'invalid_value', message: 'Function event must be provided'}]
125
+ if (typeof event !== 'object') return [{type: 'invalid_type', message: 'Function event must be an object'}]
126
+
127
+ const cleanEvent = Object.fromEntries(
128
+ Object.entries(event).filter(([key]) => MEDIA_LIBRARY_EVENT_KEYS.has(key as MediaLibraryFunctionEventKey)),
129
+ ) as BlueprintMediaLibraryFunctionResourceEvent
130
+ const errors: BlueprintError[] = []
131
+
132
+ const fullEvent = {
133
+ on: cleanEvent.on || ['publish'],
134
+ ...cleanEvent,
135
+ }
136
+ if (!Array.isArray(fullEvent.on)) errors.push({type: 'invalid_type', message: '`event.on` must be an array'})
137
+ if (fullEvent.resource) {
138
+ if (!fullEvent.resource.type || fullEvent.resource.type !== 'media-library')
139
+ errors.push({type: 'invalid_value', message: '`event.resource.type` must be "media-library"'})
140
+ } else {
141
+ errors.push({type: 'missing_parameter', message: '`resource` is required for a media library function'})
142
+ }
143
+ return errors
144
+ }