Add maximumLength schema enforcer for strings

This commit is contained in:
Nathan Sobo
2015-05-29 21:54:29 +02:00
parent b68902dd83
commit e723b26eb0
2 changed files with 16 additions and 0 deletions

View File

@@ -1380,6 +1380,16 @@ describe "Config", ->
expect(atom.config.set('foo.bar.aString', nope: 'nope')).toBe false
expect(atom.config.get('foo.bar.aString')).toBe 'ok'
describe 'when the schema has a "maximumLength" key', ->
it "trims the string to be no longer than the specified maximum", ->
schema =
type: 'string'
default: 'ok'
maximumLength: 3
atom.config.setSchema('foo.bar.aString', schema)
atom.config.set('foo.bar.aString', 'abcdefg')
expect(atom.config.get('foo.bar.aString')).toBe 'abc'
describe 'when the value has an "object" type', ->
beforeEach ->
schema =

View File

@@ -1060,6 +1060,12 @@ Config.addSchemaEnforcers
throw new Error("Validation failed at #{keyPath}, #{JSON.stringify(value)} must be a string")
value
validateMaximumLength: (keyPath, value, schema) ->
if typeof schema.maximumLength is 'number' and value.length > schema.maximumLength
value.slice(0, schema.maximumLength)
else
value
'null':
# null sort of isnt supported. It will just unset in this case
coerce: (keyPath, value, schema) ->