Skip to content

Commit dbd45c8

Browse files
committed
impl
Signed-off-by: Jan Kowalleck <[email protected]>
1 parent 0cd7722 commit dbd45c8

File tree

1 file changed

+37
-41
lines changed

1 file changed

+37
-41
lines changed

src/validation/validators/json.ts

+37-41
Original file line numberDiff line numberDiff line change
@@ -27,37 +27,35 @@ import { BaseValidator } from './_helpers'
2727
let _ajv: Ajv | undefined
2828

2929
async function getAjv (): Promise<Ajv> {
30-
return await new Promise<Ajv>(async (resolve, reject) => {
31-
if (_ajv === undefined) {
32-
let Ajv, addFormats, addFormats2019
33-
try {
34-
[Ajv, addFormats, addFormats2019] = await Promise.all([
35-
import('ajv').then((m) => m.default),
36-
import('ajv-formats').then((m) => m.default),
37-
/* @ts-expect-error TS7016 */
38-
import('ajv-formats-draft2019')
39-
])
40-
} catch {
41-
return reject(new Error('No JSON validator available. Please install all of the optional libraries: ajv, ajv-formats, ajv-formats-draft2019'));
42-
}
43-
44-
const ajv = new Ajv({
45-
useDefaults: true,
46-
formats: { string: true },
47-
strict: false,
48-
strictSchema: false,
49-
addUsedSchema: false,
50-
schemas: {
51-
'http://cyclonedx.org/schema/spdx.SNAPSHOT.schema.json': JSON.parse(readFileSync(FILES.SPDX.JSON_SCHEMA, 'utf-8')),
52-
'http://cyclonedx.org/schema/jsf-0.82.SNAPSHOT.schema.json': JSON.parse(readFileSync(FILES.JSF.JSON_SCHEMA, 'utf-8'))
53-
}
54-
})
55-
addFormats(ajv)
56-
addFormats2019(ajv)
57-
_ajv = ajv
30+
if (_ajv === undefined) {
31+
let Ajv, addFormats, addFormats2019
32+
try {
33+
[Ajv, addFormats, addFormats2019] = await Promise.all([
34+
import('ajv').then((m) => m.default),
35+
import('ajv-formats').then((m) => m.default),
36+
/* @ts-expect-error TS7016 */
37+
import('ajv-formats-draft2019')
38+
])
39+
} catch {
40+
throw new Error('No JSON validator available. Please install all of the optional libraries: ajv, ajv-formats, ajv-formats-draft2019')
5841
}
59-
return resolve(_ajv)
60-
})
42+
43+
const ajv = new Ajv({
44+
useDefaults: true,
45+
formats: { string: true },
46+
strict: false,
47+
strictSchema: false,
48+
addUsedSchema: false,
49+
schemas: {
50+
'http://cyclonedx.org/schema/spdx.SNAPSHOT.schema.json': JSON.parse(readFileSync(FILES.SPDX.JSON_SCHEMA, 'utf-8')),
51+
'http://cyclonedx.org/schema/jsf-0.82.SNAPSHOT.schema.json': JSON.parse(readFileSync(FILES.JSF.JSON_SCHEMA, 'utf-8'))
52+
}
53+
})
54+
addFormats(ajv)
55+
addFormats2019(ajv)
56+
_ajv = ajv
57+
}
58+
return _ajv
6159
}
6260

6361
abstract class BaseJsonValidator extends BaseValidator {
@@ -67,17 +65,15 @@ abstract class BaseJsonValidator extends BaseValidator {
6765
/**
6866
* Promise rejects with {@link Validation.ValidationError | ValidationError}
6967
*/
70-
validate (data: any): Promise<void> {
71-
return new Promise<void>(async (resolve, reject) => {
72-
const file = this._getSchemaFiles()
73-
if (file === undefined) {
74-
return reject(new ValidationError(`not implemented for version: ${this.version}`));
75-
}
76-
const validator = (await getAjv()).compile(JSON.parse(readFileSync(file, 'utf-8')))
77-
return validator(data)
78-
? resolve()
79-
: reject(new ValidationError(`invalid to CycloneDX ${this.version}`, validator.errors))
80-
})
68+
async validate (data: any): Promise<void> {
69+
const file = this._getSchemaFiles()
70+
if (file === undefined) {
71+
throw new ValidationError(`not implemented for version: ${this.version}`)
72+
}
73+
const validator = (await getAjv()).compile(JSON.parse(readFileSync(file, 'utf-8')))
74+
if (!validator(data)) {
75+
throw new ValidationError(`invalid to CycloneDX ${this.version}`, validator.errors)
76+
}
8177
}
8278
}
8379

0 commit comments

Comments
 (0)