import Ajv , { Schema , ValidateFunction } from 'ajv' ; import { inject , injectable } from 'inversify' ; import { iocTypes } from '../ioc-types' ; import { BaselineFileContent } from './baseline-types' ; export const baselineSchema = { type : 'object' , properties : { metadata : { type : 'object' , properties : { fileFormatVersion : { const : '1' } , } , required : [ 'fileFormatVersion' ] , additionalProperties : false , } , results : { type : 'array' , items : { type : 'object' , properties : { rule : { type : 'string' } , urls : { type : 'array' , items : { type : 'string' } } , cssSelector : { type : 'string' } , xpathSelector : { type : 'string' } , htmlSnippet : { type : 'string' } , } , required : [ 'rule' , 'cssSelector' , 'htmlSnippet' , 'urls' ] , additionalProperties : false , } , } , } , required : [ 'metadata' , 'results' ] , additionalProperties : false , } ; @ injectable ( ) export class BaselineSchemaValidator { private readonly ajvValidator : ValidateFunction < BaselineFileContent > ; public constructor ( @ inject ( iocTypes . ajv ) private readonly ajvInstance ) { this . ajvValidator = this . ajvInstance . compile ( baselineSchema ) ; } public validate ( unvalidatedObject ) { const isValid = this . ajvValidator ( unvalidatedObject ) ; if ( isValid ) { return unvalidatedObject ; } else { throw new Error ( `Baseline content did not match expected format. Error(s):\n\n${ JSON . stringify ( this . ajvValidator . errors ) }` ) ; } } }