Skip to content

Commit

Permalink
feat: add strict mode
Browse files Browse the repository at this point in the history
In strict mode `moddle` will fail on unknown property
access / instantiation.
  • Loading branch information
nikku committed Dec 17, 2022
1 parent 8d14647 commit dbc885e
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 3 deletions.
6 changes: 5 additions & 1 deletion lib/moddle.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,19 @@ import {
* var moddle = new Moddle([pkg]);
*
* @param {Array<Package>} 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);

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

this.typeCache = {};

this.config = config;
}


Expand Down
6 changes: 6 additions & 0 deletions lib/properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }>`);

Expand Down
4 changes: 2 additions & 2 deletions test/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -36,7 +36,7 @@ export function createModelBuilder(base) {
return pkg;
});

return new Moddle(packages);
return new Moddle(packages, config);
}

return createModel;
Expand Down
65 changes: 65 additions & 0 deletions test/spec/moddle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <foo> on <props:ComplexCount>/);

expect(() => {
element.set('foo', 10);
}).to.throw(/unknown property <foo> on <props:ComplexCount>/);
});


it('fail instantiating with unknown property', function() {

// then
expect(() => {
moddle.create('props:ComplexCount', {
foo: 10
});
}).to.throw(/unknown property <foo> on <props:ComplexCount>/);
});

});

});

});

0 comments on commit dbc885e

Please sign in to comment.