Permalink
Browse files

check if interpolated route validate against Route.rules. see #34

  • Loading branch information...
1 parent 625ee7e commit 06a91126f558fd16253d15c7afc45c15d547f59a @millermedeiros committed Apr 18, 2012
Showing with 24 additions and 5 deletions.
  1. +5 −1 dev/src/route.js
  2. +11 −0 dev/tests/spec/interpolate.spec.js
  3. +6 −2 dist/crossroads.js
  4. +2 −2 dist/crossroads.min.js
View
@@ -94,7 +94,11 @@
},
interpolate : function(replacements) {
- return crossroads.patternLexer.interpolate(this._pattern, replacements);
+ var str = crossroads.patternLexer.interpolate(this._pattern, replacements);
+ if (! this._validateParams(str) ) {
+ throw new Error('Generated string doesn\'t validate against `Route.rules`.');
+ }
+ return str;
},
dispose : function () {
@@ -54,4 +54,15 @@ describe('Route.interpolate()', function(){
}).toThrow( 'Route pattern should be a string.' );
});
+ it('should throw an error if generated string doesn\'t validate against rules', function () {
+ var a = crossroads.addRoute('/{foo}/:bar:');
+ a.rules = {
+ foo : ['lorem', 'news'],
+ bar : /^\d+$/
+ };
+ expect( function(){
+ a.interpolate({foo: 'lorem', bar: 'ipsum'});
+ }).toThrow( 'Generated string doesn\'t validate against `Route.rules`.' );
+ });
+
});
View
@@ -2,7 +2,7 @@
* crossroads <http://millermedeiros.github.com/crossroads.js/>
* License: MIT
* Author: Miller Medeiros
- * Version: 0.9.0-alpha (2012/4/18 12:59)
+ * Version: 0.9.0-alpha (2012/4/18 13:27)
*/
(function (define) {
@@ -303,7 +303,11 @@ define(['signals'], function (signals) {
},
interpolate : function(replacements) {
- return crossroads.patternLexer.interpolate(this._pattern, replacements);
+ var str = crossroads.patternLexer.interpolate(this._pattern, replacements);
+ if (! this._validateParams(str) ) {
+ throw new Error('Generated string doesn\'t validate against `Route.rules`.');
+ }
+ return str;
},
dispose : function () {
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

0 comments on commit 06a9112

Please sign in to comment.