This repository has been archived by the owner on Feb 19, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 97f6cd7
Showing
9 changed files
with
2,197 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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') | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.