Handle validation of schema types

This commit is contained in:
Ben Ogle
2014-09-23 16:52:32 -07:00
parent f909d32826
commit 1a8c5ba551
2 changed files with 23 additions and 8 deletions

View File

@@ -505,6 +505,9 @@ describe "Config", ->
atom.config.set('foo.bar.anInt', null)
expect(atom.config.get('foo.bar.anInt')).toBe 12
atom.config.set('foo.bar.anInt', 'nope')
expect(atom.config.get('foo.bar.anInt')).toBe 12
describe 'when the value has an "integer" and "string" type', ->
beforeEach ->
schema =
@@ -567,6 +570,9 @@ describe "Config", ->
expect(atom.config.get('foo.bar.aString')).toBe 'yep'
it 'will not set non-strings', ->
atom.config.set('foo.bar.aString', null)
expect(atom.config.set('foo.bar.aString', null)).toBe false
expect(atom.config.get('foo.bar.aString')).toBe 'ok'
expect(atom.config.set('foo.bar.aString', 123)).toBe false
expect(atom.config.get('foo.bar.aString')).toBe 'ok'

View File

@@ -39,6 +39,7 @@ class Config
@addTypeFilter(typeName, filterFunction)
@executeTypeFilters: (value, schema) ->
failedValidation = false
types = schema.type
types = [types] unless Array.isArray(types)
for type in types
@@ -46,9 +47,12 @@ class Config
if filterFunctions = @typeFilters[type]
for filter in filterFunctions
value = filter.call(this, value, schema)
failedValidation = false
break
catch e
;
failedValidation = true
throw new Error('value is not valid') if failedValidation
value
# Created during initialization, available as `atom.config`
@@ -145,15 +149,19 @@ class Config
# * `keyPath` The {String} name of the key.
# * `value` The value of the setting.
#
# Returns the `value`.
# Returns a {Boolean} true if the value was set.
set: (keyPath, value) ->
value = @scrubValue(keyPath, value)
try
value = @scrubValue(keyPath, value)
catch e
return false
if @get(keyPath) isnt value
defaultValue = _.valueForKeyPath(@defaultSettings, keyPath)
value = undefined if _.isEqual(defaultValue, value)
_.setValueForKeyPath(@settings, keyPath, value)
@update()
value
true
# Extended: Get the {String} path to the config file being used.
getUserConfigPath: ->
@@ -407,9 +415,10 @@ Config.addTypeFilters
else
!!value
'string': (value, schema) ->
throw new Error unless typeof value is 'string'
value
'string':
typeCheck: (value, schema) ->
throw new Error() if typeof value isnt 'string'
value
'null':
# null sort of isnt supported. It will just unset in this case