@@ -149,10 +149,19 @@ async function validateArchitectureAgainstPattern(architecture: object, pattern:
149149
150150 const patternResolved = applyArchitectureOptionsToPattern ( architecture , pattern , debug ) ;
151151
152- const jsonSchemaValidator = new JsonSchemaValidator ( schemaDirectory , patternResolved , debug ) ;
153- await jsonSchemaValidator . initialize ( ) ;
154-
155152 let jsonSchemaValidations = [ ] ;
153+ let jsonSchemaValidator : JsonSchemaValidator ;
154+ try {
155+ jsonSchemaValidator = new JsonSchemaValidator ( schemaDirectory , patternResolved , debug ) ;
156+ await jsonSchemaValidator . initialize ( ) ;
157+ } catch ( error ) {
158+ const errorMessage = toErrorMessage ( error ) ;
159+ logger . error ( `JSON Schema compilation failed: ${ errorMessage } ` ) ;
160+ jsonSchemaValidations = [
161+ new ValidationOutput ( 'json-schema' , 'error' , errorMessage , '/' )
162+ ] ;
163+ return new ValidationOutcome ( jsonSchemaValidations , spectralResult . spectralIssues , true , warnings ) ;
164+ }
156165
157166 const schemaErrors = jsonSchemaValidator . validate ( architecture ) ;
158167 if ( schemaErrors . length > 0 ) {
@@ -184,10 +193,11 @@ async function validatePatternOnly(pattern: object, schemaDirectory: SchemaDirec
184193
185194 try {
186195 // Compile pattern as a schema to check if it's valid
187- new JsonSchemaValidator ( schemaDirectory , pattern , debug ) ;
196+ const jsonSchemaValidator = new JsonSchemaValidator ( schemaDirectory , pattern , debug ) ;
197+ await jsonSchemaValidator . initialize ( ) ;
188198 } catch ( error ) {
189199 errors = true ;
190- jsonSchemaErrors . push ( new ValidationOutput ( 'json-schema' , 'error' , error . message , '/' ) ) ;
200+ jsonSchemaErrors . push ( new ValidationOutput ( 'json-schema' , 'error' , toErrorMessage ( error ) , '/' ) ) ;
191201 }
192202
193203 return new ValidationOutcome ( jsonSchemaErrors , spectralValidationResults . spectralIssues , errors , warnings ) ; // added spectral to return object
@@ -294,6 +304,20 @@ function prettifyJson(json) {
294304 return JSON . stringify ( json , null , 4 ) ;
295305}
296306
307+ function toErrorMessage ( error : unknown ) : string {
308+ if ( error instanceof Error ) {
309+ return error . message ;
310+ }
311+ if ( typeof error === 'string' ) {
312+ return error ;
313+ }
314+ try {
315+ return JSON . stringify ( error ) ;
316+ } catch {
317+ return 'Unknown error' ;
318+ }
319+ }
320+
297321export function stripRefs ( obj : object ) : string {
298322 return JSON . stringify ( obj ) . replaceAll ( '$ref' , 'ref' ) ;
299323}
@@ -364,4 +388,4 @@ function mergeSpectralResults(spectralResultPattern: SpectralResult, spectralRes
364388 const warnings : boolean = spectralResultPattern . warnings || spectralResultArchitecture . warnings ;
365389 const spectralValidations = spectralResultPattern . spectralIssues . concat ( spectralResultArchitecture . spectralIssues ) ;
366390 return new SpectralResult ( warnings , errors , spectralValidations ) ;
367- }
391+ }
0 commit comments