Skip to content

Commit c9ec376

Browse files
authored
Merge pull request #135 from mcode/config-validator
Adding config file validator
2 parents d5e3013 + 058d331 commit c9ec376

File tree

5 files changed

+54
-12
lines changed

5 files changed

+54
-12
lines changed

src/application/app.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const { sendEmailNotification, zipErrors } = require('./tools/emailNotifications
77
const { extractDataForPatients } = require('./tools/mcodeExtraction');
88
const { maskMRN } = require('../helpers/patientUtils');
99
const { parsePatientIds } = require('../helpers/appUtils');
10+
const { validateConfig } = require('../helpers/configValidator');
1011

1112
function getConfig(pathToConfig) {
1213
// Checks pathToConfig points to valid JSON file
@@ -20,7 +21,7 @@ function getConfig(pathToConfig) {
2021

2122
function checkInputAndConfig(config, fromDate, toDate) {
2223
// Check input args and needed config variables based on client being used
23-
const { patientIdCsvPath } = config;
24+
validateConfig(config);
2425

2526
// Check if `fromDate` is a valid date
2627
if (fromDate && !moment(fromDate).isValid()) {
@@ -31,11 +32,6 @@ function checkInputAndConfig(config, fromDate, toDate) {
3132
if (toDate && !moment(toDate).isValid()) {
3233
throw new Error('-t/--to-date is not a valid date.');
3334
}
34-
35-
// Check if there is a path to the MRN CSV within our config JSON
36-
if (!patientIdCsvPath) {
37-
throw new Error('patientIdCsvPath is required in config file');
38-
}
3935
}
4036

4137
async function mcodeApp(Client, fromDate, toDate, pathToConfig, pathToRunLogs, debug, allEntries) {

src/helpers/configValidator.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const Ajv = require('ajv');
2+
const metaSchema = require('ajv/lib/refs/json-schema-draft-06.json');
3+
const logger = require('./logger');
4+
const configSchema = require('./schemas/config.schema.json');
5+
6+
const ajv = new Ajv({ logger: false, allErrors: true });
7+
ajv.addMetaSchema(metaSchema);
8+
const validator = ajv.addSchema(configSchema, 'config');
9+
10+
function validateConfig(config) {
11+
logger.debug('Validating config file');
12+
const valid = validator.validate('config', config);
13+
const errors = ajv.errorsText(validator.errors, { dataVar: 'config' });
14+
if (!valid) throw new Error(`Error(s) found in config file: ${errors}`);
15+
logger.debug('Config file validated successfully');
16+
}
17+
18+
module.exports = {
19+
validateConfig,
20+
};

src/helpers/schemas/config.schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$id": "csv-config",
3-
"$schema": "https://json-schema.org/draft/2020-12/schema",
3+
"$schema": "http://json-schema.org/draft-06/schema#",
44
"description": "Schema for mcode-extraction-framework config files",
55
"type": "object",
66
"properties": {

test/application/app.test.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,19 @@ describe('App Tests', () => {
2020
});
2121

2222
describe('checkInputAndConfig', () => {
23+
const config = { patientIdCsvPath: '', extractors: [] };
2324
it('should throw error when fromDate is invalid.', () => {
24-
expect(() => checkInputAndConfig(testConfig, '2020-06-31')).toThrowError('-f/--from-date is not a valid date.');
25+
expect(() => checkInputAndConfig(config, '2020-06-31')).toThrowError('-f/--from-date is not a valid date.');
2526
});
2627
it('should throw error when toDate is invalid date.', () => {
27-
expect(() => checkInputAndConfig(testConfig, '2020-06-30', '2020-06-31')).toThrowError('-t/--to-date is not a valid date.');
28+
expect(() => checkInputAndConfig(config, '2020-06-30', '2020-06-31')).toThrowError('-t/--to-date is not a valid date.');
2829
});
29-
it('should throw error when patientIdCsvPath not provided in config', () => {
30-
expect(() => checkInputAndConfig({})).toThrowError('patientIdCsvPath is required in config file');
30+
it('should throw error when config is not valid', () => {
31+
expect(() => checkInputAndConfig({}))
32+
.toThrowError('Error(s) found in config file: config should have required property \'patientIdCsvPath\', config should have required property \'extractors\'');
3133
});
3234
it('should not throw error when all args are valid', () => {
33-
expect(() => checkInputAndConfig(testConfig, '2020-06-01', '2020-06-30')).not.toThrowError();
35+
expect(() => checkInputAndConfig(config, '2020-06-01', '2020-06-30')).not.toThrowError();
3436
});
3537
});
3638
});

test/helpers/configValidator.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const { validateConfig } = require('../../src/helpers/configValidator.js');
2+
3+
describe('validateConfig', () => {
4+
const missingPropertyConfig = { patientIdCsvPath: '' };
5+
const wrongTypeConfig = { patientIdCsvPath: '', extractors: 12 };
6+
const wrongFormatConfig = { patientIdCsvPath: '', extractors: [], commonExtractorArgs: { baseFhirUrl: 'wrong' } };
7+
const validConfig = { patientIdCsvPath: '', extractors: [] };
8+
9+
test('Should throw error when config file is missing required property', () => {
10+
expect(() => validateConfig(missingPropertyConfig)).toThrowError('Error(s) found in config file: config should have required property \'extractors\'');
11+
});
12+
13+
test('Should throw error when property is of incorrect type', () => {
14+
expect(() => validateConfig(wrongTypeConfig)).toThrowError('Error(s) found in config file: config.extractors should be array');
15+
});
16+
17+
test('Should throw error when property has incorrect format', () => {
18+
expect(() => validateConfig(wrongFormatConfig)).toThrowError('Error(s) found in config file: config.commonExtractorArgs.baseFhirUrl should match format "uri"');
19+
});
20+
21+
test('Should not throw error when config file is valid', () => {
22+
expect(() => validateConfig(validConfig)).not.toThrow();
23+
});
24+
});

0 commit comments

Comments
 (0)