From 2526ba0efb2e395b1370e19aead709f3452db73d Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Tue, 23 Sep 2014 17:03:51 -0700 Subject: [PATCH] Add an object filter --- spec/config-spec.coffee | 26 ++++++++++++++++++++++++++ src/config.coffee | 7 +++++++ 2 files changed, 33 insertions(+) diff --git a/spec/config-spec.coffee b/spec/config-spec.coffee index 17b8cfcf8..2d8a63794 100644 --- a/spec/config-spec.coffee +++ b/spec/config-spec.coffee @@ -576,3 +576,29 @@ describe "Config", -> expect(atom.config.set('foo.bar.aString', 123)).toBe false expect(atom.config.get('foo.bar.aString')).toBe 'ok' + describe 'when the value has an "object" type', -> + beforeEach -> + schema = + type: 'object' + properties: + anInt: + type: 'integer' + default: 12 + nestedObject: + type: 'object' + properties: + nestedBool: + type: 'boolean' + default: false + atom.config.setSchema('foo.bar', schema) + + it 'converts and validates all the children', -> + atom.config.set 'foo.bar', + anInt: '23' + nestedObject: + nestedBool: 't' + expect(atom.config.get('foo.bar')).toEqual + anInt: 23 + nestedObject: + nestedBool: true + diff --git a/src/config.coffee b/src/config.coffee index bdd6f8335..6394a7e77 100644 --- a/src/config.coffee +++ b/src/config.coffee @@ -425,3 +425,10 @@ Config.addTypeFilters coercion: (value, schema) -> throw new Error() unless value == null value + + 'object': + typeCheck: (value, schema) -> + return value unless schema.properties? + for prop, childSchema of schema.properties + value[prop] = @executeTypeFilters(value[prop], childSchema) if prop of value + value