Permalink
Browse files

instanceof RegExp doesn't work everywhere so use ducktyping

  • Loading branch information...
1 parent 77817fb commit aac877eff56fc9bc13fc8c446d8673decb9e32ab @bronson committed Oct 6, 2011
Showing with 4 additions and 1 deletion.
  1. +4 −1 lib/valid.js
View
@@ -287,6 +287,9 @@ Valid.JsonObject = function(path, value, schema) {
Valid.JsonField = function(path, value, schema) {
+ // need to duck type RegExps because instanceof isn't working reliably
+ var isRegExp = function(o) { return o && o.test && o.exec && o.source && (o.global === true || o.global === false) && (o.ignoreCase === true || o.ignoreCase === false) && (o.multiline === true || o.multiline === false) };
+
switch(typeof schema) {
case 'string':
case 'number':
@@ -296,7 +299,7 @@ Valid.JsonField = function(path, value, schema) {
break;
case 'function':
- if(schema instanceof RegExp) {
+ if(isRegExp(schema)) {
var reresult = Valid.match(schema).check(value);
if(reresult) this.JsonError(path, reresult);
} else {

0 comments on commit aac877e

Please sign in to comment.