@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,54 @@
1
+ name: CI
2
+
3
+ on:
4
+ merge_group:
5
+ pull_request:
6
+ push:
7
+ branches: [main]
8
+
9
+ concurrency:
10
+ group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
11
+ cancel-in-progress: true
12
+
13
+ permissions:
14
+ contents: read # for checkout
15
+
16
+ jobs:
17
+ lint:
18
+ name: Lint
19
+ runs-on: ubuntu-latest
20
+ steps:
21
+ - uses: actions/checkout@v4
22
+ - uses: actions/setup-node@v4
23
+ with:
24
+ cache: npm
25
+ node-version: lts/*
26
+ - run: npm ci
27
+ - run: npm run lint
28
+
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
+ name: Test
36
+ runs-on: ${{ matrix.os }}
37
+ strategy:
38
+ fail-fast: false
39
+ matrix:
40
+ # Run the testing suite on each major OS with the latest LTS release of Node.js
41
+ os: [macos-latest, ubuntu-latest, windows-latest]
42
+ node-version: ["lts/*"]
43
+ steps:
44
+ - uses: actions/checkout@v4
45
+ - uses: actions/setup-node@v4
46
+ with:
47
+ cache: npm
48
+ node-version: ${{ matrix.node-version }}
49
+ - run: npm ci
50
+ - run: npm test
51
+ - uses: vebr/jest-lcov-reporter@v0.2.1
52
+ with:
53
+ github-token: ${{ secrets.GITHUB_TOKEN }}
54
+ lcov-file: .tap/report/lcov.info
@@ -0,0 +1,56 @@
1
+ ---
2
+ name: Release Please
3
+
4
+ on:
5
+ push:
6
+ branches:
7
+ - main
8
+ workflow_dispatch:
9
+ inputs:
10
+ publish:
11
+ description: Publish to NPM
12
+ required: true
13
+ default: false
14
+ type: boolean
15
+
16
+ permissions:
17
+ contents: read
18
+
19
+ jobs:
20
+ release-please:
21
+ permissions:
22
+ id-token: write # to enable use of OIDC for npm provenance
23
+ # permissions for pushing commits and opening PRs are handled by the `generate-token` step
24
+ runs-on: ubuntu-latest
25
+ steps:
26
+ - uses: actions/create-github-app-token@v2
27
+ id: app-token
28
+ with:
29
+ app-id: ${{ secrets.ECOSPARK_APP_ID }}
30
+ private-key: ${{ secrets.ECOSPARK_APP_PRIVATE_KEY }}
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@v4
33
+ id: release
34
+ with:
35
+ release-type: node
36
+ token: ${{ steps.app-token.outputs.token }}
37
+
38
+ # Publish to NPM on new releases
39
+ - uses: actions/checkout@v4
40
+ if: ${{ steps.release.outputs.release_created || github.event.inputs.publish == 'true' }}
41
+ - uses: actions/setup-node@v4
42
+ if: ${{ steps.release.outputs.release_created || github.event.inputs.publish == 'true' }}
43
+ with:
44
+ cache: npm
45
+ node-version: lts/*
46
+ registry-url: "https://registry.npmjs.org"
47
+ - run: npm ci --ignore-scripts
48
+ if: ${{ steps.release.outputs.release_created || github.event.inputs.publish == 'true' }}
49
+ #- run: npm audit signatures
50
+ # if: ${{ steps.release.outputs.release_created || github.event.inputs.publish == 'true' }}
51
+ # Release Please has already incremented versions and published tags, so we just
52
+ # need to publish the new version to npm here
53
+ - run: npm publish --provenance
54
+ if: ${{ steps.release.outputs.release_created || github.event.inputs.publish == 'true' }}
55
+ env:
56
+ NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}
package/CHANGELOG.md ADDED
@@ -0,0 +1,8 @@
1
+ # Changelog
2
+
3
+ ## [0.1.0](https://github.com/sanity-io/blueprints-parser/compare/v0.0.1...v0.1.0) (2025-08-13)
4
+
5
+
6
+ ### Features
7
+
8
+ * initial release ([#3](https://github.com/sanity-io/blueprints-parser/issues/3)) ([1c4fdf4](https://github.com/sanity-io/blueprints-parser/commit/1c4fdf49e61cbbd0c6f0f661cbac09a4a3a1c2ce))
package/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # @sanity/blueprints-parser
2
+
3
+ Parser and validator for Sanity Blueprints.
4
+
5
+ ## Usage
6
+
7
+ ```ts
8
+ import blueprintParserValidator from '@sanity/blueprints-parser'
9
+
10
+ const {blueprint, errors} = blueprintParserValidator(`{
11
+ "resources": [
12
+ {
13
+ "name": "a-function",
14
+ "type": "cloud-function",
15
+ "project": "$.params.project"
16
+ }
17
+ ]
18
+ }`, {
19
+ parameters: {
20
+ project: "a-project"
21
+ }
22
+ })
23
+ if (errors?.length) {
24
+ for (const err of errors) {
25
+ console.error(`${err.type}: ${err.message}`)
26
+ }
27
+ }
28
+
29
+ console.log(blueprint.resources[0].name) // a-function
30
+ console.log(blueprint.resources[0].type) // cloud-function
31
+ console.log(blueprint.resources[0].project) // a-project
32
+ ```
package/biome.jsonc ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "$schema": "https://biomejs.dev/schemas/2.1.4/schema.json",
3
+ "vcs": {
4
+ "enabled": false,
5
+ "clientKind": "git",
6
+ "useIgnoreFile": false
7
+ },
8
+ "files": {
9
+ "ignoreUnknown": false,
10
+ "includes": ["src/**/*", "test/**/*"]
11
+ },
12
+ "formatter": {
13
+ "enabled": true,
14
+ "indentStyle": "space",
15
+ "lineWidth": 100
16
+ },
17
+ "assist": {
18
+ "actions": {
19
+ "source": {
20
+ "organizeImports": "on"
21
+ }
22
+ }
23
+ },
24
+ "linter": {
25
+ "enabled": true,
26
+ "rules": {
27
+ "recommended": true,
28
+ "complexity": {
29
+ "noForEach": "off"
30
+ }
31
+ }
32
+ },
33
+ "javascript": {
34
+ "formatter": {
35
+ "quoteStyle": "single",
36
+ "semicolons": "asNeeded",
37
+ "bracketSpacing": false
38
+ }
39
+ }
40
+ }
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@sanity/blueprints-parser",
3
+ "version": "0.1.0",
4
+ "description": "A parser and validator for Sanity Blueprint documents.",
5
+ "keywords": [
6
+ "sanity",
7
+ "blueprints"
8
+ ],
9
+ "homepage": "https://github.com/sanity-io/blueprints-parser#readme",
10
+ "bugs": {
11
+ "url": "https://github.com/sanity-io/blueprints-parser/issues"
12
+ },
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+ssh://git@github.com/sanity-io/blueprints-parser.git"
16
+ },
17
+ "type": "module",
18
+ "main": "src/index.js",
19
+ "scripts": {
20
+ "test": "npx tap run --include=test/unit/**/* --passes --coverage-report=text --coverage-report=lcov --no-browser",
21
+ "lint": "biome ci",
22
+ "lint:write": "biome check --write"
23
+ },
24
+ "license": "MIT",
25
+ "engines": {
26
+ "node": ">=22"
27
+ },
28
+ "author": "Sanity.io <hello@sanity.io>",
29
+ "devDependencies": {
30
+ "@biomejs/biome": "^2.1.4",
31
+ "@sanity/blueprints-jsonschemas": "^0.1.0",
32
+ "ajv": "^8.17.1",
33
+ "tap": "^21.1.0"
34
+ },
35
+ "publishConfig": {
36
+ "access": "public"
37
+ }
38
+ }
@@ -0,0 +1,16 @@
1
+ const firstVersion = '2024-10-01'
2
+ const formatProperties = [
3
+ '$schema',
4
+ 'blueprintVersion',
5
+ 'resources',
6
+ 'parameters',
7
+ 'values',
8
+ 'outputs',
9
+ 'metadata',
10
+ ]
11
+ const parameterTypes = ['arg', 'env-var', 'stdin', 'config']
12
+ const versionFormat = /^\d{4}-\d{2}-\d{2}$/ // YYYY-MM-DD
13
+ const nameFormat = /^[a-zA-Z][a-zA-Z0-9-_]*(?<![-_])$/
14
+ const typeFormat = /^[a-zA-Z]([a-zA-Z0-9-_]+\.?)*(?<![-_.])$/
15
+
16
+ export {firstVersion, formatProperties, parameterTypes, versionFormat, nameFormat, typeFormat}
package/src/index.js ADDED
@@ -0,0 +1,91 @@
1
+ import {firstVersion} from './constants.js'
2
+ import is from './is.js'
3
+ import references from './refs.js'
4
+ import validate from './validate.js'
5
+
6
+ export default function blueprintParserValidator(input, options = {}) {
7
+ try {
8
+ const {rawBlueprint, parseErrors} = parse(input)
9
+ if (parseErrors?.length) {
10
+ return {
11
+ blueprint: rawBlueprint || input,
12
+ errors: parseErrors,
13
+ }
14
+ }
15
+
16
+ const version = rawBlueprint.blueprintVersion || firstVersion
17
+
18
+ // Aggregate basic structural, spec violation, or input errors
19
+ const initialErrors = []
20
+ .concat(
21
+ validate.version(version),
22
+ validate.resources(rawBlueprint.resources),
23
+ validate.values(rawBlueprint.values),
24
+ validate.parameters(rawBlueprint.parameters),
25
+ validate.outputs(rawBlueprint.outputs),
26
+ validate.metadata(rawBlueprint.metadata),
27
+ validate.else(rawBlueprint),
28
+ validate.passedParameters(options),
29
+ )
30
+ .filter(Boolean)
31
+
32
+ if (initialErrors.length) {
33
+ return {
34
+ blueprint: rawBlueprint,
35
+ errors: initialErrors,
36
+ }
37
+ }
38
+
39
+ const foundRefs = references.find(rawBlueprint, options)
40
+ if (!foundRefs.length) {
41
+ return {
42
+ blueprint: rawBlueprint,
43
+ }
44
+ }
45
+
46
+ const {resolvedBlueprint, unresolvedRefs, refErrors} = references.resolve(
47
+ rawBlueprint,
48
+ foundRefs,
49
+ options,
50
+ )
51
+
52
+ const output = {blueprint: resolvedBlueprint}
53
+ if (unresolvedRefs?.length) output.unresolvedRefs = unresolvedRefs
54
+ if (refErrors.length) output.errors = refErrors
55
+
56
+ return output
57
+
58
+ /* c8 ignore next 4 */
59
+ } catch (error) {
60
+ console.log('Unknown Blueprint error', error)
61
+ throw error
62
+ }
63
+ }
64
+
65
+ function parse(input) {
66
+ if (is.string(input) || input instanceof Buffer) {
67
+ try {
68
+ return {rawBlueprint: JSON.parse(input)}
69
+ } catch (error) {
70
+ return {
71
+ parseErrors: [
72
+ {
73
+ message: 'Invalid Blueprint JSON',
74
+ type: 'json_validation_error',
75
+ error,
76
+ },
77
+ ],
78
+ }
79
+ }
80
+ } else if (is.object(input)) {
81
+ return {rawBlueprint: structuredClone(input)}
82
+ }
83
+ return {
84
+ parseErrors: [
85
+ {
86
+ message: 'Invalid input',
87
+ type: 'invalid_input',
88
+ },
89
+ ],
90
+ }
91
+ }
package/src/is.js ADDED
@@ -0,0 +1,20 @@
1
+ // Types
2
+ const array = (item) => Array.isArray(item)
3
+ // const bool = (item) => typeof item === 'boolean'
4
+ const defined = (item) => typeof item !== 'undefined'
5
+ // const nullish = (item) => typeof item === 'undefined' || item === null
6
+ const number = (item) => Number.isInteger(item) || (typeof item === 'number' && !Number.isNaN(item))
7
+ const object = (item) => item && typeof item === 'object' && !Array.isArray(item)
8
+ const ref = (item) => string(item) && item.startsWith('$.')
9
+ const scalar = (item) => string(item) || number(item)
10
+ const string = (item) => typeof item === 'string'
11
+
12
+ export default {
13
+ array,
14
+ defined,
15
+ number,
16
+ object,
17
+ ref,
18
+ scalar,
19
+ string,
20
+ }
package/src/refs.js ADDED
@@ -0,0 +1,103 @@
1
+ import is from './is.js'
2
+ export default {find, resolve}
3
+
4
+ function find(rawBlueprint, options) {
5
+ const {debug} = options
6
+ const foundRefs = []
7
+
8
+ function walk(item, path) {
9
+ if (is.object(item)) {
10
+ for (const [name, value] of Object.entries(item)) {
11
+ const cur = `${path}.${name}`
12
+ if (is.ref(value)) foundRefs.push({path: cur, property: name, ref: value, item})
13
+ else if (is.object(value) || is.array(value)) walk(value, cur)
14
+ }
15
+ }
16
+ if (is.array(item)) {
17
+ item.forEach((value, index) => {
18
+ const cur = `${path}[${index}]`
19
+ if (is.ref(value)) foundRefs.push({path: cur, property: value, ref: value, item, index})
20
+ else if (is.object(value)) walk(value, cur)
21
+ })
22
+ }
23
+ }
24
+
25
+ // Top-level Blueprint properties that may contain a reference
26
+ const properties = ['resources', 'parameters', 'outputs']
27
+ for (const property of properties) {
28
+ // Run over the list
29
+ if (rawBlueprint[property]?.length) {
30
+ // Inspect each property, or (recursively) walk if it's an object
31
+ for (const item of rawBlueprint[property]) {
32
+ const top = `${property}.${item.name}`
33
+ walk(item, top)
34
+ }
35
+ }
36
+ }
37
+
38
+ /* c8 ignore next 4 */
39
+ if (debug) {
40
+ console.log(`[Debug] Found ${foundRefs.length} references:`, foundRefs.length ? foundRefs : '')
41
+ }
42
+
43
+ return foundRefs
44
+ }
45
+
46
+ function resolve(rawBlueprint, foundRefs, options) {
47
+ const {parameters = {}} = options
48
+ const refs = {}
49
+ const unresolvedRefs = []
50
+ const refErrors = []
51
+ for (const foundRef of foundRefs) {
52
+ const {property, ref, item} = foundRef
53
+ const refPath = ref.replace(/^\$\./, '')
54
+ // Early return from an already found reference
55
+ if (refs[refPath]) {
56
+ item[property] = refs[refPath]
57
+ continue
58
+ }
59
+
60
+ // Parameters are special, (try to) find them in options.parameters passed by the caller
61
+ const parts = refPath.split('.')
62
+ const param = parts[1]
63
+ if (['parameters', 'params'].includes(parts[0])) {
64
+ const found = parameters[param]
65
+ if (is.scalar(found)) {
66
+ refs[refPath] = found
67
+ if (is.object(item)) item[property] = refs[refPath]
68
+ if (is.array(item)) {
69
+ const index = item.findIndex((i) => i === property)
70
+ item[index] = refs[refPath]
71
+ }
72
+ } else {
73
+ refErrors.push({
74
+ message: `Reference error '${ref}': '${param}' not found in passed parameters`,
75
+ type: 'missing_parameter',
76
+ })
77
+ }
78
+ continue
79
+ }
80
+
81
+ // Will prob need to refactor the following to get a bit more readable, and introduce more subtle behavior
82
+ const found = parts.reduce((obj, i) => {
83
+ if (obj?.[i]) return obj[i]
84
+ if (is.array(obj)) return obj.find(({name}) => name === i)
85
+ }, rawBlueprint)
86
+ if (is.scalar(found)) {
87
+ refs[refPath] = found
88
+ if (is.object(item)) item[property] = refs[refPath]
89
+ if (is.array(item)) {
90
+ const index = item.findIndex((i) => i === property)
91
+ item[index] = refs[refPath]
92
+ }
93
+ } else {
94
+ unresolvedRefs.push(foundRef)
95
+ }
96
+ }
97
+
98
+ return {
99
+ resolvedBlueprint: rawBlueprint,
100
+ unresolvedRefs: unresolvedRefs.length ? unresolvedRefs : undefined,
101
+ refErrors,
102
+ }
103
+ }