@sanity/blueprints-parser 0.3.1 → 0.4.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 CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.4.0](https://github.com/sanity-io/blueprints-parser/compare/v0.3.1...v0.4.0) (2026-02-11)
4
+
5
+
6
+ ### Features
7
+
8
+ * Ability to specify types that cannot be referenced ([#16](https://github.com/sanity-io/blueprints-parser/issues/16)) ([4984cff](https://github.com/sanity-io/blueprints-parser/commit/4984cff4e532e5b6fc6f3f244d5555b0ec707ac3))
9
+
3
10
  ## [0.3.1](https://github.com/sanity-io/blueprints-parser/compare/v0.3.0...v0.3.1) (2026-02-06)
4
11
 
5
12
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanity/blueprints-parser",
3
- "version": "0.3.1",
3
+ "version": "0.4.0",
4
4
  "description": "A parser and validator for Sanity Blueprint documents.",
5
5
  "keywords": [
6
6
  "sanity",
package/src/index.d.ts CHANGED
@@ -1,17 +1,23 @@
1
+ /** Scalar values */
1
2
  export type Scalar = string | number
2
3
 
4
+ /** Input types the parser can accept */
3
5
  export type BlueprintInput = string | Buffer | object
4
6
 
7
+ /** Options to control the parser */
5
8
  export type ParserOptions = {
6
9
  debug?: boolean
7
10
  parameters?: Record<string, Scalar>
11
+ invalidReferenceTypes?: Array<string>
8
12
  }
9
13
 
14
+ /** The representation of a resource in the system. */
10
15
  export type Resource = {
11
16
  name: string
12
17
  type: string
13
18
  } & Record<string, unknown>
14
19
 
20
+ /** Parameters that can be passed in */
15
21
  export type Parameter = {
16
22
  name: string
17
23
  type: 'arg' | 'argument' | 'env-var' | 'envVar' | 'config' | 'stdin'
@@ -26,20 +32,29 @@ export type Parameter = {
26
32
  }
27
33
  )
28
34
 
35
+ /** Output from the parser based on the blueprint's output values */
29
36
  export type Output = {
30
37
  name: string
31
38
  value: string
32
39
  }
33
40
 
41
+ /** The blueprint document */
34
42
  export type Blueprint = {
43
+ /** The version of the parser logic to use, use YYYY-MM-DD format */
35
44
  blueprintVersion?: string
45
+ /** The list of resources to be managed by this blueprint */
36
46
  resources?: Array<Resource>
47
+ /** Values that can be referenced in the blueprint */
37
48
  values?: Record<string, Scalar>
49
+ /** Parameters that were provided from configuration or the environment */
38
50
  parameters?: Array<Parameter>
51
+ /** A list of values to be returned when the blueprint is deployed */
39
52
  outputs?: Array<Output>
53
+ /** Extra data that is not used by the parser */
40
54
  metadata?: Record<string, unknown>
41
55
  }
42
56
 
57
+ /** Errors that the parser might return */
43
58
  export type ParserError = {
44
59
  message: string
45
60
  } & (
@@ -51,6 +66,7 @@ export type ParserError = {
51
66
  type: 'invalid_input'
52
67
  }
53
68
  )
69
+ /** The return type of the internal parser */
54
70
  export type ParserOuput =
55
71
  | {
56
72
  ok: true
@@ -61,11 +77,13 @@ export type ParserOuput =
61
77
  parseErrors: Array<ParserError>
62
78
  }
63
79
 
80
+ /** An error that occurred during validation */
64
81
  export type ValidationError = {
65
82
  type: string
66
83
  message: string
67
84
  }
68
85
 
86
+ /** A reference that could not be resolved during parsing */
69
87
  export type UnresolvedReference = {
70
88
  /** The location where the reference was found */
71
89
  path: string
@@ -73,6 +91,7 @@ export type UnresolvedReference = {
73
91
  ref: string
74
92
  }
75
93
 
94
+ /** A reference found during parsing */
76
95
  export type Reference = UnresolvedReference & {
77
96
  /** The object or array containing the reference */
78
97
  container: object | Array
@@ -80,13 +99,16 @@ export type Reference = UnresolvedReference & {
80
99
  property: string | number
81
100
  }
82
101
 
102
+ /** An error with the use of a reference */
83
103
  export type ReferenceError = {
84
104
  type: string
85
105
  message: string
86
106
  }
87
107
 
108
+ /** A union of all error types */
88
109
  export type BlueprintError = ParserError | ValidationError | ReferenceError
89
110
 
111
+ /** The output of the parser */
90
112
  export type BlueprintOutput =
91
113
  | {
92
114
  result: 'valid'
@@ -108,6 +130,11 @@ export type BlueprintOutput =
108
130
  errors: Array<ValidationError>
109
131
  }
110
132
 
133
+ /**
134
+ * Parses the given document and returns any error found, or the parsed and partially resolved document.
135
+ * @param input The document to be parsed
136
+ * @param options Options to control the parser
137
+ */
111
138
  export default function blueprintParserValidator(
112
139
  input: BlueprintInput,
113
140
  options: ParserOptions = {},
package/src/refs.js CHANGED
@@ -64,7 +64,7 @@ function find(validatedBlueprint, options) {
64
64
  * }}
65
65
  */
66
66
  function resolve(blueprint, foundRefs, options) {
67
- const {parameters = {}} = options
67
+ const {parameters = {}, invalidReferenceTypes} = options
68
68
 
69
69
  /** @type {Record<string, import('.').Reference>} */
70
70
  const refs = {}
@@ -119,9 +119,18 @@ function resolve(blueprint, foundRefs, options) {
119
119
  foundRef.container[foundRef.property] = blueprint.blueprintVersion
120
120
  } else if (refType === 'resources') {
121
121
  // check that the resource is in the list
122
- if (blueprint.resources?.some((resource) => resource.name === refName)) {
123
- // all resources references must be resolved during deployment
124
- unresolvedRefs.push({path: foundRef.path, ref: foundRef.ref})
122
+ const found = blueprint.resources?.find((resource) => resource.name === refName)
123
+ if (found) {
124
+ // check if we allow references to the found resource's type
125
+ if (invalidReferenceTypes?.includes(found.type)) {
126
+ refErrors.push({
127
+ message: `Reference error '${ref}': '${refName}' type '${found.type}' cannot be referenced`,
128
+ type: 'invalid_reference',
129
+ })
130
+ } else {
131
+ // all resources references must be resolved during deployment
132
+ unresolvedRefs.push({path: foundRef.path, ref: foundRef.ref})
133
+ }
125
134
  } else {
126
135
  refErrors.push({
127
136
  message: `Reference error '${ref}': '${refName}' not found in blueprint resources`,
@@ -32,6 +32,10 @@ export default {
32
32
  },
33
33
  ],
34
34
  },
35
+ error: {
36
+ type: 'missing_parameter',
37
+ message: "Reference error '$.parameters.memory': 'memory' not found in passed parameters",
38
+ },
35
39
  },
36
40
 
37
41
  missingResourceReference: {
@@ -53,6 +57,11 @@ export default {
53
57
  },
54
58
  ],
55
59
  },
60
+ error: {
61
+ type: 'missing_resource',
62
+ message:
63
+ "Reference error '$.resources.another-function.memory': 'another-function' not found in blueprint resources",
64
+ },
56
65
  },
57
66
 
58
67
  missingValueReference: {
@@ -74,6 +83,10 @@ export default {
74
83
  },
75
84
  ],
76
85
  },
86
+ error: {
87
+ type: 'missing_value',
88
+ message: "Reference error '$.values.memory': 'memory' not found in blueprint values",
89
+ },
77
90
  },
78
91
 
79
92
  invalidReferenceTypeMetadata: {
@@ -95,6 +108,10 @@ export default {
95
108
  },
96
109
  ],
97
110
  },
111
+ error: {
112
+ type: 'invalid_reference',
113
+ message: "Reference error '$.metadata.memory': invalid reference to metadata",
114
+ },
98
115
  },
99
116
 
100
117
  invalidReferenceType: {
@@ -116,6 +133,49 @@ export default {
116
133
  },
117
134
  ],
118
135
  },
136
+ error: {
137
+ type: 'invalid_reference',
138
+ message: "Reference error '$.invalid.memory': invalid reference type invalid",
139
+ },
140
+ },
141
+
142
+ invalidReferenceResourceType: {
143
+ options: {
144
+ invalidReferenceTypes: ['cloud-function'],
145
+ },
146
+ input: {
147
+ resources: [
148
+ {
149
+ name: 'a-function-1',
150
+ type: 'cloud-function',
151
+ config: {memory: 1000},
152
+ },
153
+ {
154
+ name: 'a-function-2',
155
+ type: 'cloud-function',
156
+ config: {memory: '$.resources.a-function-1.config.memory'},
157
+ },
158
+ ],
159
+ },
160
+ expected: {
161
+ resources: [
162
+ {
163
+ name: 'a-function-1',
164
+ type: 'cloud-function',
165
+ config: {memory: 1000},
166
+ },
167
+ {
168
+ name: 'a-function-2',
169
+ type: 'cloud-function',
170
+ config: {memory: '$.resources.a-function-1.config.memory'},
171
+ },
172
+ ],
173
+ },
174
+ error: {
175
+ type: 'invalid_reference',
176
+ message:
177
+ "Reference error '$.resources.a-function-1.config.memory': 'a-function-1' type 'cloud-function' cannot be referenced",
178
+ },
119
179
  },
120
180
 
121
181
  unresolvedParameterReferenceAndRegularReference: {
@@ -177,5 +237,9 @@ export default {
177
237
  ref: '$.resources.a-function.config.disk',
178
238
  },
179
239
  ],
240
+ error: {
241
+ type: 'missing_parameter',
242
+ message: "Reference error '$.parameters.memory': 'memory' not found in passed parameters",
243
+ },
180
244
  },
181
245
  }
@@ -102,7 +102,7 @@ tap.test('Basic input: errors', async (t) => {
102
102
  tap.test('Reference resolution', async (t) => {
103
103
  const references = Object.entries(mocks.references)
104
104
  const referenceErrors = Object.entries(mocks.referenceErrors)
105
- const tests = references.length * 4 + referenceErrors.length * 4
105
+ const tests = references.length * 4 + referenceErrors.length * 5
106
106
  t.plan(tests)
107
107
 
108
108
  // Basic reference resolution
@@ -123,17 +123,26 @@ tap.test('Reference resolution', async (t) => {
123
123
  }
124
124
 
125
125
  // Basic reference errors
126
- for (const [name, {input, expected, unresolved}] of referenceErrors) {
126
+ for (const [name, {options, input, expected, unresolved, error}] of referenceErrors) {
127
127
  const output = blueprintParserValidator(input, {
128
128
  debug: true,
129
129
  parameters: {},
130
+ ...options,
130
131
  })
131
132
  t.ok(output.result, 'result is true despite reference errors')
132
133
  if (output.result === 'reference_errors') {
133
134
  const {blueprint, unresolvedRefs, errors} = output
134
135
  t.same(blueprint, expected, `${name}: returned expected blueprint`)
135
136
  t.same(unresolvedRefs, unresolved, `${name}: returned expected unresolved references`)
136
- t.ok(errors, `${name}: parser / validator found an error`)
137
+ t.ok(errors && errors.length > 0, `${name}: parser / validator found an error`)
138
+ if (error && errors && errors.length > 0) {
139
+ t.ok(
140
+ errors.some((err) => err.type === error.type && err.message === error.message),
141
+ JSON.stringify(errors),
142
+ )
143
+ } else {
144
+ t.ok(true) // makes planning easier
145
+ }
137
146
  }
138
147
  }
139
148
  })