@sanity/blueprints 0.7.1 → 0.9.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 +85 -1
  11. package/dist/definers/functions.js +108 -4
  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 +45 -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 +20 -1
  51. package/dist/validation/functions.js +144 -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 +6 -6
  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 +152 -3
  70. package/src/definers/resources.ts +6 -1
  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 +48 -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 +152 -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
package/src/types/cors.ts CHANGED
@@ -1,18 +1,44 @@
1
- import type {BlueprintResource} from '../index.js'
1
+ import type {BlueprintProjectResourceLifecycle, BlueprintResource} from '../index.js'
2
2
 
3
3
  /**
4
4
  * Represents a CORS Origin resource.
5
+ * @see https://www.sanity.io/docs/content-lake/cors
5
6
  */
6
- export interface BlueprintCorsOriginResource extends BlueprintResource {
7
+ export interface BlueprintCorsOriginResource extends BlueprintResource<BlueprintProjectResourceLifecycle> {
7
8
  type: 'sanity.project.cors'
9
+ /**
10
+ * A CORS origin will be defined using the following format: `protocol://hostname[:port]`
11
+ *
12
+ * The protocol and host name are required while the port is optional. Wildcards (*) are allowed.
13
+ */
8
14
  origin: string
15
+ /**
16
+ * When adding a CORS origin, you will also need to decide whether or not to allow credentials. If you allow credentials, the origin will be allowed to send authenticated requests using the token or session of a logged in user.
17
+ *
18
+ * If this origin hosts a studio, you will need to allow credentials. Otherwise, you should probably select not to allow credentials.
19
+ *
20
+ * Allowing credentials from wildcard origins is dangerous. Any domain that matches the given pattern will be able to send requests on the user's behalf if they are logged in to your studio.
21
+ */
9
22
  allowCredentials?: boolean
10
23
 
11
- /** The `project` attribute must be defined if your blueprint is scoped to an organization. */
24
+ /**
25
+ * The project ID of the project that contains your CORS Origin.
26
+ *
27
+ * The `project` attribute must be defined if your blueprint is scoped to an organization. */
12
28
  project?: string
13
29
  }
14
30
 
15
- /** Configuration for a CORS Origin resource. */
31
+ /**
32
+ * Configuration for a CORS Origin resource.
33
+ * @see https://www.sanity.io/docs/content-lake/cors
34
+ */
16
35
  export type BlueprintCorsOriginConfig = Omit<BlueprintCorsOriginResource, 'type' | 'allowCredentials'> & {
36
+ /**
37
+ * When adding a CORS origin, you will also need to decide whether or not to allow credentials. If you allow credentials, the origin will be allowed to send authenticated requests using the token or session of a logged in user.
38
+ *
39
+ * If this origin hosts a studio, you will need to allow credentials. Otherwise, you should probably select not to allow credentials.
40
+ *
41
+ * Allowing credentials from wildcard origins is dangerous. Any domain that matches the given pattern will be able to send requests on the user's behalf if they are logged in to your studio.
42
+ */
17
43
  allowCredentials?: boolean
18
44
  }
@@ -2,18 +2,30 @@ import type {BlueprintResource} from '../index.js'
2
2
 
3
3
  export type AclMode = 'public' | 'private' | 'custom'
4
4
 
5
- /** Represents a Dataset resource. */
5
+ /**
6
+ * Represents a Dataset resource.
7
+ * @see https://www.sanity.io/docs/content-lake/datasets
8
+ */
6
9
  export interface BlueprintDatasetResource extends BlueprintResource {
7
10
  type: 'sanity.project.dataset'
11
+ /** The name of the dataset. Must be unique within a project. */
8
12
  datasetName: string
13
+ /** The ACL mode to set for the new dataset. Defaults to public. */
9
14
  aclMode?: AclMode
10
15
 
11
- /** The `project` attribute must be defined if your blueprint is scoped to an organization. */
16
+ /**
17
+ * The project ID of the project that contains your Dataset.
18
+ *
19
+ * The `project` attribute must be defined if your blueprint is scoped to an organization.
20
+ */
12
21
  project?: string
13
22
  }
14
23
 
15
- /** Configuration for a Dataset resource. */
24
+ /**
25
+ * Configuration for a Dataset resource.
26
+ * @see https://www.sanity.io/docs/content-lake/datasets
27
+ */
16
28
  export type BlueprintDatasetConfig = Omit<BlueprintDatasetResource, 'type' | 'datasetName'> & {
17
- /** The name of the dataset, defaults to the name of the resource. */
29
+ /** The name of the dataset. Must be unique within a project. Defaults to the name of the resource. */
18
30
  datasetName?: string
19
31
  }
@@ -2,47 +2,95 @@ import type {BlueprintResource} from '../index.js'
2
2
 
3
3
  // --- Function Event Types ---
4
4
 
5
+ /** Base event configuration shared by document and media library function types */
5
6
  export interface BlueprintFunctionBaseResourceEvent {
7
+ /** Event types that trigger the function. Defaults to ['publish'] */
6
8
  on?: [BlueprintFunctionResourceEventName, ...BlueprintFunctionResourceEventName[]]
9
+ /** GROQ filter expression to match specific documents (e.g., "_type == 'post'") */
7
10
  filter?: string
11
+ /** GROQ projection to specify which fields to include (e.g., "{_id, title, slug}") */
8
12
  projection?: `{${string}}`
13
+ /** Include draft documents in addition to published documents */
9
14
  includeDrafts?: boolean
10
15
  }
16
+ /** Event configuration for document functions */
11
17
  export interface BlueprintDocumentFunctionResourceEvent extends BlueprintFunctionBaseResourceEvent {
18
+ /** Include all versions of documents, not just the current version */
12
19
  includeAllVersions?: boolean
20
+ /** Optional dataset resource scoping for the function */
13
21
  resource?: BlueprintFunctionResourceEventResourceDataset
14
22
  }
23
+ /** Event configuration for media library asset functions */
15
24
  export interface BlueprintMediaLibraryFunctionResourceEvent extends BlueprintFunctionBaseResourceEvent {
25
+ /** Media library resource scoping (required for media library functions) */
16
26
  resource: BlueprintFunctionResourceEventResourceMediaLibrary
17
27
  }
28
+ /** Union type of all function resource event configurations */
18
29
  export type BlueprintFunctionResourceEvent = BlueprintDocumentFunctionResourceEvent | BlueprintMediaLibraryFunctionResourceEvent
19
30
 
31
+ /**
32
+ * Dataset resource for scoping document functions to specific datasets
33
+ * @example { type: 'dataset', id: 'my-project.production' }
34
+ */
35
+ export interface BlueprintScheduleFunctionExplicitResourceEvent {
36
+ minute: string
37
+ hour: string
38
+ dayOfMonth: string
39
+ month: string
40
+ dayOfWeek: string
41
+ }
42
+ export interface BlueprintScheduleFunctionExpressionResourceEvent {
43
+ expression: string
44
+ }
45
+ export type BlueprintScheduleFunctionResourceEvent =
46
+ | BlueprintScheduleFunctionExplicitResourceEvent
47
+ | BlueprintScheduleFunctionExpressionResourceEvent
48
+
20
49
  interface BlueprintFunctionResourceEventResourceDataset {
21
50
  type: 'dataset'
22
51
  /** @description A dataset ID in the format <projectId>.<datasetName>. <datasetName> can be `*` to signify "all datasets in project with ID <projectId>." */
23
52
  id: string
24
53
  }
54
+ /** Media library resource for scoping media library functions */
25
55
  interface BlueprintFunctionResourceEventResourceMediaLibrary {
26
56
  type: 'media-library'
27
57
  id: string
28
58
  }
29
59
 
60
+ /** Events that can trigger a function */
30
61
  type BlueprintFunctionResourceEventName = 'publish' | 'create' | 'delete' | 'update'
31
62
 
32
63
  // --- Main Function Types ---
33
64
 
65
+ /** Base function resource with common properties for all function types */
34
66
  export interface BlueprintBaseFunctionResource extends BlueprintResource {
67
+ /** Human-readable display name for the function */
35
68
  displayName?: string
69
+ /** Path to the function source code */
36
70
  src: string
71
+ /** Execution timeout in seconds */
37
72
  timeout?: number
73
+ /** Memory allocation in gigabytes */
38
74
  memory?: number
75
+ /** Environment variables provided to the function */
39
76
  env?: Record<string, string>
77
+ /** Token provided during function invocation */
78
+ robotToken?: string
40
79
  }
80
+ /** A function resource triggered by document events in Sanity datasets */
41
81
  export interface BlueprintDocumentFunctionResource extends BlueprintBaseFunctionResource {
42
82
  type: 'sanity.function.document'
83
+ /** Event configuration specifying when and how the function is triggered */
43
84
  event: BlueprintDocumentFunctionResourceEvent
44
85
  }
86
+ /** A function resource triggered by media library asset events */
45
87
  export interface BlueprintMediaLibraryAssetFunctionResource extends BlueprintBaseFunctionResource {
46
88
  type: 'sanity.function.media-library.asset'
89
+ /** Event configuration specifying when and how the function is triggered */
47
90
  event: BlueprintMediaLibraryFunctionResourceEvent
48
91
  }
92
+ export interface BlueprintScheduleFunctionResource extends BlueprintBaseFunctionResource {
93
+ type: 'sanity.function.cron'
94
+ event: BlueprintScheduleFunctionResourceEvent
95
+ timezone?: string
96
+ }
@@ -1,7 +1,60 @@
1
+ /**
2
+ * Define deployment behaviour once the policy has been deployed. The following behaviours are allowed:
3
+ *
4
+ * - **retain** If a stack is destroyed, the resource is detached from the stack and will not be destroyed. If the resource is removed from the blueprint, deployment will fail.
5
+ * - **replace** if a stack is deployed, the resource will be destroyed and created instead of performing an update. If the resource is removed from the blueprint, it will be destroyed. If the stack is destroyed, the resource will be destroyed.
6
+ * - **allow** if a stack is destroyed, the resource will be destroyed along with it. If the resource is removed it will be destroyed
7
+ * - **protect** if a stack is deployed the resource will not be updated. If the resource is removed from the blueprint or the stack is destroyed, deployment will fail.
8
+ */
9
+ export type BlueprintResourceDeletionPolicy = 'allow' | 'retain' | 'replace' | 'protect'
10
+
11
+ /**
12
+ * An ownership action that will cause the referenced resource to be attached to the current stack.
13
+ */
14
+ export interface BlueprintOwnershipAttachAction {
15
+ type: 'attach'
16
+ /** The identifier of the resource to be attached to the current stack */
17
+ id: string
18
+ }
19
+ /**
20
+ * A union of all possible ownership actions.
21
+ */
22
+ export type BlueprintOwnershipAction = BlueprintOwnershipAttachAction
23
+
24
+ /**
25
+ * An ownership action that will cause the referenced project-contained resource to be attached to the current stack.
26
+ */
27
+ export interface BlueprintProjectOwnershipAttachAction extends BlueprintOwnershipAttachAction {
28
+ /** The identifier of the project for resources contained in a project */
29
+ projectId: string
30
+ }
31
+ /**
32
+ * A union of all possible ownership actions for resources belonging to projects.
33
+ */
34
+ export type BlueprintProjectOwnershipAction = BlueprintProjectOwnershipAttachAction
35
+
36
+ /**
37
+ * Defines the lifcycle policy for this resource.
38
+ */
39
+ export interface BlueprintResourceLifecycle {
40
+ deletionPolicy?: BlueprintResourceDeletionPolicy
41
+
42
+ ownershipAction?: BlueprintOwnershipAction
43
+ }
44
+
45
+ /**
46
+ * Defines the lifcycle policy for this resource.
47
+ */
48
+ export interface BlueprintProjectResourceLifecycle extends BlueprintResourceLifecycle {
49
+ ownershipAction?: BlueprintProjectOwnershipAction
50
+ }
51
+
1
52
  /**
2
53
  * The base type for all resources.
3
54
  */
4
- export interface BlueprintResource {
55
+ export interface BlueprintResource<Lifecycle extends BlueprintResourceLifecycle = BlueprintResourceLifecycle> {
5
56
  type: string
6
57
  name: string
58
+
59
+ lifecycle?: Lifecycle
7
60
  }
@@ -0,0 +1,35 @@
1
+ import type {BlueprintResource} from './resources'
2
+
3
+ /** Resource types that robots can be attached to. */
4
+ export type RobotResourceType = 'organization' | 'project'
5
+
6
+ /**
7
+ * Defines the robot's roles within a given resource.
8
+ */
9
+ export interface RobotMembership {
10
+ resourceType: RobotResourceType
11
+ resourceId: string
12
+ roleNames: string[]
13
+ }
14
+
15
+ /**
16
+ * A robot that provides a token for automated access.
17
+ * @see https://www.sanity.io/docs/content-lake/http-auth#k4c21d7b829fe
18
+ */
19
+ export interface BlueprintRobotResource extends BlueprintResource {
20
+ type: 'sanity.access.robot'
21
+ /** A descriptive label for the robot and its use case */
22
+ label: string
23
+ memberships: RobotMembership[]
24
+
25
+ resourceType?: RobotResourceType
26
+ resourceId?: string
27
+ }
28
+
29
+ /**
30
+ * Configuration for a robot that provides a token for automated access.
31
+ * @see https://www.sanity.io/docs/content-lake/http-auth#k4c21d7b829fe
32
+ */
33
+ export type BlueprintRobotConfig = Omit<BlueprintRobotResource, 'type' | 'label' | 'token'> & {
34
+ label?: string
35
+ }
@@ -1,13 +1,21 @@
1
1
  import type {BlueprintResource} from '../index.js'
2
2
 
3
+ /**
4
+ * A permission definition for a role.
5
+ */
3
6
  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
+ /** Predefined permission name (e.g., 'sanity-all-documents') */
8
+ name: string
9
+ /** Permission action (e.g., 'read', 'mode') */
10
+ action: string
11
+ /** Additional parameters for the permission */
12
+ params?: Record<string, unknown>
7
13
  }
8
14
 
9
- export interface BlueprintRoleConfig {
10
- name: string
15
+ /**
16
+ * Configuration for a custom role.
17
+ */
18
+ export interface BlueprintRoleConfig extends Omit<BlueprintResource, 'type'> {
11
19
  title: string
12
20
  description?: string
13
21
  appliesToUsers: boolean
@@ -15,10 +23,16 @@ export interface BlueprintRoleConfig {
15
23
  permissions: RolePermission[]
16
24
  }
17
25
 
26
+ /**
27
+ * A custom role resource
28
+ */
18
29
  export interface BlueprintRoleResource extends BlueprintRoleConfig, BlueprintResource {
19
30
  type: 'sanity.access.role'
20
31
  }
21
32
 
33
+ /**
34
+ * A custom role resource that is tied to a specific project
35
+ */
22
36
  export interface BlueprintProjectRoleResource extends BlueprintRoleResource {
23
37
  resourceType: 'project'
24
38
  resourceId: string
@@ -1,6 +1,9 @@
1
1
  import type {BlueprintResource} from '../index.js'
2
2
 
3
+ /** Types of events that can trigger a webhook */
3
4
  export type WebhookTrigger = 'create' | 'update' | 'delete'
5
+
6
+ /** A webhook resource definition */
4
7
  export interface BlueprintDocumentWebhookResource extends BlueprintResource {
5
8
  type: 'sanity.project.webhook'
6
9
  project?: string
@@ -19,4 +22,6 @@ export interface BlueprintDocumentWebhookResource extends BlueprintResource {
19
22
  dataset?: string
20
23
  apiVersion: string
21
24
  }
25
+
26
+ /** Configuration for a webhook */
22
27
  export type BlueprintDocumentWebhookConfig = Omit<BlueprintDocumentWebhookResource, 'type'>
@@ -1,5 +1,9 @@
1
1
  import type {BlueprintError} from '../types/errors'
2
2
 
3
+ /**
4
+ * Executes the given validator function and throws a formatted error if any are returned.
5
+ * @param validator A function that returns a list of validation errors.
6
+ */
3
7
  export function runValidation(validator: () => BlueprintError[]) {
4
8
  const errors = validator()
5
9
  if (errors.length > 0) {
@@ -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'}]
@@ -142,3 +175,110 @@ function validateMediaLibraryFunctionEvent(event: unknown): BlueprintError[] {
142
175
  }
143
176
  return errors
144
177
  }
178
+
179
+ export function validateScheduleFunction(functionResource: unknown): BlueprintError[] {
180
+ if (!functionResource) return [{type: 'invalid_value', message: 'Function config must be provided'}]
181
+ if (typeof functionResource !== 'object') return [{type: 'invalid_type', message: 'Function config must be an object'}]
182
+
183
+ const errors: BlueprintError[] = []
184
+
185
+ if ('event' in functionResource) {
186
+ errors.push(...validateScheduleFunctionEvent(functionResource.event))
187
+ } else {
188
+ errors.push({type: 'missing_parameter', message: '`event` is required for a schedule function'})
189
+ }
190
+
191
+ if ('type' in functionResource && functionResource.type !== 'sanity.function.cron') {
192
+ errors.push({type: 'invalid_value', message: '`type` must be `sanity.function.cron`'})
193
+ }
194
+
195
+ if ('timezone' in functionResource) {
196
+ errors.push(...validateScheduleFunctionTimezone(functionResource.timezone))
197
+ }
198
+
199
+ errors.push(...validateFunction(functionResource))
200
+
201
+ return errors
202
+ }
203
+
204
+ function validateScheduleFunctionEvent(event: unknown): BlueprintError[] {
205
+ if (!event) return [{type: 'invalid_value', message: 'Function event must be provided'}]
206
+ if (typeof event !== 'object') return [{type: 'invalid_type', message: 'Function event must be an object'}]
207
+
208
+ const errors: BlueprintError[] = []
209
+
210
+ const hasExpression = 'expression' in event
211
+ const hasExplicitFields = 'minute' in event || 'hour' in event || 'dayOfMonth' in event || 'month' in event || 'dayOfWeek' in event
212
+
213
+ if (hasExpression && hasExplicitFields) {
214
+ errors.push({
215
+ type: 'invalid_property',
216
+ message: 'Cannot specify both `expression` and explicit cron fields (`minute`, `hour`, `dayOfMonth`, `month`, `dayOfWeek`)',
217
+ })
218
+ } else if (!hasExpression && !hasExplicitFields) {
219
+ errors.push({
220
+ type: 'missing_parameter',
221
+ message: 'Either `expression` or explicit cron fields (`minute`, `hour`, `dayOfMonth`, `month`, `dayOfWeek`) must be provided',
222
+ })
223
+ } else if (hasExplicitFields) {
224
+ if (!('minute' in event)) {
225
+ errors.push({
226
+ type: 'missing_parameter',
227
+ message: '`minute` must be provided',
228
+ })
229
+ } else if (typeof event.minute !== 'string') {
230
+ errors.push({type: 'invalid_type', message: '`minute` must be a string'})
231
+ }
232
+ if (!('hour' in event)) {
233
+ errors.push({
234
+ type: 'missing_parameter',
235
+ message: '`hour` must be provided',
236
+ })
237
+ } else if (typeof event.hour !== 'string') {
238
+ errors.push({type: 'invalid_type', message: '`hour` must be a string'})
239
+ }
240
+ if (!('dayOfWeek' in event)) {
241
+ errors.push({
242
+ type: 'missing_parameter',
243
+ message: '`dayOfWeek` must be provided',
244
+ })
245
+ } else if (typeof event.dayOfWeek !== 'string') {
246
+ errors.push({type: 'invalid_type', message: '`dayOfWeek` must be a string'})
247
+ }
248
+ if (!('month' in event)) {
249
+ errors.push({
250
+ type: 'missing_parameter',
251
+ message: '`month` must be provided',
252
+ })
253
+ } else if (typeof event.month !== 'string') {
254
+ errors.push({type: 'invalid_type', message: '`month` must be a string'})
255
+ }
256
+ if (!('dayOfMonth' in event)) {
257
+ errors.push({
258
+ type: 'missing_parameter',
259
+ message: '`dayOfMonth` must be provided',
260
+ })
261
+ } else if (typeof event.dayOfMonth !== 'string') {
262
+ errors.push({type: 'invalid_type', message: '`dayOfMonth` must be a string'})
263
+ }
264
+ }
265
+
266
+ return errors
267
+ }
268
+
269
+ function validateScheduleFunctionTimezone(timezone: unknown): BlueprintError[] {
270
+ if (typeof timezone !== 'string') return [{type: 'invalid_type', message: 'Function timezone must be a string'}]
271
+
272
+ const errors: BlueprintError[] = []
273
+
274
+ try {
275
+ Intl.DateTimeFormat(undefined, {timeZone: timezone})
276
+ } catch {
277
+ errors.push({
278
+ type: 'invalid_value',
279
+ message: '`timezone` must be a valid IANA timezone',
280
+ })
281
+ }
282
+
283
+ return errors
284
+ }