From dbc885e1c5437302c46d4900107a19251f60192e Mon Sep 17 00:00:00 2001 From: Nico Rehwaldt Date: Sat, 17 Dec 2022 17:35:09 +0100 Subject: [PATCH] feat: add `strict` mode In strict mode `moddle` will fail on unknown property access / instantiation. --- lib/moddle.js | 6 ++++- lib/properties.js | 6 +++++ test/helper.js | 4 +-- test/spec/moddle.js | 65 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 3 deletions(-) diff --git a/lib/moddle.js b/lib/moddle.js index 64bfd47..0b27fa6 100644 --- a/lib/moddle.js +++ b/lib/moddle.js @@ -35,8 +35,10 @@ import { * var moddle = new Moddle([pkg]); * * @param {Array} packages the packages to contain + * + * @param { { strict?: boolean } } [config] moddle configuration */ -export default function Moddle(packages) { +export default function Moddle(packages, config = {}) { this.properties = new Properties(this); @@ -44,6 +46,8 @@ export default function Moddle(packages) { this.registry = new Registry(packages, this.properties); this.typeCache = {}; + + this.config = config; } diff --git a/lib/properties.js b/lib/properties.js index ab1ca58..2e4fe53 100644 --- a/lib/properties.js +++ b/lib/properties.js @@ -145,6 +145,12 @@ Properties.prototype.getProperty = function(target, name) { return null; } + const strict = model.config.strict === true; + + if (strict) { + throw new TypeError(`unknown property <${ name }> on <${ target.$type }>`); + } + // eslint-disable-next-line no-undef typeof console !== 'undefined' && console.warn(`unknown property <${ name }> on <${ target.$type }>`); diff --git a/test/helper.js b/test/helper.js index c71079a..2205cf4 100644 --- a/test/helper.js +++ b/test/helper.js @@ -19,7 +19,7 @@ export function createModelBuilder(base) { throw new Error('[test-util] must specify a base directory'); } - function createModel(packageNames) { + function createModel(packageNames, config = {}) { var packages = map(packageNames, function(f) { var pkg = cache[f]; @@ -36,7 +36,7 @@ export function createModelBuilder(base) { return pkg; }); - return new Moddle(packages); + return new Moddle(packages, config); } return createModel; diff --git a/test/spec/moddle.js b/test/spec/moddle.js index 63e53cf..08e3d53 100644 --- a/test/spec/moddle.js +++ b/test/spec/moddle.js @@ -376,4 +376,69 @@ describe('moddle', function() { }); + + describe('property access (strict)', function() { + + const moddle = createModel([ + 'properties' + ], { + strict: true + }); + + + it('should configure in strict mode', function() { + + // then + expect(moddle.config.strict).to.be.true; + }); + + + describe('typed', function() { + + it('should access basic', function() { + + // when + const element = moddle.create('props:ComplexCount', { + count: 10 + }); + + // then + expect(element.get('count')).to.eql(10); + expect(element.get('props:count')).to.eql(10); + + // available under base name + expect(element.count).to.exist; + }); + + + it('fail accessing unknown property', function() { + + // when + const element = moddle.create('props:ComplexCount'); + + // then + expect(() => { + element.get('foo'); + }).to.throw(/unknown property on /); + + expect(() => { + element.set('foo', 10); + }).to.throw(/unknown property on /); + }); + + + it('fail instantiating with unknown property', function() { + + // then + expect(() => { + moddle.create('props:ComplexCount', { + foo: 10 + }); + }).to.throw(/unknown property on /); + }); + + }); + + }); + });