11import { readJsonSync } from "fs-extra" ;
22import { resolve } from "path" ;
3- import Ajv from "ajv" ;
3+ import Ajv , { ValidateFunction } from "ajv" ;
44import { clearLocalDbConfig , DbConfig } from "./db-config" ;
55import { findDuplicateStrings } from "../../pure/text-utils" ;
66import {
@@ -9,22 +9,22 @@ import {
99} from "../db-validation-errors" ;
1010
1111export class DbConfigValidator {
12- private readonly schema : any ;
12+ private readonly validateSchemaFn : ValidateFunction ;
1313
1414 constructor ( extensionPath : string ) {
1515 const schemaPath = resolve ( extensionPath , "databases-schema.json" ) ;
16- this . schema = readJsonSync ( schemaPath ) ;
16+ const schema = readJsonSync ( schemaPath ) ;
17+ const schemaValidator = new Ajv ( { allErrors : true } ) ;
18+ this . validateSchemaFn = schemaValidator . compile ( schema ) ;
1719 }
1820
1921 public validate ( dbConfig : DbConfig ) : DbConfigValidationError [ ] {
20- const ajv = new Ajv ( { allErrors : true } ) ;
21-
2222 const localDbs = clearLocalDbConfig ( dbConfig ) ;
2323
24- ajv . validate ( this . schema , dbConfig ) ;
24+ this . validateSchemaFn ( dbConfig ) ;
2525
26- if ( ajv . errors ) {
27- return ajv . errors . map ( ( error ) => ( {
26+ if ( this . validateSchemaFn . errors ) {
27+ return this . validateSchemaFn . errors . map ( ( error ) => ( {
2828 kind : DbConfigValidationErrorKind . InvalidConfig ,
2929 message : `${ error . instancePath } ${ error . message } ` ,
3030 } ) ) ;
0 commit comments