import * as ajv from 'ajv'; import addFormats from 'ajv-formats'; import axios from 'axios'; import { InvalidCredentialSchemaIdException } from '../exceptions/invalidCredentialSchemaIdException.js'; export default class JsonSchemaWrapper { private readonly ajv: ajv.Ajv; constructor() { const Ajv2020 = ajv.Ajv; this.ajv = new Ajv2020({ allErrors: true, loadSchema: this.loadSchema(), }); addFormats.default(this.ajv); } public validateJson(data: object, schema: object): boolean { const validate = this.ajv.compile(schema); return validate(data); } private loadSchema = () => { return async (url: string): Promise => { try { const response = await axios.get(url); return response.data; } catch (e) { throw new InvalidCredentialSchemaIdException(url); } }; }; }