From 8f426d9a952b93786ca8dabbb8bd4317570a037a Mon Sep 17 00:00:00 2001 From: Thomas Rueckstiess Date: Thu, 9 Jul 2015 18:27:29 +0200 Subject: [PATCH 1/2] make query bar more user friendly - empty input is turned into {} - single quotes are replaced with double quotes - missing quotes around key names are added --- src/refine-view/index.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/refine-view/index.js b/src/refine-view/index.js index 83fcfc2dfe0..2de25f188b4 100644 --- a/src/refine-view/index.js +++ b/src/refine-view/index.js @@ -1,5 +1,7 @@ var AmpersandView = require('ampersand-view'); var EJSON = require('mongodb-extended-json'); +var _ = require('lodash'); +var debug = require('debug')('scout:refine-view:index'); module.exports = AmpersandView.extend({ template: require('./index.jade'), @@ -36,9 +38,21 @@ module.exports = AmpersandView.extend({ 'input [data-hook=refine-input]': 'inputChanged', 'submit form': 'submit' }, + _cleanupInput: function (input) { + var output = input; + // accept whitespace-only input as empty query + if (_.trim(output) === '') { + output = '{}'; + } + // replace single quotes with double quotes + output = output.replace(/'/g, '"'); + // wrap field names in double quotes + output = output.replace(/([{,])\s*([^,{\s\'"]+)\s*:/g, ' $1 "$2" : '); + return output; + }, inputChanged: function() { // validate user input on the fly - var queryStr = this.queryByHook('refine-input').value; + var queryStr = this._cleanupInput(this.queryByHook('refine-input').value); try { EJSON.parse(queryStr); } catch (e) { @@ -48,10 +62,9 @@ module.exports = AmpersandView.extend({ this.valid = true; }, buttonClicked: function() { - var queryStr = this.queryByHook('refine-input').value; + var queryStr = this._cleanupInput(this.queryByHook('refine-input').value); var queryObj = EJSON.parse(queryStr); this.model.query = queryObj; - // Modifying the query will reset field-list#schema and because we're using // good ampersand, outgoing views will be removed for us automatically. }, From 22201a590d56271a585a125e7244b13ae66f93b6 Mon Sep 17 00:00:00 2001 From: Thomas Rueckstiess Date: Thu, 9 Jul 2015 19:14:47 +0200 Subject: [PATCH 2/2] added language model validation for user input --- package.json | 1 + src/refine-view/index.js | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 41ef0d0b976..55527c860fa 100644 --- a/package.json +++ b/package.json @@ -77,6 +77,7 @@ "lodash": "^3.9.3", "moment": "^2.10.3", "mongodb-extended-json": "^1.3.1", + "mongodb-language-model": "^0.2.0", "mongodb-schema": "^3.0.0", "numeral": "^1.5.3", "octicons": "https://github.com/github/octicons/archive/v2.2.0.tar.gz", diff --git a/src/refine-view/index.js b/src/refine-view/index.js index 2de25f188b4..17bb2e8d6bf 100644 --- a/src/refine-view/index.js +++ b/src/refine-view/index.js @@ -2,6 +2,7 @@ var AmpersandView = require('ampersand-view'); var EJSON = require('mongodb-extended-json'); var _ = require('lodash'); var debug = require('debug')('scout:refine-view:index'); +var Query = require('mongodb-language-model').Query; module.exports = AmpersandView.extend({ template: require('./index.jade'), @@ -54,7 +55,10 @@ module.exports = AmpersandView.extend({ // validate user input on the fly var queryStr = this._cleanupInput(this.queryByHook('refine-input').value); try { - EJSON.parse(queryStr); + // is it valid eJSON? + var queryObj = EJSON.parse(queryStr); + // is it a valid parsable Query according to the language? + var languageObj = new Query(queryObj, {parse: true}); } catch (e) { this.valid = false; return;