Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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

  • Loading branch information...
commit 06a91126f558fd16253d15c7afc45c15d547f59a 1 parent 625ee7e
Miller Medeiros authored
6 dev/src/route.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 () {
11 dev/tests/spec/interpolate.spec.js
View
@@ -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`.' );
+ });
+
});
8 dist/crossroads.js
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 () {
4 dist/crossroads.min.js
View
@@ -2,6 +2,6 @@
* 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(a){a(["signals"],function(a){function d(a,b){if(a.indexOf)return a.indexOf(b);var c=a.length;while(c--)if(a[c]===b)return c;return-1}function e(a,b){return"[object "+b+"]"===Object.prototype.toString.call(a)}function f(a){return e(a,"RegExp")}function g(a){return e(a,"Array")}function h(a){return typeof a=="function"}function i(a){var b;return a===null||a==="null"?b=null:a==="true"?b=!0:a==="false"?b=!1:a===c||a==="undefined"?b=c:a===""||isNaN(a)?b=a:b=parseFloat(a),b}function j(a){var b=a.length,c=[];while(b--)c[b]=i(a[b]);return c}function k(){this._routes=[],this._prevRoutes=[],this.bypassed=new a.Signal,this.routed=new a.Signal}function l(c,d,e,g){var h=f(c),i=b.patternLexer;this._router=g,this._pattern=c,this._paramsIds=h?null:i.getParamIds(this._pattern),this._optionalParamsIds=h?null:i.getOptionalParamsIds(this._pattern),this._matchRegexp=h?c:i.compilePattern(c),this.matched=new a.Signal,this.switched=new a.Signal,d&&this.matched.add(d),this._priority=e||0}var b,c;return k.prototype={greedy:!1,greedyEnabled:!0,normalizeFn:null,create:function(){return new k},shouldTypecast:!1,addRoute:function(a,b,c){var d=new l(a,b,c,this);return this._sortedInsert(d),d},removeRoute:function(a){var b=d(this._routes,a);b!==-1&&this._routes.splice(b,1),a._destroy()},removeAllRoutes:function(){var a=this.getNumRoutes();while(a--)this._routes[a]._destroy();this._routes.length=0},parse:function(a,b){a=a||"",b=b||[];var c=this._getMatchedRoutes(a),d=0,e=c.length,f;if(e){this._notifyPrevRoutes(a),this._prevRoutes=c;while(d<e)f=c[d],f.route.matched.dispatch.apply(f.route.matched,b.concat(f.params)),f.isFirst=!d,this.routed.dispatch.apply(this.routed,b.concat([a,f])),d+=1}else this.bypassed.dispatch.apply(this.bypassed,b.concat([a]))},_notifyPrevRoutes:function(a){var b=0,c;while(c=this._prevRoutes[b++])c.route.switched&&c.route.switched.dispatch(a)},getNumRoutes:function(){return this._routes.length},_sortedInsert:function(a){var b=this._routes,c=b.length;do--c;while(b[c]&&a._priority<=b[c]._priority);b.splice(c+1,0,a)},_getMatchedRoutes:function(a){var b=[],c=this._routes,d=c.length,e;while(e=c[--d]){(!b.length||this.greedy||e.greedy)&&e.match(a)&&b.push({route:e,params:e._getParamsArray(a)});if(!this.greedyEnabled&&b.length)break}return b},toString:function(){return"[crossroads numRoutes:"+this.getNumRoutes()+"]"}},b=new k,b.VERSION="0.9.0-alpha",b.NORM_AS_ARRAY=function(a,b){return[b.vals_]},b.NORM_AS_OBJECT=function(a,b){return[b]},l.prototype={greedy:!1,rules:void 0,match:function(a){return this._matchRegexp.test(a)&&this._validateParams(a)},_validateParams:function(a){var b=this.rules,c=this._getParamsObject(a),d;for(d in b)if(d!=="normalize_"&&b.hasOwnProperty(d)&&!this._isValidParam(a,d,c))return!1;return!0},_isValidParam:function(a,b,c){var e=this.rules[b],i=c[b],j=!1;return i==null&&this._optionalParamsIds&&d(this._optionalParamsIds,b)!==-1?j=!0:f(e)?j=e.test(i):g(e)?j=d(e,i)!==-1:h(e)&&(j=e(i,a,c)),j},_getParamsObject:function(a){var c=this._router.shouldTypecast,d=b.patternLexer.getParamValues(a,this._matchRegexp,c),e={},f=d.length;while(f--)e[f]=d[f],this._paramsIds&&(e[this._paramsIds[f]]=d[f]);return e.request_=c?i(a):a,e.vals_=d,e},_getParamsArray:function(a){var c=this.rules?this.rules.normalize_:null,d;return c=c||this._router.normalizeFn,c&&h(c)?d=c(a,this._getParamsObject(a)):d=b.patternLexer.getParamValues(a,this._matchRegexp,this._router.shouldTypecast),d},interpolate:function(a){return b.patternLexer.interpolate(this._pattern,a)},dispose:function(){this._router.removeRoute(this)},_destroy:function(){this.matched.dispose(),this.switched.dispose(),this.matched=this.switched=this._pattern=this._matchRegexp=null},toString:function(){return'[Route pattern:"'+this._pattern+'", numListeners:'+this.matched.getNumListeners()+"]"}},b.patternLexer=function(){function h(){var a,b;for(a in d)d.hasOwnProperty(a)&&(b=d[a],b.id="__CR_"+a+"__",b.save="save"in b?b.save.replace("{{id}}",b.id):b.id,b.rRestore=new RegExp(b.id,"g"))}function i(a,b){var c=[],d;while(d=a.exec(b))c.push(d[1]);return c}function k(a){return i(c,a)}function l(a){return i(d.OP.rgx,a)}function m(c){return c=c||"",c?(g===e&&(c=c.replace(b,"")),c=n(c,"rgx","save"),c=c.replace(a,"\\$&"),c=n(c,"rRestore","res"),g===e&&(c="/?"+c+"/?")):c="/?",new RegExp("^"+c+"$")}function n(a,b,c){var e,f;for(f in d)d.hasOwnProperty(f)&&(e=d[f],a=a.replace(e[b],e[c]));return a}function o(a,b,c){var d=b.exec(a);return d&&(d.shift(),c&&(d=j(d))),d}function p(a,b){if(typeof a!="string")throw new Error("Route pattern should be a string.");var e=function(a,c){var d;if(c in b){d=b[c];if(a.indexOf("*")===-1&&d.indexOf("/")!==-1)throw new Error('Invalid value "'+d+'" for segment "'+a+'".')}else{if(a.indexOf("{")!==-1)throw new Error("The segment "+a+" is required.");d=""}return d};return d.OS.trail||(d.OS.trail=new RegExp("(?:"+d.OS.id+")+$")),a.replace(d.OS.rgx,d.OS.save).replace(c,e).replace(d.OS.trail,"").replace(d.OS.rRestore,"/")}var a=/[\\.+*?\^$\[\](){}\/'#]/g,b=/^\/|\/$/g,c=/(?:\{|:)([^}:*]+)\*?(?:\}|:)/g,d={OS:{rgx:/([:}]|\w(?=\/))\/?(:)/g,save:"$1{{id}}$2",res:"\\/?"},RS:{rgx:/([:}])\/?(\{)/g,save:"$1{{id}}$2",res:"\\/"},OR:{rgx:/:([^:]+)\*:/g,res:"(.*)?"},RR:{rgx:/\{([^}]+)\*\}/g,res:"(.+)"},RP:{rgx:/\{([^}*]+)\*?\}/g,res:"([^\\/]+)"},OP:{rgx:/:([^:*]+)\*?:/g,res:"([^\\/]+)?/?"}},e=1,f=2,g=e;return h(),{strict:function(){g=f},loose:function(){g=e},getParamIds:k,getOptionalParamsIds:l,getParamValues:o,compilePattern:m,interpolate:p}}(),b})})(typeof define=="function"&&define.amd?define:function(a,b){typeof module!="undefined"&&module.exports?module.exports=b(require(a[0])):window.crossroads=b(window[a[0]])})
+(function(a){a(["signals"],function(a){function d(a,b){if(a.indexOf)return a.indexOf(b);var c=a.length;while(c--)if(a[c]===b)return c;return-1}function e(a,b){return"[object "+b+"]"===Object.prototype.toString.call(a)}function f(a){return e(a,"RegExp")}function g(a){return e(a,"Array")}function h(a){return typeof a=="function"}function i(a){var b;return a===null||a==="null"?b=null:a==="true"?b=!0:a==="false"?b=!1:a===c||a==="undefined"?b=c:a===""||isNaN(a)?b=a:b=parseFloat(a),b}function j(a){var b=a.length,c=[];while(b--)c[b]=i(a[b]);return c}function k(){this._routes=[],this._prevRoutes=[],this.bypassed=new a.Signal,this.routed=new a.Signal}function l(c,d,e,g){var h=f(c),i=b.patternLexer;this._router=g,this._pattern=c,this._paramsIds=h?null:i.getParamIds(this._pattern),this._optionalParamsIds=h?null:i.getOptionalParamsIds(this._pattern),this._matchRegexp=h?c:i.compilePattern(c),this.matched=new a.Signal,this.switched=new a.Signal,d&&this.matched.add(d),this._priority=e||0}var b,c;return k.prototype={greedy:!1,greedyEnabled:!0,normalizeFn:null,create:function(){return new k},shouldTypecast:!1,addRoute:function(a,b,c){var d=new l(a,b,c,this);return this._sortedInsert(d),d},removeRoute:function(a){var b=d(this._routes,a);b!==-1&&this._routes.splice(b,1),a._destroy()},removeAllRoutes:function(){var a=this.getNumRoutes();while(a--)this._routes[a]._destroy();this._routes.length=0},parse:function(a,b){a=a||"",b=b||[];var c=this._getMatchedRoutes(a),d=0,e=c.length,f;if(e){this._notifyPrevRoutes(a),this._prevRoutes=c;while(d<e)f=c[d],f.route.matched.dispatch.apply(f.route.matched,b.concat(f.params)),f.isFirst=!d,this.routed.dispatch.apply(this.routed,b.concat([a,f])),d+=1}else this.bypassed.dispatch.apply(this.bypassed,b.concat([a]))},_notifyPrevRoutes:function(a){var b=0,c;while(c=this._prevRoutes[b++])c.route.switched&&c.route.switched.dispatch(a)},getNumRoutes:function(){return this._routes.length},_sortedInsert:function(a){var b=this._routes,c=b.length;do--c;while(b[c]&&a._priority<=b[c]._priority);b.splice(c+1,0,a)},_getMatchedRoutes:function(a){var b=[],c=this._routes,d=c.length,e;while(e=c[--d]){(!b.length||this.greedy||e.greedy)&&e.match(a)&&b.push({route:e,params:e._getParamsArray(a)});if(!this.greedyEnabled&&b.length)break}return b},toString:function(){return"[crossroads numRoutes:"+this.getNumRoutes()+"]"}},b=new k,b.VERSION="0.9.0-alpha",b.NORM_AS_ARRAY=function(a,b){return[b.vals_]},b.NORM_AS_OBJECT=function(a,b){return[b]},l.prototype={greedy:!1,rules:void 0,match:function(a){return this._matchRegexp.test(a)&&this._validateParams(a)},_validateParams:function(a){var b=this.rules,c=this._getParamsObject(a),d;for(d in b)if(d!=="normalize_"&&b.hasOwnProperty(d)&&!this._isValidParam(a,d,c))return!1;return!0},_isValidParam:function(a,b,c){var e=this.rules[b],i=c[b],j=!1;return i==null&&this._optionalParamsIds&&d(this._optionalParamsIds,b)!==-1?j=!0:f(e)?j=e.test(i):g(e)?j=d(e,i)!==-1:h(e)&&(j=e(i,a,c)),j},_getParamsObject:function(a){var c=this._router.shouldTypecast,d=b.patternLexer.getParamValues(a,this._matchRegexp,c),e={},f=d.length;while(f--)e[f]=d[f],this._paramsIds&&(e[this._paramsIds[f]]=d[f]);return e.request_=c?i(a):a,e.vals_=d,e},_getParamsArray:function(a){var c=this.rules?this.rules.normalize_:null,d;return c=c||this._router.normalizeFn,c&&h(c)?d=c(a,this._getParamsObject(a)):d=b.patternLexer.getParamValues(a,this._matchRegexp,this._router.shouldTypecast),d},interpolate:function(a){var c=b.patternLexer.interpolate(this._pattern,a);if(!this._validateParams(c))throw new Error("Generated string doesn't validate against `Route.rules`.");return c},dispose:function(){this._router.removeRoute(this)},_destroy:function(){this.matched.dispose(),this.switched.dispose(),this.matched=this.switched=this._pattern=this._matchRegexp=null},toString:function(){return'[Route pattern:"'+this._pattern+'", numListeners:'+this.matched.getNumListeners()+"]"}},b.patternLexer=function(){function h(){var a,b;for(a in d)d.hasOwnProperty(a)&&(b=d[a],b.id="__CR_"+a+"__",b.save="save"in b?b.save.replace("{{id}}",b.id):b.id,b.rRestore=new RegExp(b.id,"g"))}function i(a,b){var c=[],d;while(d=a.exec(b))c.push(d[1]);return c}function k(a){return i(c,a)}function l(a){return i(d.OP.rgx,a)}function m(c){return c=c||"",c?(g===e&&(c=c.replace(b,"")),c=n(c,"rgx","save"),c=c.replace(a,"\\$&"),c=n(c,"rRestore","res"),g===e&&(c="/?"+c+"/?")):c="/?",new RegExp("^"+c+"$")}function n(a,b,c){var e,f;for(f in d)d.hasOwnProperty(f)&&(e=d[f],a=a.replace(e[b],e[c]));return a}function o(a,b,c){var d=b.exec(a);return d&&(d.shift(),c&&(d=j(d))),d}function p(a,b){if(typeof a!="string")throw new Error("Route pattern should be a string.");var e=function(a,c){var d;if(c in b){d=b[c];if(a.indexOf("*")===-1&&d.indexOf("/")!==-1)throw new Error('Invalid value "'+d+'" for segment "'+a+'".')}else{if(a.indexOf("{")!==-1)throw new Error("The segment "+a+" is required.");d=""}return d};return d.OS.trail||(d.OS.trail=new RegExp("(?:"+d.OS.id+")+$")),a.replace(d.OS.rgx,d.OS.save).replace(c,e).replace(d.OS.trail,"").replace(d.OS.rRestore,"/")}var a=/[\\.+*?\^$\[\](){}\/'#]/g,b=/^\/|\/$/g,c=/(?:\{|:)([^}:*]+)\*?(?:\}|:)/g,d={OS:{rgx:/([:}]|\w(?=\/))\/?(:)/g,save:"$1{{id}}$2",res:"\\/?"},RS:{rgx:/([:}])\/?(\{)/g,save:"$1{{id}}$2",res:"\\/"},OR:{rgx:/:([^:]+)\*:/g,res:"(.*)?"},RR:{rgx:/\{([^}]+)\*\}/g,res:"(.+)"},RP:{rgx:/\{([^}*]+)\*?\}/g,res:"([^\\/]+)"},OP:{rgx:/:([^:*]+)\*?:/g,res:"([^\\/]+)?/?"}},e=1,f=2,g=e;return h(),{strict:function(){g=f},loose:function(){g=e},getParamIds:k,getOptionalParamsIds:l,getParamValues:o,compilePattern:m,interpolate:p}}(),b})})(typeof define=="function"&&define.amd?define:function(a,b){typeof module!="undefined"&&module.exports?module.exports=b(require(a[0])):window.crossroads=b(window[a[0]])})
Please sign in to comment.
Something went wrong with that request. Please try again.