From e723b26eb0dc3a4b7213f4d41b0a709579eab457 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Fri, 29 May 2015 21:54:29 +0200 Subject: [PATCH] Add maximumLength schema enforcer for strings --- spec/config-spec.coffee | 10 ++++++++++ src/config.coffee | 6 ++++++ 2 files changed, 16 insertions(+) diff --git a/spec/config-spec.coffee b/spec/config-spec.coffee index d64ac2d27..b75a121a1 100644 --- a/spec/config-spec.coffee +++ b/spec/config-spec.coffee @@ -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 = diff --git a/src/config.coffee b/src/config.coffee index b97ce99ec..9ab8ce607 100644 --- a/src/config.coffee +++ b/src/config.coffee @@ -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) ->