@@ -2,6 +2,11 @@ import { readJsonSync } from "fs-extra";
22import { resolve } from "path" ;
33import Ajv from "ajv" ;
44import { DbConfig } from "./db-config" ;
5+ import { findDuplicateStrings } from "../../text-utils" ;
6+ import {
7+ DbConfigValidationError ,
8+ DbConfigValidationErrorKind ,
9+ } from "../db-validation-errors" ;
510
611export class DbConfigValidator {
712 private readonly schema : any ;
@@ -14,16 +19,118 @@ export class DbConfigValidator {
1419 this . schema = readJsonSync ( schemaPath ) ;
1520 }
1621
17- public validate ( dbConfig : DbConfig ) : string [ ] {
22+ public validate ( dbConfig : DbConfig ) : DbConfigValidationError [ ] {
1823 const ajv = new Ajv ( { allErrors : true } ) ;
1924 ajv . validate ( this . schema , dbConfig ) ;
2025
2126 if ( ajv . errors ) {
22- return ajv . errors . map (
23- ( error ) => `${ error . instancePath } ${ error . message } ` ,
24- ) ;
27+ return ajv . errors . map ( ( error ) => ( {
28+ kind : DbConfigValidationErrorKind . InvalidConfig ,
29+ message : `${ error . instancePath } ${ error . message } ` ,
30+ } ) ) ;
2531 }
2632
27- return [ ] ;
33+ return [
34+ ...this . validateDbListNames ( dbConfig ) ,
35+ ...this . validateDbNames ( dbConfig ) ,
36+ ...this . validateDbNamesInLists ( dbConfig ) ,
37+ ...this . validateOwners ( dbConfig ) ,
38+ ] ;
39+ }
40+
41+ private validateDbListNames ( dbConfig : DbConfig ) : DbConfigValidationError [ ] {
42+ const errors : DbConfigValidationError [ ] = [ ] ;
43+
44+ const buildError = ( dups : string [ ] ) => ( {
45+ kind : DbConfigValidationErrorKind . DuplicateNames ,
46+ message : `There are database lists with the same name: ${ dups . join (
47+ ", " ,
48+ ) } `,
49+ } ) ;
50+
51+ const duplicateLocalDbLists = findDuplicateStrings (
52+ dbConfig . databases . local . lists . map ( ( n ) => n . name ) ,
53+ ) ;
54+
55+ if ( duplicateLocalDbLists . length > 0 ) {
56+ errors . push ( buildError ( duplicateLocalDbLists ) ) ;
57+ }
58+
59+ const duplicateRemoteDbLists = findDuplicateStrings (
60+ dbConfig . databases . remote . repositoryLists . map ( ( n ) => n . name ) ,
61+ ) ;
62+ if ( duplicateRemoteDbLists . length > 0 ) {
63+ errors . push ( buildError ( duplicateRemoteDbLists ) ) ;
64+ }
65+
66+ return errors ;
67+ }
68+
69+ private validateDbNames ( dbConfig : DbConfig ) : DbConfigValidationError [ ] {
70+ const errors : DbConfigValidationError [ ] = [ ] ;
71+
72+ const buildError = ( dups : string [ ] ) => ( {
73+ kind : DbConfigValidationErrorKind . DuplicateNames ,
74+ message : `There are databases with the same name: ${ dups . join ( ", " ) } ` ,
75+ } ) ;
76+
77+ const duplicateLocalDbs = findDuplicateStrings (
78+ dbConfig . databases . local . databases . map ( ( d ) => d . name ) ,
79+ ) ;
80+
81+ if ( duplicateLocalDbs . length > 0 ) {
82+ errors . push ( buildError ( duplicateLocalDbs ) ) ;
83+ }
84+
85+ const duplicateRemoteDbs = findDuplicateStrings (
86+ dbConfig . databases . remote . repositories ,
87+ ) ;
88+ if ( duplicateRemoteDbs . length > 0 ) {
89+ errors . push ( buildError ( duplicateRemoteDbs ) ) ;
90+ }
91+
92+ return errors ;
93+ }
94+
95+ private validateDbNamesInLists (
96+ dbConfig : DbConfig ,
97+ ) : DbConfigValidationError [ ] {
98+ const errors : DbConfigValidationError [ ] = [ ] ;
99+
100+ const buildError = ( listName : string , dups : string [ ] ) => ( {
101+ kind : DbConfigValidationErrorKind . DuplicateNames ,
102+ message : `There are databases with the same name in the ${ listName } list: ${ dups . join (
103+ ", " ,
104+ ) } `,
105+ } ) ;
106+
107+ for ( const list of dbConfig . databases . local . lists ) {
108+ const dups = findDuplicateStrings ( list . databases . map ( ( d ) => d . name ) ) ;
109+ if ( dups . length > 0 ) {
110+ errors . push ( buildError ( list . name , dups ) ) ;
111+ }
112+ }
113+
114+ for ( const list of dbConfig . databases . remote . repositoryLists ) {
115+ const dups = findDuplicateStrings ( list . repositories ) ;
116+ if ( dups . length > 0 ) {
117+ errors . push ( buildError ( list . name , dups ) ) ;
118+ }
119+ }
120+
121+ return errors ;
122+ }
123+
124+ private validateOwners ( dbConfig : DbConfig ) : DbConfigValidationError [ ] {
125+ const errors : DbConfigValidationError [ ] = [ ] ;
126+
127+ const dups = findDuplicateStrings ( dbConfig . databases . remote . owners ) ;
128+ if ( dups . length > 0 ) {
129+ errors . push ( {
130+ kind : DbConfigValidationErrorKind . DuplicateNames ,
131+ message : `There are owners with the same name: ${ dups . join ( ", " ) } ` ,
132+ } ) ;
133+ }
134+ return errors ;
28135 }
29136}
0 commit comments