@sanity/blueprints-parser 0.2.1 → 0.3.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/CHANGELOG.md +16 -0
- package/package.json +1 -1
- package/src/index.d.ts +17 -7
- package/src/refs.js +42 -35
- package/test/mocks/reference-errors.js +81 -9
- package/test/mocks/references.js +60 -23
- package/test/unit/index.test.js +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.3.0](https://github.com/sanity-io/blueprints-parser/compare/v0.2.1...v0.3.0) (2025-10-17)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### ⚠ BREAKING CHANGES
|
|
7
|
+
|
|
8
|
+
* Make references work more consistently ([#9](https://github.com/sanity-io/blueprints-parser/issues/9))
|
|
9
|
+
|
|
10
|
+
### Features
|
|
11
|
+
|
|
12
|
+
* Make references work more consistently ([#9](https://github.com/sanity-io/blueprints-parser/issues/9)) ([c2e7944](https://github.com/sanity-io/blueprints-parser/commit/c2e7944c88361fd6547db9b8932c4cab261c3b41))
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
### Bug Fixes
|
|
16
|
+
|
|
17
|
+
* release 0.3.0 ([#11](https://github.com/sanity-io/blueprints-parser/issues/11)) ([e7971b1](https://github.com/sanity-io/blueprints-parser/commit/e7971b1c595b13cc83f754e5f39332e0cf528ba5))
|
|
18
|
+
|
|
3
19
|
## [0.2.1](https://github.com/sanity-io/blueprints-parser/compare/v0.2.0...v0.2.1) (2025-09-04)
|
|
4
20
|
|
|
5
21
|
|
package/package.json
CHANGED
package/src/index.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
export type Scalar = string | number
|
|
2
|
+
|
|
1
3
|
export type BlueprintInput = string | Buffer | object
|
|
2
4
|
|
|
3
5
|
export type ParserOptions = {
|
|
4
6
|
debug?: boolean
|
|
5
|
-
parameters?: Record<string,
|
|
7
|
+
parameters?: Record<string, Scalar>
|
|
6
8
|
}
|
|
7
9
|
|
|
8
10
|
export type Resource = {
|
|
@@ -32,7 +34,7 @@ export type Output = {
|
|
|
32
34
|
export type Blueprint = {
|
|
33
35
|
blueprintVersion?: string
|
|
34
36
|
resources?: Array<Resource>
|
|
35
|
-
values?: Record<string,
|
|
37
|
+
values?: Record<string, Scalar>
|
|
36
38
|
parameters?: Array<Parameter>
|
|
37
39
|
outputs?: Array<Output>
|
|
38
40
|
metadata?: Record<string, unknown>
|
|
@@ -64,11 +66,19 @@ export type ValidationError = {
|
|
|
64
66
|
message: string
|
|
65
67
|
}
|
|
66
68
|
|
|
67
|
-
export type
|
|
69
|
+
export type UnresolvedReference = {
|
|
70
|
+
/** The location where the reference was found */
|
|
68
71
|
path: string
|
|
69
|
-
|
|
72
|
+
/** The content of the reference (e.g. $.resources.project-1.id) */
|
|
70
73
|
ref: string
|
|
71
|
-
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export type Reference = UnresolvedReference & {
|
|
77
|
+
/** The object or array containing the reference */
|
|
78
|
+
container: object | Array
|
|
79
|
+
/** The key or index of the reference in the container */
|
|
80
|
+
property: string | number
|
|
81
|
+
}
|
|
72
82
|
|
|
73
83
|
export type ReferenceError = {
|
|
74
84
|
type: string
|
|
@@ -81,12 +91,12 @@ export type BlueprintOutput =
|
|
|
81
91
|
| {
|
|
82
92
|
result: 'valid'
|
|
83
93
|
blueprint: Blueprint
|
|
84
|
-
unresolvedRefs?: Array<
|
|
94
|
+
unresolvedRefs?: Array<UnresolvedReference>
|
|
85
95
|
}
|
|
86
96
|
| {
|
|
87
97
|
result: 'reference_errors'
|
|
88
98
|
blueprint: Blueprint
|
|
89
|
-
unresolvedRefs?: Array<
|
|
99
|
+
unresolvedRefs?: Array<UnresolvedReference>
|
|
90
100
|
errors: Array<ReferenceError>
|
|
91
101
|
}
|
|
92
102
|
| {
|
package/src/refs.js
CHANGED
|
@@ -19,14 +19,14 @@ function find(validatedBlueprint, options) {
|
|
|
19
19
|
if (is.object(item)) {
|
|
20
20
|
for (const [name, value] of Object.entries(item)) {
|
|
21
21
|
const cur = `${path}.${name}`
|
|
22
|
-
if (is.ref(value)) foundRefs.push({path: cur,
|
|
22
|
+
if (is.ref(value)) foundRefs.push({path: cur, ref: value, container: item, property: name})
|
|
23
23
|
else if (is.object(value) || is.array(value)) walk(value, cur)
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
26
|
if (is.array(item)) {
|
|
27
27
|
item.forEach((value, index) => {
|
|
28
28
|
const cur = `${path}[${index}]`
|
|
29
|
-
if (is.ref(value)) foundRefs.push({path: cur,
|
|
29
|
+
if (is.ref(value)) foundRefs.push({path: cur, ref: value, container: item, property: index})
|
|
30
30
|
else if (is.object(value)) walk(value, cur)
|
|
31
31
|
})
|
|
32
32
|
}
|
|
@@ -59,59 +59,66 @@ function find(validatedBlueprint, options) {
|
|
|
59
59
|
* @param {import('.').ParserOptions} options
|
|
60
60
|
* @returns {{
|
|
61
61
|
* resolvedBlueprint: import('.').Blueprint
|
|
62
|
-
* unresolvedRefs: Array<import('.').
|
|
62
|
+
* unresolvedRefs: Array<import('.').UnresolvedReference> | undefined
|
|
63
63
|
* refErrors: Array<import('.').ReferenceError>
|
|
64
64
|
* }}
|
|
65
65
|
*/
|
|
66
66
|
function resolve(blueprint, foundRefs, options) {
|
|
67
67
|
const {parameters = {}} = options
|
|
68
|
+
|
|
69
|
+
/** @type {Record<string, import('.').Reference>} */
|
|
68
70
|
const refs = {}
|
|
71
|
+
/** @type {Array<import('.').UnresolvedReference>} */
|
|
69
72
|
const unresolvedRefs = []
|
|
70
73
|
const refErrors = []
|
|
71
74
|
for (const foundRef of foundRefs) {
|
|
72
|
-
const {
|
|
73
|
-
const refPath = ref.replace(/^\$\./, '')
|
|
75
|
+
const {ref} = foundRef
|
|
74
76
|
// Early return from an already found reference
|
|
75
|
-
if (refs[
|
|
76
|
-
|
|
77
|
+
if (refs[ref]) {
|
|
78
|
+
foundRef.container[foundRef.property] = refs[ref].container[refs[ref].property]
|
|
77
79
|
continue
|
|
78
80
|
}
|
|
79
81
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
const
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
const index = item.findIndex((i) => i === property)
|
|
90
|
-
item[index] = refs[refPath]
|
|
91
|
-
}
|
|
82
|
+
refs[ref] = foundRef
|
|
83
|
+
|
|
84
|
+
const parts = ref.split('.')
|
|
85
|
+
const refType = parts[1]
|
|
86
|
+
const refName = parts[2]
|
|
87
|
+
if (refType === 'parameters' || refType === 'params') {
|
|
88
|
+
const param = parameters[refName]
|
|
89
|
+
if (is.scalar(param)) {
|
|
90
|
+
foundRef.container[foundRef.property] = param
|
|
92
91
|
} else {
|
|
93
92
|
refErrors.push({
|
|
94
|
-
message: `Reference error '${ref}': '${
|
|
93
|
+
message: `Reference error '${ref}': '${refName}' not found in passed parameters`,
|
|
95
94
|
type: 'missing_parameter',
|
|
96
95
|
})
|
|
97
96
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
refs[refPath] = found
|
|
108
|
-
if (is.object(item)) item[property] = refs[refPath]
|
|
109
|
-
if (is.array(item)) {
|
|
110
|
-
const index = item.findIndex((i) => i === property)
|
|
111
|
-
item[index] = refs[refPath]
|
|
97
|
+
} else if (refType === 'values') {
|
|
98
|
+
const value = blueprint.values?.[refName]
|
|
99
|
+
if (is.scalar(value)) {
|
|
100
|
+
foundRef.container[foundRef.property] = value
|
|
101
|
+
} else {
|
|
102
|
+
refErrors.push({
|
|
103
|
+
message: `Reference error '${ref}': '${refName}' not found in blueprint values`,
|
|
104
|
+
type: 'missing_value',
|
|
105
|
+
})
|
|
112
106
|
}
|
|
107
|
+
} else if (refType === 'metadata' || refType === 'outputs') {
|
|
108
|
+
refErrors.push({
|
|
109
|
+
message: `Reference error '${ref}': invalid reference to ${refType}`,
|
|
110
|
+
type: 'invalid_reference',
|
|
111
|
+
})
|
|
112
|
+
} else if (refType === 'blueprintVersion') {
|
|
113
|
+
foundRef.container[foundRef.property] = blueprint.blueprintVersion
|
|
114
|
+
} else if (refType === 'resources') {
|
|
115
|
+
// all resources references must be resolved during deployment
|
|
116
|
+
unresolvedRefs.push({path: foundRef.path, ref: foundRef.ref})
|
|
113
117
|
} else {
|
|
114
|
-
|
|
118
|
+
refErrors.push({
|
|
119
|
+
message: `Reference error '${ref}': invalid reference type ${refType}`,
|
|
120
|
+
type: 'invalid_reference',
|
|
121
|
+
})
|
|
115
122
|
}
|
|
116
123
|
}
|
|
117
124
|
|
|
@@ -34,6 +34,69 @@ export default {
|
|
|
34
34
|
},
|
|
35
35
|
},
|
|
36
36
|
|
|
37
|
+
unresolvedValueReference: {
|
|
38
|
+
input: {
|
|
39
|
+
resources: [
|
|
40
|
+
{
|
|
41
|
+
name: 'a-function',
|
|
42
|
+
type: 'cloud-function',
|
|
43
|
+
config: {memory: '$.values.memory'},
|
|
44
|
+
},
|
|
45
|
+
],
|
|
46
|
+
},
|
|
47
|
+
expected: {
|
|
48
|
+
resources: [
|
|
49
|
+
{
|
|
50
|
+
name: 'a-function',
|
|
51
|
+
type: 'cloud-function',
|
|
52
|
+
config: {memory: '$.values.memory'},
|
|
53
|
+
},
|
|
54
|
+
],
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
|
|
58
|
+
invalidReferenceTypeMetadata: {
|
|
59
|
+
input: {
|
|
60
|
+
resources: [
|
|
61
|
+
{
|
|
62
|
+
name: 'a-function',
|
|
63
|
+
type: 'cloud-function',
|
|
64
|
+
config: {memory: '$.metadata.memory'},
|
|
65
|
+
},
|
|
66
|
+
],
|
|
67
|
+
},
|
|
68
|
+
expected: {
|
|
69
|
+
resources: [
|
|
70
|
+
{
|
|
71
|
+
name: 'a-function',
|
|
72
|
+
type: 'cloud-function',
|
|
73
|
+
config: {memory: '$.metadata.memory'},
|
|
74
|
+
},
|
|
75
|
+
],
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
|
|
79
|
+
invalidReferenceType: {
|
|
80
|
+
input: {
|
|
81
|
+
resources: [
|
|
82
|
+
{
|
|
83
|
+
name: 'a-function',
|
|
84
|
+
type: 'cloud-function',
|
|
85
|
+
config: {memory: '$.invalid.memory'},
|
|
86
|
+
},
|
|
87
|
+
],
|
|
88
|
+
},
|
|
89
|
+
expected: {
|
|
90
|
+
resources: [
|
|
91
|
+
{
|
|
92
|
+
name: 'a-function',
|
|
93
|
+
type: 'cloud-function',
|
|
94
|
+
config: {memory: '$.invalid.memory'},
|
|
95
|
+
},
|
|
96
|
+
],
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
|
|
37
100
|
unresolvedParameterReferenceAndRegularReference: {
|
|
38
101
|
input: {
|
|
39
102
|
resources: [
|
|
@@ -42,7 +105,14 @@ export default {
|
|
|
42
105
|
type: 'cloud-function',
|
|
43
106
|
config: {
|
|
44
107
|
memory: '$.parameters.memory',
|
|
45
|
-
disk: '
|
|
108
|
+
disk: '20G',
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
name: 'another-function',
|
|
113
|
+
type: 'cloud-function',
|
|
114
|
+
config: {
|
|
115
|
+
disk: '$.resources.a-function.config.disk',
|
|
46
116
|
},
|
|
47
117
|
},
|
|
48
118
|
],
|
|
@@ -61,7 +131,14 @@ export default {
|
|
|
61
131
|
type: 'cloud-function',
|
|
62
132
|
config: {
|
|
63
133
|
memory: '$.parameters.memory',
|
|
64
|
-
disk: '
|
|
134
|
+
disk: '20G',
|
|
135
|
+
},
|
|
136
|
+
},
|
|
137
|
+
{
|
|
138
|
+
name: 'another-function',
|
|
139
|
+
type: 'cloud-function',
|
|
140
|
+
config: {
|
|
141
|
+
disk: '$.resources.a-function.config.disk',
|
|
65
142
|
},
|
|
66
143
|
},
|
|
67
144
|
],
|
|
@@ -75,13 +152,8 @@ export default {
|
|
|
75
152
|
},
|
|
76
153
|
unresolved: [
|
|
77
154
|
{
|
|
78
|
-
path: 'resources.
|
|
79
|
-
|
|
80
|
-
ref: '$.values.disk',
|
|
81
|
-
item: {
|
|
82
|
-
memory: '$.parameters.memory',
|
|
83
|
-
disk: '$.values.disk',
|
|
84
|
-
},
|
|
155
|
+
path: 'resources.another-function.config.disk',
|
|
156
|
+
ref: '$.resources.a-function.config.disk',
|
|
85
157
|
},
|
|
86
158
|
],
|
|
87
159
|
},
|
package/test/mocks/references.js
CHANGED
|
@@ -84,6 +84,28 @@ export default {
|
|
|
84
84
|
unresolved: undefined,
|
|
85
85
|
},
|
|
86
86
|
|
|
87
|
+
blueprintVersionReference: {
|
|
88
|
+
input: {
|
|
89
|
+
blueprintVersion: '2025-01-01',
|
|
90
|
+
outputs: [
|
|
91
|
+
{
|
|
92
|
+
name: 'version',
|
|
93
|
+
value: '$.blueprintVersion',
|
|
94
|
+
},
|
|
95
|
+
],
|
|
96
|
+
},
|
|
97
|
+
|
|
98
|
+
expected: {
|
|
99
|
+
blueprintVersion: '2025-01-01',
|
|
100
|
+
outputs: [
|
|
101
|
+
{
|
|
102
|
+
name: 'version',
|
|
103
|
+
value: '2025-01-01',
|
|
104
|
+
},
|
|
105
|
+
],
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
|
|
87
109
|
deepyNestedObjectReference: {
|
|
88
110
|
input: {
|
|
89
111
|
resources: [
|
|
@@ -105,7 +127,7 @@ export default {
|
|
|
105
127
|
{
|
|
106
128
|
name: 'a-function',
|
|
107
129
|
type: 'cloud-function',
|
|
108
|
-
config: {memory:
|
|
130
|
+
config: {memory: '$.resources.another-function.config.env.memory'},
|
|
109
131
|
},
|
|
110
132
|
{
|
|
111
133
|
name: 'another-function',
|
|
@@ -115,7 +137,12 @@ export default {
|
|
|
115
137
|
],
|
|
116
138
|
},
|
|
117
139
|
|
|
118
|
-
unresolved:
|
|
140
|
+
unresolved: [
|
|
141
|
+
{
|
|
142
|
+
path: 'resources.a-function.config.memory',
|
|
143
|
+
ref: '$.resources.another-function.config.env.memory',
|
|
144
|
+
},
|
|
145
|
+
],
|
|
119
146
|
},
|
|
120
147
|
|
|
121
148
|
arrayReference: {
|
|
@@ -142,12 +169,17 @@ export default {
|
|
|
142
169
|
{
|
|
143
170
|
name: 'a-function',
|
|
144
171
|
type: 'cloud-function',
|
|
145
|
-
projects: ['project-1', 'a-project', 'project-2'],
|
|
172
|
+
projects: ['project-1', '$.resources.a-project.name', 'project-2'],
|
|
146
173
|
},
|
|
147
174
|
],
|
|
148
175
|
},
|
|
149
176
|
|
|
150
|
-
unresolved:
|
|
177
|
+
unresolved: [
|
|
178
|
+
{
|
|
179
|
+
path: 'resources.a-function.projects[1]',
|
|
180
|
+
ref: '$.resources.a-project.name',
|
|
181
|
+
},
|
|
182
|
+
],
|
|
151
183
|
},
|
|
152
184
|
|
|
153
185
|
// This is so weird please don't do this omg
|
|
@@ -184,7 +216,7 @@ export default {
|
|
|
184
216
|
config: {
|
|
185
217
|
settings: [
|
|
186
218
|
{
|
|
187
|
-
projects: ['project-1', 'project-2', 'a-project'],
|
|
219
|
+
projects: ['project-1', 'project-2', '$.resources.a-project.name'],
|
|
188
220
|
},
|
|
189
221
|
],
|
|
190
222
|
},
|
|
@@ -192,7 +224,12 @@ export default {
|
|
|
192
224
|
],
|
|
193
225
|
},
|
|
194
226
|
|
|
195
|
-
unresolved:
|
|
227
|
+
unresolved: [
|
|
228
|
+
{
|
|
229
|
+
path: 'resources.a-function.config.settings[0].projects[2]',
|
|
230
|
+
ref: '$.resources.a-project.name',
|
|
231
|
+
},
|
|
232
|
+
],
|
|
196
233
|
},
|
|
197
234
|
|
|
198
235
|
arrayReferenceToParams: {
|
|
@@ -272,13 +309,18 @@ export default {
|
|
|
272
309
|
{
|
|
273
310
|
name: 'a-function',
|
|
274
311
|
type: 'cloud-function',
|
|
275
|
-
config: {memory:
|
|
312
|
+
config: {memory: 1000},
|
|
313
|
+
},
|
|
314
|
+
{
|
|
315
|
+
name: 'another-function',
|
|
316
|
+
type: 'cloud-function',
|
|
317
|
+
config: {memory: '$.resources.a-function.config.memory'},
|
|
276
318
|
},
|
|
277
319
|
],
|
|
278
320
|
outputs: [
|
|
279
321
|
{
|
|
280
322
|
name: 'configured-memory',
|
|
281
|
-
value: '$.
|
|
323
|
+
value: '$.resources.another-function.config.memory',
|
|
282
324
|
},
|
|
283
325
|
],
|
|
284
326
|
},
|
|
@@ -288,29 +330,30 @@ export default {
|
|
|
288
330
|
{
|
|
289
331
|
name: 'a-function',
|
|
290
332
|
type: 'cloud-function',
|
|
291
|
-
config: {memory:
|
|
333
|
+
config: {memory: 1000},
|
|
334
|
+
},
|
|
335
|
+
{
|
|
336
|
+
name: 'another-function',
|
|
337
|
+
type: 'cloud-function',
|
|
338
|
+
config: {memory: '$.resources.a-function.config.memory'},
|
|
292
339
|
},
|
|
293
340
|
],
|
|
294
341
|
outputs: [
|
|
295
342
|
{
|
|
296
343
|
name: 'configured-memory',
|
|
297
|
-
value: '$.
|
|
344
|
+
value: '$.resources.another-function.config.memory',
|
|
298
345
|
},
|
|
299
346
|
],
|
|
300
347
|
},
|
|
301
348
|
|
|
302
349
|
unresolved: [
|
|
303
350
|
{
|
|
304
|
-
path: 'resources.
|
|
305
|
-
|
|
306
|
-
ref: '$.values.memory',
|
|
307
|
-
item: {memory: '$.values.memory'},
|
|
351
|
+
path: 'resources.another-function.config.memory',
|
|
352
|
+
ref: '$.resources.a-function.config.memory',
|
|
308
353
|
},
|
|
309
354
|
{
|
|
310
355
|
path: 'outputs.configured-memory.value',
|
|
311
|
-
|
|
312
|
-
ref: '$.values.memory',
|
|
313
|
-
item: {name: 'configured-memory', value: '$.values.memory'},
|
|
356
|
+
ref: '$.resources.another-function.config.memory',
|
|
314
357
|
},
|
|
315
358
|
],
|
|
316
359
|
},
|
|
@@ -339,10 +382,7 @@ export default {
|
|
|
339
382
|
unresolved: [
|
|
340
383
|
{
|
|
341
384
|
path: 'resources.a-function.projects[2]',
|
|
342
|
-
property: '$.resources.a-project.name',
|
|
343
385
|
ref: '$.resources.a-project.name',
|
|
344
|
-
item: ['project-1', 'project-2', '$.resources.a-project.name', 'project-4'],
|
|
345
|
-
index: 2,
|
|
346
386
|
},
|
|
347
387
|
],
|
|
348
388
|
},
|
|
@@ -383,10 +423,7 @@ export default {
|
|
|
383
423
|
unresolved: [
|
|
384
424
|
{
|
|
385
425
|
path: 'resources.a-function.config.settings[0].projects[0]',
|
|
386
|
-
property: '$.resources.a-project.name',
|
|
387
426
|
ref: '$.resources.a-project.name',
|
|
388
|
-
item: ['$.resources.a-project.name', 'project-2', 'project-3'],
|
|
389
|
-
index: 0,
|
|
390
427
|
},
|
|
391
428
|
],
|
|
392
429
|
},
|
package/test/unit/index.test.js
CHANGED
|
@@ -111,7 +111,7 @@ tap.test('Reference resolution', async (t) => {
|
|
|
111
111
|
debug: true,
|
|
112
112
|
parameters,
|
|
113
113
|
})
|
|
114
|
-
t.same(output.result, 'valid')
|
|
114
|
+
t.same(output.result, 'valid', `${name}: returned expected result`)
|
|
115
115
|
if (output.result === 'valid') {
|
|
116
116
|
const {blueprint, unresolvedRefs} = output
|
|
117
117
|
t.same(blueprint, expected, `${name}: returned expected blueprint`)
|