Skip to content

Commit 458398a

Browse files
committed
feat: add config validation to db:check command
1 parent 9a3ca5a commit 458398a

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

check-db.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
require("dotenv").config();
22
const { Elm } = require("./check-db-app");
3-
const { getProcessesAsString } = require("./lib");
3+
const { getComponentConfigAsString, getProcessesAsString } = require("./lib");
44

55
const elmApp = Elm.CheckDb.init({
66
flags: {
7-
detailedProcesses: getProcessesAsString((detailed = true)),
8-
nonDetailedProcesses: getProcessesAsString((detailed = false)),
7+
detailedProcessesJson: getProcessesAsString((detailed = true)),
8+
nonDetailedProcessesJson: getProcessesAsString((detailed = false)),
9+
componentConfigJson: getComponentConfigAsString(),
910
},
1011
});
1112

lib/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ function setupTracker(spec) {
5555
};
5656
}
5757

58+
function getComponentConfigAsString() {
59+
return fs.readFileSync("public/data/components/config.json", "utf8").toString();
60+
}
61+
5862
function getProcessesAsString(detailed = false) {
5963
return JSON.stringify(
6064
JSON.parse(
@@ -76,6 +80,7 @@ const dataFiles = {
7680

7781
module.exports = {
7882
dataFiles,
83+
getComponentConfigAsString,
7984
getProcessesAsString,
8085
setupTracker,
8186
};

src/CheckDb.elm

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
port module CheckDb exposing (main)
22

33
import Data.Component as Component exposing (Component)
4+
import Data.Component.Config as ComponentConfig
45
import Data.Example exposing (Example)
56
import Data.Process as Process exposing (Process)
67
import Data.Scope as Scope
@@ -12,8 +13,9 @@ import Static.Db as StaticDb exposing (Db)
1213

1314

1415
type alias Flags =
15-
{ detailedProcesses : String
16-
, nonDetailedProcesses : String
16+
{ componentConfigJson : String
17+
, detailedProcessesJson : String
18+
, nonDetailedProcessesJson : String
1719
}
1820

1921

@@ -54,6 +56,16 @@ backtick string =
5456
"`" ++ string ++ "`"
5557

5658

59+
{-| Validates component config JSON against a static database.
60+
-}
61+
checkComponentConfig : Db -> String -> List Error
62+
checkComponentConfig db =
63+
ComponentConfig.parse db
64+
>> Result.mapError List.singleton
65+
>> Result.map (always [])
66+
>> Result.withDefault []
67+
68+
5769
{-| Returns missing component ids referenced by a component item.
5870
-}
5971
checkComponentItemId : Set String -> Component.Item -> List String
@@ -250,10 +262,10 @@ checkProcessId knownProcessStringIds component fieldName processId =
250262
[]
251263

252264

253-
{-| Checks a static database and returns a list of errors.
265+
{-| Checks a static database and config comformity, then returns a list of errors.
254266
-}
255-
checkStaticDatabase : String -> Result String Db -> List Error
256-
checkStaticDatabase dbName dbResult =
267+
checkStaticDatabase : String -> String -> Result String Db -> List Error
268+
checkStaticDatabase config dbName dbResult =
257269
let
258270
section title =
259271
dbName ++ " - " ++ title
@@ -273,16 +285,17 @@ checkStaticDatabase dbName dbResult =
273285
|> addGroupedErrors (section "Examples components checks") (checkExamplesComponentIds knownComponentStringIds db)
274286
|> addGroupedErrors (section "Components processes checks") (checkComponentsProcessIds knownProcessStringIds db)
275287
|> addGroupedErrors (section "Scoping checks") (checkExamplesScope db)
288+
|> addGroupedErrors (section "Component config checks") (checkComponentConfig db config)
276289

277290

278291
{-| Decodes both static databases, executes checks, and returns grouped errors.
279292
-}
280293
checkStaticDatabases : Flags -> Result (List Error) ()
281-
checkStaticDatabases { detailedProcesses, nonDetailedProcesses } =
294+
checkStaticDatabases { componentConfigJson, detailedProcessesJson, nonDetailedProcessesJson } =
282295
case
283-
List.concatMap (\( dbName, dbResult ) -> checkStaticDatabase dbName dbResult)
284-
[ ( "Detailed Db", StaticDb.db detailedProcesses )
285-
, ( "Non-detailed Db", StaticDb.db nonDetailedProcesses )
296+
List.concatMap (\( dbName, dbResult ) -> checkStaticDatabase componentConfigJson dbName dbResult)
297+
[ ( "Detailed Db", StaticDb.db detailedProcessesJson )
298+
, ( "Non-detailed Db", StaticDb.db nonDetailedProcessesJson )
286299
]
287300
of
288301
[] ->

0 commit comments

Comments
 (0)