1+ function isNonNullObject ( value ) {
2+ return typeof value === 'object' && value !== null ;
3+ }
4+
15// throws if the user is using the wrong query field value type
26function checkFieldValueType ( name , value , isHttp ) {
3- var message = '' ;
4- var received = value ;
5- var addReceived = true ;
6- if ( [ '$in' , '$nin' , '$or' , '$and' , '$mod' , '$nor' , '$all' ] . indexOf ( name ) !== - 1 ) {
7+ let message = '' ;
8+ let received = value ;
9+ let addReceived = true ;
10+ if ( [ '$in' , '$nin' , '$or' , '$and' , '$mod' , '$nor' , '$all' ] . indexOf ( name ) !== - 1 ) {
711 if ( ! Array . isArray ( value ) ) {
812 message = 'Query operator ' + name + ' must be an array.' ;
913
1014 }
1115 }
1216
13- if ( [ '$not' , '$elemMatch' , '$allMatch' ] . indexOf ( name ) !== - 1 ) {
14- if ( ! ( ! Array . isArray ( value ) && typeof value === 'object' && value !== null ) ) {
17+ if ( [ '$not' , '$elemMatch' , '$allMatch' ] . indexOf ( name ) !== - 1 ) {
18+ if ( ! ( ! Array . isArray ( value ) && isNonNullObject ( value ) ) ) {
1519 message = 'Query operator ' + name + ' must be an object.' ;
1620 }
1721 }
@@ -21,8 +25,8 @@ function checkFieldValueType(name, value, isHttp) {
2125 message = 'Query operator $mod must be in the format [divisor, remainder], ' +
2226 'where divisor and remainder are both integers.' ;
2327 } else {
24- var divisor = value [ 0 ] ;
25- var mod = value [ 1 ] ;
28+ const divisor = value [ 0 ] ;
29+ const mod = value [ 1 ] ;
2630 if ( divisor === 0 ) {
2731 message = 'Query operator $mod\'s divisor cannot be 0, cannot divide by zero.' ;
2832 addReceived = false ;
@@ -44,8 +48,8 @@ function checkFieldValueType(name, value, isHttp) {
4448 }
4549
4650 if ( name === '$type' ) {
47- var allowed = [ 'null' , 'boolean' , 'number' , 'string' , 'array' , 'object' ] ;
48- var allowedStr = '"' + allowed . slice ( 0 , allowed . length - 1 ) . join ( '", "' ) + '", or "' + allowed [ allowed . length - 1 ] + '"' ;
51+ const allowed = [ 'null' , 'boolean' , 'number' , 'string' , 'array' , 'object' ] ;
52+ const allowedStr = '"' + allowed . slice ( 0 , allowed . length - 1 ) . join ( '", "' ) + '", or "' + allowed [ allowed . length - 1 ] + '"' ;
4953 if ( typeof value !== 'string' ) {
5054 message = 'Query operator $type must be a string. Supported values: ' + allowedStr + '.' ;
5155 } else if ( allowed . indexOf ( value ) == - 1 ) {
@@ -72,44 +76,36 @@ function checkFieldValueType(name, value, isHttp) {
7276
7377 if ( message ) {
7478 if ( addReceived ) {
75-
76- var type = received === null
77- ? ' '
78- : Array . isArray ( received )
79- ? ' array'
80- : ' ' + typeof received ;
81- var receivedStr = typeof received === 'object' && received !== null
82- ? JSON . stringify ( received , null , '\t' )
83- : received ;
79+ const type = received === null
80+ ? ' '
81+ : Array . isArray ( received )
82+ ? ' array'
83+ : ' ' + typeof received ;
84+ const receivedStr = isNonNullObject ( received )
85+ ? JSON . stringify ( received , null , '\t' )
86+ : received ;
8487
8588 message += ' Received' + type + ': ' + receivedStr ;
8689 }
8790 throw new Error ( message ) ;
8891 }
8992}
9093
91-
92- var requireValidation = [ '$all' , '$allMatch' , '$and' , '$elemMatch' , '$exists' , '$in' , '$mod' , '$nin' , '$nor' , '$not' , '$or' , '$regex' , '$size' , '$type' ] ;
93-
94- var arrayTypeComparisonOperators = [ '$in' , '$nin' , '$mod' , '$all' ] ;
95-
96- var equalityOperators = [ '$eq' , '$gt' , '$gte' , '$lt' , '$lte' ] ;
94+ const requireValidation = [ '$all' , '$allMatch' , '$and' , '$elemMatch' , '$exists' , '$in' , '$mod' , '$nin' , '$nor' , '$not' , '$or' , '$regex' , '$size' , '$type' ] ;
95+ const arrayTypeComparisonOperators = [ '$in' , '$nin' , '$mod' , '$all' ] ;
96+ const equalityOperators = [ '$eq' , '$gt' , '$gte' , '$lt' , '$lte' ] ;
9797
9898// recursively walks down the a query selector validating any operators
99- function validateSelector ( input , isHttp ) {
99+ export default function validateSelector ( input , isHttp ) {
100100 if ( Array . isArray ( input ) ) {
101- for ( var entry of input ) {
102- if ( typeof entry === 'object' && entry !== null ) {
101+ for ( const entry of input ) {
102+ if ( isNonNullObject ( entry ) ) {
103103 validateSelector ( entry , isHttp ) ;
104104 }
105105 }
106- } else {
107- var fields = Object . keys ( input ) ;
108-
109- for ( var i = 0 ; i < fields . length ; i ++ ) {
110- var key = fields [ i ] ;
111- var value = input [ key ] ;
112-
106+ }
107+ else {
108+ for ( const [ key , value ] of Object . entries ( input ) ) {
113109 if ( requireValidation . indexOf ( key ) !== - 1 ) {
114110 checkFieldValueType ( key , value , isHttp ) ;
115111 }
@@ -121,11 +117,9 @@ function validateSelector(input, isHttp) {
121117 // skip, their values are already valid
122118 continue ;
123119 }
124- if ( typeof value === 'object' && value !== null ) {
120+ if ( isNonNullObject ( value ) ) {
125121 validateSelector ( value , isHttp ) ;
126122 }
127123 }
128124 }
129125}
130-
131- export default validateSelector ;
0 commit comments