@sanity/blueprints-parser 0.1.0 → 0.2.1
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/.github/workflows/ci.yaml +2 -1
- package/CHANGELOG.md +14 -0
- package/jsconfig.json +7 -0
- package/package.json +7 -4
- package/src/constants.js +1 -1
- package/src/index.d.ts +108 -0
- package/src/index.js +48 -29
- package/src/is.js +1 -1
- package/src/refs.js +26 -6
- package/src/validate.js +334 -246
- package/test/integration/imports.test.js +8 -0
- package/test/integration/package.json +11 -0
- package/test/mocks/basic-errors.js +3 -0
- package/test/unit/index.test.js +93 -50
|
@@ -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: [
|
|
42
|
+
node-version: [20, 22, 24]
|
|
43
43
|
steps:
|
|
44
44
|
- uses: actions/checkout@v4
|
|
45
45
|
- uses: actions/setup-node@v4
|
|
@@ -47,6 +47,7 @@ jobs:
|
|
|
47
47
|
cache: npm
|
|
48
48
|
node-version: ${{ matrix.node-version }}
|
|
49
49
|
- run: npm ci
|
|
50
|
+
- run: cd test/integration; npm install
|
|
50
51
|
- run: npm test
|
|
51
52
|
- uses: vebr/jest-lcov-reporter@v0.2.1
|
|
52
53
|
with:
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.2.1](https://github.com/sanity-io/blueprints-parser/compare/v0.2.0...v0.2.1) (2025-09-04)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Bug Fixes
|
|
7
|
+
|
|
8
|
+
* Support node 20.19 ([#7](https://github.com/sanity-io/blueprints-parser/issues/7)) ([d85c24e](https://github.com/sanity-io/blueprints-parser/commit/d85c24e172e5e898cc6689f003c11efb0f2afde0))
|
|
9
|
+
|
|
10
|
+
## [0.2.0](https://github.com/sanity-io/blueprints-parser/compare/v0.1.0...v0.2.0) (2025-08-29)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### Features
|
|
14
|
+
|
|
15
|
+
* Type definitions ([#5](https://github.com/sanity-io/blueprints-parser/issues/5)) ([690711a](https://github.com/sanity-io/blueprints-parser/commit/690711a860d3304c56569d277aa278945b5cde66))
|
|
16
|
+
|
|
3
17
|
## [0.1.0](https://github.com/sanity-io/blueprints-parser/compare/v0.0.1...v0.1.0) (2025-08-13)
|
|
4
18
|
|
|
5
19
|
|
package/jsconfig.json
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sanity/blueprints-parser",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "A parser and validator for Sanity Blueprint documents.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"sanity",
|
|
@@ -16,19 +16,22 @@
|
|
|
16
16
|
},
|
|
17
17
|
"type": "module",
|
|
18
18
|
"main": "src/index.js",
|
|
19
|
+
"types": "src/index.d.ts",
|
|
19
20
|
"scripts": {
|
|
20
|
-
"test": "
|
|
21
|
+
"test": "npm run test:unit && npm run test:integration",
|
|
22
|
+
"test:unit": "npx tap run --include=test/unit/**/* --coverage-report=text --coverage-report=lcov --no-browser",
|
|
23
|
+
"test:integration": "cd test/integration && npm test",
|
|
21
24
|
"lint": "biome ci",
|
|
22
25
|
"lint:write": "biome check --write"
|
|
23
26
|
},
|
|
24
27
|
"license": "MIT",
|
|
25
28
|
"engines": {
|
|
26
|
-
"node": ">=22"
|
|
29
|
+
"node": ">=20.19 <22 || >=22.12"
|
|
27
30
|
},
|
|
28
31
|
"author": "Sanity.io <hello@sanity.io>",
|
|
29
32
|
"devDependencies": {
|
|
30
33
|
"@biomejs/biome": "^2.1.4",
|
|
31
|
-
"@sanity/blueprints-jsonschemas": "^0.1.
|
|
34
|
+
"@sanity/blueprints-jsonschemas": "^0.1.1",
|
|
32
35
|
"ajv": "^8.17.1",
|
|
33
36
|
"tap": "^21.1.0"
|
|
34
37
|
},
|
package/src/constants.js
CHANGED
|
@@ -8,7 +8,7 @@ const formatProperties = [
|
|
|
8
8
|
'outputs',
|
|
9
9
|
'metadata',
|
|
10
10
|
]
|
|
11
|
-
const parameterTypes = ['arg', 'env-var', 'stdin', 'config']
|
|
11
|
+
const parameterTypes = ['arg', 'argument', 'env-var', 'envVar', 'stdin', 'config']
|
|
12
12
|
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-_]+\.?)*(?<![-_.])$/
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
export type BlueprintInput = string | Buffer | object
|
|
2
|
+
|
|
3
|
+
export type ParserOptions = {
|
|
4
|
+
debug?: boolean
|
|
5
|
+
parameters?: Record<string, string | number | boolean>
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export type Resource = {
|
|
9
|
+
name: string
|
|
10
|
+
type: string
|
|
11
|
+
} & Record<string, unknown>
|
|
12
|
+
|
|
13
|
+
export type Parameter = {
|
|
14
|
+
name: string
|
|
15
|
+
type: 'arg' | 'argument' | 'env-var' | 'envVar' | 'config' | 'stdin'
|
|
16
|
+
} & (
|
|
17
|
+
| {
|
|
18
|
+
type: 'arg' | 'argument' | 'env-var' | 'envVar'
|
|
19
|
+
input: string
|
|
20
|
+
}
|
|
21
|
+
| {
|
|
22
|
+
type: 'config'
|
|
23
|
+
settings?: Record<string, unknown>
|
|
24
|
+
}
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
export type Output = {
|
|
28
|
+
name: string
|
|
29
|
+
value: string
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export type Blueprint = {
|
|
33
|
+
blueprintVersion?: string
|
|
34
|
+
resources?: Array<Resource>
|
|
35
|
+
values?: Record<string, string | number>
|
|
36
|
+
parameters?: Array<Parameter>
|
|
37
|
+
outputs?: Array<Output>
|
|
38
|
+
metadata?: Record<string, unknown>
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export type ParserError = {
|
|
42
|
+
message: string
|
|
43
|
+
} & (
|
|
44
|
+
| {
|
|
45
|
+
type: 'json_validation_error'
|
|
46
|
+
error: unknown
|
|
47
|
+
}
|
|
48
|
+
| {
|
|
49
|
+
type: 'invalid_input'
|
|
50
|
+
}
|
|
51
|
+
)
|
|
52
|
+
export type ParserOuput =
|
|
53
|
+
| {
|
|
54
|
+
ok: true
|
|
55
|
+
rawBlueprint: Record<string, unknown>
|
|
56
|
+
}
|
|
57
|
+
| {
|
|
58
|
+
ok: false
|
|
59
|
+
parseErrors: Array<ParserError>
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export type ValidationError = {
|
|
63
|
+
type: string
|
|
64
|
+
message: string
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export type Reference = {
|
|
68
|
+
path: string
|
|
69
|
+
property: string
|
|
70
|
+
ref: string
|
|
71
|
+
} & ({item: Record<string, unknown>} | {item: Array<unknown>; index: number})
|
|
72
|
+
|
|
73
|
+
export type ReferenceError = {
|
|
74
|
+
type: string
|
|
75
|
+
message: string
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export type BlueprintError = ParserError | ValidationError | ReferenceError
|
|
79
|
+
|
|
80
|
+
export type BlueprintOutput =
|
|
81
|
+
| {
|
|
82
|
+
result: 'valid'
|
|
83
|
+
blueprint: Blueprint
|
|
84
|
+
unresolvedRefs?: Array<Reference>
|
|
85
|
+
}
|
|
86
|
+
| {
|
|
87
|
+
result: 'reference_errors'
|
|
88
|
+
blueprint: Blueprint
|
|
89
|
+
unresolvedRefs?: Array<Reference>
|
|
90
|
+
errors: Array<ReferenceError>
|
|
91
|
+
}
|
|
92
|
+
| {
|
|
93
|
+
result: 'parse_errors'
|
|
94
|
+
errors: Array<ParserError>
|
|
95
|
+
}
|
|
96
|
+
| {
|
|
97
|
+
result: 'validation_errors'
|
|
98
|
+
errors: Array<ValidationError>
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export default function blueprintParserValidator(
|
|
102
|
+
input: BlueprintInput,
|
|
103
|
+
options: ParserOptions = {},
|
|
104
|
+
): BlueprintOutput
|
|
105
|
+
|
|
106
|
+
declare module '@sanity/blueprints-parser' {
|
|
107
|
+
export default blueprintParserValidator
|
|
108
|
+
}
|
package/src/index.js
CHANGED
|
@@ -1,57 +1,65 @@
|
|
|
1
|
-
import {firstVersion} from './constants.js'
|
|
2
1
|
import is from './is.js'
|
|
3
2
|
import references from './refs.js'
|
|
4
3
|
import validate from './validate.js'
|
|
5
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Parses and validates the given input into a Blueprint object.
|
|
7
|
+
* @param {import('./index.js').BlueprintInput} input
|
|
8
|
+
* @param {import('./index.js').ParserOptions} options
|
|
9
|
+
* @returns {import('./index.js').BlueprintOutput}
|
|
10
|
+
*/
|
|
6
11
|
export default function blueprintParserValidator(input, options = {}) {
|
|
7
12
|
try {
|
|
8
|
-
const
|
|
9
|
-
if (
|
|
13
|
+
const parsedOutput = parse(input)
|
|
14
|
+
if (parsedOutput.ok === false) {
|
|
10
15
|
return {
|
|
11
|
-
|
|
12
|
-
errors: parseErrors,
|
|
16
|
+
result: 'parse_errors',
|
|
17
|
+
errors: parsedOutput.parseErrors,
|
|
13
18
|
}
|
|
14
19
|
}
|
|
15
20
|
|
|
16
|
-
const
|
|
21
|
+
const parsedBlueprint = parsedOutput.rawBlueprint
|
|
17
22
|
|
|
18
|
-
|
|
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)
|
|
23
|
+
const validationResult = validate(parsedBlueprint, options)
|
|
31
24
|
|
|
32
|
-
if (
|
|
25
|
+
if (validationResult.ok === false) {
|
|
33
26
|
return {
|
|
34
|
-
|
|
35
|
-
errors:
|
|
27
|
+
result: 'validation_errors',
|
|
28
|
+
errors: validationResult.errors,
|
|
36
29
|
}
|
|
37
30
|
}
|
|
38
31
|
|
|
39
|
-
const
|
|
32
|
+
const validatedBlueprint = validationResult.blueprint
|
|
33
|
+
|
|
34
|
+
const foundRefs = references.find(validatedBlueprint, options)
|
|
40
35
|
if (!foundRefs.length) {
|
|
41
36
|
return {
|
|
42
|
-
|
|
37
|
+
result: 'valid',
|
|
38
|
+
blueprint: validatedBlueprint,
|
|
43
39
|
}
|
|
44
40
|
}
|
|
45
41
|
|
|
46
42
|
const {resolvedBlueprint, unresolvedRefs, refErrors} = references.resolve(
|
|
47
|
-
|
|
43
|
+
validatedBlueprint,
|
|
48
44
|
foundRefs,
|
|
49
45
|
options,
|
|
50
46
|
)
|
|
51
47
|
|
|
52
|
-
|
|
48
|
+
/** @type {import('.').BlueprintOutput} */
|
|
49
|
+
let output
|
|
50
|
+
if (refErrors.length) {
|
|
51
|
+
output = {
|
|
52
|
+
result: 'reference_errors',
|
|
53
|
+
blueprint: resolvedBlueprint,
|
|
54
|
+
errors: refErrors,
|
|
55
|
+
}
|
|
56
|
+
} else {
|
|
57
|
+
output = {
|
|
58
|
+
result: 'valid',
|
|
59
|
+
blueprint: resolvedBlueprint,
|
|
60
|
+
}
|
|
61
|
+
}
|
|
53
62
|
if (unresolvedRefs?.length) output.unresolvedRefs = unresolvedRefs
|
|
54
|
-
if (refErrors.length) output.errors = refErrors
|
|
55
63
|
|
|
56
64
|
return output
|
|
57
65
|
|
|
@@ -62,12 +70,21 @@ export default function blueprintParserValidator(input, options = {}) {
|
|
|
62
70
|
}
|
|
63
71
|
}
|
|
64
72
|
|
|
73
|
+
/**
|
|
74
|
+
* Parse the raw input into a Blueprint object.
|
|
75
|
+
* @param {import('.').BlueprintInput} input
|
|
76
|
+
* @returns {import('.').ParserOuput}
|
|
77
|
+
*/
|
|
65
78
|
function parse(input) {
|
|
66
79
|
if (is.string(input) || input instanceof Buffer) {
|
|
67
80
|
try {
|
|
68
|
-
|
|
81
|
+
const rawBlueprint = JSON.parse(input.toString())
|
|
82
|
+
if (is.object(rawBlueprint)) {
|
|
83
|
+
return {ok: true, rawBlueprint}
|
|
84
|
+
}
|
|
69
85
|
} catch (error) {
|
|
70
86
|
return {
|
|
87
|
+
ok: false,
|
|
71
88
|
parseErrors: [
|
|
72
89
|
{
|
|
73
90
|
message: 'Invalid Blueprint JSON',
|
|
@@ -78,9 +95,11 @@ function parse(input) {
|
|
|
78
95
|
}
|
|
79
96
|
}
|
|
80
97
|
} else if (is.object(input)) {
|
|
81
|
-
|
|
98
|
+
const rawBlueprint = /** @type {Record<string, unknown>} */ (input)
|
|
99
|
+
return {ok: true, rawBlueprint: structuredClone(rawBlueprint)}
|
|
82
100
|
}
|
|
83
101
|
return {
|
|
102
|
+
ok: false,
|
|
84
103
|
parseErrors: [
|
|
85
104
|
{
|
|
86
105
|
message: 'Invalid input',
|
package/src/is.js
CHANGED
|
@@ -4,7 +4,7 @@ const array = (item) => Array.isArray(item)
|
|
|
4
4
|
const defined = (item) => typeof item !== 'undefined'
|
|
5
5
|
// const nullish = (item) => typeof item === 'undefined' || item === null
|
|
6
6
|
const number = (item) => Number.isInteger(item) || (typeof item === 'number' && !Number.isNaN(item))
|
|
7
|
-
const object = (item) => item && typeof item === 'object' && !Array.isArray(item)
|
|
7
|
+
const object = (item) => !!item && typeof item === 'object' && !Array.isArray(item)
|
|
8
8
|
const ref = (item) => string(item) && item.startsWith('$.')
|
|
9
9
|
const scalar = (item) => string(item) || number(item)
|
|
10
10
|
const string = (item) => typeof item === 'string'
|
package/src/refs.js
CHANGED
|
@@ -1,10 +1,20 @@
|
|
|
1
1
|
import is from './is.js'
|
|
2
2
|
export default {find, resolve}
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
/**
|
|
5
|
+
* @param {import('.').Blueprint} validatedBlueprint
|
|
6
|
+
* @param {import('.').ParserOptions} options
|
|
7
|
+
* @returns {Array<import('.').Reference>}
|
|
8
|
+
*/
|
|
9
|
+
function find(validatedBlueprint, options) {
|
|
5
10
|
const {debug} = options
|
|
11
|
+
/** @type {Array<import('.').Reference>} */
|
|
6
12
|
const foundRefs = []
|
|
7
13
|
|
|
14
|
+
/**
|
|
15
|
+
* @param {*} item
|
|
16
|
+
* @param {string} path
|
|
17
|
+
*/
|
|
8
18
|
function walk(item, path) {
|
|
9
19
|
if (is.object(item)) {
|
|
10
20
|
for (const [name, value] of Object.entries(item)) {
|
|
@@ -26,9 +36,9 @@ function find(rawBlueprint, options) {
|
|
|
26
36
|
const properties = ['resources', 'parameters', 'outputs']
|
|
27
37
|
for (const property of properties) {
|
|
28
38
|
// Run over the list
|
|
29
|
-
if (
|
|
39
|
+
if (validatedBlueprint[property]?.length) {
|
|
30
40
|
// Inspect each property, or (recursively) walk if it's an object
|
|
31
|
-
for (const item of
|
|
41
|
+
for (const item of validatedBlueprint[property]) {
|
|
32
42
|
const top = `${property}.${item.name}`
|
|
33
43
|
walk(item, top)
|
|
34
44
|
}
|
|
@@ -43,7 +53,17 @@ function find(rawBlueprint, options) {
|
|
|
43
53
|
return foundRefs
|
|
44
54
|
}
|
|
45
55
|
|
|
46
|
-
|
|
56
|
+
/**
|
|
57
|
+
* @param {import('.').Blueprint} blueprint
|
|
58
|
+
* @param {Array<import('.').Reference>} foundRefs
|
|
59
|
+
* @param {import('.').ParserOptions} options
|
|
60
|
+
* @returns {{
|
|
61
|
+
* resolvedBlueprint: import('.').Blueprint
|
|
62
|
+
* unresolvedRefs: Array<import('.').Reference> | undefined
|
|
63
|
+
* refErrors: Array<import('.').ReferenceError>
|
|
64
|
+
* }}
|
|
65
|
+
*/
|
|
66
|
+
function resolve(blueprint, foundRefs, options) {
|
|
47
67
|
const {parameters = {}} = options
|
|
48
68
|
const refs = {}
|
|
49
69
|
const unresolvedRefs = []
|
|
@@ -82,7 +102,7 @@ function resolve(rawBlueprint, foundRefs, options) {
|
|
|
82
102
|
const found = parts.reduce((obj, i) => {
|
|
83
103
|
if (obj?.[i]) return obj[i]
|
|
84
104
|
if (is.array(obj)) return obj.find(({name}) => name === i)
|
|
85
|
-
},
|
|
105
|
+
}, blueprint)
|
|
86
106
|
if (is.scalar(found)) {
|
|
87
107
|
refs[refPath] = found
|
|
88
108
|
if (is.object(item)) item[property] = refs[refPath]
|
|
@@ -96,7 +116,7 @@ function resolve(rawBlueprint, foundRefs, options) {
|
|
|
96
116
|
}
|
|
97
117
|
|
|
98
118
|
return {
|
|
99
|
-
resolvedBlueprint:
|
|
119
|
+
resolvedBlueprint: blueprint,
|
|
100
120
|
unresolvedRefs: unresolvedRefs.length ? unresolvedRefs : undefined,
|
|
101
121
|
refErrors,
|
|
102
122
|
}
|
package/src/validate.js
CHANGED
|
@@ -7,71 +7,210 @@ import {
|
|
|
7
7
|
} from './constants.js'
|
|
8
8
|
import is from './is.js'
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
10
|
+
/**
|
|
11
|
+
* @typedef {{
|
|
12
|
+
* ok: true
|
|
13
|
+
* blueprint: import('.').Blueprint
|
|
14
|
+
* } | {
|
|
15
|
+
* ok: false
|
|
16
|
+
* errors: import('.').ValidationError[]
|
|
17
|
+
* }} ValidationResult
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Verifies that the blueprint version is valid.
|
|
22
|
+
* @param {any} blueprintVersion
|
|
23
|
+
* @returns {import('.').ValidationError[]}
|
|
24
|
+
*/
|
|
25
|
+
function version(blueprintVersion) {
|
|
26
|
+
if (!is.defined(blueprintVersion)) return []
|
|
27
|
+
|
|
28
|
+
const errors = []
|
|
29
|
+
const type = 'invalid_version'
|
|
30
|
+
if (!is.string(blueprintVersion) || !versionFormat.test(blueprintVersion)) {
|
|
31
|
+
errors.push({message: `Invalid version: ${blueprintVersion}`, type})
|
|
32
|
+
return errors
|
|
33
|
+
}
|
|
34
|
+
const [y, m, d] = blueprintVersion.split('-')
|
|
35
|
+
if (y < '2024') {
|
|
36
|
+
errors.push({message: `Invalid version year: ${y}`, type})
|
|
37
|
+
}
|
|
38
|
+
if (m < '01' || m > '12') {
|
|
39
|
+
errors.push({message: `Invalid version month: ${m}`, type})
|
|
40
|
+
}
|
|
41
|
+
if (d < '01' || d > '31') {
|
|
42
|
+
errors.push({message: `Invalid version day: ${d}`, type})
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return errors
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Verifies that resource is an array of objects with name and type.
|
|
50
|
+
* @param {any} resources
|
|
51
|
+
* @returns {import('.').ValidationError[]}
|
|
52
|
+
*/
|
|
53
|
+
function resources(resources) {
|
|
54
|
+
if (!is.defined(resources)) return []
|
|
55
|
+
|
|
56
|
+
if (!is.array(resources)) {
|
|
57
|
+
return [
|
|
58
|
+
{
|
|
59
|
+
message: 'Resources must be an array',
|
|
60
|
+
type: 'invalid_type',
|
|
61
|
+
},
|
|
62
|
+
]
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const errors = []
|
|
66
|
+
const names = []
|
|
67
|
+
resources.forEach((resource) => {
|
|
68
|
+
if (!is.object(resource)) {
|
|
69
|
+
errors.push({
|
|
70
|
+
// Maybe we should break out the stringified resource into a data field?
|
|
71
|
+
message: `Resources must be an object, found: ${JSON.stringify(resource)}`,
|
|
72
|
+
type: 'invalid_type',
|
|
73
|
+
})
|
|
74
|
+
return errors
|
|
23
75
|
}
|
|
24
|
-
|
|
25
|
-
|
|
76
|
+
|
|
77
|
+
const {name, type} = resource
|
|
78
|
+
if (!is.defined(name)) {
|
|
79
|
+
errors.push({
|
|
80
|
+
message: `Resource must have a 'name' property`,
|
|
81
|
+
type: 'missing_required_property',
|
|
82
|
+
})
|
|
83
|
+
} else if (!is.string(name)) {
|
|
84
|
+
errors.push({
|
|
85
|
+
message: `Resource 'name' property must be a string, found: ${name}`,
|
|
86
|
+
type: 'invalid_type',
|
|
87
|
+
})
|
|
88
|
+
} else {
|
|
89
|
+
if (names.includes(name)) {
|
|
90
|
+
errors.push({
|
|
91
|
+
message: `All resource 'name' properties must be unique, found: ${name}`,
|
|
92
|
+
type: 'duplicate_name',
|
|
93
|
+
})
|
|
94
|
+
}
|
|
95
|
+
if (!nameFormat.test(name)) {
|
|
96
|
+
errors.push({
|
|
97
|
+
message: `Resource 'name' property is invalid, must conform to '${nameFormat}', found: ${name}`,
|
|
98
|
+
type: 'invalid_format',
|
|
99
|
+
})
|
|
100
|
+
}
|
|
101
|
+
names.push(name)
|
|
26
102
|
}
|
|
27
103
|
|
|
28
|
-
if (
|
|
29
|
-
|
|
104
|
+
if (!is.defined(type)) {
|
|
105
|
+
errors.push({
|
|
106
|
+
message: `Resource must have a 'type' property`,
|
|
107
|
+
type: 'missing_required_property',
|
|
108
|
+
})
|
|
109
|
+
} else if (!is.string(type)) {
|
|
110
|
+
errors.push({
|
|
111
|
+
message: `Resource 'type' property must be a string, found: ${type}`,
|
|
112
|
+
type: 'invalid_type',
|
|
113
|
+
})
|
|
114
|
+
} else if (!typeFormat.test(type)) {
|
|
115
|
+
errors.push({
|
|
116
|
+
message: `Resource 'type' property is invalid, must conform to '${typeFormat}', found: ${type}`,
|
|
117
|
+
type: 'invalid_format',
|
|
118
|
+
})
|
|
119
|
+
}
|
|
120
|
+
})
|
|
30
121
|
|
|
31
|
-
|
|
32
|
-
|
|
122
|
+
return errors
|
|
123
|
+
}
|
|
33
124
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
125
|
+
/**
|
|
126
|
+
* Verifies that values is an object.
|
|
127
|
+
* @param {any} values
|
|
128
|
+
* @returns {import('.').ValidationError[]}
|
|
129
|
+
*/
|
|
130
|
+
function values(values) {
|
|
131
|
+
if (!is.defined(values)) return []
|
|
132
|
+
|
|
133
|
+
if (!is.object(values)) {
|
|
134
|
+
return [
|
|
135
|
+
{
|
|
136
|
+
message: 'Values must be an object',
|
|
137
|
+
type: 'invalid_type',
|
|
138
|
+
},
|
|
139
|
+
]
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const errors = []
|
|
143
|
+
if (Object.keys(values).length) {
|
|
144
|
+
Object.entries(values).forEach(([name, value]) => {
|
|
145
|
+
if (!is.scalar(value)) {
|
|
146
|
+
errors.push({
|
|
147
|
+
message: `Values property '${name}' must be scalar (string or number)`,
|
|
38
148
|
type: 'invalid_type',
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
|
|
149
|
+
})
|
|
150
|
+
}
|
|
151
|
+
if (is.ref(value)) {
|
|
152
|
+
errors.push({
|
|
153
|
+
message: `Values property '${name}' cannot be a reference, found: ${value}`,
|
|
154
|
+
type: 'invalid_type',
|
|
155
|
+
})
|
|
156
|
+
}
|
|
157
|
+
})
|
|
158
|
+
}
|
|
42
159
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
160
|
+
return errors
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Verifies that parameters is an array of object with name and type.
|
|
165
|
+
* @param {any} parameters
|
|
166
|
+
* @returns {import('.').ValidationError[]}
|
|
167
|
+
*/
|
|
168
|
+
function parameters(parameters) {
|
|
169
|
+
if (!is.defined(parameters)) return []
|
|
170
|
+
|
|
171
|
+
if (!is.array(parameters)) {
|
|
172
|
+
return [
|
|
173
|
+
{
|
|
174
|
+
message: 'Parameters must be an array',
|
|
175
|
+
type: 'invalid_type',
|
|
176
|
+
},
|
|
177
|
+
]
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const errors = []
|
|
181
|
+
const names = []
|
|
182
|
+
if (parameters.length) {
|
|
183
|
+
parameters.forEach((param) => {
|
|
184
|
+
if (!is.object(param)) {
|
|
185
|
+
errors.push({
|
|
186
|
+
// Maybe we should break out the stringified parameter into a data field?
|
|
187
|
+
message: `Parameters must be an object, found: ${JSON.stringify(param)}`,
|
|
50
188
|
type: 'invalid_type',
|
|
51
189
|
})
|
|
190
|
+
return errors
|
|
52
191
|
}
|
|
53
192
|
|
|
54
|
-
const {name, type} =
|
|
193
|
+
const {name, type} = param
|
|
55
194
|
if (!is.defined(name)) {
|
|
56
195
|
errors.push({
|
|
57
|
-
message: `
|
|
196
|
+
message: `Parameter must have a 'name' property`,
|
|
58
197
|
type: 'missing_required_property',
|
|
59
198
|
})
|
|
60
199
|
} else if (!is.string(name)) {
|
|
61
200
|
errors.push({
|
|
62
|
-
message: `
|
|
201
|
+
message: `Parameter 'name' property must be a string, found: ${name}`,
|
|
63
202
|
type: 'invalid_type',
|
|
64
203
|
})
|
|
65
204
|
} else {
|
|
66
205
|
if (names.includes(name)) {
|
|
67
206
|
errors.push({
|
|
68
|
-
message: `All
|
|
207
|
+
message: `All parameter 'name' properties must be unique, found: ${name}`,
|
|
69
208
|
type: 'duplicate_name',
|
|
70
209
|
})
|
|
71
210
|
}
|
|
72
211
|
if (!nameFormat.test(name)) {
|
|
73
212
|
errors.push({
|
|
74
|
-
message: `
|
|
213
|
+
message: `Parameter 'name' property is invalid, must conform to '${nameFormat}', found: ${name}`,
|
|
75
214
|
type: 'invalid_format',
|
|
76
215
|
})
|
|
77
216
|
}
|
|
@@ -80,247 +219,196 @@ export default {
|
|
|
80
219
|
|
|
81
220
|
if (!is.defined(type)) {
|
|
82
221
|
errors.push({
|
|
83
|
-
message: `
|
|
222
|
+
message: `Parameter must have a 'type' property`,
|
|
84
223
|
type: 'missing_required_property',
|
|
85
224
|
})
|
|
86
225
|
} else if (!is.string(type)) {
|
|
87
226
|
errors.push({
|
|
88
|
-
message: `
|
|
227
|
+
message: `Parameter 'type' property must be a string, found: ${type}`,
|
|
89
228
|
type: 'invalid_type',
|
|
90
229
|
})
|
|
91
|
-
} else if (!
|
|
230
|
+
} else if (!parameterTypes.includes(type)) {
|
|
92
231
|
errors.push({
|
|
93
|
-
message: `
|
|
94
|
-
type: '
|
|
232
|
+
message: `Unknown parameter 'type', found: ${type}`,
|
|
233
|
+
type: 'invalid_value',
|
|
95
234
|
})
|
|
96
235
|
}
|
|
97
236
|
})
|
|
237
|
+
}
|
|
98
238
|
|
|
99
|
-
|
|
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
|
-
}
|
|
239
|
+
return errors
|
|
240
|
+
}
|
|
113
241
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
242
|
+
/**
|
|
243
|
+
* Verifies that the output is an array of objects with name and value.
|
|
244
|
+
* @param {any} outputs
|
|
245
|
+
* @returns {import('.').ValidationError[]}
|
|
246
|
+
*/
|
|
247
|
+
function outputs(outputs) {
|
|
248
|
+
if (!is.defined(outputs)) return []
|
|
249
|
+
|
|
250
|
+
if (!is.array(outputs)) {
|
|
251
|
+
return [
|
|
252
|
+
{
|
|
253
|
+
message: 'Outputs must be an array',
|
|
254
|
+
type: 'invalid_type',
|
|
255
|
+
},
|
|
256
|
+
]
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
const errors = []
|
|
260
|
+
const names = []
|
|
261
|
+
outputs.forEach((output) => {
|
|
262
|
+
if (!is.object(output)) {
|
|
263
|
+
errors.push({
|
|
264
|
+
// Maybe we should break out the stringified output into a data field?
|
|
265
|
+
message: `Outputs must be an object, found: ${JSON.stringify(output)}`,
|
|
266
|
+
type: 'invalid_type',
|
|
129
267
|
})
|
|
268
|
+
return errors
|
|
130
269
|
}
|
|
131
270
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
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
|
-
}
|
|
271
|
+
const {name, value} = output
|
|
272
|
+
if (!is.defined(name)) {
|
|
273
|
+
errors.push({
|
|
274
|
+
message: `Output must have a 'name' property`,
|
|
275
|
+
type: 'missing_required_property',
|
|
202
276
|
})
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
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)) {
|
|
277
|
+
} else if (!is.string(name)) {
|
|
278
|
+
errors.push({
|
|
279
|
+
message: `Output 'name' property must be a string, found: ${name}`,
|
|
280
|
+
type: 'invalid_type',
|
|
281
|
+
})
|
|
282
|
+
} else {
|
|
283
|
+
if (names.includes(name)) {
|
|
238
284
|
errors.push({
|
|
239
|
-
message: `
|
|
240
|
-
type: '
|
|
285
|
+
message: `All output 'name' properties must be unique, found: ${name}`,
|
|
286
|
+
type: 'duplicate_name',
|
|
241
287
|
})
|
|
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
288
|
}
|
|
257
|
-
|
|
258
|
-
if (!is.defined(value)) {
|
|
289
|
+
if (!nameFormat.test(name)) {
|
|
259
290
|
errors.push({
|
|
260
|
-
message: `Output must
|
|
261
|
-
type: '
|
|
291
|
+
message: `Output 'name' property is invalid, must conform to '${nameFormat}', found: ${name}`,
|
|
292
|
+
type: 'invalid_format',
|
|
262
293
|
})
|
|
263
294
|
}
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
if (errors.length) return errors
|
|
267
|
-
},
|
|
268
|
-
|
|
269
|
-
metadata: (metadata) => {
|
|
270
|
-
if (!is.defined(metadata)) return
|
|
295
|
+
names.push(name)
|
|
296
|
+
}
|
|
271
297
|
|
|
272
|
-
if (!is.
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
},
|
|
278
|
-
]
|
|
298
|
+
if (!is.defined(value)) {
|
|
299
|
+
errors.push({
|
|
300
|
+
message: `Output must have a 'value' property`,
|
|
301
|
+
type: 'missing_required_property',
|
|
302
|
+
})
|
|
279
303
|
}
|
|
280
|
-
}
|
|
304
|
+
})
|
|
281
305
|
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
if (!properties.length) return
|
|
306
|
+
return errors
|
|
307
|
+
}
|
|
285
308
|
|
|
286
|
-
|
|
309
|
+
/**
|
|
310
|
+
* Verifies that metadata is an object.
|
|
311
|
+
* @param {any} metadata
|
|
312
|
+
* @returns {import('.').ValidationError[]}
|
|
313
|
+
*/
|
|
314
|
+
function metadata(metadata) {
|
|
315
|
+
if (!is.defined(metadata)) return []
|
|
316
|
+
|
|
317
|
+
if (!is.object(metadata)) {
|
|
318
|
+
return [
|
|
319
|
+
{
|
|
320
|
+
message: 'Metadata must be an object',
|
|
321
|
+
type: 'invalid_type',
|
|
322
|
+
},
|
|
323
|
+
]
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
return []
|
|
327
|
+
}
|
|
287
328
|
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
329
|
+
/**
|
|
330
|
+
* Verifies that there are no extra properties.
|
|
331
|
+
* @param {Record<string, any>} rawBlueprint
|
|
332
|
+
* @returns {import('.').ValidationError[]}
|
|
333
|
+
*/
|
|
334
|
+
function extra(rawBlueprint) {
|
|
335
|
+
const properties = Object.keys(rawBlueprint)
|
|
336
|
+
if (!properties.length) return []
|
|
337
|
+
|
|
338
|
+
const errors = []
|
|
339
|
+
|
|
340
|
+
for (const property of properties) {
|
|
341
|
+
if (!formatProperties.includes(property)) {
|
|
342
|
+
errors.push({
|
|
343
|
+
message: `Found invalid Blueprint property: ${property}`,
|
|
344
|
+
type: 'invalid_property',
|
|
345
|
+
})
|
|
295
346
|
}
|
|
347
|
+
}
|
|
296
348
|
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
passedParameters: (options) => {
|
|
301
|
-
const {parameters} = options
|
|
302
|
-
if (!is.defined(parameters)) return
|
|
349
|
+
return errors
|
|
350
|
+
}
|
|
303
351
|
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
352
|
+
/**
|
|
353
|
+
* Verifies that the blueprint version is valid.
|
|
354
|
+
* @param {import('.').ParserOptions} options
|
|
355
|
+
* @returns {import('.').ValidationError[]}
|
|
356
|
+
*/
|
|
357
|
+
function passedParameters(options) {
|
|
358
|
+
const {parameters} = options
|
|
359
|
+
if (!is.defined(parameters)) return []
|
|
360
|
+
|
|
361
|
+
if (!is.object(parameters)) {
|
|
362
|
+
return [
|
|
363
|
+
{
|
|
364
|
+
message: 'Passed parameters must be an object',
|
|
365
|
+
type: 'invalid_type',
|
|
366
|
+
},
|
|
367
|
+
]
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
const errors = []
|
|
371
|
+
|
|
372
|
+
for (const [name, value] of Object.entries(parameters)) {
|
|
373
|
+
if (!is.scalar(value)) {
|
|
374
|
+
errors.push({
|
|
375
|
+
message: `Passed parameter '${name}' must be scalar (string or number)`,
|
|
376
|
+
type: 'invalid_type',
|
|
377
|
+
})
|
|
311
378
|
}
|
|
379
|
+
}
|
|
312
380
|
|
|
313
|
-
|
|
381
|
+
return errors
|
|
382
|
+
}
|
|
314
383
|
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
384
|
+
/**
|
|
385
|
+
* Validates a parsed blueprint.
|
|
386
|
+
* @param {Record<string, any>} parsedBlueprint
|
|
387
|
+
* @param {import('.').ParserOptions} options
|
|
388
|
+
* @returns {ValidationResult}
|
|
389
|
+
*/
|
|
390
|
+
export default function validate(parsedBlueprint, options) {
|
|
391
|
+
// Aggregate basic structural, spec violation, or input errors
|
|
392
|
+
const errors = [].concat(
|
|
393
|
+
version(parsedBlueprint.blueprintVersion),
|
|
394
|
+
resources(parsedBlueprint.resources),
|
|
395
|
+
values(parsedBlueprint.values),
|
|
396
|
+
parameters(parsedBlueprint.parameters),
|
|
397
|
+
outputs(parsedBlueprint.outputs),
|
|
398
|
+
metadata(parsedBlueprint.metadata),
|
|
399
|
+
extra(parsedBlueprint),
|
|
400
|
+
passedParameters(options),
|
|
401
|
+
)
|
|
402
|
+
|
|
403
|
+
if (errors.length) {
|
|
404
|
+
return {
|
|
405
|
+
ok: false,
|
|
406
|
+
errors,
|
|
322
407
|
}
|
|
408
|
+
}
|
|
323
409
|
|
|
324
|
-
|
|
325
|
-
|
|
410
|
+
return {
|
|
411
|
+
ok: true,
|
|
412
|
+
blueprint: /** @type {import('.').Blueprint} */ (parsedBlueprint),
|
|
413
|
+
}
|
|
326
414
|
}
|
package/test/unit/index.test.js
CHANGED
|
@@ -1,96 +1,138 @@
|
|
|
1
|
-
import {readFile} from 'node:fs/promises'
|
|
2
|
-
import {join} from 'node:path'
|
|
3
1
|
import schemas from '@sanity/blueprints-jsonschemas'
|
|
4
2
|
import Ajv from 'ajv/dist/2020.js'
|
|
5
3
|
import tap from 'tap'
|
|
6
4
|
import blueprintParserValidator from '../../src/index.js'
|
|
7
5
|
import mocks from '../mocks/all.js'
|
|
8
6
|
|
|
9
|
-
const cwd = process.cwd()
|
|
10
7
|
const ajv = new Ajv()
|
|
11
8
|
|
|
12
|
-
tap.test('Basic input', (t) => {
|
|
9
|
+
tap.test('Basic input', async (t) => {
|
|
13
10
|
t.plan(8)
|
|
14
11
|
const input = {blueprintVersion: '2024-10-01'}
|
|
15
12
|
const inputJSON = JSON.stringify(input)
|
|
16
|
-
let result
|
|
17
13
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
t.notOk(result.errors, 'Parser / validator found no issues')
|
|
14
|
+
/** @type {import('../../src/index.js').BlueprintOutput} */
|
|
15
|
+
let output
|
|
21
16
|
|
|
22
|
-
|
|
23
|
-
t.same(
|
|
24
|
-
|
|
17
|
+
output = blueprintParserValidator(input)
|
|
18
|
+
t.same(
|
|
19
|
+
output.result === 'valid' && output.blueprint,
|
|
20
|
+
input,
|
|
21
|
+
'Parser / validator accepted raw JS input',
|
|
22
|
+
)
|
|
23
|
+
t.notOk('errors' in output, 'Parser / validator found no issues')
|
|
25
24
|
|
|
26
|
-
|
|
27
|
-
t.same(
|
|
28
|
-
|
|
25
|
+
output = blueprintParserValidator(inputJSON)
|
|
26
|
+
t.same(
|
|
27
|
+
output.result === 'valid' && output.blueprint,
|
|
28
|
+
input,
|
|
29
|
+
'Parser / validator accepted JSON input',
|
|
30
|
+
)
|
|
31
|
+
t.notOk('errors' in output, 'Parser / validator found no issues')
|
|
29
32
|
|
|
30
|
-
|
|
31
|
-
t.same(
|
|
32
|
-
|
|
33
|
+
output = blueprintParserValidator(Buffer.from(inputJSON))
|
|
34
|
+
t.same(
|
|
35
|
+
output.result === 'valid' && output.blueprint,
|
|
36
|
+
input,
|
|
37
|
+
'Parser / validator accepted JSON buffer',
|
|
38
|
+
)
|
|
39
|
+
t.notOk('errors' in output, 'Parser / validator found no issues')
|
|
40
|
+
|
|
41
|
+
output = blueprintParserValidator(input, {parameters: {ok: 'hello'}})
|
|
42
|
+
t.same(
|
|
43
|
+
output.result === 'valid' && output.blueprint,
|
|
44
|
+
input,
|
|
45
|
+
'Parser / validator accepted raw JS input and scalar params',
|
|
46
|
+
)
|
|
47
|
+
t.notOk('errors' in output, 'Parser / validator found no issues')
|
|
33
48
|
})
|
|
34
49
|
|
|
35
|
-
tap.test('Basic input: errors', (t) => {
|
|
50
|
+
tap.test('Basic input: errors', async (t) => {
|
|
36
51
|
t.plan(10)
|
|
37
52
|
const input = {blueprintVersion: 'nah'}
|
|
38
53
|
const invalidInput = '{'
|
|
39
54
|
const validInput = {}
|
|
55
|
+
|
|
56
|
+
/** @type {import('../../src/index.js').BlueprintOutput} */
|
|
40
57
|
let result
|
|
41
58
|
|
|
42
59
|
result = blueprintParserValidator(invalidInput)
|
|
60
|
+
t.ok(result.result === 'parse_errors')
|
|
43
61
|
t.same(
|
|
44
|
-
result.
|
|
45
|
-
|
|
46
|
-
'Parser / validator returned
|
|
62
|
+
result.result === 'parse_errors' && result.errors.length,
|
|
63
|
+
1,
|
|
64
|
+
'Parser / validator returned error on invalid JSON',
|
|
47
65
|
)
|
|
48
|
-
t.same(result.errors.length, 1, 'Parser / validator returned error on invalid JSON')
|
|
49
66
|
|
|
50
67
|
result = blueprintParserValidator(undefined)
|
|
51
|
-
t.
|
|
52
|
-
t.same(
|
|
68
|
+
t.ok(result.result === 'parse_errors')
|
|
69
|
+
t.same(
|
|
70
|
+
result.result === 'parse_errors' && result.errors.length,
|
|
71
|
+
1,
|
|
72
|
+
'Parser / validator returned error on invalid input (undefined)',
|
|
73
|
+
)
|
|
53
74
|
|
|
54
75
|
result = blueprintParserValidator(input)
|
|
55
|
-
t.
|
|
56
|
-
t.same(
|
|
76
|
+
t.ok(result.result === 'validation_errors', 'Parser / validator accepted invalid raw JS input')
|
|
77
|
+
t.same(
|
|
78
|
+
result.result === 'validation_errors' && result.errors.length,
|
|
79
|
+
1,
|
|
80
|
+
'Parser / validator returned a basic format error',
|
|
81
|
+
)
|
|
57
82
|
|
|
83
|
+
// @ts-expect-error Passing invalid input to the parameters attribute
|
|
58
84
|
result = blueprintParserValidator(validInput, {parameters: 'hi'})
|
|
59
|
-
t.
|
|
60
|
-
t.same(
|
|
85
|
+
t.ok(result.result === 'validation_errors', 'Parser / validator accepted result input')
|
|
86
|
+
t.same(
|
|
87
|
+
result.result === 'validation_errors' && result.errors.length,
|
|
88
|
+
1,
|
|
89
|
+
'Parser / validator returned a basic format error',
|
|
90
|
+
)
|
|
61
91
|
|
|
92
|
+
// @ts-expect-error Passing invalid input to the parameters attribute
|
|
62
93
|
result = blueprintParserValidator(validInput, {parameters: {ok: {}}})
|
|
63
|
-
t.
|
|
64
|
-
t.same(
|
|
94
|
+
t.ok(result.result === 'validation_errors', 'Parser / validator accepted result input')
|
|
95
|
+
t.same(
|
|
96
|
+
result.result === 'validation_errors' && result.errors.length,
|
|
97
|
+
1,
|
|
98
|
+
'Parser / validator returned a basic format error',
|
|
99
|
+
)
|
|
65
100
|
})
|
|
66
101
|
|
|
67
102
|
tap.test('Reference resolution', async (t) => {
|
|
68
103
|
const references = Object.entries(mocks.references)
|
|
69
104
|
const referenceErrors = Object.entries(mocks.referenceErrors)
|
|
70
|
-
const tests = references.length *
|
|
105
|
+
const tests = references.length * 4 + referenceErrors.length * 4
|
|
71
106
|
t.plan(tests)
|
|
72
107
|
|
|
73
108
|
// Basic reference resolution
|
|
74
109
|
for (const [name, {input, parameters, expected, unresolved}] of references) {
|
|
75
|
-
const
|
|
110
|
+
const output = blueprintParserValidator(input, {
|
|
76
111
|
debug: true,
|
|
77
112
|
parameters,
|
|
78
113
|
})
|
|
79
|
-
t.same(
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
114
|
+
t.same(output.result, 'valid')
|
|
115
|
+
if (output.result === 'valid') {
|
|
116
|
+
const {blueprint, unresolvedRefs} = output
|
|
117
|
+
t.same(blueprint, expected, `${name}: returned expected blueprint`)
|
|
118
|
+
t.same(unresolvedRefs, unresolved, `${name}: returned expected unresolved references`)
|
|
119
|
+
t.notOk('errors' in output, `${name}: found no issues`)
|
|
120
|
+
}
|
|
83
121
|
}
|
|
84
122
|
|
|
85
123
|
// Basic reference errors
|
|
86
124
|
for (const [name, {input, expected, unresolved}] of referenceErrors) {
|
|
87
|
-
const
|
|
125
|
+
const output = blueprintParserValidator(input, {
|
|
88
126
|
debug: true,
|
|
89
127
|
parameters: {},
|
|
90
128
|
})
|
|
91
|
-
t.
|
|
92
|
-
|
|
93
|
-
|
|
129
|
+
t.ok(output.result, 'result is true despite reference errors')
|
|
130
|
+
if (output.result === 'reference_errors') {
|
|
131
|
+
const {blueprint, unresolvedRefs, errors} = output
|
|
132
|
+
t.same(blueprint, expected, `${name}: returned expected blueprint`)
|
|
133
|
+
t.same(unresolvedRefs, unresolved, `${name}: returned expected unresolved references`)
|
|
134
|
+
t.ok(errors, `${name}: parser / validator found an error`)
|
|
135
|
+
}
|
|
94
136
|
}
|
|
95
137
|
})
|
|
96
138
|
|
|
@@ -100,31 +142,32 @@ tap.test('2024-10-01', async (t) => {
|
|
|
100
142
|
const basic = Object.entries(mocks.basic)
|
|
101
143
|
const basicErrors = Object.entries(mocks.basicErrors)
|
|
102
144
|
const tests =
|
|
103
|
-
basic.length *
|
|
145
|
+
basic.length * 4 +
|
|
104
146
|
// allow tests to opt out of JSON Schema validation, when it's not as flexible as regular logic
|
|
105
147
|
basicErrors.reduce((a, [, b]) => a + (b.metadata?.ignore ? 1 : 3), 0)
|
|
106
148
|
t.plan(tests)
|
|
107
149
|
|
|
108
150
|
// Check the basic structure of a document
|
|
109
151
|
for (const [name, input] of basic) {
|
|
110
|
-
const
|
|
111
|
-
t.
|
|
112
|
-
|
|
152
|
+
const output = blueprintParserValidator(input)
|
|
153
|
+
t.same(output.result, 'valid', `${name}: expected valid result`)
|
|
154
|
+
t.notOk('errors' in output, `${name}: parser / validator found no issues`)
|
|
155
|
+
if (output.result !== 'valid') console.log(`${name} errors:`, output.errors)
|
|
113
156
|
|
|
114
|
-
const
|
|
115
|
-
t.ok(
|
|
157
|
+
const result = ajv.validate(jsonSchema20241001, input)
|
|
158
|
+
t.ok(result, `${name}: JSON Schema validator found no issues`)
|
|
116
159
|
t.notOk(ajv.errors, `${name}: JSON Schema validator found no issues`)
|
|
117
160
|
if (ajv.errors?.length) console.log(`${name} errors (AJV):`, ajv.errors)
|
|
118
161
|
}
|
|
119
162
|
|
|
120
163
|
// Basic validation errors
|
|
121
164
|
for (const [name, input] of basicErrors) {
|
|
122
|
-
const
|
|
123
|
-
t.ok(errors, `${name}: parser / validator found an error`)
|
|
165
|
+
const output = blueprintParserValidator(input)
|
|
166
|
+
t.ok(output.result !== 'valid' && output.errors, `${name}: parser / validator found an error`)
|
|
124
167
|
|
|
125
168
|
if (!input.metadata?.ignore) {
|
|
126
|
-
const
|
|
127
|
-
t.notOk(
|
|
169
|
+
const result = ajv.validate(jsonSchema20241001, input)
|
|
170
|
+
t.notOk(result, `${name}: JSON Schema validator found an error`)
|
|
128
171
|
t.ok(ajv.errors, `${name}: JSON Schema validator found an error`)
|
|
129
172
|
}
|
|
130
173
|
}
|