Skip to content

Commit

Permalink
fix(query): leave coordinates array as-is
Browse files Browse the repository at this point in the history
  • Loading branch information
Pier-Luc Gendreau committed Mar 14, 2016
1 parent 1312741 commit 3cd645a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
},
"dependencies": {
"async": "~1.5.0",
"is-coordinates": "~1.0.0",
"lodash": "~4.6.0",
"mongoose-detective": "~0.1.0",
"moredots": "~0.1.0",
Expand Down
3 changes: 2 additions & 1 deletion src/middleware/prepareQuery.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const _ = require('lodash')
const isCoordinates = require('is-coordinates')

module.exports = function (options) {
function jsonQueryParser (key, value) {
Expand All @@ -23,7 +24,7 @@ module.exports = function (options) {
} else if (value[0] === '=') {
return { $eq: value.substr(1) }*/
}
} else if (_.isArray(value) && key[0] !== '$') {
} else if (_.isArray(value) && key[0] !== '$' && key !== 'coordinates' && !isCoordinates(value)) {
return { $in: value }
}

Expand Down
29 changes: 28 additions & 1 deletion test/integration/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ module.exports = function (createFn, setup, dismantle) {
item: createdProduct._id,
number: 1
}
}
},
coordinates: [45.2667, 72.1500]
}, {
name: 'John',
age: 24,
Expand Down Expand Up @@ -317,6 +318,32 @@ module.exports = function (createFn, setup, dismantle) {
})
})

it('GET /Customer?query={"$near": { "$geometry": { "coordinates": [45.2667, 72.1500] } }} 200 - coordinates', (done) => {
request.get({
url: `${testUrl}/api/v1/Customer`,
qs: {
query: JSON.stringify({
coordinates: {
$near: {
$geometry: {
type: 'Point',
coordinates: [45.2667, 72.1500]
},
$maxDistance: 1000
}
}
})
},
json: true
}, (err, res, body) => {
assert.ok(!err)
console.log(res.body)
assert.equal(res.statusCode, 200)
assert.equal(body.length, 1)
done()
})
})

it('GET /Customer?query=invalidJson 400 - invalid json', (done) => {
request.get({
url: `${testUrl}/api/v1/Customer`,
Expand Down
3 changes: 2 additions & 1 deletion test/integration/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ module.exports = function () {
}],
returns: [{ type: Schema.Types.ObjectId, ref: 'Product' }],
creditCard: { type: String, access: 'protected' },
ssn: { type: String, access: 'private' }
ssn: { type: String, access: 'private' },
coordinates: { type: [Number], index: '2dsphere' }
})
}

Expand Down

0 comments on commit 3cd645a

Please sign in to comment.