@@ -167,31 +167,9 @@ function transformQueryKeyValue(className, key, value, schema) {
167
167
case '_perishable_token' :
168
168
case '_email_verify_token' : return { key, value}
169
169
case '$or' :
170
- if ( ! ( value instanceof Array ) ) {
171
- throw new Parse . Error ( Parse . Error . INVALID_QUERY , 'bad $or format - use an array value' ) ;
172
- }
173
- if ( value . some ( subQuery => subQuery . ACL ) ) {
174
- throw new Parse . Error ( Parse . Error . INVALID_QUERY , 'Cannot query on ACL.' ) ;
175
- Object . keys ( subQuery ) . forEach ( restKey => {
176
- if ( ! specialQuerykeys . includes ( restKey ) && ! restKey . match ( / ^ [ a - z A - Z ] [ a - z A - Z 0 - 9 _ \. ] * $ / ) ) {
177
- throw new Parse . Error ( Parse . Error . INVALID_KEY_NAME , `Invalid key name: ${ restKey } ` ) ;
178
- }
179
- } ) ;
180
- }
181
- return { key : '$or' , value : value . map ( subQuery => transformWhere ( className , subQuery , { } , schema ) ) } ;
170
+ return { key : '$or' , value : value . map ( subQuery => transformWhere ( className , subQuery , schema ) ) } ;
182
171
case '$and' :
183
- if ( ! ( value instanceof Array ) ) {
184
- throw new Parse . Error ( Parse . Error . INVALID_QUERY , 'bad $and format - use an array value' ) ;
185
- }
186
- if ( value . some ( subQuery => subQuery . ACL ) ) {
187
- throw new Parse . Error ( Parse . Error . INVALID_QUERY , 'Cannot query on ACL.' ) ;
188
- Object . keys ( subQuery ) . forEach ( restKey => {
189
- if ( ! specialQuerykeys . includes ( restKey ) && ! restKey . match ( / ^ [ a - z A - Z ] [ a - z A - Z 0 - 9 _ \. ] * $ / ) ) {
190
- throw new Parse . Error ( Parse . Error . INVALID_KEY_NAME , `Invalid key name: ${ restKey } ` ) ;
191
- }
192
- } ) ;
193
- }
194
- return { key : '$and' , value : value . map ( subQuery => transformWhere ( className , subQuery , { } , schema ) ) } ;
172
+ return { key : '$and' , value : value . map ( subQuery => transformWhere ( className , subQuery , schema ) ) } ;
195
173
default :
196
174
// Other auth data
197
175
const authDataMatch = key . match ( / ^ a u t h D a t a \. ( [ a - z A - Z 0 - 9 _ ] + ) \. i d $ / ) ;
@@ -233,17 +211,42 @@ function transformQueryKeyValue(className, key, value, schema) {
233
211
}
234
212
}
235
213
214
+ const validateQuery = query => {
215
+ if ( query . ACL ) {
216
+ throw new Parse . Error ( Parse . Error . INVALID_QUERY , 'Cannot query on ACL.' ) ;
217
+ }
218
+
219
+ if ( query . $or ) {
220
+ if ( query . $or instanceof Array ) {
221
+ query . $or . forEach ( validateQuery ) ;
222
+ } else {
223
+ throw new Parse . Error ( Parse . Error . INVALID_QUERY , 'Bad $or format - use an array value.' ) ;
224
+ }
225
+ }
226
+
227
+ if ( query . $and ) {
228
+ if ( query . $and instanceof Array ) {
229
+ query . $and . forEach ( validateQuery ) ;
230
+ } else {
231
+ throw new Parse . Error ( Parse . Error . INVALID_QUERY , 'Bad $and format - use an array value.' ) ;
232
+ }
233
+ }
234
+
235
+ Object . keys ( query ) . forEach ( key => {
236
+ if ( ! specialQuerykeys . includes ( key ) && ! key . match ( / ^ [ a - z A - Z ] [ a - z A - Z 0 - 9 _ \. ] * $ / ) ) {
237
+ throw new Parse . Error ( Parse . Error . INVALID_KEY_NAME , `Invalid key name: ${ key } ` ) ;
238
+ }
239
+ } ) ;
240
+ }
241
+
236
242
// Main exposed method to help run queries.
237
243
// restWhere is the "where" clause in REST API form.
238
244
// Returns the mongo form of the query.
239
245
// Throws a Parse.Error if the input query is invalid.
240
246
const specialQuerykeys = [ '$and' , '$or' , '_rperm' , '_wperm' , '_perishable_token' , '_email_verify_token' ] ;
241
- function transformWhere ( className , restWhere , { validate = true } = { } , schema ) {
247
+ function transformWhere ( className , restWhere , schema ) {
242
248
let mongoWhere = { } ;
243
249
for ( let restKey in restWhere ) {
244
- if ( validate && ! specialQuerykeys . includes ( restKey ) && ! restKey . match ( / ^ [ a - z A - Z ] [ a - z A - Z 0 - 9 _ \. ] * $ / ) ) {
245
- throw new Parse . Error ( Parse . Error . INVALID_KEY_NAME , `Invalid key name: ${ restKey } ` ) ;
246
- }
247
250
let out = transformQueryKeyValue ( className , restKey , restWhere [ restKey ] , schema ) ;
248
251
mongoWhere [ out . key ] = out . value ;
249
252
}
@@ -1045,6 +1048,7 @@ var FileCoder = {
1045
1048
1046
1049
module . exports = {
1047
1050
transformKey,
1051
+ validateQuery,
1048
1052
parseObjectToMongoObjectForCreate,
1049
1053
transformUpdate,
1050
1054
transformWhere,
0 commit comments