Skip to content

Commit 70a6d18

Browse files
authored
Merge pull request #595 from ZIMkaRU/feature/rework-config-keeper-to-validate-and-keep-strict-data-structure
Rework config keeper to validate and keep strict data structure
2 parents b73d339 + 614ab30 commit 70a6d18

27 files changed

Lines changed: 828 additions & 313 deletions

package-lock.json

Lines changed: 214 additions & 57 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@
1010
},
1111
"license": "Apache-2.0",
1212
"dependencies": {
13+
"ajv": "8.17.1",
14+
"ajv-formats": "3.0.1",
1315
"archiver": "7.0.1",
1416
"bfx-svc-test-helper": "git+https://github.com/bitfinexcom/bfx-svc-test-helper.git",
1517
"bittorrent-dht": "10.0.2",
1618
"changelog-parser": "3.0.1",
1719
"clean-stack": "6.0.0",
18-
"cron-validate": "1.4.3",
20+
"compare-versions": "6.1.1",
21+
"cron-validate": "1.5.3",
1922
"electron-alert": "0.1.20",
2023
"electron-log": "4.4.8",
2124
"electron-updater": "6.7.0",

server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ process.send = process.send || (() => {})
2626
process.env.NODE_CONFIG_DIR = pathToExpressConfDir
2727

2828
const env = { ...process.env }
29-
const serializeError = require('./src/helpers/utils')
29+
const { serializeError } = require('./src/helpers/utils')
3030

3131
const {
3232
WrongPathToUserDataError,

src/change-reports-folder.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ module.exports = ({ pathToUserDocuments }) => {
5555
}
5656

5757
await pauseApp()
58-
const isSaved = await getConfigsKeeperByName('main')
58+
const isSaved = await getConfigsKeeperByName()
5959
.saveConfigs({
6060
reportFilesPathVersion: REPORT_FILES_PATH_VERSION,
6161
pathToUserReportFiles: filePaths[0]

src/change-sync-frequency.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
const cronValidate = require('cron-validate')
4+
const cron = cronValidate.default ?? cronValidate
45
const i18next = require('i18next')
56

67
const { createModalWindow } = require('./window-creators')
@@ -39,7 +40,7 @@ const _getTime = (timeFormat, time) => {
3940
}
4041

4142
const _getTimeDataFromRule = (rule) => {
42-
const cronResult = cronValidate(rule)
43+
const cronResult = cron(rule)
4344

4445
if (!cronResult.isValid()) {
4546
return { timeFormat: 'hours', timeValue: 2 }
@@ -95,7 +96,7 @@ const _fireAlert = async (params) => {
9596
}
9697

9798
module.exports = () => {
98-
const configsKeeper = getConfigsKeeperByName('main')
99+
const configsKeeper = getConfigsKeeperByName()
99100

100101
const getAlertOpts = (timeFormat, timeData) => {
101102
const timeFormatMap = {

src/configs-keeper.js

Lines changed: 0 additions & 171 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict'
2+
3+
const SCHEMA_DOMAIN = require('../schema.domain')
4+
5+
const getSchemaIds = (schemaNames, schemaDomain) => {
6+
const domain = schemaDomain ?? SCHEMA_DOMAIN
7+
const names = Object.entries(schemaNames ?? {})
8+
9+
const schemaIds = {}
10+
11+
for (const [key, name] of names) {
12+
schemaIds[key] = `${domain}/${name}`
13+
}
14+
15+
return schemaIds
16+
}
17+
18+
module.exports = {
19+
getSchemaIds
20+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
'use strict'
2+
3+
const path = require('node:path')
4+
const Ajv = require('ajv')
5+
const addFormats = require('ajv-formats')
6+
const cronValidate = require('cron-validate')
7+
const cron = cronValidate.default ?? cronValidate
8+
const { validateStrict } = require('compare-versions')
9+
10+
const isDevEnv = process.env.NODE_ENV === 'development'
11+
12+
const SCHEMA_IDS = require('./schema.ids')
13+
const schemas = require('./schemas')
14+
const {
15+
DataValidationSchemaDefError,
16+
DataValidationError
17+
} = require('../../errors')
18+
19+
const ajv = new Ajv({
20+
// Compile schema on initialization
21+
schemas,
22+
23+
// Strict mode
24+
strict: true,
25+
strictRequired: true,
26+
allowMatchingProperties: true,
27+
allowUnionTypes: true,
28+
29+
coerceTypes: true,
30+
useDefaults: 'empty',
31+
removeAdditional: true,
32+
$data: true,
33+
ownProperties: true,
34+
allErrors: true,
35+
messages: true,
36+
formats: { reserved: true },
37+
verbose: isDevEnv
38+
})
39+
addFormats(ajv)
40+
ajv.addFormat('abs-path', {
41+
type: 'string',
42+
validate: (val) => path.isAbsolute(val)
43+
})
44+
ajv.addFormat('cron-expression', {
45+
type: 'string',
46+
validate: (val) => cron(val).isValid()
47+
})
48+
ajv.addFormat('semver', {
49+
type: 'string',
50+
validate: (val) => validateStrict(val)
51+
})
52+
53+
const validate = (configs, schemaId) => {
54+
const validate = ajv.getSchema(schemaId)
55+
56+
if (typeof validate !== 'function') {
57+
console.error(new DataValidationSchemaDefError())
58+
59+
return false
60+
}
61+
62+
const res = validate(configs)
63+
64+
if (validate.errors) {
65+
console.debug(new DataValidationError({
66+
message: `ERR_DATA_IS_INVALID_WITH_ID: ${schemaId}`,
67+
data: validate.errors
68+
}))
69+
}
70+
71+
return res
72+
}
73+
74+
module.exports = {
75+
SCHEMA_IDS,
76+
validate
77+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict'
2+
3+
module.exports = 'report.bitfinex.com'
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
'use strict'
2+
3+
const SCHEMA_NAMES = require('./schema.names')
4+
const { getSchemaIds } = require('./helpers')
5+
6+
module.exports = getSchemaIds(SCHEMA_NAMES)

0 commit comments

Comments
 (0)