@sanity/blueprints-parser 0.3.1 → 0.5.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/.gitattributes +2 -0
- package/.github/workflows/ci.yaml +19 -15
- package/.github/workflows/release-please.yaml +4 -4
- package/CHANGELOG.md +14 -0
- package/biome.jsonc +4 -7
- package/package.json +7 -7
- package/src/constants.js +1 -1
- package/src/index.d.ts +47 -2
- package/src/index.js +1 -1
- package/src/refs.js +61 -18
- package/test/mocks/reference-errors.js +106 -0
- package/test/mocks/references.js +63 -0
- package/test/unit/index.test.js +64 -41
package/.gitattributes
ADDED
|
@@ -11,15 +11,15 @@ concurrency:
|
|
|
11
11
|
cancel-in-progress: true
|
|
12
12
|
|
|
13
13
|
permissions:
|
|
14
|
-
contents: read
|
|
14
|
+
contents: read
|
|
15
15
|
|
|
16
16
|
jobs:
|
|
17
17
|
lint:
|
|
18
18
|
name: Lint
|
|
19
19
|
runs-on: ubuntu-latest
|
|
20
20
|
steps:
|
|
21
|
-
- uses: actions/checkout@
|
|
22
|
-
- uses: actions/setup-node@
|
|
21
|
+
- uses: actions/checkout@v7
|
|
22
|
+
- uses: actions/setup-node@v7
|
|
23
23
|
with:
|
|
24
24
|
cache: npm
|
|
25
25
|
node-version: lts/*
|
|
@@ -27,29 +27,33 @@ jobs:
|
|
|
27
27
|
- run: npm run lint
|
|
28
28
|
|
|
29
29
|
test:
|
|
30
|
-
permissions:
|
|
31
|
-
# Required to checkout the code
|
|
32
|
-
contents: read
|
|
33
|
-
# Required to put a comment into the pull-request
|
|
34
|
-
pull-requests: write
|
|
35
30
|
name: Test
|
|
36
31
|
runs-on: ${{ matrix.os }}
|
|
37
32
|
strategy:
|
|
38
33
|
fail-fast: false
|
|
39
34
|
matrix:
|
|
40
|
-
# Run the testing suite on each major OS with the latest LTS release of Node.js
|
|
41
35
|
os: [macos-latest, ubuntu-latest, windows-latest]
|
|
42
|
-
node-version: [
|
|
36
|
+
node-version: [22, 24, 26]
|
|
43
37
|
steps:
|
|
44
|
-
- uses: actions/checkout@
|
|
45
|
-
- uses: actions/setup-node@
|
|
38
|
+
- uses: actions/checkout@v7
|
|
39
|
+
- uses: actions/setup-node@v7
|
|
46
40
|
with:
|
|
47
41
|
cache: npm
|
|
48
42
|
node-version: ${{ matrix.node-version }}
|
|
49
43
|
- run: npm ci
|
|
50
44
|
- run: cd test/integration; npm install
|
|
51
45
|
- run: npm test
|
|
52
|
-
|
|
46
|
+
|
|
47
|
+
coverage:
|
|
48
|
+
permissions:
|
|
49
|
+
contents: read
|
|
50
|
+
name: Coverage
|
|
51
|
+
runs-on: ubuntu-latest
|
|
52
|
+
steps:
|
|
53
|
+
- uses: actions/checkout@v7
|
|
54
|
+
- uses: actions/setup-node@v7
|
|
53
55
|
with:
|
|
54
|
-
|
|
55
|
-
|
|
56
|
+
cache: npm
|
|
57
|
+
node-version: lts/*
|
|
58
|
+
- run: npm ci
|
|
59
|
+
- run: npm run test:unit:coverage
|
|
@@ -23,22 +23,22 @@ jobs:
|
|
|
23
23
|
# permissions for pushing commits and opening PRs are handled by the `generate-token` step
|
|
24
24
|
runs-on: ubuntu-latest
|
|
25
25
|
steps:
|
|
26
|
-
- uses: actions/create-github-app-token@
|
|
26
|
+
- uses: actions/create-github-app-token@v3
|
|
27
27
|
id: app-token
|
|
28
28
|
with:
|
|
29
29
|
app-id: ${{ secrets.ECOSPARK_APP_ID }}
|
|
30
30
|
private-key: ${{ secrets.ECOSPARK_APP_PRIVATE_KEY }}
|
|
31
31
|
# This action will create a release PR when regular conventional commits are pushed to main, it'll also detect if a release PR is merged and npm publish should happen
|
|
32
|
-
- uses: googleapis/release-please-action@
|
|
32
|
+
- uses: googleapis/release-please-action@v5
|
|
33
33
|
id: release
|
|
34
34
|
with:
|
|
35
35
|
release-type: node
|
|
36
36
|
token: ${{ steps.app-token.outputs.token }}
|
|
37
37
|
|
|
38
38
|
# Publish to NPM on new releases
|
|
39
|
-
- uses: actions/checkout@
|
|
39
|
+
- uses: actions/checkout@v7
|
|
40
40
|
if: ${{ steps.release.outputs.release_created || github.event.inputs.publish == 'true' }}
|
|
41
|
-
- uses: actions/setup-node@
|
|
41
|
+
- uses: actions/setup-node@v7
|
|
42
42
|
if: ${{ steps.release.outputs.release_created || github.event.inputs.publish == 'true' }}
|
|
43
43
|
with:
|
|
44
44
|
cache: npm
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.5.0](https://github.com/sanity-io/blueprints-parser/compare/v0.4.0...v0.5.0) (2026-08-13)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* unresolvedRef gets source and target results ([#18](https://github.com/sanity-io/blueprints-parser/issues/18)) ([81c5bdd](https://github.com/sanity-io/blueprints-parser/commit/81c5bdd5f01bf1a2f54df7bb352bce4e917a99d9))
|
|
9
|
+
|
|
10
|
+
## [0.4.0](https://github.com/sanity-io/blueprints-parser/compare/v0.3.1...v0.4.0) (2026-02-11)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### Features
|
|
14
|
+
|
|
15
|
+
* 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))
|
|
16
|
+
|
|
3
17
|
## [0.3.1](https://github.com/sanity-io/blueprints-parser/compare/v0.3.0...v0.3.1) (2026-02-06)
|
|
4
18
|
|
|
5
19
|
|
package/biome.jsonc
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
|
-
"$schema": "https://biomejs.dev/schemas/2.
|
|
2
|
+
"$schema": "https://biomejs.dev/schemas/2.5.8/schema.json",
|
|
3
3
|
"vcs": {
|
|
4
|
-
"enabled":
|
|
4
|
+
"enabled": true,
|
|
5
5
|
"clientKind": "git",
|
|
6
|
-
"useIgnoreFile":
|
|
6
|
+
"useIgnoreFile": true
|
|
7
7
|
},
|
|
8
8
|
"files": {
|
|
9
9
|
"ignoreUnknown": false,
|
|
@@ -24,10 +24,7 @@
|
|
|
24
24
|
"linter": {
|
|
25
25
|
"enabled": true,
|
|
26
26
|
"rules": {
|
|
27
|
-
"
|
|
28
|
-
"complexity": {
|
|
29
|
-
"noForEach": "off"
|
|
30
|
-
}
|
|
27
|
+
"preset": "recommended"
|
|
31
28
|
}
|
|
32
29
|
},
|
|
33
30
|
"javascript": {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sanity/blueprints-parser",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "A parser and validator for Sanity Blueprint documents.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"sanity",
|
|
@@ -18,22 +18,22 @@
|
|
|
18
18
|
"main": "src/index.js",
|
|
19
19
|
"types": "src/index.d.ts",
|
|
20
20
|
"scripts": {
|
|
21
|
-
"test": "npm run test:unit && npm run test:integration",
|
|
22
|
-
"test:unit": "
|
|
21
|
+
"test": "npm run test:unit && npm run test:integration && npm run lint",
|
|
22
|
+
"test:unit": "node --test test/unit/index.test.js",
|
|
23
|
+
"test:unit:coverage": "node --test --experimental-test-coverage --test-coverage-exclude=\"test/**\" --test-coverage-lines=100 --test-coverage-branches=100 --test-coverage-functions=100 test/unit/index.test.js",
|
|
23
24
|
"test:integration": "cd test/integration && npm test",
|
|
24
25
|
"lint": "biome ci",
|
|
25
26
|
"lint:write": "biome check --write"
|
|
26
27
|
},
|
|
27
28
|
"license": "MIT",
|
|
28
29
|
"engines": {
|
|
29
|
-
"node": ">=
|
|
30
|
+
"node": ">=22.12"
|
|
30
31
|
},
|
|
31
32
|
"author": "Sanity.io <hello@sanity.io>",
|
|
32
33
|
"devDependencies": {
|
|
33
|
-
"@biomejs/biome": "^2.
|
|
34
|
+
"@biomejs/biome": "^2.5.8",
|
|
34
35
|
"@sanity/blueprints-jsonschemas": "^0.1.1",
|
|
35
|
-
"ajv": "^8.
|
|
36
|
-
"tap": "^21.5.1"
|
|
36
|
+
"ajv": "^8.20.0"
|
|
37
37
|
},
|
|
38
38
|
"publishConfig": {
|
|
39
39
|
"access": "public"
|
package/src/constants.js
CHANGED
|
@@ -13,4 +13,4 @@ const versionFormat = /^\d{4}-\d{2}-\d{2}$/ // YYYY-MM-DD
|
|
|
13
13
|
const nameFormat = /^[a-zA-Z][a-zA-Z0-9-_]*(?<![-_])$/
|
|
14
14
|
const typeFormat = /^[a-zA-Z]([a-zA-Z0-9-_]+\.?)*(?<![-_.])$/
|
|
15
15
|
|
|
16
|
-
export {firstVersion, formatProperties,
|
|
16
|
+
export {firstVersion, formatProperties, nameFormat, parameterTypes, typeFormat, versionFormat}
|
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,32 +77,56 @@ 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
|
|
|
69
|
-
|
|
86
|
+
/** Where a reference points, or where it was found */
|
|
87
|
+
export type ReferenceEndpoint = {
|
|
88
|
+
/** The top-level collection, e.g. `resources`, `values`, `outputs` */
|
|
89
|
+
collection: string
|
|
90
|
+
/** The name of the item within that collection */
|
|
91
|
+
name: string
|
|
92
|
+
/** The path within that item, empty when the reference is to the item itself */
|
|
93
|
+
path: string
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** The location and content of a reference */
|
|
97
|
+
export type ReferenceLocation = {
|
|
70
98
|
/** The location where the reference was found */
|
|
71
99
|
path: string
|
|
72
100
|
/** The content of the reference (e.g. $.resources.project-1.id) */
|
|
73
101
|
ref: string
|
|
74
102
|
}
|
|
75
103
|
|
|
76
|
-
|
|
104
|
+
/** A reference that could not be resolved during parsing */
|
|
105
|
+
export type UnresolvedReference = ReferenceLocation & {
|
|
106
|
+
/** `ref` in split form: where the value will come from */
|
|
107
|
+
source: ReferenceEndpoint
|
|
108
|
+
/** `path` in split form: where the value belongs */
|
|
109
|
+
target: ReferenceEndpoint
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** A reference found during parsing */
|
|
113
|
+
export type Reference = ReferenceLocation & {
|
|
77
114
|
/** The object or array containing the reference */
|
|
78
115
|
container: object | Array
|
|
79
116
|
/** The key or index of the reference in the container */
|
|
80
117
|
property: string | number
|
|
81
118
|
}
|
|
82
119
|
|
|
120
|
+
/** An error with the use of a reference */
|
|
83
121
|
export type ReferenceError = {
|
|
84
122
|
type: string
|
|
85
123
|
message: string
|
|
86
124
|
}
|
|
87
125
|
|
|
126
|
+
/** A union of all error types */
|
|
88
127
|
export type BlueprintError = ParserError | ValidationError | ReferenceError
|
|
89
128
|
|
|
129
|
+
/** The output of the parser */
|
|
90
130
|
export type BlueprintOutput =
|
|
91
131
|
| {
|
|
92
132
|
result: 'valid'
|
|
@@ -108,6 +148,11 @@ export type BlueprintOutput =
|
|
|
108
148
|
errors: Array<ValidationError>
|
|
109
149
|
}
|
|
110
150
|
|
|
151
|
+
/**
|
|
152
|
+
* Parses the given document and returns any error found, or the parsed and partially resolved document.
|
|
153
|
+
* @param input The document to be parsed
|
|
154
|
+
* @param options Options to control the parser
|
|
155
|
+
*/
|
|
111
156
|
export default function blueprintParserValidator(
|
|
112
157
|
input: BlueprintInput,
|
|
113
158
|
options: ParserOptions = {},
|
package/src/index.js
CHANGED
package/src/refs.js
CHANGED
|
@@ -2,13 +2,22 @@ import is from './is.js'
|
|
|
2
2
|
export default {find, resolve}
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* @
|
|
6
|
-
* @
|
|
7
|
-
* @
|
|
5
|
+
* @typedef {import('.').Blueprint} Blueprint
|
|
6
|
+
* @typedef {import('.').ParserOptions} ParserOptions
|
|
7
|
+
* @typedef {import('.').Reference} Reference
|
|
8
|
+
* @typedef {import('.').UnresolvedReference} UnresolvedReference
|
|
9
|
+
* @typedef {import('.').ReferenceError} ReferenceError
|
|
10
|
+
* @typedef {import('.').ReferenceEndpoint} ReferenceEndpoint
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @param {Blueprint} validatedBlueprint
|
|
15
|
+
* @param {ParserOptions} options
|
|
16
|
+
* @returns {Array<Reference>}
|
|
8
17
|
*/
|
|
9
18
|
function find(validatedBlueprint, options) {
|
|
10
19
|
const {debug} = options
|
|
11
|
-
/** @type {Array<
|
|
20
|
+
/** @type {Array<Reference>} */
|
|
12
21
|
const foundRefs = []
|
|
13
22
|
|
|
14
23
|
/**
|
|
@@ -45,7 +54,7 @@ function find(validatedBlueprint, options) {
|
|
|
45
54
|
}
|
|
46
55
|
}
|
|
47
56
|
|
|
48
|
-
/*
|
|
57
|
+
/* node:coverage ignore next 4 */
|
|
49
58
|
if (debug) {
|
|
50
59
|
console.log(`[Debug] Found ${foundRefs.length} references:`, foundRefs.length ? foundRefs : '')
|
|
51
60
|
}
|
|
@@ -54,21 +63,21 @@ function find(validatedBlueprint, options) {
|
|
|
54
63
|
}
|
|
55
64
|
|
|
56
65
|
/**
|
|
57
|
-
* @param {
|
|
58
|
-
* @param {Array<
|
|
59
|
-
* @param {
|
|
66
|
+
* @param {Blueprint} blueprint
|
|
67
|
+
* @param {Array<Reference>} foundRefs
|
|
68
|
+
* @param {ParserOptions} options
|
|
60
69
|
* @returns {{
|
|
61
|
-
* resolvedBlueprint:
|
|
62
|
-
* unresolvedRefs: Array<
|
|
63
|
-
* refErrors: Array<
|
|
70
|
+
* resolvedBlueprint: Blueprint
|
|
71
|
+
* unresolvedRefs: Array<UnresolvedReference> | undefined
|
|
72
|
+
* refErrors: Array<ReferenceError>
|
|
64
73
|
* }}
|
|
65
74
|
*/
|
|
66
75
|
function resolve(blueprint, foundRefs, options) {
|
|
67
|
-
const {parameters = {}} = options
|
|
76
|
+
const {parameters = {}, invalidReferenceTypes} = options
|
|
68
77
|
|
|
69
|
-
/** @type {Record<string,
|
|
78
|
+
/** @type {Record<string, Reference>} */
|
|
70
79
|
const refs = {}
|
|
71
|
-
/** @type {Array<
|
|
80
|
+
/** @type {Array<UnresolvedReference>} */
|
|
72
81
|
const unresolvedRefs = []
|
|
73
82
|
const refErrors = []
|
|
74
83
|
for (const foundRef of foundRefs) {
|
|
@@ -81,7 +90,7 @@ function resolve(blueprint, foundRefs, options) {
|
|
|
81
90
|
if (refs[ref]) {
|
|
82
91
|
if (refType === 'resources') {
|
|
83
92
|
// all resources references must be resolved during deployment
|
|
84
|
-
unresolvedRefs.push(
|
|
93
|
+
unresolvedRefs.push(unresolvedReference(foundRef))
|
|
85
94
|
} else {
|
|
86
95
|
foundRef.container[foundRef.property] = refs[ref].container[refs[ref].property]
|
|
87
96
|
}
|
|
@@ -119,9 +128,18 @@ function resolve(blueprint, foundRefs, options) {
|
|
|
119
128
|
foundRef.container[foundRef.property] = blueprint.blueprintVersion
|
|
120
129
|
} else if (refType === 'resources') {
|
|
121
130
|
// check that the resource is in the list
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
131
|
+
const found = blueprint.resources?.find((resource) => resource.name === refName)
|
|
132
|
+
if (found) {
|
|
133
|
+
// check if we allow references to the found resource's type
|
|
134
|
+
if (invalidReferenceTypes?.includes(found.type)) {
|
|
135
|
+
refErrors.push({
|
|
136
|
+
message: `Reference error '${ref}': '${refName}' type '${found.type}' cannot be referenced`,
|
|
137
|
+
type: 'invalid_reference',
|
|
138
|
+
})
|
|
139
|
+
} else {
|
|
140
|
+
// all resources references must be resolved during deployment
|
|
141
|
+
unresolvedRefs.push(unresolvedReference(foundRef))
|
|
142
|
+
}
|
|
125
143
|
} else {
|
|
126
144
|
refErrors.push({
|
|
127
145
|
message: `Reference error '${ref}': '${refName}' not found in blueprint resources`,
|
|
@@ -142,3 +160,28 @@ function resolve(blueprint, foundRefs, options) {
|
|
|
142
160
|
refErrors,
|
|
143
161
|
}
|
|
144
162
|
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Create an unresolved reference from a found reference.
|
|
166
|
+
* @param {Reference} foundRef
|
|
167
|
+
* @returns {UnresolvedReference}
|
|
168
|
+
*/
|
|
169
|
+
function unresolvedReference(foundRef) {
|
|
170
|
+
return {
|
|
171
|
+
path: foundRef.path,
|
|
172
|
+
ref: foundRef.ref,
|
|
173
|
+
source: endpoint(foundRef.ref.slice(2)),
|
|
174
|
+
target: endpoint(foundRef.path),
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Create a reference endpoint from a location string.
|
|
180
|
+
* @param {string} location
|
|
181
|
+
* @returns {ReferenceEndpoint}
|
|
182
|
+
*/
|
|
183
|
+
function endpoint(location) {
|
|
184
|
+
const [collection, name, ...path] = location.split('.')
|
|
185
|
+
|
|
186
|
+
return {collection, name: name ?? '', path: path.join('.')}
|
|
187
|
+
}
|
|
@@ -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: {
|
|
@@ -175,7 +235,53 @@ export default {
|
|
|
175
235
|
{
|
|
176
236
|
path: 'resources.another-function.config.disk',
|
|
177
237
|
ref: '$.resources.a-function.config.disk',
|
|
238
|
+
source: {collection: 'resources', name: 'a-function', path: 'config.disk'},
|
|
239
|
+
target: {collection: 'resources', name: 'another-function', path: 'config.disk'},
|
|
178
240
|
},
|
|
179
241
|
],
|
|
242
|
+
error: {
|
|
243
|
+
type: 'missing_parameter',
|
|
244
|
+
message: "Reference error '$.parameters.memory': 'memory' not found in passed parameters",
|
|
245
|
+
},
|
|
246
|
+
},
|
|
247
|
+
|
|
248
|
+
// collection-only reference has no name to resolve
|
|
249
|
+
repeatedCollectionOnlyReference: {
|
|
250
|
+
input: {
|
|
251
|
+
resources: [
|
|
252
|
+
{
|
|
253
|
+
name: 'a-function',
|
|
254
|
+
type: 'cloud-function',
|
|
255
|
+
config: {
|
|
256
|
+
first: '$.resources',
|
|
257
|
+
second: '$.resources',
|
|
258
|
+
},
|
|
259
|
+
},
|
|
260
|
+
],
|
|
261
|
+
},
|
|
262
|
+
expected: {
|
|
263
|
+
resources: [
|
|
264
|
+
{
|
|
265
|
+
name: 'a-function',
|
|
266
|
+
type: 'cloud-function',
|
|
267
|
+
config: {
|
|
268
|
+
first: '$.resources',
|
|
269
|
+
second: '$.resources',
|
|
270
|
+
},
|
|
271
|
+
},
|
|
272
|
+
],
|
|
273
|
+
},
|
|
274
|
+
unresolved: [
|
|
275
|
+
{
|
|
276
|
+
path: 'resources.a-function.config.second',
|
|
277
|
+
ref: '$.resources',
|
|
278
|
+
source: {collection: 'resources', name: '', path: ''},
|
|
279
|
+
target: {collection: 'resources', name: 'a-function', path: 'config.second'},
|
|
280
|
+
},
|
|
281
|
+
],
|
|
282
|
+
error: {
|
|
283
|
+
type: 'missing_resource',
|
|
284
|
+
message: "Reference error '$.resources': 'undefined' not found in blueprint resources",
|
|
285
|
+
},
|
|
180
286
|
},
|
|
181
287
|
}
|
package/test/mocks/references.js
CHANGED
|
@@ -141,6 +141,8 @@ export default {
|
|
|
141
141
|
{
|
|
142
142
|
path: 'resources.a-function.config.memory',
|
|
143
143
|
ref: '$.resources.another-function.config.env.memory',
|
|
144
|
+
source: {collection: 'resources', name: 'another-function', path: 'config.env.memory'},
|
|
145
|
+
target: {collection: 'resources', name: 'a-function', path: 'config.memory'},
|
|
144
146
|
},
|
|
145
147
|
],
|
|
146
148
|
},
|
|
@@ -178,6 +180,8 @@ export default {
|
|
|
178
180
|
{
|
|
179
181
|
path: 'resources.a-function.projects[1]',
|
|
180
182
|
ref: '$.resources.a-project.name',
|
|
183
|
+
source: {collection: 'resources', name: 'a-project', path: 'name'},
|
|
184
|
+
target: {collection: 'resources', name: 'a-function', path: 'projects[1]'},
|
|
181
185
|
},
|
|
182
186
|
],
|
|
183
187
|
},
|
|
@@ -228,6 +232,12 @@ export default {
|
|
|
228
232
|
{
|
|
229
233
|
path: 'resources.a-function.config.settings[0].projects[2]',
|
|
230
234
|
ref: '$.resources.a-project.name',
|
|
235
|
+
source: {collection: 'resources', name: 'a-project', path: 'name'},
|
|
236
|
+
target: {
|
|
237
|
+
collection: 'resources',
|
|
238
|
+
name: 'a-function',
|
|
239
|
+
path: 'config.settings[0].projects[2]',
|
|
240
|
+
},
|
|
231
241
|
},
|
|
232
242
|
],
|
|
233
243
|
},
|
|
@@ -358,14 +368,20 @@ export default {
|
|
|
358
368
|
{
|
|
359
369
|
path: 'resources.another-function.config.memory',
|
|
360
370
|
ref: '$.resources.a-function.config.memory',
|
|
371
|
+
source: {collection: 'resources', name: 'a-function', path: 'config.memory'},
|
|
372
|
+
target: {collection: 'resources', name: 'another-function', path: 'config.memory'},
|
|
361
373
|
},
|
|
362
374
|
{
|
|
363
375
|
path: 'outputs.configured-memory-1.value',
|
|
364
376
|
ref: '$.resources.another-function.config.memory',
|
|
377
|
+
source: {collection: 'resources', name: 'another-function', path: 'config.memory'},
|
|
378
|
+
target: {collection: 'outputs', name: 'configured-memory-1', path: 'value'},
|
|
365
379
|
},
|
|
366
380
|
{
|
|
367
381
|
path: 'outputs.configured-memory-2.value',
|
|
368
382
|
ref: '$.resources.another-function.config.memory',
|
|
383
|
+
source: {collection: 'resources', name: 'another-function', path: 'config.memory'},
|
|
384
|
+
target: {collection: 'outputs', name: 'configured-memory-2', path: 'value'},
|
|
369
385
|
},
|
|
370
386
|
],
|
|
371
387
|
},
|
|
@@ -403,6 +419,8 @@ export default {
|
|
|
403
419
|
{
|
|
404
420
|
path: 'resources.a-function.projects[2]',
|
|
405
421
|
ref: '$.resources.a-project.name',
|
|
422
|
+
source: {collection: 'resources', name: 'a-project', path: 'name'},
|
|
423
|
+
target: {collection: 'resources', name: 'a-function', path: 'projects[2]'},
|
|
406
424
|
},
|
|
407
425
|
],
|
|
408
426
|
},
|
|
@@ -452,6 +470,51 @@ export default {
|
|
|
452
470
|
{
|
|
453
471
|
path: 'resources.a-function.config.settings[0].projects[0]',
|
|
454
472
|
ref: '$.resources.a-project.name',
|
|
473
|
+
source: {collection: 'resources', name: 'a-project', path: 'name'},
|
|
474
|
+
target: {
|
|
475
|
+
collection: 'resources',
|
|
476
|
+
name: 'a-function',
|
|
477
|
+
path: 'config.settings[0].projects[0]',
|
|
478
|
+
},
|
|
479
|
+
},
|
|
480
|
+
],
|
|
481
|
+
},
|
|
482
|
+
|
|
483
|
+
unresolvedRootReference: {
|
|
484
|
+
input: {
|
|
485
|
+
resources: [
|
|
486
|
+
{
|
|
487
|
+
name: 'a-project',
|
|
488
|
+
type: 'cloud-project',
|
|
489
|
+
},
|
|
490
|
+
{
|
|
491
|
+
name: 'a-function',
|
|
492
|
+
type: 'cloud-function',
|
|
493
|
+
project: '$.resources.a-project',
|
|
494
|
+
},
|
|
495
|
+
],
|
|
496
|
+
},
|
|
497
|
+
|
|
498
|
+
expected: {
|
|
499
|
+
resources: [
|
|
500
|
+
{
|
|
501
|
+
name: 'a-project',
|
|
502
|
+
type: 'cloud-project',
|
|
503
|
+
},
|
|
504
|
+
{
|
|
505
|
+
name: 'a-function',
|
|
506
|
+
type: 'cloud-function',
|
|
507
|
+
project: '$.resources.a-project',
|
|
508
|
+
},
|
|
509
|
+
],
|
|
510
|
+
},
|
|
511
|
+
|
|
512
|
+
unresolved: [
|
|
513
|
+
{
|
|
514
|
+
path: 'resources.a-function.project',
|
|
515
|
+
ref: '$.resources.a-project',
|
|
516
|
+
source: {collection: 'resources', name: 'a-project', path: ''},
|
|
517
|
+
target: {collection: 'resources', name: 'a-function', path: 'project'},
|
|
455
518
|
},
|
|
456
519
|
],
|
|
457
520
|
},
|
package/test/unit/index.test.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
+
import {test} from 'node:test'
|
|
1
2
|
import schemas from '@sanity/blueprints-jsonschemas'
|
|
2
3
|
import Ajv from 'ajv/dist/2020.js'
|
|
3
|
-
import tap from 'tap'
|
|
4
4
|
import blueprintParserValidator from '../../src/index.js'
|
|
5
5
|
import mocks from '../mocks/all.js'
|
|
6
6
|
|
|
7
7
|
const ajv = new Ajv()
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
test('Basic input', async (t) => {
|
|
10
10
|
t.plan(8)
|
|
11
11
|
const input = {blueprintVersion: '2024-10-01'}
|
|
12
12
|
const inputJSON = JSON.stringify(input)
|
|
@@ -15,39 +15,39 @@ tap.test('Basic input', async (t) => {
|
|
|
15
15
|
let output
|
|
16
16
|
|
|
17
17
|
output = blueprintParserValidator(input)
|
|
18
|
-
t.
|
|
18
|
+
t.assert.deepStrictEqual(
|
|
19
19
|
output.result === 'valid' && output.blueprint,
|
|
20
20
|
input,
|
|
21
21
|
'Parser / validator accepted raw JS input',
|
|
22
22
|
)
|
|
23
|
-
t.
|
|
23
|
+
t.assert.ok(!('errors' in output), 'Parser / validator found no issues')
|
|
24
24
|
|
|
25
25
|
output = blueprintParserValidator(inputJSON)
|
|
26
|
-
t.
|
|
26
|
+
t.assert.deepStrictEqual(
|
|
27
27
|
output.result === 'valid' && output.blueprint,
|
|
28
28
|
input,
|
|
29
29
|
'Parser / validator accepted JSON input',
|
|
30
30
|
)
|
|
31
|
-
t.
|
|
31
|
+
t.assert.ok(!('errors' in output), 'Parser / validator found no issues')
|
|
32
32
|
|
|
33
33
|
output = blueprintParserValidator(Buffer.from(inputJSON))
|
|
34
|
-
t.
|
|
34
|
+
t.assert.deepStrictEqual(
|
|
35
35
|
output.result === 'valid' && output.blueprint,
|
|
36
36
|
input,
|
|
37
37
|
'Parser / validator accepted JSON buffer',
|
|
38
38
|
)
|
|
39
|
-
t.
|
|
39
|
+
t.assert.ok(!('errors' in output), 'Parser / validator found no issues')
|
|
40
40
|
|
|
41
41
|
output = blueprintParserValidator(input, {parameters: {ok: 'hello'}})
|
|
42
|
-
t.
|
|
42
|
+
t.assert.deepStrictEqual(
|
|
43
43
|
output.result === 'valid' && output.blueprint,
|
|
44
44
|
input,
|
|
45
45
|
'Parser / validator accepted raw JS input and scalar params',
|
|
46
46
|
)
|
|
47
|
-
t.
|
|
47
|
+
t.assert.ok(!('errors' in output), 'Parser / validator found no issues')
|
|
48
48
|
})
|
|
49
49
|
|
|
50
|
-
|
|
50
|
+
test('Basic input: errors', async (t) => {
|
|
51
51
|
t.plan(10)
|
|
52
52
|
const input = {blueprintVersion: 'nah'}
|
|
53
53
|
const invalidInput = '{'
|
|
@@ -57,24 +57,27 @@ tap.test('Basic input: errors', async (t) => {
|
|
|
57
57
|
let result
|
|
58
58
|
|
|
59
59
|
result = blueprintParserValidator(invalidInput)
|
|
60
|
-
t.ok(result.result === 'parse_errors')
|
|
61
|
-
t.
|
|
60
|
+
t.assert.ok(result.result === 'parse_errors')
|
|
61
|
+
t.assert.strictEqual(
|
|
62
62
|
result.result === 'parse_errors' && result.errors.length,
|
|
63
63
|
1,
|
|
64
64
|
'Parser / validator returned error on invalid JSON',
|
|
65
65
|
)
|
|
66
66
|
|
|
67
67
|
result = blueprintParserValidator(undefined)
|
|
68
|
-
t.ok(result.result === 'parse_errors')
|
|
69
|
-
t.
|
|
68
|
+
t.assert.ok(result.result === 'parse_errors')
|
|
69
|
+
t.assert.strictEqual(
|
|
70
70
|
result.result === 'parse_errors' && result.errors.length,
|
|
71
71
|
1,
|
|
72
72
|
'Parser / validator returned error on invalid input (undefined)',
|
|
73
73
|
)
|
|
74
74
|
|
|
75
75
|
result = blueprintParserValidator(input)
|
|
76
|
-
t.ok(
|
|
77
|
-
|
|
76
|
+
t.assert.ok(
|
|
77
|
+
result.result === 'validation_errors',
|
|
78
|
+
'Parser / validator accepted invalid raw JS input',
|
|
79
|
+
)
|
|
80
|
+
t.assert.strictEqual(
|
|
78
81
|
result.result === 'validation_errors' && result.errors.length,
|
|
79
82
|
1,
|
|
80
83
|
'Parser / validator returned a basic format error',
|
|
@@ -82,8 +85,8 @@ tap.test('Basic input: errors', async (t) => {
|
|
|
82
85
|
|
|
83
86
|
// @ts-expect-error Passing invalid input to the parameters attribute
|
|
84
87
|
result = blueprintParserValidator(validInput, {parameters: 'hi'})
|
|
85
|
-
t.ok(result.result === 'validation_errors', 'Parser / validator accepted result input')
|
|
86
|
-
t.
|
|
88
|
+
t.assert.ok(result.result === 'validation_errors', 'Parser / validator accepted result input')
|
|
89
|
+
t.assert.strictEqual(
|
|
87
90
|
result.result === 'validation_errors' && result.errors.length,
|
|
88
91
|
1,
|
|
89
92
|
'Parser / validator returned a basic format error',
|
|
@@ -91,18 +94,18 @@ tap.test('Basic input: errors', async (t) => {
|
|
|
91
94
|
|
|
92
95
|
// @ts-expect-error Passing invalid input to the parameters attribute
|
|
93
96
|
result = blueprintParserValidator(validInput, {parameters: {ok: {}}})
|
|
94
|
-
t.ok(result.result === 'validation_errors', 'Parser / validator accepted result input')
|
|
95
|
-
t.
|
|
97
|
+
t.assert.ok(result.result === 'validation_errors', 'Parser / validator accepted result input')
|
|
98
|
+
t.assert.strictEqual(
|
|
96
99
|
result.result === 'validation_errors' && result.errors.length,
|
|
97
100
|
1,
|
|
98
101
|
'Parser / validator returned a basic format error',
|
|
99
102
|
)
|
|
100
103
|
})
|
|
101
104
|
|
|
102
|
-
|
|
105
|
+
test('Reference resolution', async (t) => {
|
|
103
106
|
const references = Object.entries(mocks.references)
|
|
104
107
|
const referenceErrors = Object.entries(mocks.referenceErrors)
|
|
105
|
-
const tests = references.length * 4 + referenceErrors.length *
|
|
108
|
+
const tests = references.length * 4 + referenceErrors.length * 5
|
|
106
109
|
t.plan(tests)
|
|
107
110
|
|
|
108
111
|
// Basic reference resolution
|
|
@@ -111,34 +114,51 @@ tap.test('Reference resolution', async (t) => {
|
|
|
111
114
|
debug: true,
|
|
112
115
|
parameters,
|
|
113
116
|
})
|
|
114
|
-
t.
|
|
117
|
+
t.assert.strictEqual(output.result, 'valid', `${name}: returned expected result`)
|
|
115
118
|
if (output.result === 'valid') {
|
|
116
119
|
const {blueprint, unresolvedRefs} = output
|
|
117
|
-
t.
|
|
118
|
-
t.
|
|
119
|
-
|
|
120
|
+
t.assert.deepStrictEqual(blueprint, expected, `${name}: returned expected blueprint`)
|
|
121
|
+
t.assert.deepStrictEqual(
|
|
122
|
+
unresolvedRefs,
|
|
123
|
+
unresolved,
|
|
124
|
+
`${name}: returned expected unresolved references`,
|
|
125
|
+
)
|
|
126
|
+
t.assert.ok(!('errors' in output), `${name}: found no issues`)
|
|
120
127
|
} else {
|
|
121
|
-
t.
|
|
128
|
+
t.assert.ok(!output.errors, `${name}: ${JSON.stringify(output.errors)}`)
|
|
122
129
|
}
|
|
123
130
|
}
|
|
124
131
|
|
|
125
132
|
// Basic reference errors
|
|
126
|
-
for (const [name, {input, expected, unresolved}] of referenceErrors) {
|
|
133
|
+
for (const [name, {options, input, expected, unresolved, error}] of referenceErrors) {
|
|
127
134
|
const output = blueprintParserValidator(input, {
|
|
128
135
|
debug: true,
|
|
129
136
|
parameters: {},
|
|
137
|
+
...options,
|
|
130
138
|
})
|
|
131
|
-
t.ok(output.result, 'result is true despite reference errors')
|
|
139
|
+
t.assert.ok(output.result, 'result is true despite reference errors')
|
|
132
140
|
if (output.result === 'reference_errors') {
|
|
133
141
|
const {blueprint, unresolvedRefs, errors} = output
|
|
134
|
-
t.
|
|
135
|
-
t.
|
|
136
|
-
|
|
142
|
+
t.assert.deepStrictEqual(blueprint, expected, `${name}: returned expected blueprint`)
|
|
143
|
+
t.assert.deepStrictEqual(
|
|
144
|
+
unresolvedRefs,
|
|
145
|
+
unresolved,
|
|
146
|
+
`${name}: returned expected unresolved references`,
|
|
147
|
+
)
|
|
148
|
+
t.assert.ok(errors && errors.length > 0, `${name}: parser / validator found an error`)
|
|
149
|
+
if (error && errors && errors.length > 0) {
|
|
150
|
+
t.assert.ok(
|
|
151
|
+
errors.some((err) => err.type === error.type && err.message === error.message),
|
|
152
|
+
JSON.stringify(errors),
|
|
153
|
+
)
|
|
154
|
+
} else {
|
|
155
|
+
t.assert.ok(true) // makes planning easier
|
|
156
|
+
}
|
|
137
157
|
}
|
|
138
158
|
}
|
|
139
159
|
})
|
|
140
160
|
|
|
141
|
-
|
|
161
|
+
test('2024-10-01', async (t) => {
|
|
142
162
|
const jsonSchema20241001 = schemas['2024-10-01'].sanity.blueprint
|
|
143
163
|
|
|
144
164
|
const basic = Object.entries(mocks.basic)
|
|
@@ -152,25 +172,28 @@ tap.test('2024-10-01', async (t) => {
|
|
|
152
172
|
// Check the basic structure of a document
|
|
153
173
|
for (const [name, input] of basic) {
|
|
154
174
|
const output = blueprintParserValidator(input)
|
|
155
|
-
t.
|
|
156
|
-
t.
|
|
175
|
+
t.assert.strictEqual(output.result, 'valid', `${name}: expected valid result`)
|
|
176
|
+
t.assert.ok(!('errors' in output), `${name}: parser / validator found no issues`)
|
|
157
177
|
if (output.result !== 'valid') console.log(`${name} errors:`, output.errors)
|
|
158
178
|
|
|
159
179
|
const result = ajv.validate(jsonSchema20241001, input)
|
|
160
|
-
t.ok(result, `${name}: JSON Schema validator found no issues`)
|
|
161
|
-
t.
|
|
180
|
+
t.assert.ok(result, `${name}: JSON Schema validator found no issues`)
|
|
181
|
+
t.assert.ok(!ajv.errors, `${name}: JSON Schema validator found no issues`)
|
|
162
182
|
if (ajv.errors?.length) console.log(`${name} errors (AJV):`, ajv.errors)
|
|
163
183
|
}
|
|
164
184
|
|
|
165
185
|
// Basic validation errors
|
|
166
186
|
for (const [name, input] of basicErrors) {
|
|
167
187
|
const output = blueprintParserValidator(input)
|
|
168
|
-
t.ok(
|
|
188
|
+
t.assert.ok(
|
|
189
|
+
output.result !== 'valid' && output.errors,
|
|
190
|
+
`${name}: parser / validator found an error`,
|
|
191
|
+
)
|
|
169
192
|
|
|
170
193
|
if (!input.metadata?.ignore) {
|
|
171
194
|
const result = ajv.validate(jsonSchema20241001, input)
|
|
172
|
-
t.
|
|
173
|
-
t.ok(ajv.errors, `${name}: JSON Schema validator found an error`)
|
|
195
|
+
t.assert.ok(!result, `${name}: JSON Schema validator found an error`)
|
|
196
|
+
t.assert.ok(ajv.errors, `${name}: JSON Schema validator found an error`)
|
|
174
197
|
}
|
|
175
198
|
}
|
|
176
199
|
})
|