1
1
import { readJsonSync } from "fs-extra" ;
2
2
import { resolve } from "path" ;
3
- import Ajv from "ajv" ;
3
+ import Ajv , { ValidateFunction } from "ajv" ;
4
4
import { clearLocalDbConfig , DbConfig } from "./db-config" ;
5
5
import { findDuplicateStrings } from "../../pure/text-utils" ;
6
6
import {
@@ -9,22 +9,22 @@ import {
9
9
} from "../db-validation-errors" ;
10
10
11
11
export class DbConfigValidator {
12
- private readonly schema : any ;
12
+ private readonly validateSchemaFn : ValidateFunction ;
13
13
14
14
constructor ( extensionPath : string ) {
15
15
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 ) ;
17
19
}
18
20
19
21
public validate ( dbConfig : DbConfig ) : DbConfigValidationError [ ] {
20
- const ajv = new Ajv ( { allErrors : true } ) ;
21
-
22
22
const localDbs = clearLocalDbConfig ( dbConfig ) ;
23
23
24
- ajv . validate ( this . schema , dbConfig ) ;
24
+ this . validateSchemaFn ( dbConfig ) ;
25
25
26
- if ( ajv . errors ) {
27
- return ajv . errors . map ( ( error ) => ( {
26
+ if ( this . validateSchemaFn . errors ) {
27
+ return this . validateSchemaFn . errors . map ( ( error ) => ( {
28
28
kind : DbConfigValidationErrorKind . InvalidConfig ,
29
29
message : `${ error . instancePath } ${ error . message } ` ,
30
30
} ) ) ;
0 commit comments