@@ -27,37 +27,35 @@ import { BaseValidator } from './_helpers'
27
27
let _ajv : Ajv | undefined
28
28
29
29
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' )
58
41
}
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
61
59
}
62
60
63
61
abstract class BaseJsonValidator extends BaseValidator {
@@ -67,17 +65,15 @@ abstract class BaseJsonValidator extends BaseValidator {
67
65
/**
68
66
* Promise rejects with {@link Validation.ValidationError | ValidationError}
69
67
*/
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
+ }
81
77
}
82
78
}
83
79
0 commit comments