@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.
- package/dist/definers/blueprints.d.ts +1 -22
- package/dist/definers/blueprints.js +3 -2
- package/dist/definers/blueprints.js.map +1 -1
- package/dist/definers/cors.d.ts +1 -15
- package/dist/definers/cors.js +5 -12
- package/dist/definers/cors.js.map +1 -1
- package/dist/definers/datasets.d.ts +1 -15
- package/dist/definers/datasets.js +5 -19
- package/dist/definers/datasets.js.map +1 -1
- package/dist/definers/functions.d.ts +4 -41
- package/dist/definers/functions.js +27 -37
- package/dist/definers/functions.js.map +1 -1
- package/dist/definers/resources.d.ts +1 -1
- package/dist/definers/resources.js +4 -8
- package/dist/definers/resources.js.map +1 -1
- package/dist/definers/roles.d.ts +4 -22
- package/dist/definers/roles.js +11 -21
- package/dist/definers/roles.js.map +1 -1
- package/dist/definers/webhooks.d.ts +1 -21
- package/dist/definers/webhooks.js +8 -64
- package/dist/definers/webhooks.js.map +1 -1
- package/dist/index.d.ts +15 -1
- package/dist/index.js +18 -1
- package/dist/index.js.map +1 -1
- package/dist/types/blueprints.d.ts +22 -0
- package/dist/types/blueprints.js +2 -0
- package/dist/types/blueprints.js.map +1 -0
- package/dist/types/cors.d.ts +15 -0
- package/dist/types/cors.js +2 -0
- package/dist/types/cors.js.map +1 -0
- package/dist/types/datasets.d.ts +15 -0
- package/dist/types/datasets.js +2 -0
- package/dist/types/datasets.js.map +1 -0
- package/dist/types/errors.d.ts +5 -0
- package/dist/types/errors.js +2 -0
- package/dist/types/errors.js.map +1 -0
- package/dist/types/functions.d.ts +41 -0
- package/dist/types/functions.js +2 -0
- package/dist/types/functions.js.map +1 -0
- package/dist/types/resources.js +2 -0
- package/dist/types/resources.js.map +1 -0
- package/dist/types/roles.d.ts +21 -0
- package/dist/types/roles.js +2 -0
- package/dist/types/roles.js.map +1 -0
- package/dist/types/webhooks.d.ts +21 -0
- package/dist/types/webhooks.js +2 -0
- package/dist/types/webhooks.js.map +1 -0
- package/dist/utils/validation.d.ts +2 -0
- package/dist/utils/validation.js +8 -0
- package/dist/utils/validation.js.map +1 -0
- package/dist/validation/blueprints.d.ts +2 -0
- package/dist/validation/blueprints.js +14 -0
- package/dist/validation/blueprints.js.map +1 -0
- package/dist/validation/cors.d.ts +2 -0
- package/dist/validation/cors.js +32 -0
- package/dist/validation/cors.js.map +1 -0
- package/dist/validation/datasets.d.ts +2 -0
- package/dist/validation/datasets.js +39 -0
- package/dist/validation/datasets.js.map +1 -0
- package/dist/validation/functions.d.ts +4 -0
- package/dist/validation/functions.js +125 -0
- package/dist/validation/functions.js.map +1 -0
- package/dist/validation/resources.d.ts +3 -0
- package/dist/validation/resources.js +25 -0
- package/dist/validation/resources.js.map +1 -0
- package/dist/validation/roles.d.ts +8 -0
- package/dist/validation/roles.js +57 -0
- package/dist/validation/roles.js.map +1 -0
- package/dist/validation/webhooks.d.ts +2 -0
- package/dist/validation/webhooks.js +110 -0
- package/dist/validation/webhooks.js.map +1 -0
- package/package.json +1 -1
- package/src/definers/blueprints.ts +3 -27
- package/src/definers/cors.ts +7 -34
- package/src/definers/datasets.ts +7 -42
- package/src/definers/functions.ts +64 -93
- package/src/definers/resources.ts +6 -8
- package/src/definers/roles.ts +20 -50
- package/src/definers/webhooks.ts +10 -95
- package/src/index.ts +20 -1
- package/src/types/blueprints.ts +26 -0
- package/src/types/cors.ts +18 -0
- package/src/types/datasets.ts +19 -0
- package/src/types/errors.ts +5 -0
- package/src/types/functions.ts +48 -0
- package/src/types/roles.ts +25 -0
- package/src/types/webhooks.ts +22 -0
- package/src/utils/validation.ts +9 -0
- package/src/validation/blueprints.ts +16 -0
- package/src/validation/cors.ts +34 -0
- package/src/validation/datasets.ts +41 -0
- package/src/validation/functions.ts +144 -0
- package/src/validation/resources.ts +27 -0
- package/src/validation/roles.ts +60 -0
- package/src/validation/webhooks.ts +109 -0
- package/dist/types.js +0 -2
- package/dist/types.js.map +0 -1
- /package/dist/{types.d.ts → types/resources.d.ts} +0 -0
- /package/src/{types.ts → types/resources.ts} +0 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type {BlueprintError, BlueprintResource} from '../index.js'
|
|
2
|
+
import {runValidation} from '../utils/validation.js'
|
|
3
|
+
|
|
4
|
+
export function validateResource(resourceConfig: unknown): BlueprintError[] {
|
|
5
|
+
if (!resourceConfig) return [{type: 'invalid_value', message: 'Resource config must be provided'}]
|
|
6
|
+
if (typeof resourceConfig !== 'object') return [{type: 'invalid_type', message: 'Resource config must be an object'}]
|
|
7
|
+
|
|
8
|
+
const errors: BlueprintError[] = []
|
|
9
|
+
|
|
10
|
+
if (!('name' in resourceConfig)) {
|
|
11
|
+
errors.push({type: 'missing_parameter', message: '`name` is required'})
|
|
12
|
+
} else if (typeof resourceConfig.name !== 'string') {
|
|
13
|
+
errors.push({type: 'invalid_type', message: '`name` must be a string'})
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (!('type' in resourceConfig)) {
|
|
17
|
+
errors.push({type: 'missing_parameter', message: '`type` is required'})
|
|
18
|
+
} else if (typeof resourceConfig.type !== 'string') {
|
|
19
|
+
errors.push({type: 'invalid_type', message: '`type` must be a string'})
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return errors
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function assertResource(resource: unknown): asserts resource is BlueprintResource {
|
|
26
|
+
runValidation(() => validateResource(resource))
|
|
27
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type {BlueprintError} from '../index.js'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Validates a role.
|
|
5
|
+
* @param parameters The configuration of the role
|
|
6
|
+
* @returns The errors
|
|
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'}]
|
|
11
|
+
|
|
12
|
+
const errors: BlueprintError[] = []
|
|
13
|
+
|
|
14
|
+
if (!('name' in parameters)) {
|
|
15
|
+
errors.push({type: 'missing_parameter', message: 'Role name is required'})
|
|
16
|
+
} else if (typeof parameters.name !== 'string') {
|
|
17
|
+
errors.push({type: 'invalid_type', message: 'Role name must be a string'})
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (!('type' in parameters)) {
|
|
21
|
+
errors.push({type: 'missing_parameter', message: 'Role type is required'})
|
|
22
|
+
} else if (parameters.type !== 'sanity.access.role') {
|
|
23
|
+
errors.push({type: 'invalid_value', message: 'Role type must be `sanity.access.role`'})
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (!('title' in parameters)) {
|
|
27
|
+
errors.push({type: 'missing_parameter', message: 'Role title is required'})
|
|
28
|
+
} else if (typeof parameters.title !== 'string') {
|
|
29
|
+
errors.push({type: 'invalid_type', message: 'Role title must be a string'})
|
|
30
|
+
} else if (parameters.title.length > 100) {
|
|
31
|
+
errors.push({type: 'invalid_value', message: 'Role title must be 100 characters or less'})
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (!('permissions' in parameters) || !Array.isArray(parameters.permissions) || parameters.permissions.length < 1) {
|
|
35
|
+
errors.push({type: 'invalid_value', message: 'Role must have at least one permission'})
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return errors
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function validateProjectRole(parameters: unknown): BlueprintError[] {
|
|
42
|
+
const errors = validateRole(parameters)
|
|
43
|
+
|
|
44
|
+
// only do validation if parameters is provided, otherwise just return the errors
|
|
45
|
+
if (parameters && typeof parameters === 'object') {
|
|
46
|
+
if (!('resourceType' in parameters)) {
|
|
47
|
+
errors.push({type: 'missing_parameter', message: 'Role resource type must be `project`'})
|
|
48
|
+
} else if (parameters.resourceType !== 'project') {
|
|
49
|
+
errors.push({type: 'invalid_value', message: 'Role resource type must be `project`'})
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (!('resourceId' in parameters)) {
|
|
53
|
+
errors.push({type: 'missing_parameter', message: 'Role resource ID is required'})
|
|
54
|
+
} else if (typeof parameters.resourceId !== 'string') {
|
|
55
|
+
errors.push({type: 'invalid_type', message: 'Role resource ID must be a string'})
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return errors
|
|
60
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import type {BlueprintError} from '../index.js'
|
|
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'}]
|
|
6
|
+
|
|
7
|
+
const errors: BlueprintError[] = []
|
|
8
|
+
|
|
9
|
+
if (!('name' in parameters)) {
|
|
10
|
+
errors.push({type: 'missing_parameter', message: 'Webhook name is required'})
|
|
11
|
+
} else if (typeof parameters.name !== 'string') {
|
|
12
|
+
errors.push({type: 'invalid_type', message: 'Webhook name must be a string'})
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
if (!('type' in parameters)) {
|
|
16
|
+
errors.push({type: 'missing_parameter', message: 'Webhook type is required'})
|
|
17
|
+
} else if (parameters.type !== 'sanity.project.webhook') {
|
|
18
|
+
errors.push({type: 'invalid_value', message: 'Webhook type must be `sanity.project.webhook`'})
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if ('displayName' in parameters) {
|
|
22
|
+
if (typeof parameters.displayName !== 'string') {
|
|
23
|
+
errors.push({type: 'invalid_type', message: 'Display name must be a string'})
|
|
24
|
+
} else if (parameters.displayName.length > 100) {
|
|
25
|
+
errors.push({type: 'invalid_value', message: 'Display name must be 100 characters or less'})
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (!('url' in parameters)) {
|
|
30
|
+
errors.push({type: 'missing_parameter', message: 'Webhook URL is required'})
|
|
31
|
+
} else if (typeof parameters.url !== 'string') {
|
|
32
|
+
errors.push({type: 'invalid_type', message: 'Webhook URL must be a string'})
|
|
33
|
+
} else {
|
|
34
|
+
// Validate URL format
|
|
35
|
+
try {
|
|
36
|
+
new URL(parameters.url)
|
|
37
|
+
} catch {
|
|
38
|
+
errors.push({type: 'invalid_value', message: 'Webhook URL must be a valid URL'})
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (!('on' in parameters) || !Array.isArray(parameters.on) || parameters.on.length === 0) {
|
|
43
|
+
errors.push({type: 'invalid_value', message: 'At least one event type must be specified in the "on" field'})
|
|
44
|
+
} else {
|
|
45
|
+
// Validate event types
|
|
46
|
+
const validEvents = ['create', 'update', 'delete']
|
|
47
|
+
if (parameters.on) {
|
|
48
|
+
const invalidEvents = parameters.on.filter((event: string) => !validEvents.includes(event))
|
|
49
|
+
if (invalidEvents.length > 0) {
|
|
50
|
+
errors.push({
|
|
51
|
+
type: 'invalid_value',
|
|
52
|
+
message: `Invalid event types: ${invalidEvents.join(', ')}. Valid events are: ${validEvents.join(', ')}`,
|
|
53
|
+
})
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Validate dataset pattern
|
|
59
|
+
if (!('dataset' in parameters)) {
|
|
60
|
+
errors.push({type: 'missing_parameter', message: 'Webhook dataset is required'})
|
|
61
|
+
} else if (typeof parameters.dataset !== 'string' || !/^[a-z0-9-_]+$/.test(parameters.dataset)) {
|
|
62
|
+
errors.push({type: 'invalid_format', message: 'Dataset must match pattern: ^[a-z0-9-_]+$'})
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Validate HTTP method
|
|
66
|
+
if ('httpMethod' in parameters) {
|
|
67
|
+
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') {
|
|
70
|
+
errors.push({type: 'invalid_type', message})
|
|
71
|
+
} else {
|
|
72
|
+
if (!validMethods.includes(parameters.httpMethod)) {
|
|
73
|
+
errors.push({
|
|
74
|
+
type: 'invalid_value',
|
|
75
|
+
message,
|
|
76
|
+
})
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Validate status
|
|
82
|
+
if ('status' in parameters) {
|
|
83
|
+
const message = 'Status must be either "enabled" or "disabled"'
|
|
84
|
+
if (typeof parameters.status !== 'string') {
|
|
85
|
+
errors.push({type: 'invalid_type', message})
|
|
86
|
+
} else if (!['enabled', 'disabled'].includes(parameters.status)) {
|
|
87
|
+
errors.push({type: 'invalid_value', message})
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Validate headers pattern
|
|
92
|
+
if ('headers' in parameters) {
|
|
93
|
+
if (typeof parameters.headers !== 'object' || !parameters.headers || Array.isArray(parameters.headers)) {
|
|
94
|
+
errors.push({type: 'invalid_type', message: 'Webhook headers must be an object'})
|
|
95
|
+
} else {
|
|
96
|
+
const headerNamePattern = /^[a-zA-Z][a-zA-Z0-9-_]*$/
|
|
97
|
+
for (const [key, value] of Object.entries(parameters.headers)) {
|
|
98
|
+
if (!headerNamePattern.test(key)) {
|
|
99
|
+
errors.push({type: 'invalid_format', message: `Header key "${key}" must match pattern: ${headerNamePattern.source}`})
|
|
100
|
+
}
|
|
101
|
+
if (typeof value !== 'string') {
|
|
102
|
+
errors.push({type: 'invalid_type', message: `Header value for "${key}" must be a string`})
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return errors
|
|
109
|
+
}
|
package/dist/types.js
DELETED
package/dist/types.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
|
File without changes
|
|
File without changes
|