mirror of
https://github.com/atom/atom.git
synced 2026-01-22 21:38:10 -05:00
Add maximumLength schema enforcer for strings
This commit is contained in:
@@ -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 =
|
||||
|
||||
@@ -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) ->
|
||||
|
||||
Reference in New Issue
Block a user