Skip to content

Commit ed6e85a

Browse files
committed
feat: add strict mode
In strict mode `moddle` will fail on unknown property access / instantiation.
1 parent 3ee31d9 commit ed6e85a

File tree

4 files changed

+78
-3
lines changed

4 files changed

+78
-3
lines changed

lib/moddle.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,19 @@ import {
3535
* var moddle = new Moddle([pkg]);
3636
*
3737
* @param {Array<Package>} packages the packages to contain
38+
*
39+
* @param { { strict?: boolean } } [config] moddle configuration
3840
*/
39-
export default function Moddle(packages) {
41+
export default function Moddle(packages, config = {}) {
4042

4143
this.properties = new Properties(this);
4244

4345
this.factory = new Factory(this, this.properties);
4446
this.registry = new Registry(packages, this.properties);
4547

4648
this.typeCache = {};
49+
50+
this.config = config;
4751
}
4852

4953

lib/properties.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,12 @@ Properties.prototype.getProperty = function(target, name) {
145145
return null;
146146
}
147147

148+
const strict = model.config.strict === true;
149+
150+
if (strict) {
151+
throw new TypeError(`unknown property <${ name }> on <${ target.$type }>`);
152+
}
153+
148154
// eslint-disable-next-line no-undef
149155
typeof console !== 'undefined' && console.warn(`unknown property <${ name }> on <${ target.$type }>`);
150156

test/helper.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export function createModelBuilder(base) {
2121
throw new Error('[test-util] must specify a base directory');
2222
}
2323

24-
function createModel(packageNames) {
24+
function createModel(packageNames, config = {}) {
2525

2626
var packages = map(packageNames, function(f) {
2727
var pkg = cache[f];
@@ -38,7 +38,7 @@ export function createModelBuilder(base) {
3838
return pkg;
3939
});
4040

41-
return new Moddle(packages);
41+
return new Moddle(packages, config);
4242
}
4343

4444
return createModel;

test/spec/moddle.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,4 +400,69 @@ describe('moddle', function() {
400400

401401
});
402402

403+
404+
describe('property access (strict)', function() {
405+
406+
const moddle = createModel([
407+
'properties'
408+
], {
409+
strict: true
410+
});
411+
412+
413+
it('should configure in strict mode', function() {
414+
415+
// then
416+
expect(moddle.config.strict).to.be.true;
417+
});
418+
419+
420+
describe('typed', function() {
421+
422+
it('should access basic', function() {
423+
424+
// when
425+
const element = moddle.create('props:ComplexCount', {
426+
count: 10
427+
});
428+
429+
// then
430+
expect(element.get('count')).to.eql(10);
431+
expect(element.get('props:count')).to.eql(10);
432+
433+
// available under base name
434+
expect(element.count).to.exist;
435+
});
436+
437+
438+
it('fail accessing unknown property', function() {
439+
440+
// when
441+
const element = moddle.create('props:ComplexCount');
442+
443+
// then
444+
expect(() => {
445+
element.get('foo');
446+
}).to.throw(/unknown property <foo> on <props:ComplexCount>/);
447+
448+
expect(() => {
449+
element.set('foo', 10);
450+
}).to.throw(/unknown property <foo> on <props:ComplexCount>/);
451+
});
452+
453+
454+
it('fail instantiating with unknown property', function() {
455+
456+
// then
457+
expect(() => {
458+
moddle.create('props:ComplexCount', {
459+
foo: 10
460+
});
461+
}).to.throw(/unknown property <foo> on <props:ComplexCount>/);
462+
});
463+
464+
});
465+
466+
});
467+
403468
});

0 commit comments

Comments
 (0)