String type must be strict.

It makes sense to coerce from more general -> more specific data types.
eg. string -> int, etc. But coercing the other way is problematic
in the case of chaining because the more general type will swallow the
specific type. eg. Setting `false` on type: [‘string’, ‘boolean’] will 
coerce the boolean to a string, and will never allow the value to be
a boolean.
This commit is contained in:
Ben Ogle 2014-09-29 15:50:54 -07:00
parent 9808264b7f
commit b54deccfae
2 changed files with 11 additions and 13 deletions

View File

@ -701,21 +701,21 @@ describe "Config", ->
atom.config.set('foo.bar.aString', 'yep')
expect(atom.config.get('foo.bar.aString')).toBe 'yep'
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'
it 'will only set strings', ->
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.get('foo.bar.aString')).toBe 'ok'
expect(atom.config.set('foo.bar.aString', null)).toBe false
expect(atom.config.get('foo.bar.aString')).toBe '123'
expect(atom.config.get('foo.bar.aString')).toBe 'ok'
expect(atom.config.set('foo.bar.aString', [])).toBe false
expect(atom.config.get('foo.bar.aString')).toBe '123'
expect(atom.config.get('foo.bar.aString')).toBe 'ok'
expect(atom.config.set('foo.bar.aString', nope: 'nope')).toBe false
expect(atom.config.get('foo.bar.aString')).toBe '123'
expect(atom.config.get('foo.bar.aString')).toBe 'ok'
describe 'when the value has an "object" type', ->
beforeEach ->

View File

@ -667,12 +667,10 @@ Config.addSchemaEnforcers
throw new Error("Validation failed at #{keyPath}, #{JSON.stringify(value)} must be a boolean or the string 'true' or 'false'")
'string':
coerce: (keyPath, value, schema) ->
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")
validate: (keyPath, value, schema) ->
unless typeof value is 'string'
throw new Error("Validation failed at #{keyPath}, #{JSON.stringify(value)} must be a string")
value
'null':
# null sort of isnt supported. It will just unset in this case