@sanity/blueprints-parser 0.1.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.
@@ -0,0 +1,326 @@
1
+ import {
2
+ formatProperties,
3
+ nameFormat,
4
+ parameterTypes,
5
+ typeFormat,
6
+ versionFormat,
7
+ } from './constants.js'
8
+ import is from './is.js'
9
+
10
+ export default {
11
+ version: (blueprintVersion) => {
12
+ const errors = []
13
+ const type = 'invalid_version'
14
+ if (!is.string(blueprintVersion) || !versionFormat.test(blueprintVersion)) {
15
+ return errors.push({message: `Invalid version: ${blueprintVersion}`, type})
16
+ }
17
+ const [y, m, d] = blueprintVersion.split('-')
18
+ if (y < '2024') {
19
+ errors.push({message: `Invalid version year: ${y}`, type})
20
+ }
21
+ if (m < '01' || m > '12') {
22
+ errors.push({message: `Invalid version month: ${m}`, type})
23
+ }
24
+ if (d < '01' || d > '31') {
25
+ errors.push({message: `Invalid version day: ${d}`, type})
26
+ }
27
+
28
+ if (errors.length) return errors
29
+ },
30
+
31
+ resources: (resources) => {
32
+ if (!is.defined(resources)) return
33
+
34
+ if (!is.array(resources)) {
35
+ return [
36
+ {
37
+ message: 'Resources must be an array',
38
+ type: 'invalid_type',
39
+ },
40
+ ]
41
+ }
42
+
43
+ const errors = []
44
+ const names = []
45
+ resources.forEach((resource) => {
46
+ if (!is.object(resource)) {
47
+ return errors.push({
48
+ // Maybe we should break out the stringified resource into a data field?
49
+ message: `Resources must be an object, found: ${JSON.stringify(resource)}`,
50
+ type: 'invalid_type',
51
+ })
52
+ }
53
+
54
+ const {name, type} = resource
55
+ if (!is.defined(name)) {
56
+ errors.push({
57
+ message: `Resource must have a 'name' property`,
58
+ type: 'missing_required_property',
59
+ })
60
+ } else if (!is.string(name)) {
61
+ errors.push({
62
+ message: `Resource 'name' property must be a string, found: ${name}`,
63
+ type: 'invalid_type',
64
+ })
65
+ } else {
66
+ if (names.includes(name)) {
67
+ errors.push({
68
+ message: `All resource 'name' properties must be unique, found: ${name}`,
69
+ type: 'duplicate_name',
70
+ })
71
+ }
72
+ if (!nameFormat.test(name)) {
73
+ errors.push({
74
+ message: `Resource 'name' property is invalid, must conform to '${nameFormat}', found: ${name}`,
75
+ type: 'invalid_format',
76
+ })
77
+ }
78
+ names.push(name)
79
+ }
80
+
81
+ if (!is.defined(type)) {
82
+ errors.push({
83
+ message: `Resource must have a 'type' property`,
84
+ type: 'missing_required_property',
85
+ })
86
+ } else if (!is.string(type)) {
87
+ errors.push({
88
+ message: `Resource 'type' property must be a string, found: ${type}`,
89
+ type: 'invalid_type',
90
+ })
91
+ } else if (!typeFormat.test(type)) {
92
+ errors.push({
93
+ message: `Resource 'type' property is invalid, must conform to '${typeFormat}', found: ${type}`,
94
+ type: 'invalid_format',
95
+ })
96
+ }
97
+ })
98
+
99
+ if (errors.length) return errors
100
+ },
101
+
102
+ values: (values) => {
103
+ if (!is.defined(values)) return
104
+
105
+ if (!is.object(values)) {
106
+ return [
107
+ {
108
+ message: 'Values must be an object',
109
+ type: 'invalid_type',
110
+ },
111
+ ]
112
+ }
113
+
114
+ const errors = []
115
+ if (Object.keys(values).length) {
116
+ Object.entries(values).forEach(([name, value]) => {
117
+ if (!is.scalar(value)) {
118
+ errors.push({
119
+ message: `Values property '${name}' must be scalar (string or number)`,
120
+ type: 'invalid_type',
121
+ })
122
+ }
123
+ if (is.ref(value)) {
124
+ errors.push({
125
+ message: `Values property '${name}' cannot be a reference, found: ${value}`,
126
+ type: 'invalid_type',
127
+ })
128
+ }
129
+ })
130
+ }
131
+
132
+ if (errors.length) return errors
133
+ },
134
+
135
+ parameters: (parameters) => {
136
+ if (!is.defined(parameters)) return
137
+
138
+ if (!is.array(parameters)) {
139
+ return [
140
+ {
141
+ message: 'Parameters must be an array',
142
+ type: 'invalid_type',
143
+ },
144
+ ]
145
+ }
146
+
147
+ const errors = []
148
+ const names = []
149
+ if (parameters.length) {
150
+ parameters.forEach((param) => {
151
+ if (!is.object(param)) {
152
+ return errors.push({
153
+ // Maybe we should break out the stringified parameter into a data field?
154
+ message: `Parameters must be an object, found: ${JSON.stringify(param)}`,
155
+ type: 'invalid_type',
156
+ })
157
+ }
158
+
159
+ const {name, type} = param
160
+ if (!is.defined(name)) {
161
+ errors.push({
162
+ message: `Parameter must have a 'name' property`,
163
+ type: 'missing_required_property',
164
+ })
165
+ } else if (!is.string(name)) {
166
+ errors.push({
167
+ message: `Parameter 'name' property must be a string, found: ${name}`,
168
+ type: 'invalid_type',
169
+ })
170
+ } else {
171
+ if (names.includes(name)) {
172
+ errors.push({
173
+ message: `All parameter 'name' properties must be unique, found: ${name}`,
174
+ type: 'duplicate_name',
175
+ })
176
+ }
177
+ if (!nameFormat.test(name)) {
178
+ errors.push({
179
+ message: `Parameter 'name' property is invalid, must conform to '${nameFormat}', found: ${name}`,
180
+ type: 'invalid_format',
181
+ })
182
+ }
183
+ names.push(name)
184
+ }
185
+
186
+ if (!is.defined(type)) {
187
+ errors.push({
188
+ message: `Parameter must have a 'type' property`,
189
+ type: 'missing_required_property',
190
+ })
191
+ } else if (!is.string(type)) {
192
+ errors.push({
193
+ message: `Parameter 'type' property must be a string, found: ${type}`,
194
+ type: 'invalid_type',
195
+ })
196
+ } else if (!parameterTypes.includes(type)) {
197
+ errors.push({
198
+ message: `Unknown parameter 'type', found: ${type}`,
199
+ type: 'invalid_value',
200
+ })
201
+ }
202
+ })
203
+ }
204
+
205
+ if (errors.length) return errors
206
+ },
207
+
208
+ outputs: (outputs) => {
209
+ if (!is.defined(outputs)) return
210
+
211
+ if (!is.array(outputs)) {
212
+ return [
213
+ {
214
+ message: 'Outputs must be an array',
215
+ type: 'invalid_type',
216
+ },
217
+ ]
218
+ }
219
+
220
+ const errors = []
221
+ const names = []
222
+ outputs.forEach((output) => {
223
+ if (!is.object(output)) {
224
+ return errors.push({
225
+ // Maybe we should break out the stringified output into a data field?
226
+ message: `Outputs must be an object, found: ${JSON.stringify(output)}`,
227
+ type: 'invalid_type',
228
+ })
229
+ }
230
+
231
+ const {name, value} = output
232
+ if (!is.defined(name)) {
233
+ errors.push({
234
+ message: `Output must have a 'name' property`,
235
+ type: 'missing_required_property',
236
+ })
237
+ } else if (!is.string(name)) {
238
+ errors.push({
239
+ message: `Output 'name' property must be a string, found: ${name}`,
240
+ type: 'invalid_type',
241
+ })
242
+ } else {
243
+ if (names.includes(name)) {
244
+ errors.push({
245
+ message: `All output 'name' properties must be unique, found: ${name}`,
246
+ type: 'duplicate_name',
247
+ })
248
+ }
249
+ if (!nameFormat.test(name)) {
250
+ errors.push({
251
+ message: `Output 'name' property is invalid, must conform to '${nameFormat}', found: ${name}`,
252
+ type: 'invalid_format',
253
+ })
254
+ }
255
+ names.push(name)
256
+ }
257
+
258
+ if (!is.defined(value)) {
259
+ errors.push({
260
+ message: `Output must have a 'value' property`,
261
+ type: 'missing_required_property',
262
+ })
263
+ }
264
+ })
265
+
266
+ if (errors.length) return errors
267
+ },
268
+
269
+ metadata: (metadata) => {
270
+ if (!is.defined(metadata)) return
271
+
272
+ if (!is.object(metadata)) {
273
+ return [
274
+ {
275
+ message: 'Metadata must be an object',
276
+ type: 'invalid_type',
277
+ },
278
+ ]
279
+ }
280
+ },
281
+
282
+ else: (rawBlueprint) => {
283
+ const properties = Object.keys(rawBlueprint)
284
+ if (!properties.length) return
285
+
286
+ const errors = []
287
+
288
+ for (const property of properties) {
289
+ if (!formatProperties.includes(property)) {
290
+ errors.push({
291
+ message: `Found invalid Blueprint property: ${property}`,
292
+ type: 'invalid_property',
293
+ })
294
+ }
295
+ }
296
+
297
+ if (errors.length) return errors
298
+ },
299
+
300
+ passedParameters: (options) => {
301
+ const {parameters} = options
302
+ if (!is.defined(parameters)) return
303
+
304
+ if (!is.object(parameters)) {
305
+ return [
306
+ {
307
+ message: 'Passed parameters must be an object',
308
+ type: 'invalid_type',
309
+ },
310
+ ]
311
+ }
312
+
313
+ const errors = []
314
+
315
+ for (const [name, value] of Object.entries(parameters)) {
316
+ if (!is.scalar(value)) {
317
+ errors.push({
318
+ message: `Passed parameter '${name}' must be scalar (string or number)`,
319
+ type: 'invalid_type',
320
+ })
321
+ }
322
+ }
323
+
324
+ if (errors.length) return errors
325
+ },
326
+ }
@@ -0,0 +1,6 @@
1
+ import basic from './basic.js'
2
+ import basicErrors from './basic-errors.js'
3
+ import referenceErrors from './reference-errors.js'
4
+ import references from './references.js'
5
+
6
+ export default {basic, basicErrors, references, referenceErrors}
@@ -0,0 +1,274 @@
1
+ const ignoreJSONSchemaTest = {metadata: {ignore: true}}
2
+
3
+ export default {
4
+ // blueprintVersion
5
+ blueprintVersionType: {
6
+ blueprintVersion: true,
7
+ },
8
+ blueprintVersionNonDate: {
9
+ blueprintVersion: 'meh',
10
+ },
11
+ blueprintVersionInvalidYear: {
12
+ blueprintVersion: '2000-01-01',
13
+ ...ignoreJSONSchemaTest,
14
+ },
15
+ blueprintVersionInvalidMonth: {
16
+ blueprintVersion: '2024-13-01',
17
+ ...ignoreJSONSchemaTest,
18
+ },
19
+ blueprintVersionInvalidDay: {
20
+ blueprintVersion: '2024-10-33',
21
+ ...ignoreJSONSchemaTest,
22
+ },
23
+
24
+ // resources
25
+ resourcesType: {
26
+ resources: {},
27
+ },
28
+ resourcesResourceType: {
29
+ resources: ['hi'],
30
+ },
31
+ resourcesMissingName: {
32
+ resources: [
33
+ {
34
+ type: 'a-type',
35
+ },
36
+ ],
37
+ },
38
+ resourcesMissingType: {
39
+ resources: [
40
+ {
41
+ name: 'a-resource',
42
+ },
43
+ ],
44
+ },
45
+ resourcesInvalidName: {
46
+ resources: [
47
+ {
48
+ name: 123,
49
+ type: 'a-type',
50
+ },
51
+ ],
52
+ },
53
+ resourcesInvalidNameFormat: {
54
+ resources: [
55
+ {
56
+ name: '@-resource',
57
+ type: 'a-type',
58
+ },
59
+ ],
60
+ },
61
+ resourcesInvalidNameFormatTrailingNonAlphanumeric: {
62
+ resources: [
63
+ {
64
+ name: 'a-resource-',
65
+ type: 'a-type',
66
+ },
67
+ ],
68
+ },
69
+ resourcesInvalidType: {
70
+ resources: [
71
+ {
72
+ name: 'a-resource',
73
+ type: true,
74
+ },
75
+ ],
76
+ },
77
+ resourcesInvalidTypeFormat: {
78
+ resources: [
79
+ {
80
+ name: 'a-resource',
81
+ type: '@-type',
82
+ },
83
+ ],
84
+ },
85
+ resourcesInvalidTypeFormatTrailingNonAlphanumeric: {
86
+ resources: [
87
+ {
88
+ name: 'a-resource',
89
+ type: 'a-type-',
90
+ },
91
+ ],
92
+ },
93
+ resourcesInvalidTypeFormatTrailingPeriod: {
94
+ resources: [
95
+ {
96
+ name: 'a-resource',
97
+ type: 'a.type.',
98
+ },
99
+ ],
100
+ },
101
+ resourcesDuplicateName: {
102
+ resources: [
103
+ {
104
+ name: 'a-resource',
105
+ type: 'a-type',
106
+ },
107
+ {
108
+ name: 'a-resource',
109
+ type: 'a-type',
110
+ },
111
+ ],
112
+ ...ignoreJSONSchemaTest,
113
+ },
114
+
115
+ // values
116
+ valuesType: {
117
+ values: 'hi',
118
+ },
119
+ valuesNonScalar: {
120
+ values: {anObject: {}},
121
+ },
122
+ valuesReference: {
123
+ values: {aRef: '$.values.yo'},
124
+ ...ignoreJSONSchemaTest,
125
+ },
126
+
127
+ // parameters
128
+ parametersType: {
129
+ parameters: {},
130
+ },
131
+ parametersParameterType: {
132
+ parameters: ['hi'],
133
+ },
134
+ parametersMissingName: {
135
+ parameters: [
136
+ {
137
+ type: 'stdin',
138
+ },
139
+ ],
140
+ },
141
+ parametersMissingType: {
142
+ parameters: [
143
+ {
144
+ name: 'a-param',
145
+ },
146
+ ],
147
+ },
148
+ parametersInvalidName: {
149
+ parameters: [
150
+ {
151
+ name: 123,
152
+ type: 'stdin',
153
+ },
154
+ ],
155
+ },
156
+ parametersInvalidNameFormat: {
157
+ parameters: [
158
+ {
159
+ name: '@-parameter',
160
+ type: 'stdin',
161
+ },
162
+ ],
163
+ },
164
+ parametersInvalidNameFormatTrailingNonAlphanumeric: {
165
+ parameters: [
166
+ {
167
+ name: 'a-parameter-',
168
+ type: 'stdin',
169
+ },
170
+ ],
171
+ },
172
+ parametersInvalidType: {
173
+ parameters: [
174
+ {
175
+ name: 'a-param',
176
+ type: true,
177
+ },
178
+ ],
179
+ },
180
+ parametersInvalidTypeString: {
181
+ parameters: [
182
+ {
183
+ name: 'a-param',
184
+ type: 'idklol',
185
+ },
186
+ ],
187
+ },
188
+ parametersDuplicateName: {
189
+ parameters: [
190
+ {
191
+ name: 'a-param',
192
+ type: 'stdin',
193
+ },
194
+ {
195
+ name: 'a-param',
196
+ type: 'stdin',
197
+ },
198
+ ],
199
+ ...ignoreJSONSchemaTest,
200
+ },
201
+
202
+ // outputs
203
+ outputsType: {
204
+ outputs: {},
205
+ },
206
+ outputsParameterType: {
207
+ outputs: ['hi'],
208
+ },
209
+ outputsMissingName: {
210
+ outputs: [
211
+ {
212
+ value: 'a-value',
213
+ },
214
+ ],
215
+ },
216
+ outputsMissingValue: {
217
+ outputs: [
218
+ {
219
+ name: 'an-output',
220
+ },
221
+ ],
222
+ },
223
+ outputsInvalidName: {
224
+ outputs: [
225
+ {
226
+ name: 123,
227
+ value: 'a-value',
228
+ },
229
+ ],
230
+ },
231
+ outputsInvalidNameFormat: {
232
+ outputs: [
233
+ {
234
+ name: '@n-output',
235
+ value: 'a-value',
236
+ },
237
+ ],
238
+ },
239
+ outputsInvalidNameFormatTrailingNonAlphanumeric: {
240
+ outputs: [
241
+ {
242
+ name: 'an-output-',
243
+ type: 'a-value',
244
+ },
245
+ ],
246
+ },
247
+ outputsDuplicateName: {
248
+ outputs: [
249
+ {
250
+ name: 'an-output',
251
+ value: 'a-value',
252
+ },
253
+ {
254
+ name: 'an-output',
255
+ value: 'a-value',
256
+ },
257
+ ],
258
+ ...ignoreJSONSchemaTest,
259
+ },
260
+
261
+ // metadata
262
+ metadataType: {
263
+ metadata: false,
264
+ },
265
+
266
+ // else
267
+ unknownProperty: {
268
+ idklol: [],
269
+ },
270
+ knownAndUnknownProperty: {
271
+ resources: [],
272
+ idklol: [],
273
+ },
274
+ }
@@ -0,0 +1,58 @@
1
+ export default {
2
+ min: {},
3
+
4
+ // resources
5
+ minResources: {
6
+ resources: [],
7
+ },
8
+ minResourcesWithResource: {
9
+ resources: [
10
+ {
11
+ name: 'a-resource',
12
+ type: 'a-type',
13
+ },
14
+ ],
15
+ },
16
+
17
+ // values
18
+ minValues: {
19
+ values: {},
20
+ },
21
+ minValuesWithValue: {
22
+ values: {hello: 'there'},
23
+ },
24
+
25
+ // parameters
26
+ minParameters: {
27
+ parameters: [],
28
+ },
29
+ minParametersWithParameter: {
30
+ parameters: [
31
+ {
32
+ name: 'a-param',
33
+ type: 'stdin',
34
+ },
35
+ ],
36
+ },
37
+
38
+ // outputs
39
+ minOutputs: {
40
+ outputs: [],
41
+ },
42
+ minOutputsWithOutput: {
43
+ outputs: [
44
+ {
45
+ name: 'an-output',
46
+ value: true,
47
+ },
48
+ ],
49
+ },
50
+
51
+ // metadata
52
+ minMetadata: {
53
+ metadata: {},
54
+ },
55
+ minMetadataWithMetadata: {
56
+ metadata: {ok: true},
57
+ },
58
+ }