forked from grahamscott/lucene-validator-amd-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathluceneValidator.js
168 lines (136 loc) · 6.21 KB
/
luceneValidator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
"use strict";
define([], function(){
// Makes wildcard queries case-insensitive if true.
// Refer to http://www.mail-archive.com/[email protected]/msg00646.html
var wildcardCaseInsensitive = true;
return {
setWildcardCaseInsensitive: function(bool){
wildcardCaseInsensitive = bool;
},
removeEscapes: function(query){
return query.replace(/\\./g, "");
},
checkAllowedCharacters: function(query){
if(/[^a-zA-Z0-9_+\-:.()\"*?&|!{}\[\]\^~\\@#\/$%'= ]/.test(query)){
return "The allowed characters are a-z A-Z 0-9. _ + - : () \" & * ? | ! {} [ ] ^ ~ \\ @ = # % $ ' /.";
}
},
checkAsterisk: function(query){
if(/^[\*]*$|[\s]\*|^\*[^\s]/.test(query)){
return "The wildcard (*) character must be preceded by at least one alphabet or number.";
}
},
checkAmpersands: function(query){
// NB: doesn't handle term1 && term2 && term3 in Firebird 0.7
var matches = query.match(/[&]{2}/);
if(matches && matches.length > 0){
matches = query.match(/^([a-zA-Z0-9_+\-:.()\"*?&|!{}\[\]\^~\\@#\/$%'=]+( && )?[a-zA-Z0-9_+\-:.()\"*?|!{}\[\]\^~\\@#\/$%'=]+[ ]*)+$/); // note missing & in pattern
if(!matches){
return "Queries containing the special characters && must be in the form: term1 && term2.";
}
}
},
checkCaret: function(query){
if(/[^\\]\^([^\s]*[^0-9.]+)|[^\\]\^$/.test(query)){
return "The caret (^) character must be preceded by alphanumeric characters and followed by numbers.";
}
},
checkTilde: function(query){
if(/[^\\]~[^\s]*[^0-9\s]+/.test(query)){
return "The tilde (~) character must be preceded by alphanumeric characters and followed by numbers.";
}
},
checkExclamationMark: function(query){
// NB: doesn't handle term1 ! term2 ! term3 or term1 !term2
if(!/^[^!]*$|^([a-zA-Z0-9_+\-:.()\"*?&|!{}\[\]\^~\\@#\/$%'=]+( ! )?[a-zA-Z0-9_+\-:.()\"*?&|!{}\[\]\^~\\@#\/$%'=]+[ ]*)+$/.test(query)){
return "Queries containing the special character ! must be in the form: term1 ! term2.";
}
},
checkQuestionMark: function(query){
if(/^(\?)|([^a-zA-Z0-9_+\-:.()\"*?&|!{}\[\]\^~\\@#\/$%'=]\?+)/.test(query)){
return "The question mark (?) character must be preceded by at least one alphabet or number.";
}
},
checkParentheses: function(query){
var matchLeft = query.match(/[(]/g),
matchRight = query.match(/[)]/g),
countLeft = matchLeft ? matchLeft.length : 0,
countRight = matchRight ? matchRight.length : 0;
if(!matchLeft && !matchRight){
return;
}
if(matchLeft && !matchRight || matchRight && !matchLeft){
return "Parentheses must be closed.";
}
if(((countLeft + countRight) % 2) > 0 || countLeft != countRight){
return "Parentheses must be closed.";
}
if(/\(\)/.test(query)){
return"Parentheses must contain at least one character.";
}
},
checkPlusMinus: function(query){
if(!/^[^\n+\-]*$|^([+\-]?[a-zA-Z0-9_:.()\"*?&|!{}\[\]\^~\\@#\/$%'=]+[ ]?)+$/.test(query)){
return "'+' and '-' modifiers must be followed by at least one alphabet or number.";
}
},
checkANDORNOT: function(query){
if(!/AND|OR|NOT/.test(query)){
return;
}
if(!/^([a-zA-Z0-9_+\-:.()\"*?&|!{}\[\]\^~\\@\/#$%'=]+\s*((AND )|(OR )|(AND NOT )|(NOT ))?[a-zA-Z0-9_+\-:.()\"*?&|!{}\[\]\^~\\@\/#$%'=]+[ ]*)+$/.test(query)){
return "Queries containing AND/OR/NOT must be in the form: term1 AND|OR|NOT|AND NOT term2";
}
if(/^((AND )|(OR )|(AND NOT )|(NOT ))|((\bAND)|(\bOR)|(AND NOT )|(\bNOT))[ ]*$/.test(query)){
return "Queries containing AND/OR/NOT must be in the form: term1 AND|OR|NOT|AND NOT term2";
}
},
checkQuotes: function(query){
var matches = query.match(/\"/g),
matchCount;
if(!matches){
return;
}
matchCount = matches.length;
if(matchCount % 2 !== 0){
return "Please close all quote (\") marks.";
}
if(/""/.test(query)){
return "Quotes must contain at least one character.";
}
},
checkColon: function(query){
if(/[^\\\s]:[\s]|[^\\\s]:$|[\s][^\\]?:|^[^\\\s]?:/.test(query)){
return "Field declarations (:) must be preceded by at least one alphabet or number and followed by at least one alphabet or number.";
}
},
doCheckLuceneQueryValue: function(query){
if(!query){
return;
}
query = this.removeEscapes(query);
var errorMsg,
tests = [this.checkAllowedCharacters,this.checkAsterisk,this.checkAmpersands,
this.checkCaret,this.checkTilde,this.checkExclamationMark,
this.checkQuestionMark,this.checkParentheses,this.checkPlusMinus,
this.checkANDORNOT,this.checkQuotes,this.checkColon];
for (var i = tests.length - 1; i >= 0; i--) {
errorMsg = tests[i](query);
if(errorMsg){
return errorMsg;
}
}
if(wildcardCaseInsensitive){
if(query.indexOf("*") != -1){
var j = query.indexOf(':');
if(j == -1){
query.value = query.toLowerCase();
} else {
// found a wildcard field search
query.value = query.substring(0, j) + query.substring(j).toLowerCase();
}
}
}
}
};
});