Skip to content

Commit 5f7d26b

Browse files
author
Marvel Mathew
committed
Throw error if $relativeTime is used with $exists, $ne, and $eq
1 parent 50dc4ba commit 5f7d26b

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

spec/ParseQuery.spec.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3119,8 +3119,7 @@ describe('Parse.Query testing', () => {
31193119
ttl: new Date(now - 2 * 24 * 60 * 60 * 1000), // 2 days ago
31203120
});
31213121

3122-
dropDatabase()
3123-
.then(() => Parse.Object.saveAll([obj1, obj2]))
3122+
Parse.Object.saveAll([obj1, obj2])
31243123
.then(() => {
31253124
const q = new Parse.Query('MyCustomObject');
31263125
q.greaterThan('ttl', { $relativeTime: 'in 1 day' });
@@ -3164,7 +3163,20 @@ describe('Parse.Query testing', () => {
31643163

31653164
const q = new Parse.Query('MyCustomObject');
31663165
q.greaterThan('ttl', { $relativeTime: '-12 bananas ago' });
3167-
return obj1.save({ useMasterKey: true })
3166+
obj1.save({ useMasterKey: true })
3167+
.then(() => q.find({ useMasterKey: true }))
3168+
.then(done.fail, done);
3169+
});
3170+
3171+
it_only_db('mongo')('should error when using $relativeTime with $eq, $ne, and $', function(done) {
3172+
const obj1 = new Parse.Object('MyCustomObject', {
3173+
name: 'obj1',
3174+
ttl: new Date(Date.now() + 2 * 24 * 60 * 60 * 1000), // 2 days from now
3175+
});
3176+
3177+
const q = new Parse.Query('MyCustomObject');
3178+
q.exists('ttl', { $relativeTime: 'in 1 day' });
3179+
obj1.save({ useMasterKey: true })
31683180
.then(() => q.find({ useMasterKey: true }))
31693181
.then(done.fail, done);
31703182
});

src/Adapters/Storage/Mongo/MongoTransform.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,14 @@ function transformConstraint(constraint, field) {
673673
case '$ne':
674674
case '$eq': {
675675
const val = constraint[key];
676-
if (key !== '$exists' && val && typeof val === 'object' && val.$relativeTime) {
676+
if (val && typeof val === 'object' && val.$relativeTime) {
677+
switch (key) {
678+
case '$exists':
679+
case '$ne':
680+
case '$eq':
681+
throw new Parse.Error(Parse.Error.INVALID_JSON, '$relativeTime can only be used with the $lt, $lte, $gt, and $gte operators');
682+
}
683+
677684
const parserResult = relativeTimeToDate(val.$relativeTime);
678685
if (parserResult.status === 'success') {
679686
answer[key] = parserResult.result;
@@ -684,7 +691,7 @@ function transformConstraint(constraint, field) {
684691
throw new Parse.Error(Parse.Error.INVALID_JSON, `bad $relativeTime (${key}) value. ${parserResult.info}`);
685692
}
686693

687-
answer[key] = transformer(constraint[key]);
694+
answer[key] = transformer(val);
688695
break;
689696
}
690697

0 commit comments

Comments
 (0)