Strings accept numbers too

This commit is contained in:
Ben Ogle
2014-09-25 17:24:58 -07:00
parent 2c1fa19e27
commit ef19e925e9
2 changed files with 21 additions and 8 deletions

View File

@@ -671,12 +671,21 @@ describe "Config", ->
atom.config.set('foo.bar.aString', 'yep')
expect(atom.config.get('foo.bar.aString')).toBe 'yep'
it 'will not set non-strings', ->
expect(atom.config.set('foo.bar.aString', null)).toBe false
expect(atom.config.get('foo.bar.aString')).toBe 'ok'
it 'will only set strings, numbers and booleans', ->
expect(atom.config.set('foo.bar.aString', 123)).toBe true
expect(atom.config.get('foo.bar.aString')).toBe '123'
expect(atom.config.set('foo.bar.aString', 123)).toBe false
expect(atom.config.get('foo.bar.aString')).toBe 'ok'
expect(atom.config.set('foo.bar.aString', true)).toBe false
expect(atom.config.get('foo.bar.aString')).toBe '123'
expect(atom.config.set('foo.bar.aString', null)).toBe false
expect(atom.config.get('foo.bar.aString')).toBe '123'
expect(atom.config.set('foo.bar.aString', [])).toBe false
expect(atom.config.get('foo.bar.aString')).toBe '123'
expect(atom.config.set('foo.bar.aString', nope: 'nope')).toBe false
expect(atom.config.get('foo.bar.aString')).toBe '123'
describe 'when the value has an "object" type', ->
beforeEach ->

View File

@@ -390,7 +390,8 @@ class Config
# This value is stored in Atom's internal configuration file.
#
# * `keyPath` The {String} name of the key.
# * `value` The value of the setting.
# * `value` The value of the setting. Passing `undefined` will revert the
# setting to the default value.
#
# Returns a {Boolean}
# * `true` if the value was set.
@@ -672,8 +673,11 @@ Config.addSchemaValidators
'string':
coercion: (keyPath, value, schema) ->
throw new Error("Validation failed at #{keyPath}, #{JSON.stringify(value)} must be a string") if typeof value isnt 'string'
value
switch typeof value
when 'number', 'string'
value.toString()
else
throw new Error("Validation failed at #{keyPath}, #{JSON.stringify(value)} must be a string or number")
'null':
# null sort of isnt supported. It will just unset in this case