Skip to content
This repository has been archived by the owner on Feb 19, 2021. It is now read-only.

Commit

Permalink
Get started with config handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bripkens committed Mar 2, 2017
0 parents commit 97f6cd7
Show file tree
Hide file tree
Showing 9 changed files with 2,197 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
@@ -0,0 +1,9 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
charset = utf-8
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
/node_modules
1 change: 1 addition & 0 deletions .nvmrc
@@ -0,0 +1 @@
6
20 changes: 20 additions & 0 deletions lib/config/schema.json
@@ -0,0 +1,20 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "NodeAppConfig",
"type": "object",
"properties": {
"admin": {
"type": "object",
"properties": {
"bindAddress": {
"type": "string"
},
"port": {
"type": "number"
}
},
"required": ["port"]
}
},
"required": ["admin"]
}
21 changes: 21 additions & 0 deletions lib/config/validator.js
@@ -0,0 +1,21 @@
const Ajv = require('ajv');

const ajv = new Ajv();

exports.validate = (schema, config) => {
const validate = ajv.compile(schema);
const valid = validate(config);

let message = '';
if (validate.errors) {
message = validate.errors.reduce((agg, error) => {
agg += `- ${error.message} at path ${error.dataPath || '.'}\n`;
return agg;
}, '');
}

return {
valid,
message
};
};
Empty file added lib/index.js
Empty file.
21 changes: 21 additions & 0 deletions package.json
@@ -0,0 +1,21 @@
{
"name": "node-app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test:unit": "jest",
"format": ""
},
"author": "Ben Ripkens <bripkens@gmail.com> (http://bripkens.de/)",
"license": "MIT",
"dependencies": {
"ajv": "^4.11.3",
"node-report": "^2.1.1"
},
"devDependencies": {
"chai": "^3.5.0",
"jest": "^19.0.2",
"prettier": "^0.20.0"
}
}
35 changes: 35 additions & 0 deletions test/config/validator.test.js
@@ -0,0 +1,35 @@
const {expect} = require('chai');
const Ajv = require('ajv');

const {validate} = require('../../lib/config/validator');
const schema = require('../../lib/config/schema.json');

describe('config/validator', () => {
it('must compile schema successfully', () => {
new Ajv().compile(schema);
});

it('must successfully parse valid config', () => {
const result = validate(schema, {
admin: {
port: 2345
}
});
expect(result.valid).to.equal(true);
expect(result.message).to.equal('')
});

describe('missing properties', () => {
it('must fail when required property admin is not defined', () => {
const result = validate(schema, {});
expect(result.valid).to.equal(false);
expect(result.message).to.contain('- should have required property \'admin\' at path .')
});

it('must fail when required property admin.port is not defined', () => {
const result = validate(schema, {admin: {}});
expect(result.valid).to.equal(false);
expect(result.message).to.contain('- should have required property \'port\' at path .admin')
});
});
});

0 comments on commit 97f6cd7

Please sign in to comment.