Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added ns validation

  • Loading branch information...
commit b49ce6d449d0616b125a5e50d4c6dc5a2284edca 1 parent 3dd6c8a
@goulash1971 authored
Showing with 31 additions and 1 deletion.
  1. +30 −0 lib/types/dbref.js
  2. +1 −1  tests/dbref.test.js
View
30 lib/types/dbref.js
@@ -29,6 +29,16 @@ function handleArray (value) {
return value.map (function(m) {return self.cast(m);});
}
+/**
+ * Utility function for typeof array testing
+
+ * @param {Object} the object to be tested as an array
+ * @api private
+ */
+var isArray = Array.isArray || function(obj) {
+ return toString.call(obj) === '[object Array]';
+}
+
/**
* Loader that loads the type into the mongoose infrastructure
@@ -58,6 +68,26 @@ exports.loadType = function (mongoose) {
return !!value && value instanceof dbref;
};
+ // Allows a namespace filter to be installed
+ DBRef.prototype.ns = function (spec) {
+ if (this.nsValidator) {
+ this.nsValidator = false;
+ this.validators = this.validators.filter(function(v) { return v[1] !== 'ns'; });
+ }
+ delete this.namespaces;
+ switch (typeof spec) {
+ case 'function':
+ this.validators.push ([this.nsValidator = spec, 'ns']);
+ break;
+ case 'string' :
+ spec = [spec];
+ default:
+ this.namespaces = spec;
+ this.nsValidator = function (v) { return ~spec.indexOf(v.namespace); };
+ this.validators.push ([this.nsValidator, 'ns']);
+ }
+ };
+
// Casting function using raw or processed refs
DBRef.prototype.cast = function (value) {
var oid = SchemaTypes.ObjectId;
View
2  tests/dbref.test.js
@@ -3,7 +3,7 @@ var mongoose = require('mongoose')
, Schema = mongoose.Schema
, db = mongoose.createConnection('mongodb://localhost/mongoose_dbref_tests');
-require("../").loadTypes(mongoose, 'dbref');
+require("../").loadTypes(mongoose, ['dbref']);
var JournalSchema = new Schema({
related: mongoose.SchemaTypes.DBRef

0 comments on commit b49ce6d

Please sign in to comment.
Something went wrong with that request. Please try again.