Skip to content

Commit

Permalink
Validate Ember object types in resolver
Browse files Browse the repository at this point in the history
This commit adds assertions to ensure that some key object types
are validated when they are resolved:

* Routes
* Components
* Views
* Services

I did not add a validation for controller classes because the
implementation for detection would need to be a little different (can't
simply use #detect). Also some of Ember's tests play fast (and loose)
with with the types of things they try to pass off as controllers. For
now I don't think this additional validation is worth adding given
imminent demise of Controllers.
  • Loading branch information
mitchlloyd committed May 23, 2015
1 parent 37f4325 commit 67394f7
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/ember-application/lib/system/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
import EmberObject from 'ember-runtime/system/object';
import Namespace from 'ember-runtime/system/namespace';
import helpers from 'ember-htmlbars/helpers';
import validateType from 'ember-application/utils/validate-type';

export var Resolver = EmberObject.extend({
/**
Expand Down Expand Up @@ -172,6 +173,10 @@ export default EmberObject.extend({
this._logLookup(resolved, parsedName);
}

if (resolved) {
validateType(resolved, parsedName);
}

return resolved;
},

Expand Down
24 changes: 24 additions & 0 deletions packages/ember-application/lib/utils/validate-type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
@module ember
@submodule ember-application
*/

import Route from "ember-routing/system/route";
import Component from "ember-views/views/component";
import View from "ember-views/views/view";
import Service from "ember-runtime/system/service";

let VALIDATED_TYPES = {
route: Route,
component: Component,
view: View,
service: Service
};

export default function validateType(resolvedType, parsedName) {
let expectedType = VALIDATED_TYPES[parsedName.type];

Ember.assert(`Expected ${parsedName.fullName} to resolve to an ${expectedType} but instead it was ${resolvedType}.`, function() {
return !expectedType || expectedType.detect(resolvedType);
});
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import Ember from "ember-metal/core"; // Ember.TEMPLATES
import run from "ember-metal/run_loop";
import { forEach } from "ember-metal/enumerable_utils";
import { capitalize } from "ember-runtime/system/string";
import Logger from "ember-metal/logger";
import Controller from "ember-runtime/controllers/controller";
import Route from "ember-routing/system/route";
import Component from "ember-views/views/component";
import View from "ember-views/views/view";
import Service from "ember-runtime/system/service";
import EmberObject from "ember-runtime/system/object";
import Namespace from "ember-runtime/system/namespace";
import Application from "ember-application/system/application";
Expand Down Expand Up @@ -170,3 +176,34 @@ QUnit.test("lookup description", function() {
equal(registry.describe('model:foo'), 'App.Foo', "models don't get appended at the end");

});

QUnit.test("validating resolved objects", function() {
let types = ['route', 'component', 'view', 'service'];

// Valid setup
application.FooRoute = Route.extend();
application.FooComponent = Component.extend();
application.FooView = View.extend();
application.FooService = Service.extend();

forEach(types, function(type) {
// No errors when resolving correct object types
registry.resolve(`${type}:foo`);

// Unregister to clear cache
registry.unregister(`${type}:foo`);
});

// Invalid setup
application.FooRoute = Component.extend();
application.FooComponent = View.extend();
application.FooView = Service.extend();
application.FooService = Route.extend();

forEach(types, function(type) {
let matcher = new RegExp(`to resolve to an Ember.${capitalize(type)}`);
throws(function() {
registry.resolve(`${type}:foo`);
}, matcher, `Should assert for ${type}`);
});
});

0 comments on commit 67394f7

Please sign in to comment.