Skip to content

Commit

Permalink
Merge pull request #650 from dadi/feature/config-validation-3
Browse files Browse the repository at this point in the history
Improve config validation
  • Loading branch information
eduardoboucas authored Dec 17, 2018
2 parents 61b5dc3 + 9600438 commit 245349d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/config-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module.exports = {
apis: {
requireAuthentication: false,
doc: 'Connected APIs',
format: Array,
format: 'apis-block',
default: [],
publishId: {
type: String,
Expand Down Expand Up @@ -94,7 +94,6 @@ module.exports = {
}
},
server: {
requireAuthentication: false,
host: {
doc: 'The IP address or interface to bind to',
format: 'ipaddress',
Expand Down Expand Up @@ -147,6 +146,7 @@ module.exports = {
env: 'SSL_INTERMEDIATE_CERTIFICATE_PATHS'
},
healthcheck: {
requireAuthentication: false,
enabled: {
doc: 'Healthcheck is enabled',
format: Boolean,
Expand Down
23 changes: 23 additions & 0 deletions app/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,29 @@ const initialiseConfig = () => {
return config
}

convict.addFormat({
name: 'apis-block',
validate: value => {
if (!Array.isArray(value)) {
throw new Error('must be an array')
}

if (value.length > 1) {
throw new Error('only one API is currently supported')
}

value.forEach(api => {
if (typeof api.host !== 'string' || typeof api.port !== 'number') {
throw new Error('must contain elements with `host` and `port` properties')
}

if (api.credentials !== undefined) {
throw new Error('contains a legacy `credentials` block, exposing critical information to the public')
}
})
}
})

let config = initialiseConfig()

module.exports = config
Expand Down

0 comments on commit 245349d

Please sign in to comment.