Files
fleet/frontend/components/forms/validators/validate_query/index.js
T
Gabe Hernandez efb35b537a add prettier and have it format all fleet application code (#625)
* add prettier and have it format all js code except website:
:

* trying running prettier check in CI

* fix runs on in CI

* change CI job name

* fix prettier erros and fix CI
2021-04-12 14:32:25 +01:00

34 lines
900 B
JavaScript

import sqliteParser from "sqlite-parser";
import { includes, some } from "lodash";
const BLACKLISTED_ACTIONS = [];
const invalidQueryErrorMessage = "Blacklisted query action";
const invalidQueryResponse = (message) => {
return { valid: false, error: message };
};
const validQueryResponse = { valid: true, error: null };
export const validateQuery = (queryText) => {
if (!queryText) {
return invalidQueryResponse("Query text must be present");
}
try {
const ast = sqliteParser(queryText);
const { statement } = ast;
const invalidQuery = some(statement, (obj) => {
return includes(BLACKLISTED_ACTIONS, obj.variant.toLowerCase());
});
if (invalidQuery) {
return invalidQueryResponse(invalidQueryErrorMessage);
}
return validQueryResponse;
} catch (error) {
return invalidQueryResponse(error.message);
}
};
export default validateQuery;