From 80b325c8e9935393cb23d38e3bc1be72a82241c3 Mon Sep 17 00:00:00 2001 From: Andreas Zoellner Date: Wed, 15 Mar 2017 11:22:31 -0700 Subject: [PATCH] fix(query): handle $type for arrays Fix #5079 --- lib/schema/array.js | 2 ++ lib/schema/operators/type.js | 13 +++++++++++++ lib/schematype.js | 9 ++------- 3 files changed, 17 insertions(+), 7 deletions(-) create mode 100644 lib/schema/operators/type.js diff --git a/lib/schema/array.js b/lib/schema/array.js index b5833620072..dad5cf3b5d9 100644 --- a/lib/schema/array.js +++ b/lib/schema/array.js @@ -3,6 +3,7 @@ */ var $exists = require('./operators/exists'); +var $type = require('./operators/type'); var SchemaType = require('../schematype'); var CastError = SchemaType.CastError; var Types = { @@ -319,6 +320,7 @@ handle.$minDistance = handle.$maxDistance = castToNumber; handle.$exists = $exists; +handle.$type = $type; handle.$eq = handle.$gt = diff --git a/lib/schema/operators/type.js b/lib/schema/operators/type.js new file mode 100644 index 00000000000..c8e391ac2ae --- /dev/null +++ b/lib/schema/operators/type.js @@ -0,0 +1,13 @@ +'use strict'; + +/*! + * ignore + */ + +module.exports = function(val) { + if (typeof val !== 'number' && typeof val !== 'string') { + throw new Error('$type parameter must be number or string'); + } + + return val; +}; diff --git a/lib/schematype.js b/lib/schematype.js index 146c063f63b..b19ac365289 100644 --- a/lib/schematype.js +++ b/lib/schematype.js @@ -3,6 +3,7 @@ */ var $exists = require('./schema/operators/exists'); +var $type = require('./schema/operators/type'); var utils = require('./utils'); var MongooseError = require('./error'); var CastError = MongooseError.CastError; @@ -922,13 +923,7 @@ SchemaType.prototype.$conditionalHandlers = { $ne: handleSingle, $nin: handleArray, $exists: $exists, - $type: function(val) { - if (typeof val !== 'number' && typeof val !== 'string') { - throw new Error('$type parameter must be number or string'); - } - - return val; - } + $type: $type }; /**