config validation

This commit is contained in:
Radek Stepan 2013-09-27 10:58:43 +01:00
parent 2769a83035
commit 48bb11c3f7
1 changed files with 18 additions and 0 deletions

View File

@ -18,6 +18,19 @@ defaults =
# Making NSA (err taxpayer) work for it. # Making NSA (err taxpayer) work for it.
'protocol': 'https' 'protocol': 'https'
# Validators of config fields.
validators =
'host': (value) ->
_.isString value
'protocol': (value) ->
_.isString(value) and value.match /^http(s?)$/
'token': (value) ->
_.isString value
'off_days': (value) ->
return no unless _.isArray value
( return no for day in value when day not in [ 1..7 ] )
yes
# Get (& cache) configuration from the server. # Get (& cache) configuration from the server.
module.exports = (cb) -> module.exports = (cb) ->
# Have config? # Have config?
@ -42,6 +55,11 @@ module.exports = (cb) ->
else else
config.size_label = regex.size_label config.size_label = regex.size_label
# Validate it.
for field, validator of validators when config[field]
unless validator config[field]
return cb "Config field `#{field}` misconfigured"
# Call back for each enqueued. # Call back for each enqueued.
_.each queue, (cb) -> _.each queue, (cb) ->
cb null, config cb null, config