Skip to content

Commit

Permalink
Merge branch 'master' into gh-pages
Browse files Browse the repository at this point in the history
  • Loading branch information
alxndrsn authored Aug 16, 2023
2 parents 10add23 + 384b2e1 commit 21bee2e
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 119 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"eol-last": "error",
"no-empty": "off",
"no-console": "off",
"no-tabs": "error",
"no-trailing-spaces": "error",
"semi": ["error", "always"],
"curly": ["error", "all"],
Expand Down
5 changes: 3 additions & 2 deletions bin/build-site.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

'use strict';

var http_server = require('http-server');
var fs = require('fs');
var watchGlob = require('watch-glob');
var replace = require('replace');
var exec = require('child-process-promise').exec;
var mkdirp = require('mkdirp');
Expand Down Expand Up @@ -72,6 +70,9 @@ function buildEverything() {
}

if (!process.env.BUILD) {
const http_server = require('http-server');
const watchGlob = require('watch-glob');

watchGlob('**', buildJekyll);
watchGlob('docs/static/less/*/*.less', buildCSS);
http_server.createServer({root: '_site', cache: '-1'}).listen(4000);
Expand Down
3 changes: 1 addition & 2 deletions bin/run-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ pouchdb-setup-server() {
# in CI, link pouchdb-servers dependencies on pouchdb
# modules to the current implementations
if [ -d "pouchdb-server-install" ]; then
# pouchdb server already running
exit 0
rm -rf pouchdb-server-install
fi
mkdir pouchdb-server-install
cd pouchdb-server-install
Expand Down
230 changes: 115 additions & 115 deletions packages/node_modules/pouchdb-find/src/validateSelector.js
Original file line number Diff line number Diff line change
@@ -1,91 +1,91 @@
// throws if the user is using the wrong query field value type
function checkFieldValueType(name, value, isHttp) {
var message = '';
var received = value;
var addReceived = true;
if ([ '$in', '$nin', '$or', '$and', '$mod', '$nor', '$all' ].indexOf(name) !== -1) {
if (!Array.isArray(value)) {
message = 'Query operator ' + name + ' must be an array.';

}
}

if ([ '$not', '$elemMatch', '$allMatch' ].indexOf(name) !== -1) {
if (!(!Array.isArray(value) && typeof value === 'object' && value !== null)) {
message = 'Query operator ' + name + ' must be an object.';
}
}

if (name === '$mod' && Array.isArray(value)) {
if (value.length !== 2) {
message = 'Query operator $mod must be in the format [divisor, remainder], ' +
'where divisor and remainder are both integers.';
} else {
var divisor = value[0];
var mod = value[1];
if (divisor === 0) {
message = 'Query operator $mod\'s divisor cannot be 0, cannot divide by zero.';
addReceived = false;
}
if (typeof divisor !== 'number' || parseInt(divisor, 10) !== divisor) {
message = 'Query operator $mod\'s divisor is not an integer.';
received = divisor;
}
if (parseInt(mod, 10) !== mod) {
message = 'Query operator $mod\'s remainder is not an integer.';
received = mod;
}
}
}
if (name === '$exists') {
if (typeof value !== 'boolean') {
message = 'Query operator $exists must be a boolean.';
}
}

if (name === '$type') {
var allowed = [ 'null', 'boolean', 'number', 'string', 'array', 'object' ];
var allowedStr = '"' + allowed.slice(0, allowed.length - 1).join('", "') + '", or "' + allowed[allowed.length - 1] + '"';
if (typeof value !== 'string') {
message = 'Query operator $type must be a string. Supported values: ' + allowedStr + '.';
} else if (allowed.indexOf(value) == -1) {
message = 'Query operator $type must be a string. Supported values: ' + allowedStr + '.';
}
}

if (name === '$size') {
if (parseInt(value, 10) !== value) {
message = 'Query operator $size must be a integer.';
}
}

if (name === '$regex') {
if (typeof value !== 'string') {
if (isHttp) {
message = 'Query operator $regex must be a string.';
} else if (!(value instanceof RegExp)) {
message = 'Query operator $regex must be a string or an instance ' +
'of a javascript regular expression.';
}
}
}

if (message) {
if (addReceived) {

var type = received === null
? ' '
: Array.isArray(received)
? ' array'
: ' ' + typeof received;
var receivedStr = typeof received === 'object' && received !== null
? JSON.stringify(received, null, '\t')
: received;

message += ' Received' + type + ': ' + receivedStr;
}
throw new Error(message);
}
var message = '';
var received = value;
var addReceived = true;
if ([ '$in', '$nin', '$or', '$and', '$mod', '$nor', '$all' ].indexOf(name) !== -1) {
if (!Array.isArray(value)) {
message = 'Query operator ' + name + ' must be an array.';

}
}

if ([ '$not', '$elemMatch', '$allMatch' ].indexOf(name) !== -1) {
if (!(!Array.isArray(value) && typeof value === 'object' && value !== null)) {
message = 'Query operator ' + name + ' must be an object.';
}
}

if (name === '$mod' && Array.isArray(value)) {
if (value.length !== 2) {
message = 'Query operator $mod must be in the format [divisor, remainder], ' +
'where divisor and remainder are both integers.';
} else {
var divisor = value[0];
var mod = value[1];
if (divisor === 0) {
message = 'Query operator $mod\'s divisor cannot be 0, cannot divide by zero.';
addReceived = false;
}
if (typeof divisor !== 'number' || parseInt(divisor, 10) !== divisor) {
message = 'Query operator $mod\'s divisor is not an integer.';
received = divisor;
}
if (parseInt(mod, 10) !== mod) {
message = 'Query operator $mod\'s remainder is not an integer.';
received = mod;
}
}
}
if (name === '$exists') {
if (typeof value !== 'boolean') {
message = 'Query operator $exists must be a boolean.';
}
}

if (name === '$type') {
var allowed = [ 'null', 'boolean', 'number', 'string', 'array', 'object' ];
var allowedStr = '"' + allowed.slice(0, allowed.length - 1).join('", "') + '", or "' + allowed[allowed.length - 1] + '"';
if (typeof value !== 'string') {
message = 'Query operator $type must be a string. Supported values: ' + allowedStr + '.';
} else if (allowed.indexOf(value) == -1) {
message = 'Query operator $type must be a string. Supported values: ' + allowedStr + '.';
}
}

if (name === '$size') {
if (parseInt(value, 10) !== value) {
message = 'Query operator $size must be a integer.';
}
}

if (name === '$regex') {
if (typeof value !== 'string') {
if (isHttp) {
message = 'Query operator $regex must be a string.';
} else if (!(value instanceof RegExp)) {
message = 'Query operator $regex must be a string or an instance ' +
'of a javascript regular expression.';
}
}
}

if (message) {
if (addReceived) {

var type = received === null
? ' '
: Array.isArray(received)
? ' array'
: ' ' + typeof received;
var receivedStr = typeof received === 'object' && received !== null
? JSON.stringify(received, null, '\t')
: received;

message += ' Received' + type + ': ' + receivedStr;
}
throw new Error(message);
}
}


Expand All @@ -97,35 +97,35 @@ var equalityOperators = [ '$eq', '$gt', '$gte', '$lt', '$lte' ];

// recursively walks down the a query selector validating any operators
function validateSelector(input, isHttp) {
if (Array.isArray(input)) {
for (var entry of input) {
if (typeof entry === 'object' && value !== null) {
validateSelector(entry, isHttp);
}
}
} else {
var fields = Object.keys(input);

for (var i = 0; i < fields.length; i++) {
var key = fields[i];
var value = input[key];

if (requireValidation.indexOf(key) !== -1) {
checkFieldValueType(key, value, isHttp);
}
if (equalityOperators.indexOf(key) !== -1) {
// skip, explicit comparison operators can be anything
continue;
}
if (arrayTypeComparisonOperators.indexOf(key) !== -1) {
// skip, their values are already valid
continue;
}
if (typeof value === 'object' && value !== null) {
validateSelector(value, isHttp);
}
}
}
if (Array.isArray(input)) {
for (var entry of input) {
if (typeof entry === 'object' && value !== null) {
validateSelector(entry, isHttp);
}
}
} else {
var fields = Object.keys(input);

for (var i = 0; i < fields.length; i++) {
var key = fields[i];
var value = input[key];

if (requireValidation.indexOf(key) !== -1) {
checkFieldValueType(key, value, isHttp);
}
if (equalityOperators.indexOf(key) !== -1) {
// skip, explicit comparison operators can be anything
continue;
}
if (arrayTypeComparisonOperators.indexOf(key) !== -1) {
// skip, their values are already valid
continue;
}
if (typeof value === 'object' && value !== null) {
validateSelector(value, isHttp);
}
}
}
}

export default validateSelector;

0 comments on commit 21bee2e

Please sign in to comment.