@sanity/blueprints-parser 0.2.0 → 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.
@@ -39,7 +39,7 @@ jobs:
39
39
  matrix:
40
40
  # Run the testing suite on each major OS with the latest LTS release of Node.js
41
41
  os: [macos-latest, ubuntu-latest, windows-latest]
42
- node-version: ["lts/*"]
42
+ node-version: [20, 22, 24]
43
43
  steps:
44
44
  - uses: actions/checkout@v4
45
45
  - uses: actions/setup-node@v4
package/CHANGELOG.md CHANGED
@@ -1,5 +1,28 @@
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
+
19
+ ## [0.2.1](https://github.com/sanity-io/blueprints-parser/compare/v0.2.0...v0.2.1) (2025-09-04)
20
+
21
+
22
+ ### Bug Fixes
23
+
24
+ * Support node 20.19 ([#7](https://github.com/sanity-io/blueprints-parser/issues/7)) ([d85c24e](https://github.com/sanity-io/blueprints-parser/commit/d85c24e172e5e898cc6689f003c11efb0f2afde0))
25
+
3
26
  ## [0.2.0](https://github.com/sanity-io/blueprints-parser/compare/v0.1.0...v0.2.0) (2025-08-29)
4
27
 
5
28
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanity/blueprints-parser",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "A parser and validator for Sanity Blueprint documents.",
5
5
  "keywords": [
6
6
  "sanity",
@@ -20,18 +20,18 @@
20
20
  "scripts": {
21
21
  "test": "npm run test:unit && npm run test:integration",
22
22
  "test:unit": "npx tap run --include=test/unit/**/* --coverage-report=text --coverage-report=lcov --no-browser",
23
- "test:integration": "node --test test/integration/**/*.test.js",
23
+ "test:integration": "cd test/integration && npm test",
24
24
  "lint": "biome ci",
25
25
  "lint:write": "biome check --write"
26
26
  },
27
27
  "license": "MIT",
28
28
  "engines": {
29
- "node": ">=22"
29
+ "node": ">=20.19 <22 || >=22.12"
30
30
  },
31
31
  "author": "Sanity.io <hello@sanity.io>",
32
32
  "devDependencies": {
33
33
  "@biomejs/biome": "^2.1.4",
34
- "@sanity/blueprints-jsonschemas": "^0.1.0",
34
+ "@sanity/blueprints-jsonschemas": "^0.1.1",
35
35
  "ajv": "^8.17.1",
36
36
  "tap": "^21.1.0"
37
37
  },
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, string | number | boolean>
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, string | number>
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 Reference = {
69
+ export type UnresolvedReference = {
70
+ /** The location where the reference was found */
68
71
  path: string
69
- property: string
72
+ /** The content of the reference (e.g. $.resources.project-1.id) */
70
73
  ref: string
71
- } & ({item: Record<string, unknown>} | {item: Array<unknown>; index: number})
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<Reference>
94
+ unresolvedRefs?: Array<UnresolvedReference>
85
95
  }
86
96
  | {
87
97
  result: 'reference_errors'
88
98
  blueprint: Blueprint
89
- unresolvedRefs?: Array<Reference>
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, property: name, ref: value, item})
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, property: value, ref: value, item, index})
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('.').Reference> | undefined
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 {property, ref, item} = foundRef
73
- const refPath = ref.replace(/^\$\./, '')
75
+ const {ref} = foundRef
74
76
  // Early return from an already found reference
75
- if (refs[refPath]) {
76
- item[property] = refs[refPath]
77
+ if (refs[ref]) {
78
+ foundRef.container[foundRef.property] = refs[ref].container[refs[ref].property]
77
79
  continue
78
80
  }
79
81
 
80
- // Parameters are special, (try to) find them in options.parameters passed by the caller
81
- const parts = refPath.split('.')
82
- const param = parts[1]
83
- if (['parameters', 'params'].includes(parts[0])) {
84
- const found = parameters[param]
85
- if (is.scalar(found)) {
86
- refs[refPath] = found
87
- if (is.object(item)) item[property] = refs[refPath]
88
- if (is.array(item)) {
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}': '${param}' not found in passed parameters`,
93
+ message: `Reference error '${ref}': '${refName}' not found in passed parameters`,
95
94
  type: 'missing_parameter',
96
95
  })
97
96
  }
98
- continue
99
- }
100
-
101
- // Will prob need to refactor the following to get a bit more readable, and introduce more subtle behavior
102
- const found = parts.reduce((obj, i) => {
103
- if (obj?.[i]) return obj[i]
104
- if (is.array(obj)) return obj.find(({name}) => name === i)
105
- }, blueprint)
106
- if (is.scalar(found)) {
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
- unresolvedRefs.push(foundRef)
118
+ refErrors.push({
119
+ message: `Reference error '${ref}': invalid reference type ${refType}`,
120
+ type: 'invalid_reference',
121
+ })
115
122
  }
116
123
  }
117
124
 
@@ -2,6 +2,9 @@
2
2
  "name": "blueprints-parser-integration-tests",
3
3
  "private": true,
4
4
  "type": "module",
5
+ "scripts": {
6
+ "test": "node --test"
7
+ },
5
8
  "dependencies": {
6
9
  "@sanity/blueprints-parser": "file:../.."
7
10
  }
@@ -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: '$.values.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: '$.values.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.a-function.config.disk',
79
- property: 'disk',
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
  },
@@ -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: 1000},
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: undefined,
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: undefined,
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: undefined,
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: '$.values.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: '$.values.memory',
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: '$.values.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: '$.values.memory',
344
+ value: '$.resources.another-function.config.memory',
298
345
  },
299
346
  ],
300
347
  },
301
348
 
302
349
  unresolved: [
303
350
  {
304
- path: 'resources.a-function.config.memory',
305
- property: 'memory',
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
- property: 'value',
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
  },
@@ -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`)