Skip to content

Commit

Permalink
reduce validation checks in getRawQueryValidationError
Browse files Browse the repository at this point in the history
  • Loading branch information
markov00 committed Jun 5, 2024
1 parent e4aadcf commit 72eeb44
Showing 1 changed file with 14 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export function hasInvalidOperations(
export const getRawQueryValidationError = (
text: string,
operations: Record<string, unknown>
): Array<InvalidQueryError & { message: string }> => {
): (InvalidQueryError & { message: string }) | undefined => {
// try to extract the query context here
const singleLine = text.split('\n').join('');
const languagesRegexp = /(kql|lucene)/;
Expand All @@ -108,7 +108,7 @@ export const getRawQueryValidationError = (
);
// no args or no valid operation, no more work to do here
if (allArgs.length === 0 || !containsOneValidOperation) {
return [];
return undefined;
}
// at this point each entry in allArgs may contain one or more
// in the worst case it would be a math chain of count operation
Expand All @@ -118,32 +118,30 @@ export const getRawQueryValidationError = (
arg.split('count').filter((subArg) => languagesRegexp.test(subArg))
);
const [kqlQueries, luceneQueries] = partition(flattenArgs, (arg) => /kql/.test(arg));
const errors: Array<InvalidQueryError & { message: string }> = [];
for (const kqlQuery of kqlQueries) {
const result = validateQueryQuotes(kqlQuery, 'kql');
if (result) {
errors.push({
const message = validateQueryQuotes(kqlQuery, 'kql');
if (message) {
return {
id: 'invalidQuery',
meta: {
language: 'kql',
},
message: result,
});
message,
};
}
}
for (const luceneQuery of luceneQueries) {
const result = validateQueryQuotes(luceneQuery, 'lucene');
if (result) {
errors.push({
const message = validateQueryQuotes(luceneQuery, 'lucene');
if (message) {
return {
id: 'invalidQuery',
meta: {
language: 'lucene',
},
message: result,
});
message,
};
}
}
return errors;
};

const validateQueryQuotes = (rawQuery: string, language: 'kql' | 'lucene') => {
Expand Down Expand Up @@ -501,10 +499,9 @@ export function tryToParse(
// Internally the function has the following logic:
// * if the formula contains no existing ES operation, assume it's a plain parse failure
// * if the formula contains at least one existing operation, check for query problems
// TODO: not sure why we just consider the first error here
const maybeQueryProblems = getRawQueryValidationError(formula, operations);
if (maybeQueryProblems.length > 0) {
return { error: { ...maybeQueryProblems[0], locations: [] } };
if (maybeQueryProblems) {
return { error: { ...maybeQueryProblems, locations: [] } };
}
return {
error: getMessageFromId({ id: 'failedParsing', meta: { expression: formula } }, []),
Expand Down

0 comments on commit 72eeb44

Please sign in to comment.