-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate-schema.spec.ts
80 lines (76 loc) · 2.3 KB
/
validate-schema.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import ajvLib from 'ajv'
const Ajv = ajvLib.default
import { readdirSync, readFileSync } from 'fs'
import * as path from 'path'
import addFormatsLib from 'ajv-formats'
const addFormats = addFormatsLib.default
import addKeywordsLib from 'ajv-keywords'
const addKeywords = addKeywordsLib.default
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
const ajv = new Ajv({
schemas: [
JSON.parse(
readFileSync(
path.join(process.cwd(), 'application.schema.json'),
'utf-8',
),
),
JSON.parse(
readFileSync(path.join(process.cwd(), 'manifest.schema.json'), 'utf-8'),
),
],
})
addFormats(ajv)
addKeywords(ajv)
const applicationManifests: [string, Record<string, any>][] = []
const applicationFolder = path.join(process.cwd(), 'applications')
for (const application of readdirSync(applicationFolder)) {
const manifests = readdirSync(
path.join(applicationFolder, application),
).filter((s) => s.endsWith('.json'))
if (manifests.length > 1)
throw new Error(
`${applicationFolder} has multiple JSON files, expected exactly 1.`,
)
if (manifests.length === 0) {
throw new Error(
`${applicationFolder} has no JSON file, expected exactly 1.`,
)
}
const location = path.join(applicationFolder, application, manifests[0])
applicationManifests.push([
application,
JSON.parse(readFileSync(location, 'utf-8')),
])
}
void describe('application manifests', () => {
for (const [application, source] of applicationManifests) {
void it(`${application} manifest should validate`, async () => {
const validate = ajv.getSchema(
`https://nordicsemiconductor.github.io/nrfprogrammer-firmware-images/application.schema.json`,
)
assert.notEqual(validate, undefined)
const valid = await validate?.(source)
assert.equal(validate?.errors, null)
assert.equal(valid, true)
})
}
})
void describe('manifest', () => {
void it('should validate the combined manifest', async () => {
const validate = ajv.getSchema(
`https://nordicsemiconductor.github.io/nrfprogrammer-firmware-images/manifest.schema.json`,
)
assert.notEqual(validate, undefined)
const valid = await validate?.(
JSON.parse(
readFileSync(
path.join(process.cwd(), 'assets', 'manifest.json'),
).toString(),
),
)
assert.equal(validate?.errors, null)
assert.equal(valid, true)
})
})